Skip to content

Commit 2153926

Browse files
committed
Add more test coverage
1 parent 36fa6a5 commit 2153926

11 files changed

+284
-37
lines changed

src/Record.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function associate(string $name, Model $model): void
133133
$record = $model->getDatabaseRecord();
134134

135135
if ($record->getSchema() !== $relationship->getReferencedSchema()) {
136-
throw new \InvalidArgumentException('The associated model has a record in unexpected schema');
136+
throw new \InvalidArgumentException('The associated model has a record with an unexpected schema');
137137
}
138138

139139
while ($keys) {

src/Schema.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function getSchema(string $name): Schema
9595

9696
public function getRecord(array $values): Record
9797
{
98-
$record = new Record($this);
98+
$record = $this->createRecord();
9999
$record->setDatabaseValues($values);
100100

101101
return $record;
@@ -120,4 +120,9 @@ public function createModel(array $row, string $prefix = ''): Model
120120

121121
return $this->getRecord($values)->getModel();
122122
}
123+
124+
public function createRecord(Model $model = null): Record
125+
{
126+
return new Record($this, $model);
127+
}
123128
}

tests/helpers/IntegrationTestCase.php renamed to tests/helpers/TestCase/IntegrationTestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?php
22

3-
namespace Simply\Database\Test;
3+
namespace Simply\Database\Test\TestCase;
44

55
use PHPUnit\Framework\TestCase;
66
use Simply\Container\Container;
77
use Simply\Database\Connection\Connection;
8+
use Simply\Database\Test\TestHouseSchema;
9+
use Simply\Database\Test\TestParentSchema;
10+
use Simply\Database\Test\TestPersonModel;
11+
use Simply\Database\Test\TestPersonSchema;
12+
use Simply\Database\Test\TestRepository;
813

