Skip to content

Commit ea5125a

Browse files
committed
Cover more code with tests
1 parent 2153926 commit ea5125a

File tree

7 files changed

+192
-31
lines changed

7 files changed

+192
-31
lines changed

src/RelationshipFiller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private function fillRelationships(array $records, array $relationships): void
6262
$schemaId = $this->getSchemaId($parent);
6363

6464
if (\count($fields) > 1) {
65-
throw new \RuntimeException('Filling relationships for composite foreign keys is not supported');
65+
throw new \InvalidArgumentException('Filling relationships for composite foreign keys is not supported');
6666
}
6767

6868
$isPrimaryReference = $fields === $parent->getPrimaryKey();

src/Repository.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @copyright Copyright (c) 2018 Riikka Kalliomäki
1111
* @license http://opensource.org/licenses/mit-license.php MIT License
1212
*/
13-
class Repository
13+
abstract class Repository
1414
{
1515
private $connection;
1616

@@ -36,16 +36,10 @@ protected function find(Schema $schema, array $conditions, array $order = [], in
3636
protected function findOne(Schema $schema, array $conditions): ?Model
3737
{
3838
$keys = $schema->getPrimaryKey();
39+
$order = array_fill_keys($keys, Connection::ORDER_ASCENDING);
3940

40-
if ($keys) {
41-
$order = array_fill_keys($keys, Connection::ORDER_ASCENDING);
42-
$result = $this->connection->select($schema->getFields(), $schema->getTable(), $conditions, $order, 1);
43-
} else {
44-
$result = $this->connection->select($schema->getFields(), $schema->getTable(), $conditions);
45-
}
46-
41+
$result = $this->connection->select($schema->getFields(), $schema->getTable(), $conditions, $order, 1);
4742
$result->setFetchMode(\PDO::FETCH_ASSOC);
48-
4943
$row = $result->fetch();
5044

5145
return $row ? $schema->createModel($row) : null;

tests/helpers/TestCase/IntegrationTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ public function testInvalidSortOrder()
281281
$this->connection->select(['id'], $this->personSchema->getTable(), [], ['id' => 0]);
282282
}
283283

284+
public function testSavingDeletedRecord()
285+
{
286+
$repository = $this->getTestPersonRepository();
287+
$person = $repository->createPerson('Jane', 'Doe', 20);
288+
$repository->savePerson($person);
289+
$repository->deletePerson($person);
290+
291+
$this->assertCount(0, $repository->findByFirstName('Jane'));
292+
293+
$this->expectException(\RuntimeException::class);
294+
$repository->savePerson($person);
295+
}
296+
284297
public function testUnlimitedWithoutSortOrder()
285298
{
286299
$repository = $this->getTestPersonRepository();

tests/helpers/TestCase/UnitTestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Simply\Container\Container;
7+
use Simply\Database\Schema;
78
use Simply\Database\Test\TestHouseSchema;
89
use Simply\Database\Test\TestParentSchema;
910
use Simply\Database\Test\TestPersonSchema;
@@ -27,4 +28,30 @@ protected function getPersonSchema(): TestPersonSchema
2728

2829
return $schema;
2930
}
31+
32+
protected function getCompositeForeignKeySchema(): Schema
33+
{
34+
$container = new Container();
35+
$schema = new class($container) extends Schema {
36+
protected $model = 'TestModel';
37+
protected $primaryKey = ['order_id', 'product_id'];
38+
protected $fields = ['order_id', 'product_id', 'replaced_order_id', 'replaced_product_id'];
39+
protected $table = 'test';
40+
protected $relationships = [
41+
'replacement' => [
42+
'key' => ['order_id', 'product_id'],
43+
'schema' => 'TestSchema',
44+
'field' => ['replaced_order_id', 'replaced_product_id'],
45+
],
46+
'replaced' => [
47+
'key' => ['replaced_order_id', 'replaced_product_id'],
48+
'schema' => 'TestSchema',
49+
'field' => ['order_id', 'product_id'],
50+
],
51+
];
52+
};
53+
54+
$container['TestSchema'] = $schema;
55+
return $schema;
56+
}
3057
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Simply\Database;
4+
5+
use Simply\Database\Connection\Connection;
6+
use Simply\Database\Test\TestCase\UnitTestCase;
7+
8+
/**
9+
* RelationshipFillerTest.
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 RelationshipFillerTest extends UnitTestCase
15+
{
16+
public function testEmptyRecordsList(): void
17+
{
18+
$connection = $this->createMock(Connection::class);
19+
$filler = new RelationshipFiller($connection);
20+
21+
$connection->expects($this->never())->method('select');
22+
$filler->fill([], ['spouse']);
23+
}
24+
25+
public function testMixedRecordSchemas(): void
26+
{
27+
$schema = $this->getPersonSchema();
28+
29+
$person = $schema->createRecord();
30+
$person['id'] = 1;
31+
$person->updateState(Record::STATE_INSERT);
32+
33+
$parent = $schema->getRelationship('parents')->getReferencedSchema()->createRecord();
34+
$parent['child_id'] = 1;
35+
$parent['parent_id'] = 1;
36+
$parent->updateState(Record::STATE_INSERT);
37+
38+
$connection = $this->createMock(Connection::class);
39+
$filler = new RelationshipFiller($connection);
40+
41+
$this->expectException(\InvalidArgumentException::class);
42+
$filler->fill([$person, $parent], ['spouse']);
43+
}
44+
45+
public function testFillingWithCompositeForeignKeys(): void
46+
{
47+
$schema = $this->getCompositeForeignKeySchema();
48+
49+
$order = $schema->createRecord();
50+
$order['order_id'] = 1;
51+
$order['product_id'] = 1;
52+
$order['replaced_order_id'] = 1;
53+
$order['replaced_product_id'] = 1;
54+
$order->updateState(Record::STATE_INSERT);
55+
56+
$connection = $this->createMock(Connection::class);
57+
$filler = new RelationshipFiller($connection);
58+
59+
$this->expectException(\InvalidArgumentException::class);
60+
$filler->fill([$order], ['replaced']);
61+
}
62+
63+
public function testDuplicatedRecordsInList(): void
64+
{
65+
$schema = $this->getPersonSchema();
66+
67+
$personA = $schema->createRecord();
68+
$personA['id'] = 1;
69+
$personA->updateState(Record::STATE_INSERT);
70+
71+
$personB = $schema->createRecord();
72+
$personB['id'] = 1;
73+
$personB->updateState(Record::STATE_INSERT);
74+
75+
$connection = $this->createMock(Connection::class);
76+
$filler = new RelationshipFiller($connection);
77+
78+
$this->expectException(\RuntimeException::class);
79+
$filler->fill([$personA, $personB], ['spouse']);
80+
}
81+
82+
public function testNoQueriesWhenCached()
83+
{
84+
$schema = $this->getPersonSchema();
85+
86+
$personA = $schema->createRecord();
87+
$personA['id'] = 1;
88+
$personA['spouse_id'] = 2;
89+
$personA->updateState(Record::STATE_INSERT);
90+
91+
$personB = $schema->createRecord();
92+
$personB['id'] = 2;
93+
$personB['spouse_id'] = 1;
94+
$personB->updateState(Record::STATE_INSERT);
95+
96+
$connection = $this->createMock(Connection::class);
97+
$filler = new RelationshipFiller($connection);
98+
99+
$connection->expects($this->never())->method('select');
100+
$filler->fill([$personA, $personB], ['spouse']);
101+
}
102+
}

tests/tests/RepositoryTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Simply\Database;
4+
5+
use Simply\Database\Connection\Connection;
6+
use Simply\Database\Test\TestCase\UnitTestCase;
7+
8+
/**
9+
* RepositoryTest.
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 RepositoryTest extends UnitTestCase
15+
{
16+
public function testMissingPrimaryKey(): void
17+
{
18+
$connection = $this->createMock(Connection::class);
19+
$repository = new class($connection) extends Repository {
20+
};
21+
22+
$method = new \ReflectionMethod($repository, 'findByPrimaryKey');
23+
$method->setAccessible(true);
24+
25+
$schema = $this->getPersonSchema()->getRelationship('parents')->getReferencedSchema();
26+
27+
$this->expectException(\InvalidArgumentException::class);
28+
$method->invoke($repository, $schema, [1]);
29+
}
30+
31+
public function testInvalidPrimaryKeyValue(): void
32+
{
33+
$connection = $this->createMock(Connection::class);
34+
$repository = new class($connection) extends Repository {
35+
};
36+
37+
$method = new \ReflectionMethod($repository, 'findByPrimaryKey');
38+
$method->setAccessible(true);
39+
40+
$schema = $this->getPersonSchema()->getRelationship('parents')->getReferencedSchema();
41+
42+
$this->expectException(\InvalidArgumentException::class);
43+
$method->invoke($repository, $schema, [1, [2, 3]]);
44+
}
45+
}

tests/tests/SchemaTest.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,7 @@ public function testMultipleReverseRelationships(): void
118118

119119
public function testTryingToFillCompositeForeignKey(): void
120120
{
121-
$container = new Container();
122-
$schema = new class($container) extends Schema {
123-
protected $model = 'TestModel';
124-
protected $primaryKey = ['order_id', 'product_id'];
125-
protected $fields = ['order_id', 'product_id', 'replaced_order_id', 'replaced_product_id'];
126-
protected $table = 'test';
127-
protected $relationships = [
128-
'replacement' => [
129-
'key' => ['order_id', 'product_id'],
130-
'schema' => 'TestSchema',
131-
'field' => ['replaced_order_id', 'replaced_product_id'],
132-
],
133-
'replaced' => [
134-
'key' => ['replaced_order_id', 'replaced_product_id'],
135-
'schema' => 'TestSchema',
136-
'field' => ['order_id', 'product_id'],
137-
],
138-
];
139-
};
140-
141-
$container['TestSchema'] = $schema;
121+
$schema = $this->getCompositeForeignKeySchema();
142122
$relationship = $schema->getRelationship('replaced');
143123

144124
$this->expectException(\RuntimeException::class);

0 commit comments

Comments
 (0)