diff --git a/Makefile b/Makefile index 992e847..7fcb08a 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ fix-permission: ## fix permission for docker env .PHONY: build build: ## build environment and initialize composer and project dependencies - docker build .docker/php$(DOCKER_PHP_VERSION)-cli/ -t $(DOCKER_SERVER_HOST):$(DOCKER_SERVER_PORT)/$(DOCKER_PROJECT_PATH)/php$(DOCKER_PHP_VERSION)-cli:$(DOCKER_IMAGE_VERSION) \ + docker build .docker/php$(DOCKER_PHP_VERSION)-dev/ -t $(DOCKER_SERVER_HOST):$(DOCKER_SERVER_PORT)/$(DOCKER_PROJECT_PATH)/php$(DOCKER_PHP_VERSION)-dev:$(DOCKER_IMAGE_VERSION) \ --build-arg DOCKER_SERVER_HOST=$(DOCKER_SERVER_HOST) \ --build-arg DOCKER_SERVER_PORT=$(DOCKER_SERVER_PORT) \ --build-arg DOCKER_PROJECT_PATH=$(DOCKER_PROJECT_PATH) \ diff --git a/phpstan.neon b/phpstan.neon index dab72a5..a5e4a51 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,6 @@ includes: parameters: ignoreErrors: - - '#Call to method PHPUnit\\Framework\\Assert::assertSame\(\) with .IPv.. and AdgoalCommon\\ValueObject\\Web\\IPAddressVersion will always evaluate to false\.#' - '#Parameter \#1 \$value of class AdgoalCommon\\ValueObject\\Geography\\Latitude constructor expects float, string given.#' - '#Parameter \#1 \$value of class AdgoalCommon\\ValueObject\\Geography\\Longitude constructor expects float, string given.#' - '#Parameter \#1 \$value of class AdgoalCommon\\ValueObject\\Number\\Real constructor expects float, string given.#' @@ -13,5 +12,4 @@ parameters: - '#Call to an undefined static method.*#' - '#Static call to instance method.*#' - '#Parameter \#1 \$value of class AdgoalCommon\\ValueObject\\Number\\Integer constructor expects int, float given.#' - - '#PHPDoc tag \@param for parameter $value with type float|int is not subtype of native type float\.#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 148f5a8..59a3a45 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,6 +21,12 @@ + + + src + + + diff --git a/src/DateTime/Date.php b/src/DateTime/Date.php index 5f55728..a233067 100644 --- a/src/DateTime/Date.php +++ b/src/DateTime/Date.php @@ -120,7 +120,7 @@ public function __construct(Year $year, Month $month, MonthDay $day) $nativeDateErrors = DateTime::getLastErrors(); if ($nativeDateErrors['warning_count'] > 0 || $nativeDateErrors['error_count'] > 0) { - throw new InvalidDateException($year->toNative(), $month->toNative(), $day->toNative()); + throw new InvalidDateException($year->toNative(), $month->getNumericValue(), $day->toNative()); } $this->year = $year; diff --git a/src/DateTime/DateTimeWithTimeZone.php b/src/DateTime/DateTimeWithTimeZone.php index 43509eb..0ba31ad 100644 --- a/src/DateTime/DateTimeWithTimeZone.php +++ b/src/DateTime/DateTimeWithTimeZone.php @@ -147,7 +147,7 @@ public function sameTimestampAs(ValueObjectInterface $dateTimeWithTimeZone): boo return false; } - return $this->toNativeDateTime() === $dateTimeWithTimeZone->toNativeDateTime(); + return $this->toNativeDateTime()->getTimestamp() === $dateTimeWithTimeZone->toNativeDateTime()->getTimestamp(); } /** diff --git a/src/Identity/HashedPassword.php b/src/Identity/HashedPassword.php new file mode 100644 index 0000000..40801e5 --- /dev/null +++ b/src/Identity/HashedPassword.php @@ -0,0 +1,33 @@ +value); + } +} diff --git a/src/Identity/PlainPassword.php b/src/Identity/PlainPassword.php new file mode 100644 index 0000000..fe11dc3 --- /dev/null +++ b/src/Identity/PlainPassword.php @@ -0,0 +1,83 @@ +validateContentLength($value, $minChars, $maxChars); + $this->validateContent($value, $rules); + + parent::__construct($value); + } + + /** + * @param string $value + * @param int $minChars + * @param int $maxChars + */ + private function validateContentLength(string $value, int $minChars, int $maxChars): void + { + if ($minChars > $maxChars) { + throw new InvalidNativeArgumentException($minChars, ['int (min should be <= max']); + } + + if (mb_strlen($value) < $minChars || mb_strlen($value) > $maxChars) { + throw new InvalidNativeArgumentException( + 'password', + [sprintf('string (length: >= %d, <=%d)', $minChars, $maxChars)] + ); + } + } + + /** + * @param string $value + * @param int[] $rules + */ + private function validateContent(string $value, array $rules): void + { + if (in_array(self::MUST_CONTAINS_LOWER_LETTER, $rules) && !preg_match('/[a-z]/', $value)) { + throw new InvalidNativeArgumentException('password', ['string (lower letter required)']); + } + + if (in_array(self::MUST_CONTAINS_UPPER_LETTER, $rules) && !preg_match('/[A-Z]/', $value)) { + throw new InvalidNativeArgumentException('password', ['string (upper letter required)']); + } + + if (in_array(self::MUST_CONTAINS_DIGIT, $rules) && !preg_match('/[0-9]/', $value)) { + throw new InvalidNativeArgumentException('password', ['string (digit required)']); + } + + if (in_array(self::MUST_CONTAINS_SPECIAL_SYMBOL, $rules) && !preg_match('/[^a-z0-9 ]/i', $value)) { + throw new InvalidNativeArgumentException('password', ['string (special symbol required)']); + } + } +} diff --git a/src/Identity/UUID.php b/src/Identity/UUID.php index 20d9a0b..6afa4eb 100644 --- a/src/Identity/UUID.php +++ b/src/Identity/UUID.php @@ -5,7 +5,6 @@ namespace AdgoalCommon\ValueObject\Identity; use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; -use AdgoalCommon\ValueObject\StringLiteral\StringLiteral; use AdgoalCommon\ValueObject\ValueObjectInterface; use Exception; use Ramsey\Uuid\Uuid as BaseUuid; diff --git a/src/Money/Money.php b/src/Money/Money.php index d304efd..161b3e0 100644 --- a/src/Money/Money.php +++ b/src/Money/Money.php @@ -137,7 +137,7 @@ public function multiply(Real $multiplier, ?RoundingMode $roundingMode = null): } $amount = $this->getAmount()->toNative() * $multiplier->toNative(); - $roundedAmount = new IntegerValueObject(round($amount, 0, $roundingMode->toNative())); + $roundedAmount = new IntegerValueObject((int) round($amount, 0, $roundingMode->toNative())); return new self($roundedAmount, $this->getCurrency()); } diff --git a/src/Number/Integer.php b/src/Number/Integer.php index 2b1c908..35ae311 100644 --- a/src/Number/Integer.php +++ b/src/Number/Integer.php @@ -10,8 +10,25 @@ /** * Class Integer. */ -class Integer extends Real +class Integer implements ValueObjectInterface, NumberInterface { + /** + * @var int + */ + protected $value; + + /** + * Returns a Real object given a PHP native float as parameter. + * + * @return static + */ + public static function fromNative(): ValueObjectInterface + { + $value = func_get_arg(0); + + return new static($value); + } + /** * Returns a Integer object given a PHP native int as parameter. * @@ -25,7 +42,7 @@ public function __construct(int $value) throw new InvalidNativeArgumentException($value, ['int']); } - parent::__construct($value); + $this->value = $value; } /** @@ -51,9 +68,7 @@ public function sameValueAs(ValueObjectInterface $integer): bool */ public function toNative() { - $value = parent::toNative(); - - return (int) $value; + return $this->value; } /** @@ -91,4 +106,14 @@ public function decr(): self return $this; } + + /** + * Returns the string representation of the real value. + * + * @return string + */ + public function __toString(): string + { + return (string) $this->toNative(); + } } diff --git a/src/Number/Real.php b/src/Number/Real.php index 2d08f60..e4a3061 100644 --- a/src/Number/Real.php +++ b/src/Number/Real.php @@ -14,7 +14,7 @@ class Real implements ValueObjectInterface, NumberInterface { /** - * @var float|int + * @var float */ protected $value; @@ -33,7 +33,7 @@ public static function fromNative(): ValueObjectInterface /** * Returns a Real object given a PHP native float as parameter. * - * @param float|int $value + * @param float $value */ public function __construct(float $value) { @@ -49,9 +49,9 @@ public function __construct(float $value) /** * Returns the native value of the real number. * - * @return float|int + * @return float */ - public function toNative() + public function toNative(): float { return $this->value; } @@ -59,17 +59,30 @@ public function toNative() /** * Tells whether two Real are equal by comparing their values. * - * @param ValueObjectInterface $real + * @param ValueObjectInterface $value + * @param int $scale * * @return bool */ - public function sameValueAs(ValueObjectInterface $real): bool + public function sameValueAs(ValueObjectInterface $value, int $scale = 0): bool { - if (!$real instanceof static) { + if (!$value instanceof static) { return false; } - return $this->toNative() === $real->toNative(); + if (extension_loaded('bcmath')) { + return 0 === bccomp((string) $this->toNative(), (string) $value->toNative(), $scale); + } + + if ($scale > 0) { + $ratio = pow(10, $scale); + $selfValue = (new self($this->toNative() * $ratio))->toInteger(); + $incomeValue = (new self($value->toNative() * $ratio))->toInteger(); + + return $selfValue->toNative() === $incomeValue->toNative(); + } + + return (string) $this->toNative() === (string) $value->toNative(); } /** diff --git a/src/Web/EmailAddress.php b/src/Person/EmailAddress.php similarity index 93% rename from src/Web/EmailAddress.php rename to src/Person/EmailAddress.php index aad9abe..1f65695 100644 --- a/src/Web/EmailAddress.php +++ b/src/Person/EmailAddress.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace AdgoalCommon\ValueObject\Web; +namespace AdgoalCommon\ValueObject\Person; use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\StringLiteral\StringLiteral; +use AdgoalCommon\ValueObject\Web\Domain; /** * Class EmailAddress. diff --git a/src/Person/Name.php b/src/Person/Name.php index f07dbd6..1019eb9 100644 --- a/src/Person/Name.php +++ b/src/Person/Name.php @@ -135,7 +135,7 @@ public function sameValueAs(ValueObjectInterface $name): bool return false; } - return $this->getFullName() === $name->getFullName(); + return $this->getFullName() == $name->getFullName(); } /** diff --git a/src/Person/PhoneNumber.php b/src/Person/PhoneNumber.php new file mode 100644 index 0000000..9925a37 --- /dev/null +++ b/src/Person/PhoneNumber.php @@ -0,0 +1,23 @@ + $keyValuePair) { if (false === $keyValuePair instanceof KeyValuePair) { $type = is_object($keyValuePair) ? get_class($keyValuePair) : gettype($keyValuePair); diff --git a/src/Structure/KeyValuePair.php b/src/Structure/KeyValuePair.php index 63cc9f0..d1c331a 100644 --- a/src/Structure/KeyValuePair.php +++ b/src/Structure/KeyValuePair.php @@ -124,6 +124,6 @@ public function __toString(): string */ public function toArray(): array { - return[$this->getKey()->toNative(), $this->getValue()->toNative()]; + return [$this->getKey()->toNative() => $this->getValue()->toNative()]; } } diff --git a/src/Web/FragmentIdentifierInterface.php b/src/Web/FragmentIdentifierInterface.php index 941bed2..b3dfba1 100644 --- a/src/Web/FragmentIdentifierInterface.php +++ b/src/Web/FragmentIdentifierInterface.php @@ -4,9 +4,11 @@ namespace AdgoalCommon\ValueObject\Web; +use AdgoalCommon\ValueObject\ValueObjectInterface; + /** * Interface FragmentIdentifierInterface. */ -interface FragmentIdentifierInterface +interface FragmentIdentifierInterface extends ValueObjectInterface { } diff --git a/src/Web/IPAddress.php b/src/Web/IPAddress.php index 32ca015..e4cd70f 100644 --- a/src/Web/IPAddress.php +++ b/src/Web/IPAddress.php @@ -37,9 +37,9 @@ public function getVersion(): IPAddressVersion $isIPv4 = filter_var($this->toNative(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); if (false !== $isIPv4) { - return IPAddressVersion::IPV4(); + return IPAddressVersion::byValue(IPAddressVersion::IPV4); } - return IPAddressVersion::IPV6(); + return IPAddressVersion::byValue(IPAddressVersion::IPV6); } } diff --git a/src/Web/NullFragmentIdentifier.php b/src/Web/NullFragmentIdentifier.php index 769d180..6f31ea6 100644 --- a/src/Web/NullFragmentIdentifier.php +++ b/src/Web/NullFragmentIdentifier.php @@ -4,10 +4,12 @@ namespace AdgoalCommon\ValueObject\Web; +use AdgoalCommon\ValueObject\StringLiteral\StringLiteral; + /** * Class NullFragmentIdentifier. */ -class NullFragmentIdentifier extends FragmentIdentifier implements FragmentIdentifierInterface +class NullFragmentIdentifier extends StringLiteral implements FragmentIdentifierInterface { /** * Returns a new NullFragmentIdentifier. diff --git a/src/Web/NullQueryString.php b/src/Web/NullQueryString.php index 03dde01..a793c71 100644 --- a/src/Web/NullQueryString.php +++ b/src/Web/NullQueryString.php @@ -4,10 +4,13 @@ namespace AdgoalCommon\ValueObject\Web; +use AdgoalCommon\ValueObject\StringLiteral\StringLiteral; +use AdgoalCommon\ValueObject\Structure\Dictionary; + /** * Class NullQueryString. */ -class NullQueryString extends QueryString implements QueryStringInterface +class NullQueryString extends StringLiteral implements QueryStringInterface { /** * Returns a new NullQueryString. @@ -16,4 +19,9 @@ public function __construct() { parent::__construct(''); } + + public function toDictionary(): Dictionary + { + return Dictionary::fromNative([]); + } } diff --git a/src/Web/Path.php b/src/Web/Path.php index 76792e4..1b082f0 100644 --- a/src/Web/Path.php +++ b/src/Web/Path.php @@ -21,7 +21,7 @@ public function __construct(string $value) { $filteredValue = parse_url($value, PHP_URL_PATH); - if (null === $filteredValue || strlen($filteredValue) !== strlen($value)) { + if (!empty($value) && null === $filteredValue || strlen($filteredValue) !== strlen($value)) { throw new InvalidNativeArgumentException($value, ['string (valid url path)']); } diff --git a/src/Web/QueryStringInterface.php b/src/Web/QueryStringInterface.php index 6d58bdf..19d77fe 100644 --- a/src/Web/QueryStringInterface.php +++ b/src/Web/QueryStringInterface.php @@ -5,11 +5,12 @@ namespace AdgoalCommon\ValueObject\Web; use AdgoalCommon\ValueObject\Structure\Dictionary; +use AdgoalCommon\ValueObject\ValueObjectInterface; /** * Interface QueryStringInterface. */ -interface QueryStringInterface +interface QueryStringInterface extends ValueObjectInterface { /** * @return Dictionary diff --git a/src/Web/Url.php b/src/Web/Url.php index 4969120..c5c01be 100644 --- a/src/Web/Url.php +++ b/src/Web/Url.php @@ -11,6 +11,8 @@ /** * Class Url. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Url implements ValueObjectInterface { @@ -59,14 +61,14 @@ class Url implements ValueObjectInterface /** * QueryString ValueObject. * - * @var QueryString + * @var QueryStringInterface */ protected $queryString; /** * FragmentIdentifier ValueObject. * - * @var FragmentIdentifier + * @var FragmentIdentifierInterface */ protected $fragmentIdentifier; @@ -85,12 +87,13 @@ public static function fromNative(): ValueObjectInterface $queryString = parse_url($urlString, PHP_URL_QUERY); $fragmentId = parse_url($urlString, PHP_URL_FRAGMENT); $port = parse_url($urlString, PHP_URL_PORT); + $path = parse_url($urlString, PHP_URL_PATH); $scheme = new SchemeName(parse_url($urlString, PHP_URL_SCHEME)); $user = $user ? new StringLiteral($user) : new StringLiteral(''); $pass = $pass ? new StringLiteral($pass) : new StringLiteral(''); $domain = Domain::specifyType($host); - $path = new Path(parse_url($urlString, PHP_URL_PATH)); + $path = new Path($path ?? ''); $portNumber = $port ? new PortNumber($port) : new NullPortNumber(); $query = $queryString ? new QueryString(sprintf('?%s', $queryString)) : new NullQueryString(); $fragment = $fragmentId ? new FragmentIdentifier(sprintf('#%s', $fragmentId)) : new NullFragmentIdentifier(); @@ -101,17 +104,25 @@ public static function fromNative(): ValueObjectInterface /** * Returns a new Url object. * - * @param SchemeName $scheme - * @param StringLiteral $user - * @param StringLiteral $password - * @param Domain $domain - * @param PortNumberInterface $port - * @param Path $path - * @param QueryString $query - * @param FragmentIdentifier $fragment + * @param SchemeName $scheme + * @param StringLiteral $user + * @param StringLiteral $password + * @param Domain $domain + * @param PortNumberInterface $port + * @param Path $path + * @param QueryStringInterface $query + * @param FragmentIdentifierInterface $fragment */ - public function __construct(SchemeName $scheme, StringLiteral $user, StringLiteral $password, Domain $domain, PortNumberInterface $port, Path $path, QueryString $query, FragmentIdentifier $fragment) - { + public function __construct( + SchemeName $scheme, + StringLiteral $user, + StringLiteral $password, + Domain $domain, + PortNumberInterface $port, + Path $path, + QueryStringInterface $query, + FragmentIdentifierInterface $fragment + ) { $this->scheme = $scheme; $this->user = $user; $this->password = $password; @@ -163,9 +174,9 @@ public function getDomain(): Domain /** * Returns the fragment identifier of the Url. * - * @return FragmentIdentifier + * @return FragmentIdentifierInterface */ - public function getFragmentIdentifier(): FragmentIdentifier + public function getFragmentIdentifier(): FragmentIdentifierInterface { return clone $this->fragmentIdentifier; } @@ -203,9 +214,9 @@ public function getPort(): PortNumberInterface /** * Returns the query string of the Url. * - * @return QueryString + * @return QueryStringInterface */ - public function getQueryString(): QueryString + public function getQueryString(): QueryStringInterface { return clone $this->queryString; } @@ -250,10 +261,10 @@ public function __toString(): string $userPass = ''; if (false === $this->getUser()->isEmpty()) { - $userPass = sprintf('%s@', $this->getUser()->__toString()); + $userPass = sprintf('%s@', (string) $this->getUser()); if (false === $this->getPassword()->isEmpty()) { - $userPass = sprintf('%s:%s@', $this->getUser()->__toString(), $this->getPassword()->__toString()); + $userPass = sprintf('%s:%s@', (string) $this->getUser(), (string) $this->getPassword()); } } @@ -263,14 +274,14 @@ public function __toString(): string $port = sprintf(':%d', $this->getPort()->toNative()); } - return sprintf(' %s://%s%s%s%s%s%s', - $this->getScheme()->__toString(), + return sprintf('%s://%s%s%s%s%s%s', + (string) $this->getScheme(), $userPass, - $this->getDomain()->__toString(), + (string) $this->getDomain(), $port, - $this->getPath()->__toString(), - $this->getQueryString()->__toString(), - $this->getFragmentIdentifier()->__toString() + (string) $this->getPath(), + (string) $this->getQueryString(), + (string) $this->getFragmentIdentifier() ); } } diff --git a/tests/unit/Climate/RelativeHumidityTest.php b/tests/unit/Climate/RelativeHumidityTest.php index acdba54..d1e1f7c 100644 --- a/tests/unit/Climate/RelativeHumidityTest.php +++ b/tests/unit/Climate/RelativeHumidityTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Climate; use AdgoalCommon\ValueObject\Climate\RelativeHumidity; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class RelativeHumidityTest extends TestCase @@ -17,11 +18,10 @@ public function testFromNative(): void $this->assertTrue($fromNativeRelHum->sameValueAs($constructedRelHum)); } - /** - * @expectedException \AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException - */ public function testInvalidRelativeHumidity(): void { + $this->expectException(InvalidNativeArgumentException::class); + new RelativeHumidity(128); } } diff --git a/tests/unit/DateTime/DateTest.php b/tests/unit/DateTime/DateTest.php index 4f7b8de..32ad1dd 100644 --- a/tests/unit/DateTime/DateTest.php +++ b/tests/unit/DateTime/DateTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; use AdgoalCommon\ValueObject\DateTime\Date; +use AdgoalCommon\ValueObject\DateTime\Exception\InvalidDateException; use AdgoalCommon\ValueObject\DateTime\Month; use AdgoalCommon\ValueObject\DateTime\MonthDay; use AdgoalCommon\ValueObject\DateTime\Year; @@ -37,9 +38,10 @@ public function testNow(): void $this->assertEquals(date('Y-n-j'), (string) $date); } - /** @expectedException AdgoalCommon\ValueObject\DateTime\Exception\InvalidDateException */ public function testAlmostValidDateException(): void { + $this->expectException(InvalidDateException::class); + new Date(new Year(2013), Month::FEBRUARY(), new MonthDay(31)); } diff --git a/tests/unit/DateTime/HourTest.php b/tests/unit/DateTime/HourTest.php index 5d9db2b..0da5d88 100644 --- a/tests/unit/DateTime/HourTest.php +++ b/tests/unit/DateTime/HourTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; use AdgoalCommon\ValueObject\DateTime\Hour; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class HourTest extends TestCase @@ -23,9 +24,9 @@ public function testNow(): void $this->assertEquals(date('G'), $hour->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidHour(): void { + $this->expectException(InvalidNativeArgumentException::class); new Hour(24); } } diff --git a/tests/unit/DateTime/MinuteTest.php b/tests/unit/DateTime/MinuteTest.php index f5bfb1c..dca6e86 100644 --- a/tests/unit/DateTime/MinuteTest.php +++ b/tests/unit/DateTime/MinuteTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; use AdgoalCommon\ValueObject\DateTime\Minute; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class MinuteTest extends TestCase @@ -23,9 +24,10 @@ public function testNow(): void $this->assertEquals(intval(date('i')), $minute->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidMinute(): void { + $this->expectException(InvalidNativeArgumentException::class); + new Minute(60); } } diff --git a/tests/unit/DateTime/MonthDayTest.php b/tests/unit/DateTime/MonthDayTest.php index 6e6a531..6ceabb1 100644 --- a/tests/unit/DateTime/MonthDayTest.php +++ b/tests/unit/DateTime/MonthDayTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; use AdgoalCommon\ValueObject\DateTime\MonthDay; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class MonthDayTest extends TestCase @@ -23,9 +24,10 @@ public function testNow(): void $this->assertEquals(date('j'), $monthDay->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidMonthDay(): void { + $this->expectException(InvalidNativeArgumentException::class); + new MonthDay(32); } } diff --git a/tests/unit/DateTime/SecondTest.php b/tests/unit/DateTime/SecondTest.php index 6fac1d4..811d149 100644 --- a/tests/unit/DateTime/SecondTest.php +++ b/tests/unit/DateTime/SecondTest.php @@ -5,6 +5,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; use AdgoalCommon\ValueObject\DateTime\Second; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class SecondTest extends TestCase @@ -23,9 +24,10 @@ public function testNow(): void $this->assertEquals(intval(date('s')), $second->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidSecond(): void { + $this->expectException(InvalidNativeArgumentException::class); + new Second(60); } } diff --git a/tests/unit/DateTime/TimeZoneTest.php b/tests/unit/DateTime/TimeZoneTest.php index 7b4ad2a..9e8f914 100644 --- a/tests/unit/DateTime/TimeZoneTest.php +++ b/tests/unit/DateTime/TimeZoneTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\DateTime; +use AdgoalCommon\ValueObject\DateTime\Exception\InvalidTimeZoneException; use AdgoalCommon\ValueObject\DateTime\TimeZone; use AdgoalCommon\ValueObject\StringLiteral\StringLiteral; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; @@ -72,11 +73,10 @@ public function testToString(): void $this->assertEquals('Europe/Madrid', $timeZone->__toString()); } - /** - * @expectedException \AdgoalCommon\ValueObject\DateTime\Exception\InvalidTimeZoneException - */ public function testExceptionOnInvalidTimeZoneName(): void { + $this->expectException(InvalidTimeZoneException::class); + new TimeZone(new StringLiteral('Mars/Phobos')); } } diff --git a/tests/unit/Enum/EnumTest.php b/tests/unit/Enum/EnumTest.php index 05b18f4..49476b8 100644 --- a/tests/unit/Enum/EnumTest.php +++ b/tests/unit/Enum/EnumTest.php @@ -11,8 +11,8 @@ class EnumTest extends TestCase { public function testSameValueAs(): void { - $stub1 = $this->getMockBuilder(Enum::class)->getMock(); - $stub2 = $this->getMockBuilder(Enum::class)->getMock(); + $stub1 = $this->getMockBuilder(Enum::class)->disableOriginalConstructor()->getMock(); + $stub2 = $this->getMockBuilder(Enum::class)->disableOriginalConstructor()->getMock(); $stub1->expects($this->any()) ->method('sameValueAs') @@ -23,7 +23,7 @@ public function testSameValueAs(): void public function testToString(): void { - $stub = $this->getMockBuilder(Enum::class)->getMock(); + $stub = $this->getMockBuilder(Enum::class)->disableOriginalConstructor()->getMock(); $this->assertEquals('', $stub->__toString()); } } diff --git a/tests/unit/Geography/AddressTest.php b/tests/unit/Geography/AddressTest.php index f449fe2..ae852a0 100644 --- a/tests/unit/Geography/AddressTest.php +++ b/tests/unit/Geography/AddressTest.php @@ -36,9 +36,10 @@ public function testFromNative(): void $this->assertTrue($this->address->sameValueAs($fromNativeAddress)); } - /** @expectedException \BadMethodCallException */ public function testInvalidFromNative(): void { + $this->expectException(\BadMethodCallException::class); + Address::fromNative('invalid'); } @@ -118,7 +119,7 @@ public function testToString(): void { $addressString = <<assertTrue($this->coordinate->sameValueAs($fromNativeCoordinate)); } - /** @expectedException \BadMethodCallException */ public function testInvalidFromNative(): void { + $this->expectException(\BadMethodCallException::class); + Coordinate::fromNative(40.829137); } @@ -106,7 +108,8 @@ public function testDistanceFrom(): void ); $distance = $this->coordinate->distanceFrom($newYork); - $this->assertSame(7609068.4225575, $distance->toNative()); + + $this->assertTrue($distance->sameValueAs(new Real(7609068.4225575), 7)); } public function testToString(): void diff --git a/tests/unit/Geography/LatitudeTest.php b/tests/unit/Geography/LatitudeTest.php index afe0f92..40f3369 100644 --- a/tests/unit/Geography/LatitudeTest.php +++ b/tests/unit/Geography/LatitudeTest.php @@ -20,9 +20,10 @@ public function testNormalization(): void $this->assertEquals(90, $latitude->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidLatitude(): void { + $this->expectException(\TypeError::class); + new Latitude('invalid'); } } diff --git a/tests/unit/Geography/LongitudeTest.php b/tests/unit/Geography/LongitudeTest.php index 51aea2c..c951e47 100644 --- a/tests/unit/Geography/LongitudeTest.php +++ b/tests/unit/Geography/LongitudeTest.php @@ -20,9 +20,10 @@ public function testNormalization(): void $this->assertEquals(-179, $longitude->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidLongitude(): void { + $this->expectException(\TypeError::class); + new Longitude('invalid'); } } diff --git a/tests/unit/Geography/StreetTest.php b/tests/unit/Geography/StreetTest.php index e1be622..28457e5 100644 --- a/tests/unit/Geography/StreetTest.php +++ b/tests/unit/Geography/StreetTest.php @@ -27,9 +27,10 @@ public function testFromNative(): void $this->assertTrue($this->street->sameValueAs($fromNativeStreet)); } - /** @expectedException \BadMethodCallException */ public function testInvalidFromNative(): void { + $this->expectException(\BadMethodCallException::class); + Street::fromNative('Abbey Rd'); } diff --git a/tests/unit/Identity/HashedPasswordTest.php b/tests/unit/Identity/HashedPasswordTest.php new file mode 100644 index 0000000..a836498 --- /dev/null +++ b/tests/unit/Identity/HashedPasswordTest.php @@ -0,0 +1,82 @@ +toNative(), PASSWORD_BCRYPT); + $hashedPassword = new HashedPassword((string) $hash); + $this->assertEquals($hash, $hashedPassword->toNative()); + } + + public function testSameValueAs(): void + { + $plainPassword1 = new PlainPassword('sdlkg549F(#$'); + $plainPassword2 = new PlainPassword('sdlkg549F(#$#'); + $hash1 = password_hash($plainPassword1->toNative(), PASSWORD_BCRYPT); + $hash2 = password_hash($plainPassword2->toNative(), PASSWORD_BCRYPT); + + $hashedPassword1 = new HashedPassword((string) $hash1); + $hashedPassword2 = new HashedPassword((string) $hash1); + $hashedPassword3 = new HashedPassword((string) $hash2); + + $this->assertTrue($hashedPassword1->sameValueAs($hashedPassword2)); + $this->assertTrue($hashedPassword2->sameValueAs($hashedPassword1)); + $this->assertFalse($hashedPassword1->sameValueAs($hashedPassword3)); + + $mock = $this->getMockBuilder(ValueObjectInterface::class)->getMock(); + $this->assertFalse($hashedPassword1->sameValueAs($mock)); + } + + public function testIsEmpty(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $hash = password_hash($plainPassword->toNative(), PASSWORD_BCRYPT); + $hashedPassword = new HashedPassword((string) $hash); + $this->assertFalse($hashedPassword->isEmpty()); + } + + public function testToString(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $hash = password_hash($plainPassword->toNative(), PASSWORD_BCRYPT); + $hashedPassword = new HashedPassword((string) $hash); + $this->assertEquals($hash, (string) $hashedPassword); + } + + public function testIsPasswordValid(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $hash = password_hash($plainPassword->toNative(), PASSWORD_BCRYPT); + $hashedPassword = new HashedPassword((string) $hash); + + $this->assertTrue($hashedPassword->isPasswordValid($plainPassword)); + } + + public function testIsPasswordInvalid(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $hash = password_hash($plainPassword->toNative(), PASSWORD_BCRYPT); + $hashedPassword = new HashedPassword((string) $hash); + + $invaalidPlainPassword = new PlainPassword('sdlkg549F(#$#'); + $this->assertFalse($hashedPassword->isPasswordValid($invaalidPlainPassword)); + } + + public function testWrongEmptyPassword(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new HashedPassword(''); + } +} diff --git a/tests/unit/Identity/PlainPasswordTest.php b/tests/unit/Identity/PlainPasswordTest.php new file mode 100644 index 0000000..85293d1 --- /dev/null +++ b/tests/unit/Identity/PlainPasswordTest.php @@ -0,0 +1,98 @@ +assertEquals('sdlkg549F(#$', $plainPassword->toNative()); + } + + public function testSameValueAs(): void + { + $plainPassword1 = new PlainPassword('sdlkg549F(#$'); + $plainPassword2 = new PlainPassword('sdlkg549F(#$'); + $plainPassword3 = new PlainPassword('sdlkg549F(#$#'); + + $this->assertTrue($plainPassword1->sameValueAs($plainPassword2)); + $this->assertTrue($plainPassword2->sameValueAs($plainPassword1)); + $this->assertFalse($plainPassword1->sameValueAs($plainPassword3)); + + $mock = $this->getMockBuilder(ValueObjectInterface::class)->getMock(); + $this->assertFalse($plainPassword1->sameValueAs($mock)); + } + + public function testIsEmpty(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $this->assertFalse($plainPassword->isEmpty()); + } + + public function testToString(): void + { + $plainPassword = new PlainPassword('sdlkg549F(#$'); + $this->assertEquals('sdlkg549F(#$', (string) $plainPassword); + } + + public function testWrongEmptyPassword(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PlainPassword(''); + } + + public function testMinCharsGreaterThanMaxChars(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PlainPassword('sdlkg549F(#$', 16, 6); + } + + public function testPasswordLengthLessThanMinChars(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PlainPassword('sdlk', 6, 16); + } + + public function testPasswordLengthGreaterThanMaxChars(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PlainPassword('sdlk', 2, 3); + } + + /** + * @dataProvider passwordFormatProvider + * + * @param string $password + * @param int[] $rules + */ + public function testPasswordFormats(string $password, array $rules): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PlainPassword($password, 6, 16, $rules); + } + + /** + * @return mixed[] + */ + public function passwordFormatProvider(): array + { + return [ + 'lower required' => ['ABCDEF', [PlainPassword::MUST_CONTAINS_LOWER_LETTER]], + 'upper required' => ['abcdef', [PlainPassword::MUST_CONTAINS_UPPER_LETTER]], + 'digit required' => ['abcdef', [PlainPassword::MUST_CONTAINS_DIGIT]], + 'special required' => ['abCD69', [PlainPassword::MUST_CONTAINS_SPECIAL_SYMBOL]], + 'lower and upper required' => ['abcdef', [ + PlainPassword::MUST_CONTAINS_LOWER_LETTER, + PlainPassword::MUST_CONTAINS_UPPER_LETTER, + ]], + ]; + } +} diff --git a/tests/unit/Identity/UUIDTest.php b/tests/unit/Identity/UUIDTest.php index fd71ce4..6863ead 100644 --- a/tests/unit/Identity/UUIDTest.php +++ b/tests/unit/Identity/UUIDTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Identity; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Identity\UUID; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\ValueObjectInterface; @@ -39,9 +40,10 @@ public function testSameValueAs(): void $this->assertFalse($uuid1->sameValueAs($mock)); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalid(): void { + $this->expectException(InvalidNativeArgumentException::class); + new UUID('invalid'); } } diff --git a/tests/unit/NullValue/NullValueTest.php b/tests/unit/NullValue/NullValueTest.php index 287d970..f779a82 100644 --- a/tests/unit/NullValue/NullValueTest.php +++ b/tests/unit/NullValue/NullValueTest.php @@ -9,9 +9,10 @@ class NullValueTest extends TestCase { - /** @expectedException \BadMethodCallException */ public function testFromNative(): void { + $this->expectException(\BadMethodCallException::class); + NullValue::fromNative(); } @@ -27,7 +28,7 @@ public function testCreate(): void { $null = NullValue::create(); - $this->assertInstanceOf('AdgoalCommon\ValueObject\NullValue\NullValue', $null); + $this->assertInstanceOf(NullValue::class, $null); } public function testToString(): void diff --git a/tests/unit/Number/IntegerTest.php b/tests/unit/Number/IntegerTest.php index a2eca42..94dbf76 100644 --- a/tests/unit/Number/IntegerTest.php +++ b/tests/unit/Number/IntegerTest.php @@ -37,9 +37,10 @@ public function testToString(): void $this->assertSame('87', $integer->__toString()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidNativeArgument(): void { + $this->expectException(\TypeError::class); + new Integer(23.4); } diff --git a/tests/unit/Number/NaturalTest.php b/tests/unit/Number/NaturalTest.php index b05f432..4d6404e 100644 --- a/tests/unit/Number/NaturalTest.php +++ b/tests/unit/Number/NaturalTest.php @@ -4,14 +4,16 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Number; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Number\Natural; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; class NaturalTest extends TestCase { - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidNativeArgument(): void { + $this->expectException(InvalidNativeArgumentException::class); + new Natural(-2); } } diff --git a/tests/unit/Number/RealTest.php b/tests/unit/Number/RealTest.php index ec51a9c..47570b3 100644 --- a/tests/unit/Number/RealTest.php +++ b/tests/unit/Number/RealTest.php @@ -40,12 +40,6 @@ public function testSameValueAs(): void $this->assertFalse($real1->sameValueAs($mock)); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ - public function testInvalidNativeArgument(): void - { - new Real('invalid'); - } - public function testToInteger(): void { $real = new Real(3.14); @@ -67,6 +61,13 @@ public function testToNatural(): void public function testToString(): void { $real = new Real(.7); - $this->assertEquals('.7', $real->__toString()); + $this->assertEquals('0.7', $real->__toString()); + } + + public function testInvalidReal(): void + { + $this->expectException(\TypeError::class); + + new Real('invalid'); } } diff --git a/tests/unit/Web/EmailAddressTest.php b/tests/unit/Person/EmailAddressTest.php similarity index 63% rename from tests/unit/Web/EmailAddressTest.php rename to tests/unit/Person/EmailAddressTest.php index e29d113..be3c431 100644 --- a/tests/unit/Web/EmailAddressTest.php +++ b/tests/unit/Person/EmailAddressTest.php @@ -2,25 +2,28 @@ declare(strict_types=1); -namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +namespace AdgoalCommon\ValueObject\Tests\Unit\Person; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; +use AdgoalCommon\ValueObject\Person\EmailAddress; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; -use AdgoalCommon\ValueObject\Web\EmailAddress; +use AdgoalCommon\ValueObject\Web\Domain; class EmailAddressTest extends TestCase { public function testValidEmailAddress(): void { $email1 = new EmailAddress('foo@bar.com'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\EmailAddress', $email1); + $this->assertInstanceOf(EmailAddress::class, $email1); $email2 = new EmailAddress('foo@[120.0.0.1]'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\EmailAddress', $email2); + $this->assertInstanceOf(EmailAddress::class, $email2); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidEmailAddress(): void { + $this->expectException(InvalidNativeArgumentException::class); + new EmailAddress('invalid'); } @@ -38,6 +41,6 @@ public function testGetDomainPart(): void $domainPart = $email->getDomainPart(); $this->assertEquals('bar.com', $domainPart->toNative()); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\Domain', $domainPart); + $this->assertInstanceOf(Domain::class, $domainPart); } } diff --git a/tests/unit/Person/PhoneNumberTest.php b/tests/unit/Person/PhoneNumberTest.php new file mode 100644 index 0000000..c1f56d6 --- /dev/null +++ b/tests/unit/Person/PhoneNumberTest.php @@ -0,0 +1,51 @@ +assertEquals('375449876521', $phoneNumber->toNative()); + } + + public function testSameValueAs(): void + { + $phoneNumber1 = new PhoneNumber('375449876521'); + $phoneNumber2 = new PhoneNumber('375449876521'); + $phoneNumber3 = new PhoneNumber('375449876522'); + + $this->assertTrue($phoneNumber1->sameValueAs($phoneNumber2)); + $this->assertTrue($phoneNumber2->sameValueAs($phoneNumber1)); + $this->assertFalse($phoneNumber1->sameValueAs($phoneNumber3)); + + $mock = $this->getMockBuilder(ValueObjectInterface::class)->getMock(); + $this->assertFalse($phoneNumber1->sameValueAs($mock)); + } + + public function testIsEmpty(): void + { + $phoneNumber = new PhoneNumber('375449876521'); + $this->assertFalse($phoneNumber->isEmpty()); + } + + public function testToString(): void + { + $phoneNumber = new PhoneNumber('375449876521'); + $this->assertEquals('375449876521', (string) $phoneNumber); + } + + public function testWrongPhoneNumber(): void + { + $this->expectException(InvalidNativeArgumentException::class); + new PhoneNumber('375 (44) 987-65-21'); + } +} diff --git a/tests/unit/StringLiteral/StringLiteralTest.php b/tests/unit/StringLiteral/StringLiteralTest.php index fbc2254..c346840 100644 --- a/tests/unit/StringLiteral/StringLiteralTest.php +++ b/tests/unit/StringLiteral/StringLiteralTest.php @@ -38,9 +38,10 @@ public function testSameValueAs(): void $this->assertFalse($foo1->sameValueAs($mock)); } - /** @expectedException \AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidNativeArgument(): void { + $this->expectException(\TypeError::class); + new StringLiteral(12); } diff --git a/tests/unit/Structure/CollectionTest.php b/tests/unit/Structure/CollectionTest.php index 4ac9f6e..3da404b 100644 --- a/tests/unit/Structure/CollectionTest.php +++ b/tests/unit/Structure/CollectionTest.php @@ -26,9 +26,10 @@ protected function setUp(): void $this->collection = new Collection($array); } - /** @expectedException \InvalidArgumentException */ public function testInvalidArgument(): void { + $this->expectException(\InvalidArgumentException::class); + $array = \SplFixedArray::fromArray(['one', 'two', 'three']); new Collection($array); @@ -39,14 +40,14 @@ public function testFromNative(): void $array = \SplFixedArray::fromArray([ 'one', 'two', - [1, 2], + ['1', '2'], ]); $fromNativeCollection = Collection::fromNative($array); $innerArray = new Collection( \SplFixedArray::fromArray([ - new StringLiteral('1'), - new StringLiteral('2'), + new StringLiteral('1'), + new StringLiteral('2'), ]) ); $array = \SplFixedArray::fromArray([ @@ -102,9 +103,9 @@ public function testContains(): void public function testToArray(): void { $array = [ - new StringLiteral('one'), - new StringLiteral('two'), - new Integer(3), + (new StringLiteral('one'))->toNative(), + (new StringLiteral('two'))->toNative(), + (new Integer(3))->toNative(), ]; $this->assertEquals($array, $this->collection->toArray()); @@ -112,6 +113,6 @@ public function testToArray(): void public function testToString(): void { - $this->assertEquals('AdgoalCommon\ValueObject\Structure\Collection(3)', $this->collection->__toString()); + $this->assertEquals('a:3:{i:0;s:3:"one";i:1;s:3:"two";i:2;i:3;}', $this->collection->__toString()); } } diff --git a/tests/unit/Structure/DictionaryTest.php b/tests/unit/Structure/DictionaryTest.php index add3755..d5b69f7 100644 --- a/tests/unit/Structure/DictionaryTest.php +++ b/tests/unit/Structure/DictionaryTest.php @@ -47,9 +47,10 @@ public function testFromNative(): void $this->assertTrue($constructedDictionary->sameValueAs($fromNativeDictionary)); } - /** @expectedException \InvalidArgumentException */ public function testInvalidArgument(): void { + $this->expectException(\InvalidArgumentException::class); + $array = \SplFixedArray::fromArray(['one', 'two', 'three']); new Dictionary($array); diff --git a/tests/unit/Structure/KeyValuePairTest.php b/tests/unit/Structure/KeyValuePairTest.php index eb09e5d..d9eec9d 100644 --- a/tests/unit/Structure/KeyValuePairTest.php +++ b/tests/unit/Structure/KeyValuePairTest.php @@ -25,9 +25,10 @@ public function testFromNative(): void $this->assertTrue($this->keyValuePair->sameValueAs($fromNativePair)); } - /** @expectedException \BadMethodCallException */ public function testInvalidFromNative(): void { + $this->expectException(\BadMethodCallException::class); + KeyValuePair::fromNative('key', 'value', 'invalid'); } @@ -56,6 +57,6 @@ public function testGetValue(): void public function testToString(): void { - $this->assertEquals('key => value', $this->keyValuePair->__toString()); + $this->assertEquals('a:1:{s:3:"key";s:5:"value";}', $this->keyValuePair->__toString()); } } diff --git a/tests/unit/Web/DomainTest.php b/tests/unit/Web/DomainTest.php index 79eaf46..cc898b8 100644 --- a/tests/unit/Web/DomainTest.php +++ b/tests/unit/Web/DomainTest.php @@ -6,6 +6,8 @@ use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\Domain; +use AdgoalCommon\ValueObject\Web\Hostname; +use AdgoalCommon\ValueObject\Web\IPAddress; class DomainTest extends TestCase { @@ -14,7 +16,7 @@ public function testSpecifyType(): void $ip = Domain::specifyType('127.0.0.1'); $hostname = Domain::specifyType('example.com'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\IPAddress', $ip); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\Hostname', $hostname); + $this->assertInstanceOf(IPAddress::class, $ip); + $this->assertInstanceOf(Hostname::class, $hostname); } } diff --git a/tests/unit/Web/FragmentIdentifierTest.php b/tests/unit/Web/FragmentIdentifierTest.php index f713def..fecb729 100644 --- a/tests/unit/Web/FragmentIdentifierTest.php +++ b/tests/unit/Web/FragmentIdentifierTest.php @@ -4,8 +4,10 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\FragmentIdentifier; +use AdgoalCommon\ValueObject\Web\FragmentIdentifierInterface; use AdgoalCommon\ValueObject\Web\NullFragmentIdentifier; class FragmentIdentifierTest extends TestCase @@ -14,19 +16,20 @@ public function testValidFragmentIdentifier(): void { $fragment = new FragmentIdentifier('#id'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\FragmentIdentifier', $fragment); + $this->assertInstanceOf(FragmentIdentifierInterface::class, $fragment); } public function testNullFragmentIdentifier(): void { $fragment = new NullFragmentIdentifier(); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\FragmentIdentifier', $fragment); + $this->assertInstanceOf(FragmentIdentifierInterface::class, $fragment); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidFragmentIdentifier(): void { + $this->expectException(InvalidNativeArgumentException::class); + new FragmentIdentifier('invalìd'); } } diff --git a/tests/unit/Web/HostnameTest.php b/tests/unit/Web/HostnameTest.php index 5505e97..52d2c29 100644 --- a/tests/unit/Web/HostnameTest.php +++ b/tests/unit/Web/HostnameTest.php @@ -4,14 +4,16 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\Hostname; class HostnameTest extends TestCase { - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidHostname(): void { + $this->expectException(InvalidNativeArgumentException::class); + new Hostname('inv@lìd'); } } diff --git a/tests/unit/Web/IPAddressTest.php b/tests/unit/Web/IPAddressTest.php index 2c2c7b4..1fa9b00 100644 --- a/tests/unit/Web/IPAddressTest.php +++ b/tests/unit/Web/IPAddressTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\IPAddress; use AdgoalCommon\ValueObject\Web\IPAddressVersion; @@ -13,15 +14,16 @@ class IPAddressTest extends TestCase public function testGetVersion(): void { $ip4 = new IPAddress('127.0.0.1'); - $this->assertSame(IPAddressVersion::IPV4, $ip4->getVersion()); + $this->assertSame(IPAddressVersion::IPV4, (string) $ip4->getVersion()); $ip6 = new IPAddress('::1'); - $this->assertSame(IPAddressVersion::IPV6, $ip6->getVersion()); + $this->assertSame(IPAddressVersion::IPV6, (string) $ip6->getVersion()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidIPAddress(): void { + $this->expectException(InvalidNativeArgumentException::class); + new IPAddress('invalid'); } } diff --git a/tests/unit/Web/IPv4AddressTest.php b/tests/unit/Web/IPv4AddressTest.php index cc19844..22c892d 100644 --- a/tests/unit/Web/IPv4AddressTest.php +++ b/tests/unit/Web/IPv4AddressTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\IPv4Address; @@ -13,12 +14,13 @@ public function testValidIPv4Address(): void { $ip = new IPv4Address('127.0.0.1'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\IPv4Address', $ip); + $this->assertInstanceOf(IPv4Address::class, $ip); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidIPv4Address(): void { + $this->expectException(InvalidNativeArgumentException::class); + new IPv4Address('::1'); } } diff --git a/tests/unit/Web/IPv6AddressTest.php b/tests/unit/Web/IPv6AddressTest.php index d4ec6b0..06e997e 100644 --- a/tests/unit/Web/IPv6AddressTest.php +++ b/tests/unit/Web/IPv6AddressTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\IPv6Address; @@ -13,12 +14,13 @@ public function testValidIPv6Address(): void { $ip = new IPv6Address('::1'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\IPv6Address', $ip); + $this->assertInstanceOf(IPv6Address::class, $ip); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidIPv6Address(): void { + $this->expectException(InvalidNativeArgumentException::class); + new IPv6Address('127.0.0.1'); } } diff --git a/tests/unit/Web/PathTest.php b/tests/unit/Web/PathTest.php index d32368f..96a62b8 100644 --- a/tests/unit/Web/PathTest.php +++ b/tests/unit/Web/PathTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\Path; @@ -16,9 +17,10 @@ public function testValidPath(): void $this->assertEquals($pathString, $path->toNative()); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidPath(): void { + $this->expectException(InvalidNativeArgumentException::class); + new Path('//valid?'); } } diff --git a/tests/unit/Web/PortNumberTest.php b/tests/unit/Web/PortNumberTest.php index 2859d71..ed81d22 100644 --- a/tests/unit/Web/PortNumberTest.php +++ b/tests/unit/Web/PortNumberTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\PortNumber; @@ -13,12 +14,13 @@ public function testValidPortNumber(): void { $port = new PortNumber(80); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\PortNumber', $port); + $this->assertInstanceOf(PortNumber::class, $port); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidPortNumber(): void { + $this->expectException(InvalidNativeArgumentException::class); + new PortNumber(65536); } } diff --git a/tests/unit/Web/QueryStringTest.php b/tests/unit/Web/QueryStringTest.php index d472a8d..a06316e 100644 --- a/tests/unit/Web/QueryStringTest.php +++ b/tests/unit/Web/QueryStringTest.php @@ -4,10 +4,12 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Structure\Dictionary; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\NullQueryString; use AdgoalCommon\ValueObject\Web\QueryString; +use AdgoalCommon\ValueObject\Web\QueryStringInterface; class QueryStringTest extends TestCase { @@ -15,22 +17,23 @@ public function testValidQueryString(): void { $query = new QueryString('?foo=bar'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\QueryString', $query); + $this->assertInstanceOf(QueryString::class, $query); } public function testEmptyQueryString(): void { $query = new NullQueryString(); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\QueryString', $query); + $this->assertInstanceOf(QueryStringInterface::class, $query); $dictionary = $query->toDictionary(); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Structure\Dictionary', $dictionary); + $this->assertInstanceOf(Dictionary::class, $dictionary); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidQueryString(): void { + $this->expectException(InvalidNativeArgumentException::class); + new QueryString('invalìd'); } @@ -39,7 +42,7 @@ public function testToDictionary(): void $query = new QueryString('?foo=bar&array[]=one&array[]=two'); $dictionary = $query->toDictionary(); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Structure\Dictionary', $dictionary); + $this->assertInstanceOf(Dictionary::class, $dictionary); $array = [ 'foo' => 'bar', diff --git a/tests/unit/Web/SchemeNameTest.php b/tests/unit/Web/SchemeNameTest.php index e365b1c..7977d7d 100644 --- a/tests/unit/Web/SchemeNameTest.php +++ b/tests/unit/Web/SchemeNameTest.php @@ -4,6 +4,7 @@ namespace AdgoalCommon\ValueObject\Tests\Unit\Web; +use AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException; use AdgoalCommon\ValueObject\Tests\Unit\TestCase; use AdgoalCommon\ValueObject\Web\SchemeName; @@ -12,12 +13,13 @@ class SchemeNameTest extends TestCase public function testValidSchemeName(): void { $scheme = new SchemeName('git+ssh'); - $this->assertInstanceOf('AdgoalCommon\ValueObject\Web\SchemeName', $scheme); + $this->assertInstanceOf(SchemeName::class, $scheme); } - /** @expectedException AdgoalCommon\ValueObject\Exception\InvalidNativeArgumentException */ public function testInvalidSchemeName(): void { + $this->expectException(InvalidNativeArgumentException::class); + new SchemeName('ht*tp'); } } diff --git a/tests/unit/Web/UrlTest.php b/tests/unit/Web/UrlTest.php index 175c19a..06d939c 100644 --- a/tests/unit/Web/UrlTest.php +++ b/tests/unit/Web/UrlTest.php @@ -149,10 +149,10 @@ public function testAuthlessUrlToString(): void new QueryString('?querystring'), new FragmentIdentifier('#fragmentidentifier') ); - $this->assertSame($nativeUrlString, $authlessUrl->__toString()); + $this->assertSame($nativeUrlString, (string) $authlessUrl); Url::fromNative($nativeUrlString); - $this->assertSame($nativeUrlString, Url::fromNative($authlessUrl)->__toString()); + $this->assertSame($nativeUrlString, (string) Url::fromNative((string) $authlessUrl)); } public function testNullPortUrlToString(): void