Skip to content

Commit 8844c7b

Browse files
committed
Add additional tests
1 parent ccbe5ae commit 8844c7b

File tree

7 files changed

+156
-7
lines changed

7 files changed

+156
-7
lines changed

src/Connection/MySqlConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function select(array $fields, string $table, array $where, array $orderB
7070
return $this->query($this->formatQuery([
7171
'SELECT' => $this->formatFields($fields),
7272
'FROM' => $this->formatTable($table),
73-
'WHERE' => $this->formatConditions($where, $parameters),
73+
'WHERE' => $where ? $this->formatConditions($where, $parameters) : '',
7474
'ORDER BY' => $this->formatOrder($orderBy),
7575
'LIMIT' => $orderBy ? $this->formatLimit($limit, $parameters) : '',
7676
]), $parameters);

src/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function createModel(array $row, string $prefix = ''): Model
8484
foreach ($this->getFields() as $field) {
8585
$prefixed = $prefix . $field;
8686

87-
if (isset($row[$prefixed])) {
87+
if (array_key_exists($prefixed, $row)) {
8888
$values[$field] = $row[$prefixed];
8989
}
9090
}

tests/helpers/IntegrationTestCase.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,95 @@ public function testCrudOperations(): void
5858

5959
$this->assertNull($repository->findById($id));
6060
}
61+
62+
public function testFindMultiple(): void
63+
{
64+
$repository = new TestPersonRepository($this->connection, $this->personSchema);
65+
66+
$repository->savePerson($repository->createPerson('John', 'Doe', 20));
67+
$repository->savePerson($repository->createPerson('Jane', 'Doe', 20));
68+
69+
$this->assertCount(0, $repository->findByLastName('John'));
70+
$this->assertCount(1, $repository->findByFirstName('John'));
71+
$this->assertCount(1, $repository->findByFirstName('Jane'));
72+
$this->assertCount(2, $repository->findByLastName('Doe'));
73+
$this->assertCount(2, $repository->findByAnyFirstName(['John', 'Jane']));
74+
}
75+
76+
public function testOrderedLimits(): void
77+
{
78+
$repository = new TestPersonRepository($this->connection, $this->personSchema);
79+
80+
$repository->savePerson($repository->createPerson('Elizabeth', 'Jones', 35));
81+
$repository->savePerson($repository->createPerson('Carmen', 'Martinez', 47));
82+
$repository->savePerson($repository->createPerson('Isabell', 'Williams', 45));
83+
$repository->savePerson($repository->createPerson('Corina', 'Keith', 64));
84+
$repository->savePerson($repository->createPerson('Ruth', 'Ward', 44));
85+
$repository->savePerson($repository->createPerson('Frances', 'Gray', 41));
86+
87+
$lastName = function (TestPersonModel $person): string {
88+
return $person->getLastName();
89+
};
90+
91+
$this->assertSame(
92+
['Gray', 'Jones', 'Keith', 'Martinez', 'Ward', 'Williams'],
93+
array_map($lastName, $repository->findAllAlphabetically())
94+
);
95+
96+
$this->assertSame(
97+
['Williams', 'Ward', 'Martinez', 'Keith', 'Jones', 'Gray'],
98+
array_map($lastName, $repository->findAllAlphabetically(null, false))
99+
);
100+
101+
$this->assertSame(
102+
['Gray', 'Jones', 'Keith'],
103+
array_map($lastName, $repository->findAllAlphabetically(3))
104+
);
105+
106+
$this->assertSame(
107+
['Williams', 'Ward', 'Martinez'],
108+
array_map($lastName, $repository->findAllAlphabetically(3, false))
109+
);
110+
}
111+
112+
public function testDecimalNulls(): void
113+
{
114+
$repository = new TestPersonRepository($this->connection, $this->personSchema);
115+
116+
$jane = $repository->createPerson('Jane', 'Doe', 20);
117+
$jane->setWeight(72.1);
118+
$repository->savePerson($jane);
119+
120+
$john = $repository->createPerson('John', 'Doe', 20);
121+
$john->setWeight(82.0);
122+
$repository->savePerson($john);
123+
124+
$repository->savePerson($repository->createPerson('Jane', 'Smith', 22));
125+
126+
$this->assertSame(72.1, $repository->findOneByWeight(72.1)->getWeight());
127+
$this->assertNull($repository->findOneByWeight(null)->getWeight());
128+
$this->assertCount(2, $repository->findByAnyWeight([72.1, 82.0]));
129+
$this->assertCount(2, $repository->findByAnyWeight([72.1, null]));
130+
$this->assertCount(1, $repository->findByAnyWeight([null]));
131+
}
132+
133+
public function testBooleanFields(): void
134+
{
135+
$repository = new TestPersonRepository($this->connection, $this->personSchema);
136+
137+
$repository->savePerson($repository->createPerson('Jane', 'Doe', 20));
138+
139+
$this->assertCount(0, $repository->findByHasLicense(true));
140+
141+
$unlicensed = $repository->findByHasLicense(false);
142+
$this->assertCount(1, $unlicensed);
143+
144+
$jane = array_pop($unlicensed);
145+
$jane->giveLicense();
146+
147+
$repository->savePerson($jane);
148+
149+
$this->assertCount(1, $repository->findByHasLicense(true));
150+
$this->assertCount(0, $repository->findByHasLicense(false));
151+
}
61152
}

