Skip to content

Commit f7d48ec

Browse files
authored
Merge pull request #6 from hosmelq/tests
will added more later
2 parents 836000d + 909a6b7 commit f7d48ec

File tree

7 files changed

+74
-14
lines changed

7 files changed

+74
-14
lines changed

phpunit.xml.dist

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
55
backupGlobals="false"
66
bootstrap="vendor/autoload.php"
77
colors="true"
@@ -21,9 +21,6 @@
2121
</testsuite>
2222
</testsuites>
2323
<coverage>
24-
<include>
25-
<directory suffix=".php">./src</directory>
26-
</include>
2724
<report>
2825
<html outputDirectory="build/coverage"/>
2926
<text outputFile="build/coverage.txt"/>
@@ -33,4 +30,9 @@
3330
<logging>
3431
<junit outputFile="build/report.junit.xml"/>
3532
</logging>
33+
<source>
34+
<include>
35+
<directory suffix=".php">./src</directory>
36+
</include>
37+
</source>
3638
</phpunit>

src/HasExtendedRelationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Mrpunyapal\LaravelExtendedRelationships;
66

7-
use Illuminate\Contracts\Database\Query\Builder;
7+
use Illuminate\Database\Eloquent\Builder;
88
use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToArrayColumn;
99
use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToManyKeys;
1010
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyArrayColumn;

src/Relations/HasManyArrayColumn.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public function addConstraints(): void
1717
if (! static::$constraints) {
1818
return;
1919
}
20+
2021
$query = $this->getRelationQuery();
2122

22-
$query->wherein($this->foreignKey, $this->getParentKey());
23+
$query->whereIn($this->foreignKey, $this->getParentKey());
2324

2425
$query->whereNotNull($this->foreignKey);
2526
}
@@ -40,6 +41,7 @@ public function addEagerConstraints(array $models): void
4041
protected function getKeys(array $models, $key = null): array
4142
{
4243
$keys = [];
44+
4345
collect($models)->each(function ($value) use ($key, &$keys) {
4446
$keys = array_merge($keys, $value->getAttribute($key));
4547
});
@@ -66,6 +68,7 @@ public function matchMany(array $models, Collection $results, $relation): array
6668
$collection = $collection->merge($this->getRelationValue($dictionary, $id, 'many'));
6769
}
6870
}
71+
6972
$model->setRelation($relation, $collection);
7073
}
7174

src/Relations/HasManyKeys.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function addConstraints(): void
4545
if (! static::$constraints) {
4646
return;
4747
}
48+
4849
$foreignKeys = $this->foreignKeys;
4950

5051
$this->query->where(function ($query) use ($foreignKeys): void {

tests/ExampleTest.php

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Query\Builder as QueryBuilder;
9+
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyArrayColumn;
10+
11+
it('models are properly matched to parents', function (): void {
12+
$relation = getRelation();
13+
14+
$result1 = new HasManyArrayColumnModelStub();
15+
$result1->foreign_key = 1;
16+
$result2 = new HasManyArrayColumnModelStub();
17+
$result2->foreign_key = 2;
18+
$result3 = new HasManyArrayColumnModelStub();
19+
$result3->foreign_key = 2;
20+
21+
$model1 = new HasManyArrayColumnModelStub();
22+
$model1->companies = [1];
23+
$model2 = new HasManyArrayColumnModelStub();
24+
$model2->companies = [2];
25+
$model3 = new HasManyArrayColumnModelStub();
26+
$model3->companies = [3];
27+
28+
$relation->getRelated()->shouldReceive('newCollection')->andReturnUsing(function ($array) {
29+
return new Collection($array);
30+
});
31+
32+
$models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
33+
34+
expect($models[0]->foo)->toHaveCount(1)
35+
->get(0)->foreign_key->toBe(1)
36+
->and($models[1]->foo)->toHaveCount(2)
37+
->get(0)->foreign_key->toBe(2)
38+
->get(1)->foreign_key->toBe(2)
39+
->and($models[2]->foo)->toHaveCount(0);
40+
});
41+
42+
function getRelation()
43+
{
44+
$queryBuilder = Mockery::mock(QueryBuilder::class);
45+
$eloquentBuilder = Mockery::mock(EloquentBuilder::class, [$queryBuilder]);
46+
47+
$parent = Mockery::mock(Model::class);
48+
$related = Mockery::mock(Model::class);
49+
50+
$eloquentBuilder->shouldReceive('getModel')->andReturn($related);
51+
$eloquentBuilder->shouldReceive('whereIn')->with('foreign_key', [1, 2]);
52+
$eloquentBuilder->shouldReceive('whereNotNull')->with('foreign_key');
53+
54+
$parent->shouldReceive('getAttribute')->with('companies')->andReturn([1, 2]);
55+
56+
return new HasManyArrayColumn($eloquentBuilder, $parent, 'foreign_key', 'companies');
57+
}
58+
59+
class HasManyArrayColumnModelStub extends Model
60+
{
61+
public int $foreign_key;
62+
}

tests/TestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ class TestCase extends Orchestra
1111
protected function setUp(): void
1212
{
1313
parent::setUp();
14-
1514
}
1615
}

0 commit comments

Comments
 (0)