Skip to content

Commit 31944d6

Browse files
committed
Update Entity tests
1 parent 1fda6fd commit 31944d6

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

tests/EntityMock.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class EntityMock extends Entity
3939
protected Date $date;
4040
protected URL $url;
4141
protected mixed $mixed;
42-
protected $id; // @phpstan-ignore-line
43-
protected $data; // @phpstan-ignore-line
44-
protected $createdAt; // @phpstan-ignore-line
42+
protected int $id;
43+
protected string $data;
44+
protected Date $createdAt;
4545
protected $updatedAt; // @phpstan-ignore-line
4646

4747
public function setId(mixed $id) : static

tests/EntityTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use DateTime;
1313
use Framework\Date\Date;
1414
use Framework\HTTP\URL;
15+
use Framework\MVC\Entity;
1516
use PHPUnit\Framework\TestCase;
1617
use stdClass;
1718

@@ -166,9 +167,10 @@ public function testToModel() : void
166167
'date' => '2021-09-15 15:47:08',
167168
'url' => 'https://foo.com/',
168169
'mixed' => null,
170+
/* Properties not set:
169171
'id' => null,
170172
'data' => null,
171-
'createdAt' => null,
173+
'createdAt' => null,*/
172174
'updatedAt' => null,
173175
], $this->entity->toModel());
174176
}
@@ -190,7 +192,37 @@ public function testToString() : void
190192
$values = \json_decode($json, true);
191193
self::assertArrayHasKey('array', $values);
192194
self::assertIsArray($values['array']);
193-
self::assertArrayHasKey('id', $values);
194-
self::assertNull($values['id']);
195+
self::assertArrayNotHasKey('id', $values);
196+
}
197+
198+
public function testGetObjectVars() : void
199+
{
200+
$data = [
201+
'id2' => 1,
202+
'id5' => null,
203+
];
204+
$user = new class($data) extends Entity {
205+
protected int $id; // not set
206+
protected int $id2; // set with 1
207+
protected ?int $id3; // nullable not set
208+
protected ?int $id4 = null; // nullable set with default value (null)
209+
protected ?int $id5; // nullable set with null
210+
protected ?int $id6 = 5; // nullable set with default value (5)
211+
// @phpstan-ignore-next-line
212+
protected $id7; // property without type is null
213+
protected string | int $id8; // not set
214+
215+
public function getObjectVars() : array
216+
{
217+
return parent::getObjectVars();
218+
}
219+
};
220+
self::assertSame([
221+
'id2' => 1,
222+
'id4' => null,
223+
'id5' => null,
224+
'id6' => 5,
225+
'id7' => null,
226+
], $user->getObjectVars());
195227
}
196228
}

0 commit comments

Comments
 (0)