tests/helpers/TestPersonModel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(TestPersonSchema $schema, string $firstName, string
1919
$record['first_name'] = $firstName;
2020
$record['last_name'] = $lastName;
2121
$record['age'] = $age;
22+
$record['license'] = false;
2223

2324
parent::__construct($record);
2425
}
@@ -43,8 +44,23 @@ public function getAge(): int
4344
return $this->record['age'];
4445
}
4546

47+
public function getWeight(): ?float
48+
{
49+
return $this->record['weight'];
50+
}
51+
4652
public function increaseAge(): void
4753
{
4854
$this->record['age'] = $this->getAge() + 1;
4955
}
56+
57+
public function setWeight(float $weight): void
58+
{
59+
$this->record['weight'] = $weight;
60+
}
61+
62+
public function giveLicense(): void
63+
{
64+
$this->record['license'] = true;
65+
}
5066
}

tests/helpers/TestPersonRepository.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,46 @@ public function findById(int $id): ?TestPersonModel
3232
return $this->findByPrimaryKey($this->schema, $id);
3333
}
3434

35+
public function findByFirstName(string $name): array
36+
{
37+
return $this->find($this->schema, ['first_name' => $name]);
38+
}
39+
40+
public function findByAnyFirstName(iterable $names): array
41+
{
42+
return $this->find($this->schema, ['first_name' => array_map(function (string $name): string {
43+
return $name;
44+
}, $names)]);
45+
}
46+
47+
public function findByLastName(string $name): array
48+
{
49+
return $this->find($this->schema, ['last_name' => $name]);
50+
}
51+
52+
public function findAllAlphabetically(int $limit = null, bool $ascending = true): array
53+
{
54+
$order = $ascending ? Connection::ORDER_ASCENDING : Connection::ORDER_DESCENDING;
55+
return $this->find($this->schema, [], ['last_name' => $order], $limit);
56+
}
57+
58+
public function findOneByWeight(?float $weight): ?TestPersonModel
59+
{
60+
return $this->findOne($this->schema, ['weight' => $weight]);
61+
}
62+
63+
public function findByAnyWeight(array $weights): array
64+
{
65+
return $this->find($this->schema, ['weight' => array_map(function (?float $weight): ?float {
66+
return $weight;
67+
}, $weights)]);
68+
}
69+
70+
public function findByHasLicense(bool $hasLicense): array
71+
{
72+
return $this->find($this->schema, ['license' => $hasLicense]);
73+
}
74+
3575
public function savePerson(TestPersonModel $model): void
3676
{
3777
$this->save($model);

tests/helpers/TestPersonSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestPersonSchema extends Schema
1818

1919
protected $primaryKeys = ['id'];
2020

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

2323
protected $references;
2424
}

tests/tests/MySqlIntegrationTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ protected function setUpDatabase(Connection $connection): void
3131
$pdo->exec(sprintf('DROP TABLE IF EXISTS `%s`', $this->personSchema->getTable()));
3232
$pdo->exec(sprintf(<<<'SQL'
3333
CREATE TABLE `%s` (
34-
`id` INT PRIMARY KEY AUTO_INCREMENT,
35-
`first_name` TEXT,
36-
`last_name` TEXT,
37-
`age` INT
34+
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
35+
`first_name` TEXT NOT NULL,
36+
`last_name` TEXT NOT NULL,
37+
`age` INT NOT NULL,
38+
`weight` DECIMAL(5,2) NULL,
39+
`license` BOOL DEFAULT FALSE
3840
)
3941
SQL
4042
, $this->personSchema->getTable()));

0 commit comments

Comments
 (0)