Skip to content

Commit 02e0814

Browse files
author
Greg Bowler
committed
test: DataObject::getArray() Fixed-type arrays handle inherited values
1 parent be9af5f commit 02e0814

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/DataObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private function checkType(mixed $value, string $type):void {
210210
break;
211211

212212
default:
213-
$typeMatches = (get_class($value) === $type);
213+
$typeMatches = ($value instanceof $type);
214214
break;
215215
}
216216

test/phpunit/DataObjectTest.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Gt\DataObject\Test;
33

44
use DateTime;
5+
use DateTimeInterface;
56
use Gt\DataObject\DataObject;
67
use PHPUnit\Framework\TestCase;
78
use stdClass;
@@ -349,7 +350,7 @@ public function testJsonSerialize() {
349350
);
350351
}
351352

352-
public function testGetArrayFixedTypes() {
353+
public function testGetArrayFixedTypeInt() {
353354
$timestampArray = [
354355
49997,
355356
50000,
@@ -367,6 +368,67 @@ public function testGetArrayFixedTypes() {
367368
self::assertEquals(count($timestampArray), $i + 1);
368369
}
369370

371+
public function testGetArrayFixedTypeFloat() {
372+
$timestampArray = [
373+
49997.24567,
374+
50000.28356,
375+
49999.92846,
376+
50004.89137,
377+
50001.10239,
378+
];
379+
$sut = (new DataObject())
380+
->with("timestamps", $timestampArray);
381+
$array = $sut->getArray("timestamps", "float");
382+
foreach($array as $i => $value) {
383+
self::assertIsFloat($value);
384+
}
385+
}
386+
387+
public function testGetArrayFixedTypeBool() {
388+
$timestampArray = [
389+
true,
390+
false,
391+
true,
392+
false,
393+
false,
394+
];
395+
$sut = (new DataObject())
396+
->with("timestamps", $timestampArray);
397+
$array = $sut->getArray("timestamps", "bool");
398+
foreach($array as $i => $value) {
399+
self::assertIsBool($value);
400+
}
401+
}
402+
403+
public function testGetArrayFixedTypeString() {
404+
$wordsArray = [
405+
"one",
406+
"two",
407+
"three",
408+
"four",
409+
"five",
410+
];
411+
$sut = (new DataObject())
412+
->with("words", $wordsArray);
413+
$array = $sut->getArray("words", "string");
414+
foreach($array as $i => $value) {
415+
self::assertIsString($value);
416+
}
417+
}
418+
419+
public function testGetArrayFixedTypeDateTime() {
420+
$dateArray = [
421+
new DateTime("1st Jan 1970 00:00:00"),
422+
new DateTime("5th Apr 1988 17:21:05"),
423+
];
424+
$sut = (new DataObject())
425+
->with("dates", $dateArray);
426+
$array = $sut->getArray("dates", DateTimeInterface::class);
427+
foreach($array as $i => $value) {
428+
self::assertInstanceOf(DateTimeInterface::class, $value);
429+
}
430+
}
431+
370432
public function testGetArrayFixedTypesMismatch() {
371433
$timestampArray = [
372434
49997,

0 commit comments

Comments
 (0)