From 347151297ff5adad08a2ae080ee916e8808a7cf1 Mon Sep 17 00:00:00 2001 From: Hugo Rafael Azevedo Date: Wed, 23 Mar 2022 18:58:42 +0000 Subject: [PATCH 1/5] feat: add support for native stdclass objects --- src/ValueObjects/Traits/CanProcessEntityStateTrait.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ValueObjects/Traits/CanProcessEntityStateTrait.php b/src/ValueObjects/Traits/CanProcessEntityStateTrait.php index 169b021..64d9df5 100644 --- a/src/ValueObjects/Traits/CanProcessEntityStateTrait.php +++ b/src/ValueObjects/Traits/CanProcessEntityStateTrait.php @@ -73,6 +73,8 @@ final public function getDirty(bool $withTimestamps = false): array if (\is_object($this->{$field}) && \method_exists($this->{$field}, '__toString')) { $hasChanged = ((string) $value) !== ((string) $this->{$field}); + } elseif (\is_object($this->{$field})) { + $hasChanged = $value !== ((array) $this->{$field}); } else { $hasChanged = $value !== $this->{$field}; } From 2ca8743b065684c5575a6341e3e3e1b678cf0a3d Mon Sep 17 00:00:00 2001 From: Hugo Rafael Azevedo Date: Wed, 23 Mar 2022 18:58:53 +0000 Subject: [PATCH 2/5] tests: increase test coverage --- tests/Unit/ValueObjects/AbstractValueObjectTest.php | 6 ++++++ tests/Unit/ValueObjects/TestingValueObject.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/tests/Unit/ValueObjects/AbstractValueObjectTest.php b/tests/Unit/ValueObjects/AbstractValueObjectTest.php index 429fcfd..0d13e44 100644 --- a/tests/Unit/ValueObjects/AbstractValueObjectTest.php +++ b/tests/Unit/ValueObjects/AbstractValueObjectTest.php @@ -15,6 +15,7 @@ * @package HraDigital\Datatypes * @copyright HraDigital\Datatypes * @license MIT + * @group hugo */ class AbstractValueObjectTest extends AbstractBaseTestCase { @@ -75,6 +76,7 @@ public function testCanConvertToJsonWhileGuardingCertainAttributes(): void $this->assertArrayNotHasKey('email', $json); $this->assertArrayHasKey('title', $json); $this->assertArrayHasKey('inner', $json); + $this->assertArrayHasKey('native', $json); $this->assertArrayHasKey('title', $json['inner']); $this->assertArrayNotHasKey('active', $json['inner']); @@ -91,6 +93,7 @@ public function testCanConvertToArray(): void $this->assertArrayHasKey('email', $array); $this->assertArrayHasKey('title', $array); $this->assertArrayHasKey('inner', $array); + $this->assertArrayHasKey('native', $array); $this->assertArrayHasKey('title', $array['inner']); $this->assertArrayHasKey('active', $array['inner']); @@ -128,6 +131,7 @@ public function testCanCallDebugInfoInValueObject(): void $this->assertArrayHasKey('email', $array); $this->assertArrayHasKey('title', $array); $this->assertArrayHasKey('inner', $array); + $this->assertArrayHasKey('native', $array); $this->assertArrayHasKey('title', $array['inner']); $this->assertArrayHasKey('active', $array['inner']); @@ -160,6 +164,7 @@ public function testCanChangeAndTrackState(): void $this->assertArrayNotHasKey('email', $dirty); $this->assertArrayHasKey('title', $dirty); $this->assertArrayHasKey('inner', $dirty); + $this->assertArrayNotHasKey('native', $dirty); $this->assertArrayHasKey('title', $dirty['inner']); $this->assertArrayNotHasKey('active', $dirty['inner']); @@ -212,6 +217,7 @@ public function testCanResetState(): void $this->assertArrayNotHasKey('email', $dirty); $this->assertArrayNotHasKey('title', $dirty); $this->assertArrayNotHasKey('inner', $dirty); + $this->assertArrayNotHasKey('native', $dirty); $this->assertCount(0, $dirty); } diff --git a/tests/Unit/ValueObjects/TestingValueObject.php b/tests/Unit/ValueObjects/TestingValueObject.php index 8d62a64..b4969b9 100644 --- a/tests/Unit/ValueObjects/TestingValueObject.php +++ b/tests/Unit/ValueObjects/TestingValueObject.php @@ -34,6 +34,10 @@ class TestingValueObject extends AbstractValueObject 'active' => false, 'title' => 'My Inner Title', ], + 'native' => [ + 'active' => false, + 'title' => 'Some random native object', + ], ]; use HasPositiveIntegerIDTrait, @@ -58,12 +62,18 @@ class TestingValueObject extends AbstractValueObject ]; protected TestingNestedValueObject $inner; + protected \stdClass $native; protected function castInner(array $inner): void { $this->inner = new TestingNestedValueObject($inner); } + protected function castNative(array $native): void + { + $this->native = (object) $native; + } + public function getInner(): TestingNestedValueObject { return $this->inner; From 5ebcd83630d97beb57ee3a724b08b642df49f49f Mon Sep 17 00:00:00 2001 From: Hugo Rafael Azevedo Date: Wed, 23 Mar 2022 19:23:38 +0000 Subject: [PATCH 3/5] feat: add missing toDateString() --- src/Datetime/Datetime.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Datetime/Datetime.php b/src/Datetime/Datetime.php index 38789ad..ae58f5c 100644 --- a/src/Datetime/Datetime.php +++ b/src/Datetime/Datetime.php @@ -197,6 +197,16 @@ public function toDatetimeString(): Str return $this->toFormatInternal('Y-m-d H:i:s'); } + /** + * Returns Str instance with value in format "Y-m-d". + * + * @return Str + */ + public function toDateString(): Str + { + return $this->toFormatInternal('Y-m-d'); + } + /** * Returns Str instance with value in format "H:i:s". * From f7a20ffb6852380c5454213f60233d5dd27dca7e Mon Sep 17 00:00:00 2001 From: Hugo Rafael Azevedo Date: Wed, 23 Mar 2022 19:23:57 +0000 Subject: [PATCH 4/5] refactor: change how exception is raised --- src/Attributes/Personal/HasGenderTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Attributes/Personal/HasGenderTrait.php b/src/Attributes/Personal/HasGenderTrait.php index 2fe3e17..4e21023 100644 --- a/src/Attributes/Personal/HasGenderTrait.php +++ b/src/Attributes/Personal/HasGenderTrait.php @@ -35,7 +35,7 @@ protected function castGender(string $gender): void $genderValue->equals('Female') || $genderValue->equals('Other') )) { - throw new UnexpectedEntityValueException('$gender'); + throw UnexpectedEntityValueException::withName('$gender'); } $this->gender = $genderValue; From 2050d8926a9ca9789657f8c724cd6a779c90d6d0 Mon Sep 17 00:00:00 2001 From: Hugo Rafael Azevedo Date: Wed, 23 Mar 2022 19:24:10 +0000 Subject: [PATCH 5/5] tests: increase coverage --- tests/Unit/Attributes/GeneralTraitsVOTest.php | 10 ++ .../Unit/Attributes/PersonalTraitsVOTest.php | 19 ++++ tests/Unit/Datetime/DatetimeTest.php | 105 +++++++++++++++++- .../ValueObjects/AbstractValueObjectTest.php | 1 - 4 files changed, 133 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Attributes/GeneralTraitsVOTest.php b/tests/Unit/Attributes/GeneralTraitsVOTest.php index e7a2cc1..55dd44d 100644 --- a/tests/Unit/Attributes/GeneralTraitsVOTest.php +++ b/tests/Unit/Attributes/GeneralTraitsVOTest.php @@ -145,6 +145,16 @@ public function testBreaksIfAliasIsEmpty(): void new GeneralTraitsVO($data); } + public function testBreaksIfSurnameIsEmpty(): void + { + $this->expectException(NonEmptyStringException::class); + + $data = self::DATA; + $data['surname'] = ''; + + new GeneralTraitsVO($data); + } + public function testBreaksIfHitsIsNegative(): void { $this->expectException(NonNegativeNumberException::class); diff --git a/tests/Unit/Attributes/PersonalTraitsVOTest.php b/tests/Unit/Attributes/PersonalTraitsVOTest.php index d3409db..e7d1522 100644 --- a/tests/Unit/Attributes/PersonalTraitsVOTest.php +++ b/tests/Unit/Attributes/PersonalTraitsVOTest.php @@ -5,6 +5,7 @@ namespace HraDigital\Tests\Datatypes\Unit\Attributes; use HraDigital\Datatypes\Exceptions\Datatypes\NonEmptyStringException; +use HraDigital\Datatypes\Exceptions\Entities\UnexpectedEntityValueException; use HraDigital\Tests\Datatypes\AbstractBaseTestCase; /** @@ -36,6 +37,24 @@ public function testLoadsSuccessfully(): void $this->assertTrue($object->hasPhoto()); } + public function testLoadsSuccessfullyWithDifferentGender(): void + { + $data = self::DATA; + $data['gender'] = 'Female'; + $object = new PersonalTraitsVO($data); + + $this->assertEquals($data['gender'], (string) $object->getGender()); + } + + public function testBreaksIfGenderIsNotSupported(): void + { + $this->expectException(UnexpectedEntityValueException::class); + + $data = self::DATA; + $data['gender'] = 'Unsupported'; + new PersonalTraitsVO($data); + } + public function testBreaksWithEmptyCoutryOfBirth(): void { $data = self::DATA; diff --git a/tests/Unit/Datetime/DatetimeTest.php b/tests/Unit/Datetime/DatetimeTest.php index 1589fb2..ab33ce8 100644 --- a/tests/Unit/Datetime/DatetimeTest.php +++ b/tests/Unit/Datetime/DatetimeTest.php @@ -19,7 +19,7 @@ class DatetimeTest extends AbstractBaseTestCase { const DATETIME = '2021-05-06 10:11:12'; - public function testCanInstanciateSuccessfully(): void + public function testCanInstanciateSuccessfullyFromString(): void { $dt = Datetime::fromString(self::DATETIME); @@ -34,6 +34,109 @@ public function testCanInstanciateSuccessfully(): void $this->assertEquals($dt->jsonSerialize(), self::DATETIME); } + public function testCanInstantiateSuccessfullyFromNow(): void + { + $datetime = Datetime::now(); + $native = new \DateTime('now'); + + $this->assertEquals( + $datetime->toDateString(), + $native->format('Y-m-d') + ); + } + + public function testCanInstantiateSuccessfullyFromToday(): void + { + $datetime = Datetime::today(); + $native = new \DateTime('now'); + + $this->assertEquals( + $datetime->toDateString(), + $native->format('Y-m-d') + ); + $this->assertEquals(0, $datetime->getHour()); + $this->assertEquals(0, $datetime->getMinute()); + $this->assertEquals(0, $datetime->getSecond()); + } + + public function testCanInstantiateSuccessfullyFromTomorrow(): void + { + $datetime = Datetime::tomorrow(); + $native = new \DateTime('now'); + + $this->assertEquals( + $datetime->addDays(-1)->toDateString(), + $native->format('Y-m-d') + ); + $this->assertEquals(0, $datetime->getHour()); + $this->assertEquals(0, $datetime->getMinute()); + $this->assertEquals(0, $datetime->getSecond()); + } + + public function testCanInstantiateSuccessfullyFromYesterday(): void + { + $datetime = Datetime::yesterday(); + $native = new \DateTime('now'); + + $this->assertEquals( + $datetime->addDays(1)->toDateString(), + $native->format('Y-m-d') + ); + $this->assertEquals(0, $datetime->getHour()); + $this->assertEquals(0, $datetime->getMinute()); + $this->assertEquals(0, $datetime->getSecond()); + } + + public function testCanInstantiateSuccessfullyFromTimestamp(): void + { + $original = Datetime::now(); + $timestamp = $original->getTimestamp(); + + $fromTimestamp = Datetime::fromTimestamp($timestamp); + + $this->assertFalse($original === $fromTimestamp); + $this->assertEquals( + $original->toDatetimeString(), + $fromTimestamp->toDatetimeString() + ); + } + + public function testCanInstantiateSuccessfullyFromUnits(): void + { + $years = 2020; + $months = 11; + $days = 10; + $hours = 9; + $minutes = 30; + $seconds = 0; + $dt = Datetime::fromUnits($years, $months, $days, $hours, $minutes, $seconds); + + $this->assertEquals( + $years, + $dt->getYear() + ); + $this->assertEquals( + $months, + $dt->getMonth() + ); + $this->assertEquals( + $days, + $dt->getDay() + ); + $this->assertEquals( + $hours, + $dt->getHour() + ); + $this->assertEquals( + $minutes, + $dt->getMinute() + ); + $this->assertEquals( + $seconds, + $dt->getSecond() + ); + } + public function testCanAddInterval(): void { $dt = Datetime::fromString(self::DATETIME); diff --git a/tests/Unit/ValueObjects/AbstractValueObjectTest.php b/tests/Unit/ValueObjects/AbstractValueObjectTest.php index 0d13e44..b8fd680 100644 --- a/tests/Unit/ValueObjects/AbstractValueObjectTest.php +++ b/tests/Unit/ValueObjects/AbstractValueObjectTest.php @@ -15,7 +15,6 @@ * @package HraDigital\Datatypes * @copyright HraDigital\Datatypes * @license MIT - * @group hugo */ class AbstractValueObjectTest extends AbstractBaseTestCase {