From 62505cb5effa4f6f4cebd76fd64241665cb18226 Mon Sep 17 00:00:00 2001 From: Frank Verhoeven Date: Fri, 2 Aug 2019 13:23:45 +0200 Subject: [PATCH 1/2] Upgrade for PHP 7.2 Upgraded packages & added strict typing Changes might cause a BC due to strict typing --- .gitignore | 2 + .travis.yml | 4 +- composer.json | 8 +- phpcs.xml.dist | 11 ++- phpunit.xml.dist | 12 +-- .../ArrayContainsClassAssertionTrait.php | 5 +- src/Assertion/EnumValueGuardTrait.php | 16 ++-- src/Assertion/NumericAssertionTrait.php | 7 +- src/Collection/ImmutableCollection.php | 12 ++- .../ImmutableCollectionInterface.php | 22 ++--- src/Collection/LocaleCollection.php | 39 ++++----- src/Collection/LocaleCollectionInterface.php | 11 +-- src/Collection/MutableCollection.php | 56 ++++--------- src/Collection/MutableCollectionInterface.php | 2 +- src/Collection/RegionCodeCollection.php | 20 ++--- .../RegionCodeCollectionInterface.php | 5 +- src/Collection/StoreIds.php | 4 +- src/Collection/StringCollectionInterface.php | 1 + src/Collection/StringCollectionTrait.php | 6 +- src/Exception/NotFoundException.php | 2 +- src/Value/Arithmetic/Number.php | 83 ++++--------------- src/Value/CurrencyIso.php | 49 +++++------ src/Value/Finance/Iban.php | 2 +- src/Value/LanguageCode.php | 23 ++--- src/Value/Locale.php | 47 +++-------- src/Value/Mail/EmailAddress.php | 2 +- src/Value/Monetary/Amount.php | 11 ++- src/Value/RegionCode.php | 33 ++------ src/Value/StoreId.php | 29 ++----- src/Value/Type/AbstractUuid.php | 34 +++----- .../ArrayContainsClassAssertionTraitTest.php | 5 +- tests/Assertion/EnumValueGuardTraitTest.php | 12 +-- tests/Assertion/NumericAssertionTraitTest.php | 5 +- tests/Collection/ImmutableCollectionTest.php | 7 +- tests/Collection/LocaleCollectionTest.php | 12 +-- tests/Collection/MutableCollectionTest.php | 52 +++++------- tests/Collection/RegionCodeCollectionTest.php | 9 +- tests/Collection/StoreIdsTest.php | 12 +-- .../Collection/StringCollectionTraitTest.php | 8 +- tests/Value/Arithmetic/NumberTest.php | 38 ++++----- tests/Value/CurrencyIsoTest.php | 18 ++-- tests/Value/Finance/IbanTest.php | 6 +- tests/Value/LanguageCodeTest.php | 17 ++-- tests/Value/LocaleTest.php | 24 +++--- tests/Value/Mail/EmailAddressTest.php | 6 +- tests/Value/Monetary/AmountTest.php | 13 +-- tests/Value/RegionCodeTest.php | 24 +++--- tests/Value/StoreIdTest.php | 18 ++-- tests/Value/Type/AbstractUuidTest.php | 16 ++-- 49 files changed, 327 insertions(+), 533 deletions(-) diff --git a/.gitignore b/.gitignore index 10500ea..4030187 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor composer.lock phpunit.xml +.phpcs-cache +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index b701942..f6d2705 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - '7.0' - - '7.1' - '7.2' + - '7.3' + - '7.4' before_script: - composer self-update - composer install --prefer-source diff --git a/composer.json b/composer.json index d5f67e8..366adac 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": "^7.0", + "php": "^7.2", "ext-bcmath": "*", "ext-intl": "*", "egulias/email-validator": "^2.1", @@ -26,14 +26,14 @@ }, "require-dev": { "doctrine/orm": "2.5.x-dev as 2.5.6", - "myonlinestore/coding-standard": "^1.0", - "phpunit/phpunit": "6.4.*" + "myonlinestore/coding-standard": "^1.1.0", + "phpunit/phpunit": "^8.3.1" }, "config": { "vendor-dir": "vendor", "sort-packages": true, "platform": { - "php": "7.0.8" + "php": "7.2.19" } } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 2984ee9..6279f50 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -2,10 +2,15 @@ - ./ - - + + + + + + + src + tests diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cde30bc..4b9528e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,15 @@ - +> ./tests/ + ./src diff --git a/src/Assertion/ArrayContainsClassAssertionTrait.php b/src/Assertion/ArrayContainsClassAssertionTrait.php index 21f19e3..e8ed738 100644 --- a/src/Assertion/ArrayContainsClassAssertionTrait.php +++ b/src/Assertion/ArrayContainsClassAssertionTrait.php @@ -9,10 +9,7 @@ trait ArrayContainsClassAssertionTrait { /** - * @param array $entries - * @param string $className - * - * @return bool + * @param mixed[] $entries */ public function assertArrayContainsOnlyClass(array $entries, string $className): bool { diff --git a/src/Assertion/EnumValueGuardTrait.php b/src/Assertion/EnumValueGuardTrait.php index e0ebdbd..2abfd8a 100644 --- a/src/Assertion/EnumValueGuardTrait.php +++ b/src/Assertion/EnumValueGuardTrait.php @@ -16,19 +16,19 @@ public function guardIsValidValue($value) { $validValues = $this->getValidValues(); - if (!is_scalar($value)) { + if (!\is_scalar($value)) { throw new \InvalidArgumentException( - sprintf('given value is not a scalar value but of type %s', gettype($value)) + \sprintf('given value is not a scalar value but of type %s', \gettype($value)) ); } - if (!in_array($value, $validValues, true)) { + if (!\in_array($value, $validValues, true)) { throw new \InvalidArgumentException( - sprintf( + \sprintf( 'Invalid %s value given: "%s" (valid values: %s)', - __CLASS__, + self::class, $value, - implode(', ', $validValues) + \implode(', ', $validValues) ) ); } @@ -37,7 +37,7 @@ public function guardIsValidValue($value) } /** - * @return array + * @return mixed[] */ - abstract protected function getValidValues(); + abstract protected function getValidValues(): array; } diff --git a/src/Assertion/NumericAssertionTrait.php b/src/Assertion/NumericAssertionTrait.php index 0442409..7c4ce18 100644 --- a/src/Assertion/NumericAssertionTrait.php +++ b/src/Assertion/NumericAssertionTrait.php @@ -1,4 +1,5 @@ regionCode(); }, $this->toArray() @@ -49,19 +47,19 @@ function (Locale $locale) { public function contains($element): bool { return $this->containsWith( - function (Locale $locale) use ($element) { + static function (Locale $locale) use ($element) { return $locale->equals($element); } ); } /** - * @inheritdoc + * @return LocaleCollectionInterface[] */ - public function groupByCurrencyFormat($currencyIso, $previewAmount): array + public function groupByCurrencyFormat(CurrencyIso $currencyIso, float $previewAmount): array { return $this->groupBy( - function (Locale $locale) use ($currencyIso, $previewAmount) { + static function (Locale $locale) use ($currencyIso, $previewAmount) { $numberFormatter = new \NumberFormatter((string) $locale, \NumberFormatter::CURRENCY); return $numberFormatter->formatCurrency($previewAmount, (string) $currencyIso); @@ -69,9 +67,6 @@ function (Locale $locale) use ($currencyIso, $previewAmount) { ); } - /** - * @inheritdoc - */ public function unique(): LocaleCollectionInterface { return new LocaleCollection(\array_unique($this->toArray())); @@ -79,14 +74,12 @@ public function unique(): LocaleCollectionInterface /** * @param string[] $input - * - * @return self */ public static function fromStrings(array $input): self { return new self( - array_map( - function ($locale) { + \array_map( + static function ($locale) { try { return Locale::fromString($locale); } catch (\InvalidArgumentException $exception) { @@ -99,11 +92,9 @@ function ($locale) { } /** - * @param callable $callable - * * @return LocaleCollection[] */ - private function groupBy(callable $callable) + private function groupBy(callable $callable): array { $locales = []; diff --git a/src/Collection/LocaleCollectionInterface.php b/src/Collection/LocaleCollectionInterface.php index 4dd3d73..b934fdb 100644 --- a/src/Collection/LocaleCollectionInterface.php +++ b/src/Collection/LocaleCollectionInterface.php @@ -11,21 +11,12 @@ */ interface LocaleCollectionInterface extends ImmutableCollectionInterface, StringCollectionInterface { - /** - * @return RegionCodeCollectionInterface - */ public function asRegionCodes(): RegionCodeCollectionInterface; /** - * @param CurrencyIso $currencyIso - * @param float $previewAmount - * * @return LocaleCollectionInterface[] */ - public function groupByCurrencyFormat($currencyIso, $previewAmount): array; + public function groupByCurrencyFormat(CurrencyIso $currencyIso, float $previewAmount): array; - /** - * @return LocaleCollectionInterface - */ public function unique(): LocaleCollectionInterface; } diff --git a/src/Collection/MutableCollection.php b/src/Collection/MutableCollection.php index 03ed74c..8b6d8ae 100644 --- a/src/Collection/MutableCollection.php +++ b/src/Collection/MutableCollection.php @@ -5,18 +5,10 @@ class MutableCollection extends \ArrayObject implements MutableCollectionInterface { - /** - * @param array $entries - */ - public function __construct(array $entries = []) - { - parent::__construct($entries); - } - /** * @inheritDoc */ - public function add($element) + public function add($element): void { $this[] = $element; } @@ -24,27 +16,21 @@ public function add($element) /** * @inheritDoc */ - public function contains($element) + public function contains($element): bool { - return in_array($element, $this->getArrayCopy(), true); + return \in_array($element, $this->getArrayCopy(), true); } - /** - * @inheritDoc - */ - public function each(callable $callback) + public function each(callable $callback): void { foreach ($this as $entry) { $callback($entry); } } - /** - * @inheritDoc - */ public function equals(ImmutableCollectionInterface $otherCollection): bool { - if (\get_class($this) !== \get_class($otherCollection)) { + if (static::class !== \get_class($otherCollection)) { return false; } @@ -66,7 +52,7 @@ public function equals(ImmutableCollectionInterface $otherCollection): bool */ public function first() { - return reset($this); + return \reset($this); } /** @@ -74,13 +60,10 @@ public function first() */ public function indexOf($element) { - return array_search($element, $this->getArrayCopy(), true); + return \array_search($element, $this->getArrayCopy(), true); } - /** - * @inheritDoc - */ - public function isEmpty() + public function isEmpty(): bool { return 0 === $this->count(); } @@ -90,7 +73,7 @@ public function isEmpty() */ public function last() { - return end($this); + return \end($this); } /** @@ -98,22 +81,17 @@ public function last() */ public function reindex() { - return new static(array_values($this->toArray())); + return new static(\array_values($this->toArray())); } /** - * @inheritDoc + * @return mixed[] */ - public function toArray() + public function toArray(): array { return $this->getArrayCopy(); } - /** - * @param callable $callback - * - * @return bool - */ protected function containsWith(callable $callback): bool { foreach ($this as $entry) { @@ -126,18 +104,14 @@ protected function containsWith(callable $callback): bool } /** - * @param \Closure $closure - * * @return static */ protected function filter(\Closure $closure) { - return new static(array_filter($this->toArray(), $closure)); + return new static(\array_filter($this->toArray(), $closure)); } /** - * @param callable $callback - * * @return mixed * * @throws \OutOfBoundsException @@ -154,12 +128,10 @@ protected function firstHaving(callable $callback) } /** - * @param \Closure $closure - * * @return static */ protected function map(\Closure $closure) { - return new static(array_map($closure, $this->toArray())); + return new static(\array_map($closure, $this->toArray())); } } diff --git a/src/Collection/MutableCollectionInterface.php b/src/Collection/MutableCollectionInterface.php index 5831fcc..5d85f18 100644 --- a/src/Collection/MutableCollectionInterface.php +++ b/src/Collection/MutableCollectionInterface.php @@ -8,5 +8,5 @@ interface MutableCollectionInterface extends ImmutableCollectionInterface /** * @param mixed $element */ - public function add($element); + public function add($element): void; } diff --git a/src/Collection/RegionCodeCollection.php b/src/Collection/RegionCodeCollection.php index b821758..b87a275 100644 --- a/src/Collection/RegionCodeCollection.php +++ b/src/Collection/RegionCodeCollection.php @@ -15,41 +15,33 @@ final class RegionCodeCollection extends ImmutableCollection implements RegionCo public function __construct(array $entries = []) { parent::__construct( - array_filter( + \array_filter( $entries, - function ($entry) { + static function ($entry) { return $entry instanceof RegionCode; } ) ); } - /** - * @inheritDoc - */ public function contains($element): bool { - return in_array($element, $this->getArrayCopy()); + return \in_array($element, $this->getArrayCopy(), false); } - /** - * @inheritdoc - */ public function unique(): RegionCodeCollectionInterface { - return new self(array_unique($this->toArray())); + return new self(\array_unique($this->toArray())); } /** * @param string[] $isoCodes - * - * @return RegionCodeCollectionInterface */ public static function fromStrings(array $isoCodes): RegionCodeCollectionInterface { return new self( - array_map( - function ($isoCode) { + \array_map( + static function ($isoCode) { return new RegionCode($isoCode); }, $isoCodes diff --git a/src/Collection/RegionCodeCollectionInterface.php b/src/Collection/RegionCodeCollectionInterface.php index ea41212..aed326e 100644 --- a/src/Collection/RegionCodeCollectionInterface.php +++ b/src/Collection/RegionCodeCollectionInterface.php @@ -11,12 +11,9 @@ interface RegionCodeCollectionInterface extends ImmutableCollectionInterface, StringCollectionInterface { /** - * @return RegionCodeCollectionInterface + * @return static */ public function reindex(); - /** - * @return RegionCodeCollectionInterface - */ public function unique(): RegionCodeCollectionInterface; } diff --git a/src/Collection/StoreIds.php b/src/Collection/StoreIds.php index 3cf4dbc..e8ab6e4 100644 --- a/src/Collection/StoreIds.php +++ b/src/Collection/StoreIds.php @@ -16,8 +16,8 @@ public function __construct(array $entries = []) { parent::__construct( \array_map( - function ($entry) { - return ($entry instanceof StoreId) ? $entry : new StoreId($entry); + static function ($entry): StoreId { + return $entry instanceof StoreId ? $entry : new StoreId($entry); }, $entries ) diff --git a/src/Collection/StringCollectionInterface.php b/src/Collection/StringCollectionInterface.php index 3a82727..f2b7cc5 100644 --- a/src/Collection/StringCollectionInterface.php +++ b/src/Collection/StringCollectionInterface.php @@ -1,4 +1,5 @@ toArray()); + return \array_map('strval', $this->toArray()); } /** - * @return array + * @return mixed[] */ - abstract public function toArray(); + abstract public function toArray(): array; } diff --git a/src/Exception/NotFoundException.php b/src/Exception/NotFoundException.php index aa7beff..eab7bde 100644 --- a/src/Exception/NotFoundException.php +++ b/src/Exception/NotFoundException.php @@ -1,8 +1,8 @@ assignValue($value, $scale); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return (string) $this->value; } /** - * @param Number|self $operand - * @param int|null $scale - * * @return static */ - public function add(Number $operand, int $scale = null) + public function add(Number $operand, ?int $scale = null): Number { $this->assertOperand($operand); @@ -46,14 +39,13 @@ public function add(Number $operand, int $scale = null) } /** - * @param string $value - * @param int|null $scale + * @param mixed $value * * @return static * * @throws \InvalidArgumentException */ - public function changeValue($value, int $scale = null) + public function changeValue($value, ?int $scale = null): Number { $newMetric = clone $this; $newMetric->assignValue($value, $scale); @@ -62,68 +54,41 @@ public function changeValue($value, int $scale = null) } /** - * @param Number|self $operand - * @param int|null $scale - * * @return static */ - public function divideBy(Number $operand, int $scale = null) + public function divideBy(Number $operand, ?int $scale = null): Number { $this->assertOperand($operand); return $this->changeValue($this->value->div($operand->value, $scale), $scale); } - /** - * @param Number|self $operand - * @param int|null $scale - * - * @return bool - */ - public function equals(Number $operand, int $scale = null) + public function equals(Number $operand, ?int $scale = null): bool { $this->assertOperand($operand); return $this->value->equals($operand->value, $scale); } - /** - * @param Number|self $operand - * @param int|null $scale - * - * @return bool - */ - public function isGreaterThan(Number $operand, int $scale = null): bool + public function isGreaterThan(Number $operand, ?int $scale = null): bool { return 1 === $this->value->comp($operand->value, $scale); } - /** - * @param Number|self $operand - * @param int|null $scale - * - * @return bool - */ - public function isLessThan(Number $operand, int $scale = null): bool + public function isLessThan(Number $operand, ?int $scale = null): bool { return -1 === $this->value->comp($operand->value, $scale); } - /** - * @return bool - */ - public function isZero() + public function isZero(): bool { return $this->value->isZero(); } /** - * @param Number|self $operand - * @param int|null $scale - * * @return static */ - public function multiplyBy(Number $operand, int $scale = null) + public function multiplyBy(Number $operand, ?int $scale = null): Number { $this->assertOperand($operand); @@ -131,23 +96,17 @@ public function multiplyBy(Number $operand, int $scale = null) } /** - * @param Number|self $operand - * @param int|null $scale - * * @return static */ - public function powerTo(Number $operand, int $scale = null) + public function powerTo(Number $operand, ?int $scale = null): Number { return $this->changeValue($this->value->pow($operand->value, $scale)); } /** - * @param Number|self $operand - * @param int|null $scale - * * @return static */ - public function subtract(Number $operand, int $scale = null) + public function subtract(Number $operand, ?int $scale = null): Number { $this->assertOperand($operand); @@ -155,30 +114,24 @@ public function subtract(Number $operand, int $scale = null) } /** - * @param int $scale - * * @return static */ - public function toScale(int $scale) + public function toScale(int $scale): Number { return $this->changeValue($this->value->div(Decimal::create(10)->pow(Decimal::create($scale)), $scale)); } - /** - * @param Number $operand - */ - protected function assertOperand(Number $operand) + protected function assertOperand(Number $operand): void { // scope to perform assertions to guard against inproper operands } /** - * @param mixed $value - * @param int|null $scale + * @param mixed $value * * @throws \InvalidArgumentException */ - private function assignValue($value, int $scale = null) + private function assignValue($value, ?int $scale = null): void { if ($value instanceof self) { $value = $value->value; diff --git a/src/Value/CurrencyIso.php b/src/Value/CurrencyIso.php index cba9eb8..1537ce8 100644 --- a/src/Value/CurrencyIso.php +++ b/src/Value/CurrencyIso.php @@ -11,7 +11,7 @@ final class CurrencyIso { /** - * @var array|null + * @var string[]|int[]|null */ private static $currencies; @@ -24,53 +24,44 @@ final class CurrencyIso /** * @param string $code ISO 4217 code (https://en.wikipedia.org/wiki/ISO_4217) + * + * @throws \InvalidArgumentException */ - public function __construct($code) + public function __construct(string $code) { $currencies = $this->getCurrencies(); - if (!isset($currencies[(string) $code])) { - throw new \InvalidArgumentException(sprintf('Given value "%s" is not a valid ISO 4217 code', $code)); + if (!isset($currencies[$code])) { + throw new \InvalidArgumentException(\sprintf('Given value "%s" is not a valid ISO 4217 code', $code)); } - $this->currency = $currencies[(string) $code]['alphabeticCode']; + $this->currency = $currencies[$code]['alphabeticCode']; } - /** - * @param object $object - * - * @return bool - */ - public function equals($object): bool + public function equals(self $object): bool { - return $this == $object; + return $this->currency === $object->currency; } - /** - * @return array - */ - private function getCurrencies(): array + public function __toString(): string { - if (null === self::$currencies) { - self::$currencies = require_once __DIR__.'/../../vendor/moneyphp/money/resources/currency.php'; - } - - return self::$currencies; + return $this->currency; } - /** - * @inheritdoc - */ - public function __toString() + public function getMinorUnit(): int { - return $this->currency; + return $this->getCurrencies()[$this->currency]['minorUnit']; } /** - * @return int + * @return string[]|int[] */ - public function getMinorUnit() + private function getCurrencies(): array { - return $this->getCurrencies()[$this->currency]['minorUnit']; + if (null === self::$currencies) { + self::$currencies = require __DIR__.'/../../vendor/moneyphp/money/resources/currency.php'; + } + + return self::$currencies; } } diff --git a/src/Value/Finance/Iban.php b/src/Value/Finance/Iban.php index 4ed7eb9..159d46c 100644 --- a/src/Value/Finance/Iban.php +++ b/src/Value/Finance/Iban.php @@ -29,7 +29,7 @@ public function __construct(string $iban) public function equals(self $other): bool { - return $this == $other; + return $this->iban === $other->iban; } public function __toString(): string diff --git a/src/Value/LanguageCode.php b/src/Value/LanguageCode.php index fd143c7..e10b6f1 100644 --- a/src/Value/LanguageCode.php +++ b/src/Value/LanguageCode.php @@ -1,4 +1,5 @@ code = strtolower($code); + $this->code = \strtolower($code); } - /** - * @param LanguageCode $languageCode - * - * @return bool - */ - public function equals(LanguageCode $languageCode) + public function equals(LanguageCode $languageCode): bool { return $this->code === (string) $languageCode; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->code; } diff --git a/src/Value/Locale.php b/src/Value/Locale.php index 2f33f90..e19b142 100644 --- a/src/Value/Locale.php +++ b/src/Value/Locale.php @@ -1,11 +1,10 @@ languageCode = $languageCode; @@ -27,54 +22,38 @@ public function __construct(LanguageCode $languageCode, RegionCode $regionCode) } /** - * @param string $string - * - * @return Locale - * * @throws \InvalidArgumentException */ - public static function fromString($string) + public static function fromString(string $string): Locale { - if (false === strpos($string, '_')) { - throw new \InvalidArgumentException(sprintf('Given string "%s" is not a valid string representation of a locale', $string)); + if (false === \strpos($string, '_')) { + throw new \InvalidArgumentException( + \sprintf('Given string "%s" is not a valid string representation of a locale', $string) + ); } - $localeParts = explode('_', $string); + $localeParts = \explode('_', $string); return new self(new LanguageCode($localeParts[0]), new RegionCode($localeParts[1])); } - /** - * @param Locale $locale - * - * @return bool - */ - public function equals(Locale $locale) + public function equals(Locale $locale): bool { - return 0 === strcmp($this, $locale); + return 0 === \strcmp((string) $this, (string) $locale); } - /** - * @return LanguageCode - */ - public function languageCode() + public function languageCode(): LanguageCode { return $this->languageCode; } - /** - * @return RegionCode - */ - public function regionCode() + public function regionCode(): RegionCode { return $this->regionCode; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { - return sprintf('%s_%s', $this->languageCode, $this->regionCode); + return \sprintf('%s_%s', $this->languageCode, $this->regionCode); } } diff --git a/src/Value/Mail/EmailAddress.php b/src/Value/Mail/EmailAddress.php index a20ff23..4b89759 100644 --- a/src/Value/Mail/EmailAddress.php +++ b/src/Value/Mail/EmailAddress.php @@ -30,7 +30,7 @@ public function __construct(string $emailAddress) public function equals(self $other): bool { - return $this == $other; + return $this->emailAddress === $other->emailAddress; } public function __toString(): string diff --git a/src/Value/Monetary/Amount.php b/src/Value/Monetary/Amount.php index 5b5d61e..0e36183 100644 --- a/src/Value/Monetary/Amount.php +++ b/src/Value/Monetary/Amount.php @@ -3,24 +3,27 @@ namespace MyOnlineStore\Common\Domain\Value\Monetary; +use Litipk\BigNumbers\Decimal; use MyOnlineStore\Common\Domain\Value\Arithmetic\Number; final class Amount extends Number { + /** + * @param int|string|float|Number|Decimal $value + * + * @throws \InvalidArgumentException + */ public function __construct($value) { if (false !== \strpos((string) $value, '.')) { throw new \InvalidArgumentException( - sprintf('Amount must be a whole number, "%s" given', $value) + \sprintf('Amount must be a whole number, "%s" given', $value) ); } parent::__construct($value, 0); } - /** - * @return Amount - */ public static function asZero(): Amount { return new self(0); diff --git a/src/Value/RegionCode.php b/src/Value/RegionCode.php index 79cf25d..4e21e4b 100644 --- a/src/Value/RegionCode.php +++ b/src/Value/RegionCode.php @@ -20,21 +20,15 @@ final class RegionCode private $code; /** - * @param string $code - * * @throws \InvalidArgumentException */ - public function __construct($code) + public function __construct(string $code) { - if (null === $code) { - throw new \InvalidArgumentException('RegionCode can not be constructed with an empty ISO code'); - } - - if (!preg_match('/^[a-z]{2}$/i', $code)) { - throw new \InvalidArgumentException(sprintf('Invalid region code given: %s', $code)); + if (!\preg_match('/^[a-z]{2}$/i', $code)) { + throw new \InvalidArgumentException(\sprintf('Invalid region code given: %s', $code)); } - $this->code = strtoupper($code); + $this->code = \strtoupper($code); } public static function asNL(): self @@ -42,14 +36,9 @@ public static function asNL(): self return new self('NL'); } - /** - * @param RegionCode $otherRegion - * - * @return bool - */ - public function equals(RegionCode $otherRegion): bool + public function equals(self $otherRegion): bool { - return 0 === strcmp($this->code, (string) $otherRegion); + return 0 === \strcmp($this->code, (string) $otherRegion); } /** @@ -57,12 +46,9 @@ public function equals(RegionCode $otherRegion): bool */ public function lower(): string { - return strtolower($this->code); + return \strtolower($this->code); } - /** - * @return bool - */ public function isEuRegion(): bool { $euRegions = [ @@ -98,10 +84,7 @@ public function isEuRegion(): bool return isset($euRegions[$this->code]); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->code; } diff --git a/src/Value/StoreId.php b/src/Value/StoreId.php index 02fca36..91bffe5 100644 --- a/src/Value/StoreId.php +++ b/src/Value/StoreId.php @@ -1,51 +1,34 @@ assertIsNumeric($id)) { - throw new \InvalidArgumentException(sprintf('Given ID "%s" is not numeric', $id)); - } - $this->id = $id; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return (string) $this->id; } - /** - * @param StoreId $storeId - * - * @return bool - */ - public function equals(StoreId $storeId) + public function equals(self $storeId): bool { - return $this == $storeId; + return $this->id === $storeId->id; } } diff --git a/src/Value/Type/AbstractUuid.php b/src/Value/Type/AbstractUuid.php index 402d46c..b732702 100644 --- a/src/Value/Type/AbstractUuid.php +++ b/src/Value/Type/AbstractUuid.php @@ -14,20 +14,15 @@ abstract class AbstractUuid */ protected $uuid; - /** - * @param UuidInterface $uuid - */ private function __construct(UuidInterface $uuid) { $this->uuid = $uuid; } /** - * @param string $bytes - * * @return static */ - public static function fromBytes($bytes) + public static function fromBytes(string $bytes): AbstractUuid { return new static(Uuid::fromBytes($bytes)); } @@ -45,45 +40,36 @@ public static function fromNumericId(string $namespace, int $numericId): Abstrac } /** - * @param string $string - * * @return static + * + * @throws InvalidUuidStringException */ - public static function fromString($string) + public static function fromString(string $string): AbstractUuid { return new static(Uuid::fromString($string)); } /** * @return static + * + * @throws \Exception */ - public static function generate() + public static function generate(): AbstractUuid { return new static(Uuid::uuid4()); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->uuid->toString(); } - /** - * @return string - */ - public function bytes() + public function bytes(): string { return $this->uuid->getBytes(); } - /** - * @param AbstractUuid $otherUuid - * - * @return bool - */ - public function equals(AbstractUuid $otherUuid) + public function equals(AbstractUuid $otherUuid): bool { return $this->uuid->equals($otherUuid->uuid); } diff --git a/tests/Assertion/ArrayContainsClassAssertionTraitTest.php b/tests/Assertion/ArrayContainsClassAssertionTraitTest.php index 1310cc6..4eb28ae 100644 --- a/tests/Assertion/ArrayContainsClassAssertionTraitTest.php +++ b/tests/Assertion/ArrayContainsClassAssertionTraitTest.php @@ -5,10 +5,11 @@ use MyOnlineStore\Common\Domain\Assertion\ArrayContainsClassAssertionTrait; use MyOnlineStore\Common\Domain\Value\Locale; +use PHPUnit\Framework\TestCase; -final class ArrayContainsClassAssertionTraitTest extends \PHPUnit\Framework\TestCase +final class ArrayContainsClassAssertionTraitTest extends TestCase { - public function testAssertArrayContainsOnlyClass() + public function testAssertArrayContainsOnlyClass(): void { $trait = $this->getMockForTrait(ArrayContainsClassAssertionTrait::class); diff --git a/tests/Assertion/EnumValueGuardTraitTest.php b/tests/Assertion/EnumValueGuardTraitTest.php index 2e5d80b..99d8acc 100644 --- a/tests/Assertion/EnumValueGuardTraitTest.php +++ b/tests/Assertion/EnumValueGuardTraitTest.php @@ -1,29 +1,31 @@ trait = $this->getMockForTrait(EnumValueGuardTrait::class); } - public function testGuardIsValidValueNonScalarValueWillThrowInvalidArgumentException() + public function testGuardIsValidValueNonScalarValueWillThrowInvalidArgumentException(): void { $this->expectException(\InvalidArgumentException::class); $this->trait->guardIsValidValue([]); } - public function testGuardIsValidValueWithInvalidValueWillThrowInvalidArgumentException() + public function testGuardIsValidValueWithInvalidValueWillThrowInvalidArgumentException(): void { $this->expectException(\InvalidArgumentException::class); @@ -32,7 +34,7 @@ public function testGuardIsValidValueWithInvalidValueWillThrowInvalidArgumentExc $this->trait->guardIsValidValue('foobar'); } - public function testGuardIsValidValueWithVvalidValueWillReturnValue() + public function testGuardIsValidValueWithVvalidValueWillReturnValue(): void { $this->trait->expects(self::once())->method('getValidValues')->willReturn(['foobar']); diff --git a/tests/Assertion/NumericAssertionTraitTest.php b/tests/Assertion/NumericAssertionTraitTest.php index f9490eb..11860af 100644 --- a/tests/Assertion/NumericAssertionTraitTest.php +++ b/tests/Assertion/NumericAssertionTraitTest.php @@ -4,10 +4,11 @@ namespace MyOnlineStore\Common\Domain\Tests\Assertion; use MyOnlineStore\Common\Domain\Assertion\NumericAssertionTrait; +use PHPUnit\Framework\TestCase; -final class NumericAssertionTraitTest extends \PHPUnit\Framework\TestCase +final class NumericAssertionTraitTest extends TestCase { - public function testAssertIsNumeric() + public function testAssertIsNumeric(): void { $trait = $this->getMockForTrait(NumericAssertionTrait::class); diff --git a/tests/Collection/ImmutableCollectionTest.php b/tests/Collection/ImmutableCollectionTest.php index 64dba39..038f3e9 100644 --- a/tests/Collection/ImmutableCollectionTest.php +++ b/tests/Collection/ImmutableCollectionTest.php @@ -4,17 +4,18 @@ namespace MyOnlineStore\Common\Domain\Tests\Collection; use MyOnlineStore\Common\Domain\Collection\ImmutableCollection; +use PHPUnit\Framework\TestCase; -final class ImmutableCollectionTest extends \PHPUnit\Framework\TestCase +final class ImmutableCollectionTest extends TestCase { - public function testAdd() + public function testAdd(): void { $this->expectException(\LogicException::class); (new ImmutableCollection())->add('foo'); } - public function testOffsetSetWillThrowException() + public function testOffsetSetWillThrowException(): void { $collection = new ImmutableCollection(); diff --git a/tests/Collection/LocaleCollectionTest.php b/tests/Collection/LocaleCollectionTest.php index 9ddb465..f34b05f 100644 --- a/tests/Collection/LocaleCollectionTest.php +++ b/tests/Collection/LocaleCollectionTest.php @@ -12,7 +12,7 @@ final class LocaleCollectionTest extends TestCase { - public function testAsRegionCodesWillReturnRegionCodeCollection() + public function testAsRegionCodesWillReturnRegionCodeCollection(): void { $collection = new LocaleCollection( [ @@ -28,7 +28,7 @@ public function testAsRegionCodesWillReturnRegionCodeCollection() ); } - public function testAsString() + public function testAsString(): void { $collection = new LocaleCollection( [ @@ -41,14 +41,14 @@ public function testAsString() self::assertEquals(['nl_NL', 'nl_BE', 'de_DE'], $collection->asStrings()); } - public function testContains() + public function testContains(): void { $collection = new LocaleCollection([Locale::fromString('nl_NL'), Locale::fromString('de_DE')]); self::assertTrue($collection->contains(Locale::fromString('de_DE'))); self::assertFalse($collection->contains(Locale::fromString('nl_BE'))); } - public function testFromStringsWillConvertAndFilterInput() + public function testFromStringsWillConvertAndFilterInput(): void { $collection = LocaleCollection::fromStrings(['nl', 'nl_BE', 'fr_BE', 'FR']); @@ -60,7 +60,7 @@ public function testFromStringsWillConvertAndFilterInput() ); } - public function testGroupByCurrencyFormat() + public function testGroupByCurrencyFormat(): void { $collection = new LocaleCollection( [ @@ -79,7 +79,7 @@ public function testGroupByCurrencyFormat() ); } - public function testUnique() + public function testUnique(): void { $locales = LocaleCollection::fromStrings(['nl_NL', 'de_DE', 'nl_NL']); self::assertCount(3, $locales); diff --git a/tests/Collection/MutableCollectionTest.php b/tests/Collection/MutableCollectionTest.php index 017880c..f3b22db 100644 --- a/tests/Collection/MutableCollectionTest.php +++ b/tests/Collection/MutableCollectionTest.php @@ -5,10 +5,11 @@ use MyOnlineStore\Common\Domain\Collection\ImmutableCollection; use MyOnlineStore\Common\Domain\Collection\MutableCollection; +use PHPUnit\Framework\TestCase; -final class MutableCollectionTest extends \PHPUnit\Framework\TestCase +final class MutableCollectionTest extends TestCase { - public function testAdd() + public function testAdd(): void { $collection = new MutableCollection(['foo', 'bar']); $collection->add('baz'); @@ -16,7 +17,7 @@ public function testAdd() self::assertSame(['foo', 'bar', 'baz'], $collection->toArray()); } - public function testContains() + public function testContains(): void { $collection = new MutableCollection(['foo', 'bar']); @@ -24,7 +25,7 @@ public function testContains() self::assertFalse($collection->contains('baz')); } - public function testContainsWithWillReturnCorrectElements() + public function testContainsWithWillReturnCorrectElements(): void { $reflection = new \ReflectionClass(MutableCollection::class); $containsWith = $reflection->getMethod('containsWith'); @@ -36,7 +37,7 @@ public function testContainsWithWillReturnCorrectElements() $containsWith->invokeArgs( $collection, [ - function (\Exception $exception) { + static function (\Throwable $exception) { return 'foo' === $exception->getMessage(); }, ] @@ -47,7 +48,7 @@ function (\Exception $exception) { $containsWith->invokeArgs( $collection, [ - function (\Exception $exception) { + static function (\Throwable $exception) { return 'qux' === $exception->getMessage(); }, ] @@ -55,7 +56,7 @@ function (\Exception $exception) { ); } - public function testEachWillCallFunctionOnEachElement() + public function testEachWillCallFunctionOnEachElement(): void { $itemA = $this->createMock(\Iterator::class); $itemA->expects(self::once())->method('next'); @@ -66,13 +67,13 @@ public function testEachWillCallFunctionOnEachElement() $test = new MutableCollection([$itemA, $itemB]); $test->each( - function (\Iterator $item) { + static function (\Iterator $item): void { $item->next(); } ); } - public function testEqualsWillReturnTrueIfCollectionsAreOfTheSameTypeAndContainsTheSameElements() + public function testEqualsWillReturnTrueIfCollectionsAreOfTheSameTypeAndContainsTheSameElements(): void { $element1 = new \stdClass(); $element2 = new \stdClass(); @@ -90,7 +91,7 @@ public function testEqualsWillReturnTrueIfCollectionsAreOfTheSameTypeAndContains self::assertFalse($collection->equals(new MutableCollection([$element1]))); } - public function testFilterWillReturnCorrectElements() + public function testFilterWillReturnCorrectElements(): void { $element1 = $this->getMockBuilder(\stdClass::class)->setMethods(['isFoobar'])->getMock(); $element2 = $this->getMockBuilder(\stdClass::class)->setMethods(['isFoobar'])->getMock(); @@ -101,7 +102,6 @@ public function testFilterWillReturnCorrectElements() $extendedClass = new class([$element1, $element2]) extends MutableCollection { /** - * @param \Closure $closure * * @return static */ @@ -114,21 +114,21 @@ public function filter(\Closure $closure) self::assertEquals( [1 => $element2], $extendedClass->filter( - function (\stdClass $element) { + static function (\stdClass $element) { return $element->isFoobar(); } )->toArray() ); } - public function testFirst() + public function testFirst(): void { $collection = new MutableCollection(['foo', 'bar']); self::assertSame('foo', $collection->first()); } - public function testFirstHavingWillReturnCorrectElements() + public function testFirstHavingWillReturnCorrectElements(): void { $element1 = $this->getMockBuilder(\stdClass::class)->setMethods(['isFoobar'])->getMock(); $element2 = $this->getMockBuilder(\stdClass::class)->setMethods(['isFoobar'])->getMock(); @@ -139,8 +139,6 @@ public function testFirstHavingWillReturnCorrectElements() $extendedClass = new class([$element1, $element2]) extends MutableCollection { /** - * @param callable $callback - * * @return static * * @throws \OutOfBoundsException @@ -154,14 +152,14 @@ public function firstHaving(callable $callback) self::assertSame( $element2, $extendedClass->firstHaving( - function (\stdClass $element) { + static function (\stdClass $element) { return $element->isFoobar(); } ) ); } - public function testFirstHavingWithNoResultWillThrowException() + public function testFirstHavingWithNoResultWillThrowException(): void { $element1 = $this->getMockBuilder(\stdClass::class)->setMethods(['isFoobar'])->getMock(); @@ -170,8 +168,6 @@ public function testFirstHavingWithNoResultWillThrowException() $extendedClass = new class([$element1]) extends MutableCollection { /** - * @param callable $callback - * * @return static * * @throws \OutOfBoundsException @@ -185,13 +181,13 @@ public function firstHaving(callable $callback) $this->expectException(\OutOfBoundsException::class); $extendedClass->firstHaving( - function (\stdClass $element) { + static function (\stdClass $element) { return $element->isFoobar(); } ); } - public function testIndexOf() + public function testIndexOf(): void { $collection = new MutableCollection(['foo', 'bar']); @@ -199,21 +195,21 @@ public function testIndexOf() self::assertSame(1, $collection->indexOf('bar')); } - public function testIsEmpty() + public function testIsEmpty(): void { self::assertTrue((new MutableCollection())->isEmpty()); self::assertFalse((new MutableCollection(['foo']))->isEmpty()); self::assertFalse((new MutableCollection([null]))->isEmpty()); } - public function testLastWillReturnLastElementInArray() + public function testLastWillReturnLastElementInArray(): void { $test = new MutableCollection([1, 2, 3]); self::assertSame(3, $test->last()); } - public function testMapWillMapCorrectElements() + public function testMapWillMapCorrectElements(): void { $element1 = $this->createMock(\stdClass::class); $element2 = $this->createMock(\stdClass::class); @@ -221,8 +217,6 @@ public function testMapWillMapCorrectElements() $extendedClass = new class([$element1, $element2]) extends MutableCollection { /** - * @param \Closure $closure - * * @return static */ public function map(\Closure $closure) @@ -234,14 +228,14 @@ public function map(\Closure $closure) self::assertSame( [$element1, $element2], $extendedClass->map( - function (\stdClass $element) { + static function (\stdClass $element) { return $element; } )->toArray() ); } - public function testReindexWillReturnReindexedArray() + public function testReindexWillReturnReindexedArray(): void { $test = new MutableCollection([1 => 1, 3 => 2, 666 => 3]); diff --git a/tests/Collection/RegionCodeCollectionTest.php b/tests/Collection/RegionCodeCollectionTest.php index 7e4a643..f56e2c1 100644 --- a/tests/Collection/RegionCodeCollectionTest.php +++ b/tests/Collection/RegionCodeCollectionTest.php @@ -5,10 +5,11 @@ use MyOnlineStore\Common\Domain\Collection\RegionCodeCollection; use MyOnlineStore\Common\Domain\Value\RegionCode; +use PHPUnit\Framework\TestCase; -final class RegionCodeCollectionTest extends \PHPUnit\Framework\TestCase +final class RegionCodeCollectionTest extends TestCase { - public function testContainsShouldReturnIfTheGivenRegionCodeIsPresentInCollection() + public function testContainsShouldReturnIfTheGivenRegionCodeIsPresentInCollection(): void { $regionCodes = new RegionCodeCollection([new RegionCode('NL'), new RegionCode('DE')]); @@ -16,7 +17,7 @@ public function testContainsShouldReturnIfTheGivenRegionCodeIsPresentInCollectio self::assertFalse($regionCodes->contains(new RegionCode('GB'))); } - public function testFromCountryNamesWillReturnNewRegionCodeCollection() + public function testFromCountryNamesWillReturnNewRegionCodeCollection(): void { self::assertEquals( new RegionCodeCollection( @@ -30,7 +31,7 @@ public function testFromCountryNamesWillReturnNewRegionCodeCollection() ); } - public function testUniqueWillReturnCollectionWithUniqueCodesOnly() + public function testUniqueWillReturnCollectionWithUniqueCodesOnly(): void { $collection = new RegionCodeCollection( [ diff --git a/tests/Collection/StoreIdsTest.php b/tests/Collection/StoreIdsTest.php index 7b737d4..65158a3 100644 --- a/tests/Collection/StoreIdsTest.php +++ b/tests/Collection/StoreIdsTest.php @@ -9,29 +9,29 @@ final class StoreIdsTest extends TestCase { - public function testConstructorWillEnsureValueObjects() + public function testConstructorWillEnsureValueObjects(): void { self::assertEquals( new StoreIds([new StoreId(123), new StoreId(456), new StoreId(789)]), - new StoreIds([new StoreId(123), '456', 789]) + new StoreIds([new StoreId(123), 456, 789]) ); } - public function testGetRandomReturnsRandomElementFromCollection() + public function testGetRandomReturnsRandomElementFromCollection(): void { $storeIds = new StoreIds([new StoreId(123), new StoreId(456), new StoreId(789)]); self::assertContains($storeIds->getRandom(), $storeIds->toArray()); } - public function testGetRandomThrowsExceptionIfCollectionIsEmpty() + public function testGetRandomThrowsExceptionIfCollectionIsEmpty(): void { $this->expectException(\InvalidArgumentException::class); (new StoreIds([]))->getRandom(); } - public function testContainsWillCompareBasedOnEqualityInsteadOfIdentity() + public function testContainsWillCompareBasedOnEqualityInsteadOfIdentity(): void { $storeId = new StoreId(123); $storeIds = new StoreIds([$storeId]); @@ -40,7 +40,7 @@ public function testContainsWillCompareBasedOnEqualityInsteadOfIdentity() self::assertFalse($storeIds->contains(new StoreId(456))); } - public function testUniqueWillReturnUniqueValues() + public function testUniqueWillReturnUniqueValues(): void { $storeId1 = new StoreId(123); $storeId2 = new StoreId(456); diff --git a/tests/Collection/StringCollectionTraitTest.php b/tests/Collection/StringCollectionTraitTest.php index 9f4b34d..06b3d78 100644 --- a/tests/Collection/StringCollectionTraitTest.php +++ b/tests/Collection/StringCollectionTraitTest.php @@ -4,19 +4,19 @@ namespace MyOnlineStore\Common\Domain\Tests\Collection; use MyOnlineStore\Common\Domain\Collection\StringCollectionTrait; +use PHPUnit\Framework\TestCase; -final class StringCollectionTraitTest extends \PHPUnit\Framework\TestCase +final class StringCollectionTraitTest extends TestCase { - public function testAsStringsWillReturnArrayOfStrings() + public function testAsStringsWillReturnArrayOfStrings(): void { $exception = new \Exception('bar'); - /** @var StringCollectionTrait $trait */ $trait = $this->getMockForTrait(StringCollectionTrait::class); $trait->expects(self::once()) ->method('toArray') ->willReturn([0.00, $exception, 1337]); - self::assertEquals(['0.00', (string) $exception, '1337'], $trait->asStrings()); + self::assertEquals(['0', (string) $exception, '1337'], $trait->asStrings()); } } diff --git a/tests/Value/Arithmetic/NumberTest.php b/tests/Value/Arithmetic/NumberTest.php index 816e305..f6ad533 100644 --- a/tests/Value/Arithmetic/NumberTest.php +++ b/tests/Value/Arithmetic/NumberTest.php @@ -8,24 +8,24 @@ final class NumberTest extends TestCase { - public function testAdd() + public function testAdd(): void { self::assertEquals(new Number(666), (new Number(123))->add(new Number(543))); self::assertTrue((new Number(666.0))->equals((new Number(123))->add(new Number(543)))); - self::assertEquals(new Number(666.666), (new Number(123.123))->add(new Number(543.543))); + self::assertTrue((new Number(666.666))->equals((new Number(123.123, 14))->add(new Number(543.543)))); // operation without scale will retult in implicit natural scale self::assertEquals(new Number(666.666, 3), (new Number(123.666))->add(new Number(543))); self::assertEquals(new Number(0.0, 14), (new Number(666.6))->add(new Number(-666.6))); - self::assertEquals(new Number(666.666), (new Number(123))->add(new Number(543.666))); + self::assertTrue((new Number(666.666))->equals((new Number(123))->add(new Number(543.666)))); self::assertNotEquals(new Number(0), (new Number(666.6))->add(new Number(-666.6))); // scale mismatch self::assertNotEquals(new Number(666.6666), (new Number(123))->add(new Number(543.666))); self::assertNotEquals(new Number(666.7), (new Number(123.123))->add(new Number(543.543), 1)); // scale mismatch self::assertEquals(new Number(666.7, 1), (new Number(123.123))->add(new Number(543.543), 1)); } - public function testAssertOperand() + public function testAssertOperand(): void { $operand = new Number(543); @@ -44,7 +44,7 @@ public function testAssertOperand() $numberMock->subtract($operand); } - public function testAssignValuePreservesClass() + public function testAssignValuePreservesClass(): void { $subclassedNumber = $this->createSubclassedNumber(123); $newSubclassedNumber = $subclassedNumber->add(new Number(543)); @@ -56,7 +56,7 @@ public function testAssignValuePreservesClass() self::assertEquals(new Number(666), (new Number(543))->add($subclassedNumber)); } - public function testChangeValue() + public function testChangeValue(): void { // scale will be ignored when changing to integer inputs self::assertEquals(new Number(666), (new Number(123))->changeValue(666)); @@ -74,15 +74,15 @@ public function testChangeValue() self::assertEquals(new Number(666.6, 14), (new Number(123.1, 5))->changeValue(666.6)); } - public function testDivideBy() + public function testDivideBy(): void { self::assertEquals(new Number(111), (new Number(666))->divideBy(new Number(6))); - self::assertEquals(new Number(111.1, 14), (new Number(666.6))->divideBy(new Number(6))); + self::assertTrue((new Number(111.1))->equals((new Number(666.6))->divideBy(new Number(6)))); self::assertEquals(new Number(1.111, 14), (new Number(666.6))->divideBy(new Number('6.0E2'))); self::assertEquals(new Number(1.11, 2), (new Number(666.6))->divideBy(new Number('6.0E2'), 2)); } - public function testEquals() + public function testEquals(): void { self::assertTrue((new Number(666))->equals(new Number(666))); self::assertTrue((new Number(666))->equals(new Number(666.0))); // equals does normalizes scale difference @@ -91,7 +91,7 @@ public function testEquals() self::assertFalse((new Number(666))->equals(new Number(66.6))); } - public function testIsGreaterThan() + public function testIsGreaterThan(): void { self::assertTrue((new Number(666))->isGreaterThan(new Number(665))); self::assertTrue((new Number(6.66))->isGreaterThan(new Number(6.65))); @@ -99,7 +99,7 @@ public function testIsGreaterThan() self::assertFalse((new Number(665))->isGreaterThan(new Number(666))); } - public function testIsLessThan() + public function testIsLessThan(): void { self::assertTrue((new Number(665))->isLessThan(new Number(666))); self::assertTrue((new Number(6.65))->isLessThan(new Number(6.66))); @@ -107,7 +107,7 @@ public function testIsLessThan() self::assertFalse((new Number(666))->isLessThan(new Number(665))); } - public function testIsZero() + public function testIsZero(): void { self::assertTrue((new Number(0))->isZero()); self::assertTrue((new Number(0.0))->isZero()); @@ -117,7 +117,7 @@ public function testIsZero() self::assertFalse((new Number(-0.1))->isZero()); } - public function testMultiplyBy() + public function testMultiplyBy(): void { self::assertEquals(new Number(666), (new Number(111))->multiplyBy(new Number(6))); @@ -128,22 +128,22 @@ public function testMultiplyBy() self::assertEquals(new Number(666.66, 2), (new Number(111.11))->multiplyBy(new Number(6))); // align the scale of the operation result and expected value - self::assertEquals(new Number(666.66, 3), (new Number(111.11))->multiplyBy(new Number(6), 3)); + self::assertTrue((new Number(666.66, 3))->equals((new Number(111.11))->multiplyBy(new Number(6), 3))); } - public function testPowerTo() + public function testPowerTo(): void { self::assertEquals(new Number(256), (new Number(2))->powerTo(new Number(8))); } - public function testSubtract() + public function testSubtract(): void { self::assertEquals(new Number(666), (new Number(1000))->subtract(new Number(334))); - self::assertEquals(new Number(666.7), (new Number(1000))->subtract(new Number(333.3))); - self::assertEquals(new Number(666.7), (new Number('1.0E3'))->subtract(new Number(333.3))); + self::assertTrue((new Number(666.7))->equals((new Number(1000))->subtract(new Number(333.3)))); + self::assertTrue((new Number(666.7))->equals((new Number('1.0E3'))->subtract(new Number(333.3)))); } - public function testToScale() + public function testToScale(): void { self::assertEquals(new Number('1.23'), (new Number(123))->toScale(2)); self::assertEquals(new Number(1.23, 2), (new Number(123))->toScale(2)); diff --git a/tests/Value/CurrencyIsoTest.php b/tests/Value/CurrencyIsoTest.php index ee232ae..82cafed 100644 --- a/tests/Value/CurrencyIsoTest.php +++ b/tests/Value/CurrencyIsoTest.php @@ -4,16 +4,16 @@ namespace MyOnlineStore\Common\Domain\Tests\Value; use MyOnlineStore\Common\Domain\Value\CurrencyIso; +use PHPUnit\Framework\TestCase; -final class CurrencyIsoTest extends \PHPUnit\Framework\TestCase +final class CurrencyIsoTest extends TestCase { /** - * @return array[] + * @return string[][] */ - public function invalidArgumentProvider() + public function invalidArgumentProvider(): array { return [ - [123], ['EU'], ['eur'], ['EUr'], @@ -21,13 +21,13 @@ public function invalidArgumentProvider() ]; } - public function testEquals() + public function testEquals(): void { self::assertTrue((new CurrencyIso('EUR'))->equals(new CurrencyIso('EUR'))); self::assertFalse((new CurrencyIso('EUR'))->equals(new CurrencyIso('USD'))); } - public function testGetMinorUnit() + public function testGetMinorUnit(): void { self::assertEquals(3, (new CurrencyIso('OMR'))->getMinorUnit()); self::assertEquals(2, (new CurrencyIso('EUR'))->getMinorUnit()); @@ -37,16 +37,16 @@ public function testGetMinorUnit() /** * @dataProvider invalidArgumentProvider - * @expectedException \InvalidArgumentException * * @param mixed $argument */ - public function testInvalidTypes($argument) + public function testInvalidTypes($argument): void { + $this->expectException(\InvalidArgumentException::class); new CurrencyIso($argument); } - public function testToString() + public function testToString(): void { self::assertEquals('EUR', (string) new CurrencyIso('EUR')); self::assertEquals('USD', (string) new CurrencyIso('USD')); diff --git a/tests/Value/Finance/IbanTest.php b/tests/Value/Finance/IbanTest.php index 0ab3375..d149420 100644 --- a/tests/Value/Finance/IbanTest.php +++ b/tests/Value/Finance/IbanTest.php @@ -9,19 +9,19 @@ final class IbanTest extends TestCase { - public function testConstructorAndToString() + public function testConstructorAndToString(): void { $emailAddress = new Iban('NL45ABNA0946659707'); self::assertEquals('NL45ABNA0946659707', (string) $emailAddress); } - public function testConstructorDoesNotAcceptAnInvalidIban() + public function testConstructorDoesNotAcceptAnInvalidIban(): void { $this->expectException(InvalidIban::class); new Iban('NLD60RABO0190431121'); } - public function testEquals() + public function testEquals(): void { $emailAddress = new Iban('NL45ABNA0946659707'); diff --git a/tests/Value/LanguageCodeTest.php b/tests/Value/LanguageCodeTest.php index f531a64..250c811 100644 --- a/tests/Value/LanguageCodeTest.php +++ b/tests/Value/LanguageCodeTest.php @@ -4,41 +4,42 @@ namespace MyOnlineStore\Common\Domain\Tests\Value; use MyOnlineStore\Common\Domain\Value\LanguageCode; +use PHPUnit\Framework\TestCase; -final class LanguageCodeTest extends \PHPUnit\Framework\TestCase +final class LanguageCodeTest extends TestCase { /** * @dataProvider invalidArgumentProvider - * @expectedException \InvalidArgumentException * * @param mixed $argument */ - public function testInvalidTypes($argument) + public function testInvalidTypes($argument): void { + $this->expectException(\InvalidArgumentException::class); new LanguageCode($argument); } - public function testToString() + public function testToString(): void { self::assertEquals('nl', (string) new LanguageCode('nl')); self::assertEquals('nl', (string) new LanguageCode('NL')); self::assertEquals('moh', (string) new LanguageCode('MoH')); } - public function testEqual() + public function testEqual(): void { self::assertTrue((new LanguageCode('nl'))->equals(new LanguageCode('nl'))); self::assertFalse((new LanguageCode('nl'))->equals(new LanguageCode('en'))); } /** - * @return array[] + * @return string[][] */ - public function invalidArgumentProvider() + public function invalidArgumentProvider(): array { return [ ['n'], - ['nederland'] + ['nederland'], ]; } } diff --git a/tests/Value/LocaleTest.php b/tests/Value/LocaleTest.php index 36503a2..dbcb86f 100644 --- a/tests/Value/LocaleTest.php +++ b/tests/Value/LocaleTest.php @@ -6,10 +6,11 @@ use MyOnlineStore\Common\Domain\Value\LanguageCode; use MyOnlineStore\Common\Domain\Value\Locale; use MyOnlineStore\Common\Domain\Value\RegionCode; +use PHPUnit\Framework\TestCase; -final class LocaleTest extends \PHPUnit\Framework\TestCase +final class LocaleTest extends TestCase { - public function testEquals() + public function testEquals(): void { self::assertTrue(Locale::fromString('nl_NL')->equals(Locale::fromString('nl_NL'))); self::assertFalse(Locale::fromString('nl_BE')->equals(Locale::fromString('nl_NL'))); @@ -17,16 +18,16 @@ public function testEquals() /** * @dataProvider invalidArgumentProvider - * @expectedException \InvalidArgumentException * * @param mixed $argument */ - public function testFromInvalidString($argument) + public function testFromInvalidString($argument): void { + $this->expectException(\InvalidArgumentException::class); Locale::fromString($argument); } - public function testFromString() + public function testFromString(): void { $locale = Locale::fromString('nl_NL'); @@ -34,7 +35,7 @@ public function testFromString() self::assertEquals(new LanguageCode('nl'), $locale->languageCode()); } - public function testFromStringCaseInsensitive() + public function testFromStringCaseInsensitive(): void { $locale = Locale::fromString('DE_de'); @@ -42,31 +43,30 @@ public function testFromStringCaseInsensitive() self::assertEquals(new LanguageCode('de'), $locale->languageCode()); } - public function testToString() + public function testToString(): void { self::assertSame('nl_NL', (string) Locale::fromString('nl_NL')); self::assertSame('moh_CA', (string) Locale::fromString('moh_CA')); } - public function testLanguageCode() + public function testLanguageCode(): void { self::assertEquals(new LanguageCode('fr'), Locale::fromString('fr_BE')->languageCode()); self::assertEquals(new LanguageCode('nl'), Locale::fromString('nl_BE')->languageCode()); } - public function testRegionCode() + public function testRegionCode(): void { self::assertEquals(new RegionCode('NL'), Locale::fromString('nl_NL')->regionCode()); self::assertEquals(new RegionCode('DE'), Locale::fromString('de_DE')->regionCode()); } /** - * @return array[] + * @return string[][] */ - public function invalidArgumentProvider() + public function invalidArgumentProvider(): array { return [ - [123], ['ca_MOH'], ['nl'], ['n_NL'], diff --git a/tests/Value/Mail/EmailAddressTest.php b/tests/Value/Mail/EmailAddressTest.php index 11d54c8..58613be 100644 --- a/tests/Value/Mail/EmailAddressTest.php +++ b/tests/Value/Mail/EmailAddressTest.php @@ -9,19 +9,19 @@ final class EmailAddressTest extends TestCase { - public function testConstructorAndToString() + public function testConstructorAndToString(): void { $emailAddress = new EmailAddress('hi@mos.com'); self::assertEquals('hi@mos.com', (string) $emailAddress); } - public function testConstructorDoesNotAcceptAnInvalidEmailAddress() + public function testConstructorDoesNotAcceptAnInvalidEmailAddress(): void { $this->expectException(InvalidEmailAddress::class); new EmailAddress('hi'); } - public function testEquals() + public function testEquals(): void { $emailAddress = new EmailAddress('hi@mos.com'); diff --git a/tests/Value/Monetary/AmountTest.php b/tests/Value/Monetary/AmountTest.php index 65cf778..d3980fc 100644 --- a/tests/Value/Monetary/AmountTest.php +++ b/tests/Value/Monetary/AmountTest.php @@ -4,10 +4,11 @@ namespace MyOnlineStore\Common\Domain\Tests\Value\Monetary; use MyOnlineStore\Common\Domain\Value\Monetary\Amount; +use PHPUnit\Framework\TestCase; -final class AmountTest extends \PHPUnit\Framework\TestCase +final class AmountTest extends TestCase { - public function invalidInputProvider() + public function invalidInputProvider(): \Generator { yield ['1.23']; yield [1.23]; @@ -19,14 +20,14 @@ public function invalidInputProvider() * * @param mixed $input */ - public function testInvalidInput($input) + public function testInvalidInput($input): void { $this->expectException(\InvalidArgumentException::class); new Amount($input); } - public function validInputProvider() + public function validInputProvider(): \Generator { yield [123]; yield ['123']; @@ -37,12 +38,12 @@ public function validInputProvider() * * @param mixed $input */ - public function testValidInput($input) + public function testValidInput($input): void { self::assertInstanceOf(Amount::class, new Amount($input)); } - public function testAsZero() + public function testAsZero(): void { $zeroAmount = Amount::asZero(); diff --git a/tests/Value/RegionCodeTest.php b/tests/Value/RegionCodeTest.php index d3118b7..4731498 100644 --- a/tests/Value/RegionCodeTest.php +++ b/tests/Value/RegionCodeTest.php @@ -4,10 +4,11 @@ namespace MyOnlineStore\Common\Domain\Tests\Value; use MyOnlineStore\Common\Domain\Value\RegionCode; +use PHPUnit\Framework\TestCase; -final class RegionCodeTest extends \PHPUnit\Framework\TestCase +final class RegionCodeTest extends TestCase { - public function testEquals() + public function testEquals(): void { $regionCode = RegionCode::asNL(); self::assertTrue($regionCode->equals(new RegionCode('nl'))); @@ -17,23 +18,22 @@ public function testEquals() /** * @dataProvider invalidArgumentProvider * - * @expectedException \InvalidArgumentException - * * @param mixed $argument */ - public function testInvalidTypes($argument) + public function testInvalidTypes($argument): void { + $this->expectException(\InvalidArgumentException::class); new RegionCode($argument); } - public function testToString() + public function testToString(): void { self::assertEquals('NL', (string) RegionCode::asNL()); self::assertEquals('NL', (string) new RegionCode('nl')); self::assertEquals('DE', (string) new RegionCode('DE')); } - public function testToLower() + public function testToLower(): void { self::assertEquals('nl', RegionCode::asNL()->lower()); self::assertEquals('nl', (new RegionCode('nl'))->lower()); @@ -41,7 +41,7 @@ public function testToLower() } /** - * @return array[] + * @return string[][] */ public function invalidArgumentProvider(): array { @@ -49,12 +49,11 @@ public function invalidArgumentProvider(): array ['N'], ['Nld'], ['NLD'], - [null], ]; } /** - * @return array[] + * @return RegionCode[][]|bool[][] */ public function isEuRegionProvider(): array { @@ -68,11 +67,8 @@ public function isEuRegionProvider(): array /** * @dataProvider isEuRegionProvider - * - * @param RegionCode $regionCode - * @param bool $expected */ - public function testIsEuRegion(RegionCode $regionCode, $expected) + public function testIsEuRegion(RegionCode $regionCode, bool $expected): void { $this->assertEquals($expected, $regionCode->isEuRegion()); } diff --git a/tests/Value/StoreIdTest.php b/tests/Value/StoreIdTest.php index cf07f2d..77bf131 100644 --- a/tests/Value/StoreIdTest.php +++ b/tests/Value/StoreIdTest.php @@ -4,25 +4,17 @@ namespace MyOnlineStore\Common\Domain\Tests\Value; use MyOnlineStore\Common\Domain\Value\StoreId; +use PHPUnit\Framework\TestCase; -final class StoreIdTest extends \PHPUnit\Framework\TestCase +final class StoreIdTest extends TestCase { - public function testValidInput() + public function testValidInput(): void { $this->assertEquals('123', (string) new StoreId(123)); - $this->assertEquals('1234', (string) new StoreId('1234')); + $this->assertEquals('1234', (string) new StoreId(1234)); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Given ID "1a234" is not numeric - */ - public function testInvalidInput() - { - new StoreId('1a234'); - } - - public function testEquals() + public function testEquals(): void { $this->assertFalse((new StoreId(1))->equals(new StoreId(2))); $this->assertTrue((new StoreId(1))->equals(new StoreId(1))); diff --git a/tests/Value/Type/AbstractUuidTest.php b/tests/Value/Type/AbstractUuidTest.php index 31fadd0..6549f44 100644 --- a/tests/Value/Type/AbstractUuidTest.php +++ b/tests/Value/Type/AbstractUuidTest.php @@ -10,17 +10,17 @@ final class AbstractUuidTest extends TestCase { - public function testFromBytes() + public function testFromBytes(): void { $uuid = Uuid::fromString('3e5c88c3-8369-4404-8828-6a3927533387'); self::assertEquals( $uuid->getBytes(), - (string) UuidStub::fromBytes($uuid->getBytes())->bytes() + UuidStub::fromBytes($uuid->getBytes())->bytes() ); } - public function testFromString() + public function testFromString(): void { self::assertEquals( '3e5c88c3-8369-4404-8828-6a3927533387', @@ -28,7 +28,7 @@ public function testFromString() ); } - public function testGenerate() + public function testGenerate(): void { $uuid = Uuid::fromString((string) UuidStub::generate()); @@ -36,7 +36,7 @@ public function testGenerate() self::assertEquals(4, $uuid->getVersion()); } - public function testEquals() + public function testEquals(): void { $stub = UuidStub::fromString('b6094976-e5c7-4ddf-b35c-2f26d6fbcaec'); @@ -44,7 +44,7 @@ public function testEquals() self::assertFalse($stub->equals(UuidStub::fromString('5de6f213-1f41-42e0-8e0d-d2828f352ebe'))); } - public function testSpecialUuids() + public function testSpecialUuids(): void { $uuid1 = UuidStub::fromString('0e703880-7f48-11e8-b8d7-44a8421b9960'); $uuid2 = UuidStub::fromString('0e703936-7f48-11e8-b8d7-44a8421b9960'); @@ -55,7 +55,7 @@ public function testSpecialUuids() self::assertFalse($uuid2->equals($uuid1)); } - public function testFromNumericId() + public function testFromNumericId(): void { $uuid = UuidStub::fromNumericId('b6094976-e5c7-4ddf-b35c-2f26d6fbcaec', 123); @@ -63,7 +63,7 @@ public function testFromNumericId() self::assertNotEquals($uuid, UuidStub::fromNumericId('b6094976-e5c7-4ddf-b35c-2f26d6fbcaec', 124)); } - public function testFromNumericIdWithInvalidNamespace() + public function testFromNumericIdWithInvalidNamespace(): void { $this->expectException(InvalidUuidStringException::class); UuidStub::fromNumericId('foobar', 123); From 1457e8aa62c0cbf70963a24da939e129eb11719d Mon Sep 17 00:00:00 2001 From: Frank Verhoeven Date: Fri, 2 Aug 2019 16:13:41 +0200 Subject: [PATCH 2/2] Travis can't use 7.4 yet --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f6d2705..a362bce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: php php: - '7.2' - '7.3' - - '7.4' before_script: - composer self-update - composer install --prefer-source