diff --git a/src/BaseEnsurer.php b/src/BaseEnsurer.php new file mode 100644 index 0000000000000000000000000000000000000000..d7f110d33caeb28e3e466f6d0b4488b82674fa5c --- /dev/null +++ b/src/BaseEnsurer.php @@ -0,0 +1,343 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +namespace PhpExtended\Ensurer; + +use ArrayIterator; +use InvalidArgumentException; +use Iterator; +use PhpExtended\Inspector\Inspector; +use PhpExtended\Inspector\InspectorInterface; + +/** + * BaseEnsurer class file. + * + * This class encapsulates all the functions that all ensurers do. + * + * @author Anastaszor + * @SuppressWarnings(PHPMD.TooManyPublicMethods) + */ +abstract class BaseEnsurer implements EnsurerInterface +{ + + /** + * The inner inspector. + * + * @var InspectorInterface + */ + protected $_inspector; + + /** + * Builds a new Ensurer with the given inspector. + * + * @param ?InspectorInterface $inspector + */ + public function __construct(?InspectorInterface $inspector = null) + { + if(null === $inspector) + { + $inspector = new Inspector(); + } + + $this->_inspector = $inspector; + } + + /** + * {@inheritDoc} + * @see \Stringable::__toString() + */ + public function __toString() : string + { + return static::class.'@'.\spl_object_hash($this); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfBooleans() + */ + public function asArrayOfBooleans($value) : array + { + $values = []; + + foreach($this->asArray($value) as $key => $inner) + { + $values[$key] = $this->asBoolean($inner); + } + + return $values; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOfBooleans() + */ + public function asListOfBooleans($value) : array + { + return $this->asList($this->asArrayOfBooleans($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOfBooleans() + */ + public function asMapOfBooleans($value) : array + { + return $this->asMap($this->asArrayOfBooleans($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfIntegers() + */ + public function asArrayOfIntegers($value) : array + { + $values = []; + + foreach($this->asArray($value) as $key => $inner) + { + $values[$key] = $this->asInteger($inner); + } + + return $values; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOfIntegers() + */ + public function asListOfIntegers($value) : array + { + return $this->asList($this->asArrayOfIntegers($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOfIntegers() + */ + public function asMapOfIntegers($value) : array + { + return $this->asMap($this->asArrayOfIntegers($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfFloats() + */ + public function asArrayOfFloats($value) : array + { + $values = []; + + foreach($this->asArray($value) as $key => $inner) + { + $values[$key] = $this->asFloat($inner); + } + + return $values; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOfFloats() + */ + public function asListOfFloats($value) : array + { + return $this->asList($this->asArrayOfFloats($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOfFloats() + */ + public function asMapOfFloats($value) : array + { + return $this->asMap($this->asArrayOfFloats($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfStrings() + */ + public function asArrayOfStrings($value) : array + { + $values = []; + + foreach($this->asArray($value) as $key => $value) + { + $values[$key] = $this->asString($value); + } + + return $values; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOfStrings() + */ + public function asListOfStrings($value) : array + { + return $this->asList($this->asArrayOfStrings($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOfStrings() + */ + public function asMapOfStrings($value) : array + { + return $this->asMap($this->asArrayOfStrings($value)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfDateTimes() + */ + public function asArrayOfDateTimes($value, array $formats = []) : array + { + $values = []; + + foreach($this->asArray($value) as $key => $inner) + { + $values[$key] = $this->asDateTime($inner, $formats); + } + + return $values; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOfDateTimes() + */ + public function asListOfDateTimes($value, array $formats = []) : array + { + return $this->asList($this->asArrayOfDateTimes($value, $formats)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOfDateTimes() + */ + public function asMapOfDateTimes($value, array $formats = []) : array + { + return $this->asMap($this->asArrayOfDateTimes($value, $formats)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asList() + */ + public function asList($value) : array + { + $res = []; + + foreach($this->asArray($value) as $key => $record) + { + $res[$this->asInteger($key)] = $record; + } + + return $res; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMap() + */ + public function asMap($value) : array + { + $res = []; + + foreach($this->asArray($value) as $key => $record) + { + $res[$this->asString($key)] = $record; + } + + return $res; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOf() + */ + public function asArrayOf($value, string $className) : array + { + $res = []; + + foreach($this->asArray($value) as $key => $object) + { + $res[$key] = $this->asObjectOf($object, $className); + } + + return $res; + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asListOf() + */ + public function asListOf($value, string $className) : array + { + return $this->asList($this->asArrayOf($value, $className)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asMapOf() + */ + public function asMapOf($value, string $className) : array + { + return $this->asMap($this->asArrayOf($value, $className)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asIteratorOf() + */ + public function asIteratorOf($value, string $className) : Iterator + { + if(\is_array($value)) + { + return new EnsurerIterator($this, new ArrayIterator($value), $className); + } + + if(\is_object($value)) + { + if($value instanceof Iterator) + { + /** @psalm-suppress MixedArgumentTypeCoercion */ + return new EnsurerIterator($this, $value, $className); + } + + return new EnsurerIterator($this, new ArrayIterator([$value]), $className); + } + + $message = 'Impossible to transform value "{thing}" to "{class}".'; + $context = ['{thing}' => $this->_inspector->inspect($value), '{class}' => Iterator::class]; + + throw new InvalidArgumentException(\strtr($message, $context)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asIntIteratorOf() + */ + public function asIntIteratorOf($value, string $className) : Iterator + { + return new IntegerKeyIterator($this, $this->asIteratorOf($value, $className)); + } + + /** + * {@inheritDoc} + * @see \PhpExtended\Ensurer\EnsurerInterface::asStringIteratorOf() + */ + public function asStringIteratorOf($value, string $className) : Iterator + { + return new StringKeyIterator($this, $this->asIteratorOf($value, $className)); + } + +} diff --git a/src/IntegerKeyIterator.php b/src/IntegerKeyIterator.php new file mode 100644 index 0000000000000000000000000000000000000000..be5fe11a462a4a626c43985cdb9a4e64289ce210 --- /dev/null +++ b/src/IntegerKeyIterator.php @@ -0,0 +1,71 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +namespace PhpExtended\Ensurer; + +use InvalidArgumentException; +use Iterator; +use IteratorIterator; + +/** + * IntegerKeyIterator class file. + * + * This iterator iterates over objects and ensures that the keys are integer + * values. + * + * @author Anastaszor + * @template T of object + * @phpstan-ignore-next-line + * @extends IteratorIterator<integer, T, \Iterator<integer|string, T>> + * @psalm-suppress InvalidTemplateParam + */ +class IntegerKeyIterator extends IteratorIterator +{ + + /** + * The ensurer. + * + * @var EnsurerInterface + */ + protected $_ensurer; + + /** + * Builds a new EnsurerIterator based on the given other iterator + * for the given class. + * + * @param EnsurerInterface $ensurer + * @param Iterator<integer|string, T> $iterator + */ + public function __construct(EnsurerInterface $ensurer, Iterator $iterator) + { + parent::__construct($iterator); + $this->_ensurer = $ensurer; + } + + /** + * {@inheritDoc} + * @see \Stringable::__toString() + */ + public function __toString() : string + { + return static::class.'@'.\spl_object_hash($this); + } + + /** + * {@inheritDoc} + * @see \IteratorIterator::key() + * @throws InvalidArgumentException + */ + public function key() : int + { + return $this->_ensurer->asInteger(parent::key()); + } + +} diff --git a/src/LooseEnsurer.php b/src/LooseEnsurer.php index 6499e4c4679a2b11173f1ffde0735dccba280ab3..75d7232cc8c41100cb99e84a1d388b16ebd2e27f 100644 --- a/src/LooseEnsurer.php +++ b/src/LooseEnsurer.php @@ -10,13 +10,9 @@ namespace PhpExtended\Ensurer; -use ArrayIterator; use DateTimeImmutable; use DateTimeInterface; use InvalidArgumentException; -use Iterator; -use PhpExtended\Inspector\Inspector; -use PhpExtended\Inspector\InspectorInterface; use Traversable; /** @@ -27,42 +23,10 @@ use Traversable; * * @author Anastaszor * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) - * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ -class LooseEnsurer implements EnsurerInterface +class LooseEnsurer extends BaseEnsurer { - /** - * The inner inspector. - * - * @var InspectorInterface - */ - protected $_inspector; - - /** - * Builds a new Ensurer with the given inspector. - * - * @param ?InspectorInterface $inspector - */ - public function __construct(?InspectorInterface $inspector = null) - { - if(null === $inspector) - { - $inspector = new Inspector(); - } - - $this->_inspector = $inspector; - } - - /** - * {@inheritDoc} - * @see \Stringable::__toString() - */ - public function __toString() : string - { - return static::class.'@'.\spl_object_hash($this); - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asBooleanOrNull() @@ -125,22 +89,6 @@ class LooseEnsurer implements EnsurerInterface return $value ?? false; } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfBooleans() - */ - public function asArrayOfBooleans($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asBoolean($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asIntegerOrNull() @@ -184,22 +132,6 @@ class LooseEnsurer implements EnsurerInterface return $value ?? 0; } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfIntegers() - */ - public function asArrayOfIntegers($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asInteger($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asFloatOrNull() @@ -243,22 +175,6 @@ class LooseEnsurer implements EnsurerInterface return $value ?? 0.0; } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfFloats() - */ - public function asArrayOfFloats($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asFloat($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asStringOrNull() @@ -324,22 +240,6 @@ class LooseEnsurer implements EnsurerInterface return $value ?? ''; } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfStrings() - */ - public function asArrayOfStrings($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $value) - { - $values[$key] = $this->asString($value); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asDateTimeOrNull() @@ -382,22 +282,6 @@ class LooseEnsurer implements EnsurerInterface return $value ?? new DateTimeImmutable('@0'); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfDateTimes() - */ - public function asArrayOfDateTimes($value, array $formats = []) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asDateTime($inner, $formats); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asArray() @@ -486,48 +370,4 @@ class LooseEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOf() - */ - public function asArrayOf($value, string $className) : array - { - $res = []; - - foreach($this->asArray($value) as $key => $object) - { - $res[$key] = $this->asObjectOf($object, $className); - } - - return $res; - } - - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asIteratorOf() - */ - public function asIteratorOf($value, string $className) : Iterator - { - if(\is_array($value)) - { - return new EnsurerIterator($this, new ArrayIterator($value), $className); - } - - if(\is_object($value)) - { - if($value instanceof Iterator) - { - /** @psalm-suppress MixedArgumentTypeCoercion */ - return new EnsurerIterator($this, $value, $className); - } - - return new EnsurerIterator($this, new ArrayIterator([$value]), $className); - } - - $message = 'Impossible to transform value "{thing}" to "{class}".'; - $context = ['{thing}' => $this->_inspector->inspect($value), '{class}' => Iterator::class]; - - throw new InvalidArgumentException(\strtr($message, $context)); - } - } diff --git a/src/StrictEnsurer.php b/src/StrictEnsurer.php index 89053629c0f0810674fb5dc88e39e1ef552cc249..f98c626e23b37f1b431d93868fa8c4dd35c89e5e 100644 --- a/src/StrictEnsurer.php +++ b/src/StrictEnsurer.php @@ -10,13 +10,9 @@ namespace PhpExtended\Ensurer; -use ArrayIterator; use DateTimeImmutable; use DateTimeInterface; use InvalidArgumentException; -use Iterator; -use PhpExtended\Inspector\Inspector; -use PhpExtended\Inspector\InspectorInterface; /** * StrictEnsurer class file. @@ -25,43 +21,10 @@ use PhpExtended\Inspector\InspectorInterface; * accepts data that is already on the right type. * * @author Anastaszor - * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) - * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ -class StrictEnsurer implements EnsurerInterface +class StrictEnsurer extends BaseEnsurer { - /** - * The inner inspector. - * - * @var InspectorInterface - */ - protected $_inspector; - - /** - * Builds a new Ensurer with the given inspector. - * - * @param ?InspectorInterface $inspector - */ - public function __construct(?InspectorInterface $inspector = null) - { - if(null === $inspector) - { - $inspector = new Inspector(); - } - - $this->_inspector = $inspector; - } - - /** - * {@inheritDoc} - * @see \Stringable::__toString() - */ - public function __toString() : string - { - return static::class.'@'.\spl_object_hash($this); - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asBooleanOrNull() @@ -96,22 +59,6 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfBooleans() - */ - public function asArrayOfBooleans($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asBoolean($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asIntegerOrNull() @@ -151,22 +98,6 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfIntegers() - */ - public function asArrayOfIntegers($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asInteger($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asFloatOrNull() @@ -206,22 +137,6 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfFloats() - */ - public function asArrayOfFloats($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asFloat($inner); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asStringOrNull() @@ -256,22 +171,6 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfStrings() - */ - public function asArrayOfStrings($value) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $value) - { - $values[$key] = $this->asString($value); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asDateTimeOrNull() @@ -329,22 +228,6 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOfDateTimes() - */ - public function asArrayOfDateTimes($value, array $formats = []) : array - { - $values = []; - - foreach($this->asArray($value) as $key => $inner) - { - $values[$key] = $this->asDateTime($inner, $formats); - } - - return $values; - } - /** * {@inheritDoc} * @see \PhpExtended\Ensurer\EnsurerInterface::asArray() @@ -400,48 +283,4 @@ class StrictEnsurer implements EnsurerInterface throw new InvalidArgumentException(\strtr($message, $context)); } - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asArrayOf() - */ - public function asArrayOf($value, string $className) : array - { - $res = []; - - foreach($this->asArray($value) as $key => $object) - { - $res[$key] = $this->asObjectOf($object, $className); - } - - return $res; - } - - /** - * {@inheritDoc} - * @see \PhpExtended\Ensurer\EnsurerInterface::asIteratorOf() - */ - public function asIteratorOf($value, string $className) : Iterator - { - if(\is_array($value)) - { - return new EnsurerIterator($this, new ArrayIterator($value), $className); - } - - if(\is_object($value)) - { - if($value instanceof Iterator) - { - /** @psalm-suppress MixedArgumentTypeCoercion */ - return new EnsurerIterator($this, $value, $className); - } - - return new EnsurerIterator($this, new ArrayIterator([$value]), $className); - } - - $message = 'Impossible to transform value "{thing}" to "{class}".'; - $context = ['{thing}' => $this->_inspector->inspect($value), '{class}' => Iterator::class]; - - throw new InvalidArgumentException(\strtr($message, $context)); - } - } diff --git a/src/StringKeyIterator.php b/src/StringKeyIterator.php new file mode 100644 index 0000000000000000000000000000000000000000..1d52735b24733e3dbb836961ad421679675e762a --- /dev/null +++ b/src/StringKeyIterator.php @@ -0,0 +1,71 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +namespace PhpExtended\Ensurer; + +use InvalidArgumentException; +use Iterator; +use IteratorIterator; + +/** + * StringKeyIterator class file. + * + * This iterator iterates over objects and ensures that the keys are string + * values. + * + * @author Anastaszor + * @template T of object + * @phpstan-ignore-next-line + * @extends IteratorIterator<string, T, \Iterator<integer|string, T>> + * @psalm-suppress InvalidTemplateParam + */ +class StringKeyIterator extends IteratorIterator +{ + + /** + * The ensurer. + * + * @var EnsurerInterface + */ + protected $_ensurer; + + /** + * Builds a new EnsurerIterator based on the given other iterator + * for the given class. + * + * @param EnsurerInterface $ensurer + * @param Iterator<integer|string, T> $iterator + */ + public function __construct(EnsurerInterface $ensurer, Iterator $iterator) + { + parent::__construct($iterator); + $this->_ensurer = $ensurer; + } + + /** + * {@inheritDoc} + * @see \Stringable::__toString() + */ + public function __toString() : string + { + return static::class.'@'.\spl_object_hash($this); + } + + /** + * {@inheritDoc} + * @see \IteratorIterator::key() + * @throws InvalidArgumentException + */ + public function key() : string + { + return $this->_ensurer->asString(parent::key()); + } + +} diff --git a/test/BaseEnsurerTest.php b/test/BaseEnsurerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..632556b3f840b7f6e3db370c43b43d85c2990c40 --- /dev/null +++ b/test/BaseEnsurerTest.php @@ -0,0 +1,57 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +use PhpExtended\Ensurer\BaseEnsurer; +use PHPUnit\Framework\TestCase; + +/** + * BaseEnsurerTest test file. + * + * @author Anastaszor + * @covers \PhpExtended\Ensurer\BaseEnsurer + * + * @internal + * + * @small + */ +class BaseEnsurerTest extends TestCase +{ + + /** + * The object to test. + * + * @var BaseEnsurer + */ + protected $_object; + + public function testToString() : void + { + $this->assertNotNull($this->_object->__toString()); + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::setUp() + */ + protected function setUp() : void + { + $this->_object = $this->createMock(BaseEnsurer::class); + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::tearDown() + */ + protected function tearDown() : void + { + $this->_object = null; + } + +} diff --git a/test/IntegerKeyIteratorTest.php b/test/IntegerKeyIteratorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e1b2e97811bd3d3d00e9ba192ac66716c1b6a4ee --- /dev/null +++ b/test/IntegerKeyIteratorTest.php @@ -0,0 +1,67 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +use PhpExtended\Ensurer\IntegerKeyIterator; +use PhpExtended\Ensurer\LooseEnsurer; +use PHPUnit\Framework\TestCase; + +/** + * IntegerKeyIteratorTest test file. + * + * @author Anastaszor + * @covers \PhpExtended\Ensurer\IntegerKeyIterator + * + * @internal + * + * @small + */ +class IntegerKeyIteratorTest extends TestCase +{ + + /** + * The object to test. + * + * @var IntegerKeyIterator + */ + protected $_object; + + public function testToString() : void + { + $this->assertEquals(\get_class($this->_object).'@'.\spl_object_hash($this->_object), $this->_object->__toString()); + } + + public function testContents() : void + { + foreach($this->_object as $key => $value) + { + $this->assertIsInt($key); + $this->assertEquals(new stdClass(), $value); + } + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::setUp() + */ + protected function setUp() : void + { + $this->_object = new IntegerKeyIterator(new LooseEnsurer(), new ArrayIterator([new stdClass()])); + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::tearDown() + */ + protected function tearDown() : void + { + $this->_object = null; + } + +} diff --git a/test/LooseEnsurerTest.php b/test/LooseEnsurerTest.php index 8054eb9fb87f7e761fed03ec7c1bb69c76ce8045..ceee771b054cd3ece9b3bf5fa8a5652d4db5b9d3 100644 --- a/test/LooseEnsurerTest.php +++ b/test/LooseEnsurerTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; * LooseEnsurerTest class file. * * @author Anastaszor + * @covers \PhpExtended\Ensurer\BaseEnsurer * @covers \PhpExtended\Ensurer\LooseEnsurer * * @internal @@ -43,6 +44,11 @@ class LooseEnsurerTest extends TestCase $this->assertFalse($this->_ensurer->asBoolean(null)); } + public function testBooleanNull2() : void + { + $this->assertNull($this->_ensurer->asBooleanOrNull('\n')); + } + public function testBooleanTrue() : void { $this->assertTrue($this->_ensurer->asBoolean(true)); @@ -440,6 +446,101 @@ class LooseEnsurerTest extends TestCase ); } + public function testAsListOfBooleans() : void + { + $this->assertEquals([true], $this->_ensurer->asListOfBooleans([true])); + } + + public function testAsMapOfBooleans() : void + { + $this->assertEquals(['0' => true], $this->_ensurer->asMapOfBooleans([true])); + } + + public function testAsListOfIntegers() : void + { + $this->assertEquals([1], $this->_ensurer->asListOfIntegers([1])); + } + + public function testAsMapOfIntegers() : void + { + $this->assertEquals(['0' => 1], $this->_ensurer->asMapOfIntegers([1])); + } + + public function testAsListOfFloats() : void + { + $this->assertEquals([1.5], $this->_ensurer->asListOfFloats([1.5])); + } + + public function testAsMapOfFloats() : void + { + $this->assertEquals(['0' => 1.5], $this->_ensurer->asMapOfFloats([1.5])); + } + + public function testAsListOfStrings() : void + { + $this->assertEquals(['str'], $this->_ensurer->asListOfStrings(['str'])); + } + + public function testAsMapOfStrings() : void + { + $this->assertEquals(['0' => 'str'], $this->_ensurer->asMapOfStrings(['str'])); + } + + public function testAsListOfDateTimes() : void + { + $dt = new DateTimeImmutable(); + $this->assertEquals([$dt], $this->_ensurer->asListOfDateTimes([$dt])); + } + + public function testAsMapOfDateTimes() : void + { + $dt = new DateTimeImmutable(); + $this->assertEquals(['0' => $dt], $this->_ensurer->asMapOfDateTimes([$dt])); + } + + public function testAsList() : void + { + $expected = [true, 1, 1.5, 'str', new DateTimeImmutable()]; + $this->assertEquals($expected, $this->_ensurer->asList($expected)); + } + + public function testAsMap() : void + { + $dt = new DateTimeImmutable(); + $given = [true, 1, 1.5, 'str', $dt]; + $expected = ['0' => true, '1' => 1, '2' => 1.5, '3' => 'str', '4' => $dt]; + $this->assertEquals($expected, $this->_ensurer->asMap($given)); + } + + public function testAsListOf() : void + { + $expected = [new stdClass()]; + $this->assertEquals($expected, $this->_ensurer->asListOf($expected, stdClass::class)); + } + + public function testAsMapOf() : void + { + $this->assertEquals(['0' => new stdClass()], $this->_ensurer->asMapOf([new stdClass()], stdClass::class)); + } + + public function testAsIntIteratorOf() : void + { + foreach($this->_ensurer->asIntIteratorOf([new stdClass()], stdClass::class) as $key => $value) + { + $this->assertIsInt($key); + $this->assertInstanceOf(stdClass::class, $value); + } + } + + public function testAsStringIteratorOf() : void + { + foreach($this->_ensurer->asStringIteratorOf([new stdClass()], stdClass::class) as $key => $value) + { + $this->assertIsString($key); + $this->assertInstanceOf(stdClass::class, $value); + } + } + /** * {@inheritDoc} * @see \PHPUnit\Framework\TestCase::setUp() diff --git a/test/StrictEnsurerTest.php b/test/StrictEnsurerTest.php index 76df94869b7732cd47b870ab50dd3a64299c09b7..b7a0c1bb9ef5e77a8487681e9f67dac322a81787 100644 --- a/test/StrictEnsurerTest.php +++ b/test/StrictEnsurerTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; * StrictEnsurerTest class file. * * @author Anastaszor + * @covers \PhpExtended\Ensurer\BaseEnsurer * @covers \PhpExtended\Ensurer\StrictEnsurer * * @internal diff --git a/test/StringKeyIteratorTest.php b/test/StringKeyIteratorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..00bdf911fdf1a985590c773427c98b47e405f286 --- /dev/null +++ b/test/StringKeyIteratorTest.php @@ -0,0 +1,67 @@ +<?php declare(strict_types=1); + +/* + * This file is part of the php-extended/php-ensurer-object library + * + * (c) Anastaszor + * This source file is subject to the MIT license that + * is bundled with this source code in the file LICENSE. + */ + +use PhpExtended\Ensurer\LooseEnsurer; +use PhpExtended\Ensurer\StringKeyIterator; +use PHPUnit\Framework\TestCase; + +/** + * StringKeyIteratorTest test file. + * + * @author Anastaszor + * @covers \PhpExtended\Ensurer\StringKeyIterator + * + * @internal + * + * @small + */ +class StringKeyIteratorTest extends TestCase +{ + + /** + * The object to test. + * + * @var StringKeyIterator + */ + protected $_object; + + public function testToString() : void + { + $this->assertEquals(\get_class($this->_object).'@'.\spl_object_hash($this->_object), $this->_object->__toString()); + } + + public function testContents() : void + { + foreach($this->_object as $key => $value) + { + $this->assertIsString($key); + $this->assertEquals(new stdClass(), $value); + } + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::setUp() + */ + protected function setUp() : void + { + $this->_object = new StringKeyIterator(new LooseEnsurer(), new ArrayIterator([new stdClass()])); + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::tearDown() + */ + protected function tearDown() : void + { + $this->_object = null; + } + +}