Skip to content

Commit 12281c2

Browse files
committed
Add reference filling test
1 parent 8844c7b commit 12281c2

9 files changed

+216
-29
lines changed

src/Record.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function getModel(): Model
6363
return $this->schema->getModel($this);
6464
}
6565

66+
/**
67+
* @param string $name
68+
* @return Record[]
69+
*/
6670
public function getReference(string $name): array
6771
{
6872
if (!isset($this->relations[$name])) {
@@ -77,7 +81,7 @@ public function fillReference(string $name, array $records): void
7781
$relation = $this->getSchema()->getReference($name);
7882

7983
foreach ($records as $record) {
80-
if ($this->isRelated($relation, $record)) {
84+
if (!$this->isRelated($relation, $record)) {
8185
throw new \InvalidArgumentException('The provided records are not related to this record');
8286
}
8387
}

src/Repository.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,15 @@ protected function delete(Model $model)
145145
}
146146

147147
/**
148-
* @param Record[] $records
148+
* @param Model[] $models
149149
* @param string[] $references
150150
*/
151-
protected function fillReferences(array $records, array $references): void
151+
protected function fillReferences(array $models, array $references): void
152152
{
153+
$records = array_map(function (Model $model): Record {
154+
return $model->getDatabaseRecord();
155+
}, array_values($models));
156+
153157
$filler = new ReferenceFiller($this->connection);
154158
$filler->fill($records, $references);
155159
}

tests/helpers/IntegrationTestCase.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,34 @@ abstract class IntegrationTestCase extends TestCase
2020
/** @var TestPersonSchema */
2121
protected $personSchema;
2222

23+
/** @var TestParentSchema */
24+
protected $parentSchema;
25+
2326
abstract protected function createConnection(): Connection;
2427
abstract protected function setUpDatabase(Connection $connection): void;
2528

2629
protected function setUp()
2730
{
2831
$container = new Container();
32+
2933
$this->personSchema = new TestPersonSchema($container);
34+
$this->parentSchema = new TestParentSchema($container);
3035

3136
$container[TestPersonSchema::class] = $this->personSchema;
37+
$container[TestParentSchema::class] = $this->parentSchema;
3238

3339
$this->connection = $this->createConnection();
3440
$this->setUpDatabase($this->connection);
3541
}
3642

43+
private function getTestPersonRepository(): TestPersonRepository
44+
{
45+
return new TestPersonRepository($this->connection, $this->personSchema, $this->parentSchema);
46+
}
47+
3748
public function testCrudOperations(): void
3849
{
39-
$repository = new TestPersonRepository($this->connection, $this->personSchema);
50+
$repository = $this->getTestPersonRepository();
4051

4152
$person = $repository->createPerson('Jane', 'Doe', 20);
4253
$repository->savePerson($person);
@@ -61,7 +72,7 @@ public function testCrudOperations(): void
6172

6273
public function testFindMultiple(): void
6374
{
64-
$repository = new TestPersonRepository($this->connection, $this->personSchema);
75+
$repository = $this->getTestPersonRepository();
6576

6677
$repository->savePerson($repository->createPerson('John', 'Doe', 20));
6778
$repository->savePerson($repository->createPerson('Jane', 'Doe', 20));
@@ -75,7 +86,7 @@ public function testFindMultiple(): void
7586

7687
public function testOrderedLimits(): void
7788
{
78-
$repository = new TestPersonRepository($this->connection, $this->personSchema);
89+
$repository = $this->getTestPersonRepository();
7990

8091
$repository->savePerson($repository->createPerson('Elizabeth', 'Jones', 35));
8192
$repository->savePerson($repository->createPerson('Carmen', 'Martinez', 47));
@@ -111,7 +122,7 @@ public function testOrderedLimits(): void
111122

112123
public function testDecimalNulls(): void
113124
{
114-
$repository = new TestPersonRepository($this->connection, $this->personSchema);
125+
$repository = $this->getTestPersonRepository();
115126

116127
$jane = $repository->createPerson('Jane', 'Doe', 20);
117128
$jane->setWeight(72.1);
@@ -132,7 +143,7 @@ public function testDecimalNulls(): void
132143

133144
public function testBooleanFields(): void
134145
{
135-
$repository = new TestPersonRepository($this->connection, $this->personSchema);
146+
$repository = $this->getTestPersonRepository();
136147

137148
$repository->savePerson($repository->createPerson('Jane', 'Doe', 20));
138149

@@ -149,4 +160,37 @@ public function testBooleanFields(): void
149160
$this->assertCount(1, $repository->findByHasLicense(true));
150161
$this->assertCount(0, $repository->findByHasLicense(false));
151162
}
163+
164+
public function testLoadingReferences(): void
165+
{
166+
$repository = $this->getTestPersonRepository();
167+
168+
$jane = $repository->createPerson('Jane', 'Doe', 20);
169+
$john = $repository->createPerson('John', 'Doe', 20);
170+
$mama = $repository->createPerson('Mama', 'Doe', 40);
171+
$papa = $repository->createPerson('Papa', 'Doe', 40);
172+
173+
$repository->savePerson($jane);
174+
$repository->savePerson($john);
175+
$repository->savePerson($mama);
176+
$repository->savePerson($papa);
177+
178+
$repository->makeParent($jane, $mama);
179+
$repository->makeParent($jane, $papa);
180+
$repository->makeParent($john, $mama);
181+
$repository->makeParent($john, $papa);
182+
183+
$person = $repository->findByFirstName('John')[0];
184+
185+
$repository->loadFamily([$person]);
186+
187+
$firstName = function (TestPersonModel $model): string {
188+
return $model->getFirstName();
189+
};
190+
191+
$names = array_map($firstName, $person->getParents());
192+
sort($names);
193+
194+
$this->assertSame(['Mama', 'Papa'], $names);
195+
}
152196
}

tests/helpers/TestParentModel.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Simply\Database\Test;
4+
5+
use Simply\Database\Model;
6+
use Simply\Database\Record;
7+
8+
/**
9+
* TestParentModel.
10+
* @author Riikka Kalliomäki <[email protected]>
11+
* @copyright Copyright (c) 2018 Riikka Kalliomäki
12+
* @license http://opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
class TestParentModel extends Model
15+
{
16+
public function __construct(TestParentSchema $schema, TestPersonModel $child, TestPersonModel $parent)
17+
{
18+
$record = new Record($schema);
19+
$record['child_id'] = $child->getId();
20+
$record['parent_id'] = $parent->getId();
21+
22+
parent::__construct($record);
23+
}
24+
}

tests/helpers/TestParentSchema.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Simply\Database\Test;
4+
5+
use Simply\Database\Schema;
6+
7+
/**
8+
* TestParentJoinSchema.
9+
* @author Riikka Kalliomäki <[email protected]>
10+
* @copyright Copyright (c) 2018 Riikka Kalliomäki
11+
* @license http://opensource.org/licenses/mit-license.php MIT License
12+
*/
13+
class TestParentSchema extends Schema
14+
{
15+
protected $model;
16+
17+
protected $table = 'phpunit_tests_parent';
18+
19+
protected $primaryKeys = ['parent_id', 'child_id'];
20+
21+
protected $fields = ['parent_id', 'child_id'];
22+
23+
protected $references = [
24+
'child' => [
25+
'keys' => ['child_id'],
26+
'schema' => TestPersonSchema::class,
27+
'fields' => ['id'],
28+
],
29+
'parent' => [
30+
'keys' => ['parent_id'],
31+
'schema' => TestPersonSchema::class,
32+
'fields' => ['id'],
33+
],
34+
];
35+
}

tests/helpers/TestPersonModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,26 @@ public function giveLicense(): void
6363
{
6464
$this->record['license'] = true;
6565
}
66+
67+
public function getParents(): array
68+
{
69+
$parents = [];
70+
71+
foreach ($this->record->getReference('parents') as $relationship) {
72+
$parents[] = $relationship->getReference('parent')[0]->getModel();
73+
}
74+
75+
return $parents;
76+
}
77+
78+
public function getChildren(): array
79+
{
80+
$children = [];
81+
82+
foreach ($this->record->getReference('children') as $relationship) {
83+
$children[] = $relationship->getReference('child')[0]->getModel();
84+
}
85+
86+
return $children;
87+
}
6688
}

tests/helpers/TestPersonRepository.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,93 @@
1313
*/
1414
class TestPersonRepository extends Repository
1515
{
16-
private $schema;
17-
18-
public function __construct(Connection $connection, TestPersonSchema $schema)
19-
{
16+
private $personSchema;
17+
private $parentSchema;
18+
19+
public function __construct(
20+
Connection $connection,
21+
TestPersonSchema $personSchema,
22+
TestParentSchema $parentSchema
23+
) {
2024
parent::__construct($connection);
2125

22-
$this->schema = $schema;
26+
$this->personSchema = $personSchema;
27+
$this->parentSchema = $parentSchema;
2328
}
2429

2530
public function createPerson(string $firstName, string $lastName, int $age): TestPersonModel
2631
{
27-
return new TestPersonModel($this->schema, $firstName, $lastName, $age);
32+
return new TestPersonModel($this->personSchema, $firstName, $lastName, $age);
2833
}
2934

3035
public function findById(int $id): ?TestPersonModel
3136
{
32-
return $this->findByPrimaryKey($this->schema, $id);
37+
return $this->findByPrimaryKey($this->personSchema, $id);
3338
}
3439

40+
/**
41+
* @param string $name
42+
* @return TestPersonModel[]
43+
*/
3544
public function findByFirstName(string $name): array
3645
{
37-
return $this->find($this->schema, ['first_name' => $name]);
46+
return $this->find($this->personSchema, ['first_name' => $name]);
3847
}
3948

4049
public function findByAnyFirstName(iterable $names): array
4150
{
42-
return $this->find($this->schema, ['first_name' => array_map(function (string $name): string {
51+
return $this->find($this->personSchema, ['first_name' => array_map(function (string $name): string {
4352
return $name;
4453
}, $names)]);
4554
}
4655

4756
public function findByLastName(string $name): array
4857
{
49-
return $this->find($this->schema, ['last_name' => $name]);
58+
return $this->find($this->personSchema, ['last_name' => $name]);
5059
}
5160

5261
public function findAllAlphabetically(int $limit = null, bool $ascending = true): array
5362
{
5463
$order = $ascending ? Connection::ORDER_ASCENDING : Connection::ORDER_DESCENDING;
55-
return $this->find($this->schema, [], ['last_name' => $order], $limit);
64+
return $this->find($this->personSchema, [], ['last_name' => $order], $limit);
5665
}
5766

5867
public function findOneByWeight(?float $weight): ?TestPersonModel
5968
{
60-
return $this->findOne($this->schema, ['weight' => $weight]);
69+
return $this->findOne($this->personSchema, ['weight' => $weight]);
6170
}
6271

6372
public function findByAnyWeight(array $weights): array
6473
{
65-
return $this->find($this->schema, ['weight' => array_map(function (?float $weight): ?float {
74+
return $this->find($this->personSchema, ['weight' => array_map(function (?float $weight): ?float {
6675
return $weight;
6776
}, $weights)]);
6877
}
6978

7079
public function findByHasLicense(bool $hasLicense): array
7180
{
72-
return $this->find($this->schema, ['license' => $hasLicense]);
81+
return $this->find($this->personSchema, ['license' => $hasLicense]);
7382
}
7483

7584
public function savePerson(TestPersonModel $model): void
7685
{
7786
$this->save($model);
7887
}
7988

89+
public function makeParent(TestPersonModel $child, TestPersonModel $parent)
90+
{
91+
$relationship = new TestParentModel($this->parentSchema, $child, $parent);
92+
$this->save($relationship);
93+
}
94+
95+
public function loadFamily(array $people)
96+
{
97+
$this->fillReferences($people, [
98+
'parents.parent.children.child',
99+
'children.child.parents.parent',
100+
]);
101+
}
102+
80103
public function deletePerson(TestPersonModel $model): void
81104
{
82105
$this->delete($model);

tests/helpers/TestPersonSchema.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,16 @@ class TestPersonSchema extends Schema
2020

2121
protected $fields = ['id', 'first_name', 'last_name', 'age', 'weight', 'license'];
2222

23-
protected $references;
23+
protected $references = [
24+
'parents' => [
25+
'keys' => ['id'],
26+
'schema' => TestParentSchema::class,
27+
'fields' => ['child_id'],
28+
],
29+
'children' => [
30+
'keys' => ['id'],
31+
'schema' => TestParentSchema::class,
32+
'fields' => ['parent_id'],
33+
],
34+
];
2435
}

0 commit comments

Comments
 (0)