Skip to content

Commit

Permalink
Merge pull request #5 from HRADigital/increase-test-coverage
Browse files Browse the repository at this point in the history
Increase test coverage
  • Loading branch information
HRADigital authored Mar 23, 2022
2 parents e09cc2b + 2050d89 commit b20c5a8
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Attributes/Personal/HasGenderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Datetime/Datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
*
Expand Down
2 changes: 2 additions & 0 deletions src/ValueObjects/Traits/CanProcessEntityStateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Attributes/GeneralTraitsVOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Attributes/PersonalTraitsVOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
105 changes: 104 additions & 1 deletion tests/Unit/Datetime/DatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/ValueObjects/AbstractValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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']);
Expand All @@ -91,6 +92,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']);
Expand Down Expand Up @@ -128,6 +130,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']);
Expand Down Expand Up @@ -160,6 +163,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']);
Expand Down Expand Up @@ -212,6 +216,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);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/ValueObjects/TestingValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class TestingValueObject extends AbstractValueObject
'active' => false,
'title' => 'My Inner Title',
],
'native' => [
'active' => false,
'title' => 'Some random native object',
],
];

use HasPositiveIntegerIDTrait,
Expand All @@ -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;
Expand Down

0 comments on commit b20c5a8

Please sign in to comment.