914
/**
1015
* IntegrationTestCase.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Simply\Database\Test\TestCase;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Simply\Container\Container;
7+
use Simply\Database\Test\TestHouseSchema;
8+
use Simply\Database\Test\TestParentSchema;
9+
use Simply\Database\Test\TestPersonSchema;
10+
11+
/**
12+
* UnitTest.
13+
* @author Riikka Kalliomäki <[email protected]>
14+
* @copyright Copyright (c) 2018 Riikka Kalliomäki
15+
* @license http://opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
class UnitTestCase extends TestCase
18+
{
19+
protected function getPersonSchema(): TestPersonSchema
20+
{
21+
$container = new Container();
22+
$schema = new TestPersonSchema($container);
23+
24+
$container[TestPersonSchema::class] = $schema;
25+
$container[TestParentSchema::class] = new TestParentSchema($container);
26+
$container[TestHouseSchema::class] = new TestHouseSchema($container);
27+
28+
return $schema;
29+
}
30+
}

tests/helpers/TestHouseModel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Simply\Database\Test;
44

55
use Simply\Database\Model;
6-
use Simply\Database\Record;
76

87
/**
98
* TestHouseModel.
@@ -15,7 +14,7 @@ class TestHouseModel extends Model
1514
{
1615
public function __construct(TestHouseSchema $schema, string $street)
1716
{
18-
$record = new Record($schema, $this);
17+
$record = $schema->createRecord($this);
1918
$record['street'] = $street;
2019

2120
parent::__construct($record);

tests/helpers/TestParentModel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Simply\Database\Test;
44

55
use Simply\Database\Model;
6-
use Simply\Database\Record;
76

87
/**
98
* TestParentModel.
@@ -15,7 +14,7 @@ class TestParentModel extends Model
1514
{
1615
public function __construct(TestParentSchema $schema, TestPersonModel $child, TestPersonModel $parent)
1716
{
18-
$record = new Record($schema, $this);
17+
$record = $schema->createRecord($this);
1918

2019
$record->associate('child', $child);
2120
$record->associate('parent', $parent);

tests/helpers/TestPersonModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Simply\Database\Test;
44

55
use Simply\Database\Model;
6-
use Simply\Database\Record;
76

87
/**
98
* TestModel.
@@ -15,7 +14,8 @@ class TestPersonModel extends Model
1514
{
1615
public function __construct(TestPersonSchema $schema, string $firstName, string $lastName, int $age)
1716
{
18-
$record = new Record($schema, $this);
17+
$record = $schema->createRecord($this);
18+
1919
$record['first_name'] = $firstName;
2020
$record['last_name'] = $lastName;
2121
$record['age'] = $age;

tests/helpers/TestPlainModel.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Simply\Database\Test;
4+
5+
use Simply\Database\Model;
6+
use Simply\Database\Record;
7+
8+
/**
9+
* TestPlainModel.
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 TestPlainModel extends Model
15+
{
16+
public function __construct(Record $record)
17+
{
18+
parent::__construct($record);
19+
}
20+
}

tests/tests/MySqlIntegrationTest.php

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

55
use Simply\Database\Connection\Connection;
66
use Simply\Database\Connection\MySqlConnection;
7-
use Simply\Database\Test\IntegrationTestCase;
7+
use Simply\Database\Test\TestCase\IntegrationTestCase;
88

99
/**
1010
* MySqlIntegrationTest.

tests/tests/RecordTest.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
3+
namespace Simply\Database;
4+
5+
use Simply\Database\Test\TestCase\UnitTestCase;
6+
7+
/**
8+
* RecordTest.
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 RecordTest extends UnitTestCase
14+
{
15+
public function testPrimaryKeyOnEmptyRecord(): void
16+
{
17+
$schema = $this->getPersonSchema();
18+
$person = $schema->createRecord();
19+
20+
$this->expectException(\RuntimeException::class);
21+
$person->getPrimaryKey();
22+
}
23+
24+
public function testFetchingNoReferences(): void
25+
{
26+
$schema = $this->getPersonSchema();
27+
$person = $schema->createRecord();
28+
29+
$this->expectException(\RuntimeException::class);
30+
$person->getReferencedRecords('spouse');
31+
}
32+
33+
public function testMultipleModelAssociation(): void
34+
{
35+
$schema = $this->getPersonSchema();
36+
37+
$personA = $schema->createRecord();
38+
$personB = $schema->createRecord();
39+
40+
$this->expectException(\InvalidArgumentException::class);
41+
$personA->associate('parents', $personB->getModel());
42+
}
43+
44+
public function testAssociateWithInvalidSchema(): void
45+
{
46+
$schema = $this->getPersonSchema();
47+
48+
$person = $schema->createRecord();
49+
$house = $schema->getRelationship('home')->getReferencedSchema()->createRecord();
50+
51+
$this->expectException(\InvalidArgumentException::class);
52+
$person->associate('spouse', $house->getModel());
53+
}
54+
55+
public function testAssociatingWithNullValue(): void
56+
{
57+
$schema = $this->getPersonSchema();
58+
59+
$personA = $schema->createRecord();
60+
$personB = $schema->createRecord();
61+
62+
$this->expectException(\RuntimeException::class);
63+
$personA->associate('spouse', $personB->getModel());
64+
}
65+
66+
public function testAddAssociationToUniqueRelationship(): void
67+
{
68+
$schema = $this->getPersonSchema();
69+
70+
$personA = $schema->createRecord();
71+
$personB = $schema->createRecord();
72+
73+
$this->expectException(\InvalidArgumentException::class);
74+
$personA->addAssociation('spouse', $personB->getModel());
75+
}
76+
77+
public function testAssociateMultipleRelationship(): void
78+
{
79+
$schema = $this->getPersonSchema();
80+
81+
$personA = $schema->createRecord();
82+
$personB = $schema->createRecord();
83+
84+
$house = $schema->getRelationship('home')->getReferencedSchema()->createRecord();
85+
$house['id'] = 1;
86+
87+
$personA->associate('home', $house->getModel());
88+
89+
$this->assertSame([$house], $personA->getReferencedRecords('home'));
90+
$this->assertFalse($house->hasReferencedRecords('residents'));
91+
92+
$house->setReferencedRecords('residents', [$personA]);
93+
94+
$personB->associate('home', $house->getModel());
95+
96+
$this->assertSame([$personA, $personB], $house->getReferencedRecords('residents'));
97+
}
98+
99+
public function testGetRelatedModelForNonUnique(): void
100+
{
101+
$schema = $this->getPersonSchema();
102+
$person = $schema->createRecord();
103+
104+
$this->expectException(\RuntimeException::class);
105+
$person->getRelatedModel('parents');
106+
}
107+
108+
public function testGetEmptyRelatedModel(): void
109+
{
110+
$schema = $this->getPersonSchema();
111+
112+
$person = $schema->createRecord();
113+
$person->setReferencedRecords('spouse', []);
114+
115+
$this->assertNull($person->getRelatedModel('spouse'));
116+
}
117+
118+
public function testGetMultipleModelsForNonUnique(): void
119+
{
120+
$schema = $this->getPersonSchema();
121+
$person = $schema->createRecord();
122+
123+
$this->expectException(\RuntimeException::class);
124+
$person->getRelatedModels('spouse');
125+
}
126+
127+
public function testUnsettingRecordFields(): void
128+
{
129+
$person = $this->getPersonSchema()->createRecord();
130+
131+
$this->assertNull($person['id']);
132+
$this->assertFalse(isset($person['id']));
133+
134+
$person['id'] = 1;
135+
136+
$this->assertSame(1, $person['id']);
137+
$this->assertTrue(isset($person['id']));
138+
139+
unset($person['id']);
140+
141+
$this->assertNull($person['id']);
142+
$this->assertFalse(isset($person['id']));
143+
}
144+
145+
public function testGettingInvalidField(): void
146+
{
147+
$person = $this->getPersonSchema()->createRecord();
148+
149+
$this->expectException(\InvalidArgumentException::class);
150+
$person['not-a-field'];
151+
}
152+
153+
public function testSettingInvalidField(): void
154+
{
155+
$person = $this->getPersonSchema()->createRecord();
156+
157+
$this->expectException(\InvalidArgumentException::class);
158+
$person['not-a-field'] = 'value';
159+
}
160+
161+
public function testIncorrectValueOrder(): void
162+
{
163+
$schema = $this->getPersonSchema();
164+
$person = $schema->createRecord();
165+
166+
$values = array_fill_keys(array_reverse($schema->getFields()), 1);
167+
168+
$this->expectException(\InvalidArgumentException::class);
169+
$person->setDatabaseValues($values);
170+
}
171+
172+
public function testInvalidProxyRelation(): void
173+
{
174+
$person = $this->getPersonSchema()->createRecord();
175+
176+
$this->expectException(\RuntimeException::class);
177+
$person->getRelatedModelsByProxy('spouse', 'spouse');
178+
}
179+
180+
public function testInvalidProxiedRelation(): void
181+
{
182+
$house = $this->getPersonSchema()->getRelationship('home')->getReferencedSchema()->createRecord();
183+
184+
$this->expectException(\RuntimeException::class);
185+
$house->getRelatedModelsByProxy('residents', 'parents');
186+
}
187+
188+
public function testEmptyProxy(): void
189+
{
190+
$schema = $this->getPersonSchema();
191+
192+
$person = $schema->createRecord();
193+
$parent = $schema->getRelationship('parents')->getReferencedSchema()->createRecord();
194+
195+
$person['id'] = 1;
196+
$parent['child_id'] = 1;
197+
198+
$person->setReferencedRecords('parents', [$parent]);
199+
$parent->setReferencedRecords('parent', []);
200+
201+
$this->assertSame([], $person->getRelatedModelsByProxy('parents', 'parent'));
202+
}
203+
}

0 commit comments

Comments
 (0)