diff --git a/.travis.install.sh b/.travis.install.sh deleted file mode 100755 index cd60845..0000000 --- a/.travis.install.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh -set -x -if [ "$TRAVIS_PHP_VERSION" = 'hhvm' ] || [ "$TRAVIS_PHP_VERSION" = 'hhvm-nightly' ] ; then - curl -sS https://getcomposer.org/installer > composer-installer.php - hhvm composer-installer.php - hhvm -v ResourceLimit.SocketDefaultTimeout=30 -v Http.SlowQueryThreshold=30000 composer.phar update --prefer-source -else - composer self-update - composer update --prefer-source -fi diff --git a/.travis.yml b/.travis.yml index ae7bb60..f7cff77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,18 @@ language: php php: - - 5.6 - - 7.0 - 7.1 - - hhvm + - nightly before_script: - - ./.travis.install.sh - - if [ $TRAVIS_PHP_VERSION = '5.6' ]; then PHPUNIT_FLAGS="--coverage-clover coverage.clover"; else PHPUNIT_FLAGS=""; fi + - composer self-update + - composer update script: - - ./vendor/bin/phpunit $PHPUNIT_FLAGS; - - ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/; - - php -n ./vendor/bin/athletic -p ./tests/DoctrineTest/InstantiatorPerformance/ -f GroupedFormatter; + - ./vendor/bin/phpunit --coverage-clover coverage.clover + - ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/ + - php -n ./vendor/bin/athletic -p ./tests/DoctrineTest/InstantiatorPerformance/ -f GroupedFormatter after_script: - - if [ $TRAVIS_PHP_VERSION = '5.6' ]; then wget https://scrutinizer-ci.com/ocular.phar; php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/README.md b/README.md index a74e6ee..b66064b 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ itself: ```php $instantiator = new \Doctrine\Instantiator\Instantiator(); -$instance = $instantiator->instantiate(My\ClassName\Here::class); +$instance = $instantiator->instantiate(\My\ClassName\Here::class); ``` ## Contributing diff --git a/composer.json b/composer.json index 4823890..a8aaa65 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ } ], "require": { - "php": ">=5.3,<8.0-DEV" + "php": "^7.1" }, "require-dev": { "ext-phar": "*", "ext-pdo": "*", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0", + "phpunit/phpunit": "^6.2.3", + "squizlabs/php_codesniffer": "^3.0.2", "athletic/athletic": "~0.1.8" }, "autoload": { @@ -39,7 +39,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.1.x-dev" } } } diff --git a/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php b/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php index ea8d28c..cb57aa8 100644 --- a/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php +++ b/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php @@ -29,12 +29,7 @@ */ class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface { - /** - * @param string $className - * - * @return self - */ - public static function fromNonExistingClass($className) + public static function fromNonExistingClass(string $className) : self { if (interface_exists($className)) { return new self(sprintf('The provided type "%s" is an interface, and can not be instantiated', $className)); @@ -47,12 +42,7 @@ public static function fromNonExistingClass($className) return new self(sprintf('The provided class "%s" does not exist', $className)); } - /** - * @param ReflectionClass $reflectionClass - * - * @return self - */ - public static function fromAbstractClass(ReflectionClass $reflectionClass) + public static function fromAbstractClass(ReflectionClass $reflectionClass) : self { return new self(sprintf( 'The provided class "%s" is abstract, and can not be instantiated', diff --git a/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php b/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php index 1681e56..2b704b9 100644 --- a/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php +++ b/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php @@ -30,14 +30,10 @@ */ class UnexpectedValueException extends BaseUnexpectedValueException implements ExceptionInterface { - /** - * @param ReflectionClass $reflectionClass - * @param Exception $exception - * - * @return self - */ - public static function fromSerializationTriggeredException(ReflectionClass $reflectionClass, Exception $exception) - { + public static function fromSerializationTriggeredException( + ReflectionClass $reflectionClass, + Exception $exception + ) : self { return new self( sprintf( 'An exception was raised while trying to instantiate an instance of "%s" via un-serialization', @@ -48,22 +44,13 @@ public static function fromSerializationTriggeredException(ReflectionClass $refl ); } - /** - * @param ReflectionClass $reflectionClass - * @param string $errorString - * @param int $errorCode - * @param string $errorFile - * @param int $errorLine - * - * @return UnexpectedValueException - */ public static function fromUncleanUnSerialization( ReflectionClass $reflectionClass, - $errorString, - $errorCode, - $errorFile, - $errorLine - ) { + string $errorString, + int $errorCode, + string $errorFile, + int $errorLine + ) : self { return new self( sprintf( 'Could not produce an instance of "%s" via un-serialization, since an error was triggered ' diff --git a/src/Doctrine/Instantiator/Instantiator.php b/src/Doctrine/Instantiator/Instantiator.php index 6d5b3b6..69fe65d 100644 --- a/src/Doctrine/Instantiator/Instantiator.php +++ b/src/Doctrine/Instantiator/Instantiator.php @@ -19,7 +19,6 @@ namespace Doctrine\Instantiator; -use Closure; use Doctrine\Instantiator\Exception\InvalidArgumentException; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Exception; @@ -41,14 +40,14 @@ final class Instantiator implements InstantiatorInterface const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O'; /** - * @var \Closure[] of {@see \Closure} instances used to instantiate specific classes + * @var \callable[] used to instantiate specific classes, indexed by class name */ - private static $cachedInstantiators = array(); + private static $cachedInstantiators = []; /** - * @var object[] of objects that can directly be cloned + * @var object[] of objects that can directly be cloned, indexed by class name */ - private static $cachedCloneables = array(); + private static $cachedCloneables = []; /** * {@inheritDoc} @@ -71,11 +70,9 @@ public function instantiate($className) /** * Builds the requested object and caches it in static properties for performance * - * @param string $className - * * @return object */ - private function buildAndCacheFromFactory($className) + private function buildAndCacheFromFactory(string $className) { $factory = self::$cachedInstantiators[$className] = $this->buildFactory($className); $instance = $factory(); @@ -88,26 +85,24 @@ private function buildAndCacheFromFactory($className) } /** - * Builds a {@see \Closure} capable of instantiating the given $className without + * Builds a callable capable of instantiating the given $className without * invoking its constructor. * - * @param string $className - * - * @return Closure + * @throws InvalidArgumentException + * @throws UnexpectedValueException + * @throws \ReflectionException */ - private function buildFactory($className) + private function buildFactory(string $className) : callable { $reflectionClass = $this->getReflectionClass($className); if ($this->isInstantiableViaReflection($reflectionClass)) { - return function () use ($reflectionClass) { - return $reflectionClass->newInstanceWithoutConstructor(); - }; + return [$reflectionClass, 'newInstanceWithoutConstructor']; } $serializedString = sprintf( '%s:%d:"%s":0:{}', - $this->getSerializationFormat($reflectionClass), + self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER, strlen($className), $className ); @@ -125,8 +120,9 @@ private function buildFactory($className) * @return ReflectionClass * * @throws InvalidArgumentException + * @throws \ReflectionException */ - private function getReflectionClass($className) + private function getReflectionClass($className) : ReflectionClass { if (! class_exists($className)) { throw InvalidArgumentException::fromNonExistingClass($className); @@ -149,9 +145,9 @@ private function getReflectionClass($className) * * @return void */ - private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString) + private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString) : void { - set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) { + set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) : void { $error = UnexpectedValueException::fromUncleanUnSerialization( $reflectionClass, $message, @@ -178,7 +174,7 @@ private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionCl * * @return void */ - private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) + private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) : void { try { unserialize($serializedString); @@ -189,28 +185,15 @@ private function attemptInstantiationViaUnSerialization(ReflectionClass $reflect } } - /** - * @param ReflectionClass $reflectionClass - * - * @return bool - */ - private function isInstantiableViaReflection(ReflectionClass $reflectionClass) + private function isInstantiableViaReflection(ReflectionClass $reflectionClass) : bool { - if (\PHP_VERSION_ID >= 50600) { - return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal()); - } - - return \PHP_VERSION_ID >= 50400 && ! $this->hasInternalAncestors($reflectionClass); + return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal()); } /** * Verifies whether the given class is to be considered internal - * - * @param ReflectionClass $reflectionClass - * - * @return bool */ - private function hasInternalAncestors(ReflectionClass $reflectionClass) + private function hasInternalAncestors(ReflectionClass $reflectionClass) : bool { do { if ($reflectionClass->isInternal()) { @@ -221,53 +204,13 @@ private function hasInternalAncestors(ReflectionClass $reflectionClass) return false; } - /** - * Verifies if the given PHP version implements the `Serializable` interface serialization - * with an incompatible serialization format. If that's the case, use serialization marker - * "C" instead of "O". - * - * @link http://news.php.net/php.internals/74654 - * - * @param ReflectionClass $reflectionClass - * - * @return string the serialization format marker, either self::SERIALIZATION_FORMAT_USE_UNSERIALIZER - * or self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER - */ - private function getSerializationFormat(ReflectionClass $reflectionClass) - { - if ($this->isPhpVersionWithBrokenSerializationFormat() - && $reflectionClass->implementsInterface('Serializable') - ) { - return self::SERIALIZATION_FORMAT_USE_UNSERIALIZER; - } - - return self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER; - } - - /** - * Checks whether the current PHP runtime uses an incompatible serialization format - * - * @return bool - */ - private function isPhpVersionWithBrokenSerializationFormat() - { - return PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513; - } - /** * Checks if a class is cloneable * - * @param ReflectionClass $reflection - * - * @return bool + * Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects. */ - private function isSafeToClone(ReflectionClass $reflection) + private function isSafeToClone(ReflectionClass $reflection) : bool { - if (method_exists($reflection, 'isCloneable') && ! $reflection->isCloneable()) { - return false; - } - - // not cloneable if it implements `__clone`, as we want to avoid calling it - return ! $reflection->hasMethod('__clone'); + return $reflection->isCloneable() && ! $reflection->hasMethod('__clone'); } } diff --git a/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceEvent.php b/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceEvent.php index 3e8fc6f..f97e2da 100644 --- a/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceEvent.php +++ b/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceEvent.php @@ -37,8 +37,10 @@ class InstantiatorPerformanceEvent extends AthleticEvent /** * {@inheritDoc} */ - protected function setUp() + protected function setUp() : void { + parent::setUp(); + $this->instantiator = new Instantiator(); $this->instantiator->instantiate(__CLASS__); @@ -53,7 +55,7 @@ protected function setUp() * @baseline * @group instantiation */ - public function testInstantiateSelf() + public function testInstantiateSelf() : void { $this->instantiator->instantiate(__CLASS__); } @@ -62,7 +64,7 @@ public function testInstantiateSelf() * @iterations 20000 * @group instantiation */ - public function testInstantiateInternalClass() + public function testInstantiateInternalClass() : void { $this->instantiator->instantiate('ArrayObject'); } @@ -71,7 +73,7 @@ public function testInstantiateInternalClass() * @iterations 20000 * @group instantiation */ - public function testInstantiateSimpleSerializableAssetClass() + public function testInstantiateSimpleSerializableAssetClass() : void { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'); } @@ -80,7 +82,7 @@ public function testInstantiateSimpleSerializableAssetClass() * @iterations 20000 * @group instantiation */ - public function testInstantiateSerializableArrayObjectAsset() + public function testInstantiateSerializableArrayObjectAsset() : void { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'); } @@ -89,7 +91,7 @@ public function testInstantiateSerializableArrayObjectAsset() * @iterations 20000 * @group instantiation */ - public function testInstantiateUnCloneableAsset() + public function testInstantiateUnCloneableAsset() : void { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'); } diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php index aaee6da..e1a8542 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php @@ -20,7 +20,10 @@ namespace DoctrineTest\InstantiatorTest\Exception; use Doctrine\Instantiator\Exception\InvalidArgumentException; -use PHPUnit_Framework_TestCase; +use Doctrine\Instantiator\InstantiatorInterface; +use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset; +use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset; +use PHPUnit\Framework\TestCase; use ReflectionClass; /** @@ -30,53 +33,50 @@ * * @covers \Doctrine\Instantiator\Exception\InvalidArgumentException */ -class InvalidArgumentExceptionTest extends PHPUnit_Framework_TestCase +class InvalidArgumentExceptionTest extends TestCase { - public function testFromNonExistingTypeWithNonExistingClass() + public function testFromNonExistingTypeWithNonExistingClass() : void { $className = __CLASS__ . str_replace('.', '', uniqid('', true)); $exception = InvalidArgumentException::fromNonExistingClass($className); - $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\InvalidArgumentException', $exception); + $this->assertInstanceOf(InvalidArgumentException::class, $exception); $this->assertSame('The provided class "' . $className . '" does not exist', $exception->getMessage()); } - public function testFromNonExistingTypeWithTrait() + public function testFromNonExistingTypeWithTrait() : void { - if (PHP_VERSION_ID < 50400) { - $this->markTestSkipped('Need at least PHP 5.4.0, as this test requires traits support to run'); - } - - $exception = InvalidArgumentException::fromNonExistingClass( - 'DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset' - ); + $exception = InvalidArgumentException::fromNonExistingClass(SimpleTraitAsset::class); $this->assertSame( - 'The provided type "DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset" is a trait, ' - . 'and can not be instantiated', + sprintf('The provided type "%s" is a trait, and can not be instantiated', SimpleTraitAsset::class), $exception->getMessage() ); } - public function testFromNonExistingTypeWithInterface() + public function testFromNonExistingTypeWithInterface() : void { - $exception = InvalidArgumentException::fromNonExistingClass('Doctrine\\Instantiator\\InstantiatorInterface'); + $exception = InvalidArgumentException::fromNonExistingClass(InstantiatorInterface::class); $this->assertSame( - 'The provided type "Doctrine\\Instantiator\\InstantiatorInterface" is an interface, ' - . 'and can not be instantiated', + sprintf( + 'The provided type "%s" is an interface, and can not be instantiated', + InstantiatorInterface::class + ), $exception->getMessage() ); } - public function testFromAbstractClass() + public function testFromAbstractClass() : void { - $reflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'); + $reflection = new ReflectionClass(AbstractClassAsset::class); $exception = InvalidArgumentException::fromAbstractClass($reflection); $this->assertSame( - 'The provided class "DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset" is abstract, ' - . 'and can not be instantiated', + sprintf( + 'The provided class "%s" is abstract, and can not be instantiated', + AbstractClassAsset::class + ), $exception->getMessage() ); } diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php index 84154e7..f1902b4 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php @@ -20,8 +20,9 @@ namespace DoctrineTest\InstantiatorTest\Exception; use Doctrine\Instantiator\Exception\UnexpectedValueException; +use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset; use Exception; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; use ReflectionClass; /** @@ -31,15 +32,15 @@ * * @covers \Doctrine\Instantiator\Exception\UnexpectedValueException */ -class UnexpectedValueExceptionTest extends PHPUnit_Framework_TestCase +class UnexpectedValueExceptionTest extends TestCase { - public function testFromSerializationTriggeredException() + public function testFromSerializationTriggeredException() : void { $reflectionClass = new ReflectionClass($this); $previous = new Exception(); $exception = UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $previous); - $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\UnexpectedValueException', $exception); + $this->assertInstanceOf(UnexpectedValueException::class, $exception); $this->assertSame($previous, $exception->getPrevious()); $this->assertSame( 'An exception was raised while trying to instantiate an instance of "' @@ -48,15 +49,18 @@ public function testFromSerializationTriggeredException() ); } - public function testFromUncleanUnSerialization() + public function testFromUncleanUnSerialization() : void { - $reflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'); + $reflection = new ReflectionClass(AbstractClassAsset::class); $exception = UnexpectedValueException::fromUncleanUnSerialization($reflection, 'foo', 123, 'bar', 456); - $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\UnexpectedValueException', $exception); + $this->assertInstanceOf(UnexpectedValueException::class, $exception); $this->assertSame( - 'Could not produce an instance of "DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset" ' - . 'via un-serialization, since an error was triggered in file "bar" at line "456"', + sprintf( + 'Could not produce an instance of "%s" ' + . 'via un-serialization, since an error was triggered in file "bar" at line "456"', + AbstractClassAsset::class + ), $exception->getMessage() ); diff --git a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php index 48d4ee2..81baf09 100644 --- a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php +++ b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php @@ -19,10 +19,28 @@ namespace DoctrineTest\InstantiatorTest; +use ArrayObject; +use Doctrine\Instantiator\Exception\InvalidArgumentException; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Doctrine\Instantiator\Instantiator; -use PHPUnit_Framework_TestCase; -use ReflectionClass; +use Doctrine\Instantiator\InstantiatorInterface; +use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset; +use DoctrineTest\InstantiatorTestAsset\ArrayObjectAsset; +use DoctrineTest\InstantiatorTestAsset\ExceptionAsset; +use DoctrineTest\InstantiatorTestAsset\FinalExceptionAsset; +use DoctrineTest\InstantiatorTestAsset\PharExceptionAsset; +use DoctrineTest\InstantiatorTestAsset\SerializableArrayObjectAsset; +use DoctrineTest\InstantiatorTestAsset\SimpleSerializableAsset; +use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset; +use DoctrineTest\InstantiatorTestAsset\UnCloneableAsset; +use DoctrineTest\InstantiatorTestAsset\UnserializeExceptionArrayObjectAsset; +use DoctrineTest\InstantiatorTestAsset\WakeUpNoticesAsset; +use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset; +use Exception; +use PDORow; +use PharException; +use PHPUnit\Framework\TestCase; +use stdClass; /** * Tests for {@see \Doctrine\Instantiator\Instantiator} @@ -31,7 +49,7 @@ * * @covers \Doctrine\Instantiator\Instantiator */ -class InstantiatorTest extends PHPUnit_Framework_TestCase +class InstantiatorTest extends TestCase { /** * @var Instantiator @@ -41,27 +59,25 @@ class InstantiatorTest extends PHPUnit_Framework_TestCase /** * {@inheritDoc} */ - protected function setUp() + protected function setUp() : void { + parent::setUp(); + $this->instantiator = new Instantiator(); } /** - * @param string $className - * * @dataProvider getInstantiableClasses */ - public function testCanInstantiate($className) + public function testCanInstantiate(string $className) : void { $this->assertInstanceOf($className, $this->instantiator->instantiate($className)); } /** - * @param string $className - * * @dataProvider getInstantiableClasses */ - public function testInstantiatesSeparateInstances($className) + public function testInstantiatesSeparateInstances(string $className) : void { $instance1 = $this->instantiator->instantiate($className); $instance2 = $this->instantiator->instantiate($className); @@ -70,7 +86,7 @@ public function testInstantiatesSeparateInstances($className) $this->assertNotSame($instance1, $instance2); } - public function testExceptionOnUnSerializationException() + public function testExceptionOnUnSerializationException() : void { if (defined('HHVM_VERSION')) { $this->markTestSkipped( @@ -79,67 +95,22 @@ public function testExceptionOnUnSerializationException() ); } - $className = 'DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset'; - - if (\PHP_VERSION_ID >= 50600) { - $className = 'PDORow'; - } - - if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) { - $className = 'DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'; - } - - $this->setExpectedException('Doctrine\\Instantiator\\Exception\\UnexpectedValueException'); + $this->expectException(UnexpectedValueException::class); - $this->instantiator->instantiate($className); - } - - public function testNoticeOnUnSerializationException() - { - if (\PHP_VERSION_ID >= 50600) { - $this->markTestSkipped( - 'PHP 5.6 supports `ReflectionClass#newInstanceWithoutConstructor()` for some internal classes' - ); - } - - try { - $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset'); - - $this->fail('No exception was raised'); - } catch (UnexpectedValueException $exception) { - $wakeUpNoticesReflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset'); - $previous = $exception->getPrevious(); - - $this->assertInstanceOf('Exception', $previous); - - // in PHP 5.4.29 and PHP 5.5.13, this case is not a notice, but an exception being thrown - if (! (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513)) { - $this->assertSame( - 'Could not produce an instance of "DoctrineTest\\InstantiatorTestAsset\WakeUpNoticesAsset" ' - . 'via un-serialization, since an error was triggered in file "' - . $wakeUpNoticesReflection->getFileName() . '" at line "36"', - $exception->getMessage() - ); - - $this->assertSame('Something went bananas while un-serializing this instance', $previous->getMessage()); - $this->assertSame(\E_USER_NOTICE, $previous->getCode()); - } - } + $this->instantiator->instantiate(PDORow::class); } /** - * @param string $invalidClassName - * * @dataProvider getInvalidClassNames */ - public function testInstantiationFromNonExistingClass($invalidClassName) + public function testInstantiationFromNonExistingClass(string $invalidClassName) : void { - $this->setExpectedException('Doctrine\\Instantiator\\Exception\\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $this->instantiator->instantiate($invalidClassName); } - public function testInstancesAreNotCloned() + public function testInstancesAreNotCloned() : void { $className = 'TemporaryClass' . str_replace('.', '', uniqid('', true)); @@ -159,42 +130,27 @@ public function testInstancesAreNotCloned() * * @return string[][] */ - public function getInstantiableClasses() + public function getInstantiableClasses() : array { - $classes = array( - array('stdClass'), - array(__CLASS__), - array('Doctrine\\Instantiator\\Instantiator'), - array('Exception'), - array('PharException'), - array('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\ExceptionAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\FinalExceptionAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\PharExceptionAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\XMLReaderAsset'), - ); - - if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) { - return $classes; - } - - $classes = array_merge( - $classes, - array( - array('PharException'), - array('ArrayObject'), - array('DoctrineTest\\InstantiatorTestAsset\\ArrayObjectAsset'), - array('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'), - ) - ); - - if (\PHP_VERSION_ID >= 50600) { - $classes[] = array('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset'); - $classes[] = array('DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset'); - } - - return $classes; + return [ + [stdClass::class], + [__CLASS__], + [Instantiator::class], + [Exception::class], + [PharException::class], + [SimpleSerializableAsset::class], + [ExceptionAsset::class], + [FinalExceptionAsset::class], + [PharExceptionAsset::class], + [UnCloneableAsset::class], + [XMLReaderAsset::class], + [PharException::class], + [ArrayObject::class], + [ArrayObjectAsset::class], + [SerializableArrayObjectAsset::class], + [WakeUpNoticesAsset::class], + [UnserializeExceptionArrayObjectAsset::class], + ]; } /** @@ -202,18 +158,13 @@ public function getInstantiableClasses() * * @return string[][] */ - public function getInvalidClassNames() + public function getInvalidClassNames() : array { - $classNames = array( - array(__CLASS__ . str_replace('.', '', uniqid('', true))), - array('Doctrine\\Instantiator\\InstantiatorInterface'), - array('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'), - ); - - if (\PHP_VERSION_ID >= 50400) { - $classNames[] = array('DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset'); - } - - return $classNames; + return [ + [__CLASS__ . str_replace('.', '', uniqid('', true))], + [InstantiatorInterface::class], + [AbstractClassAsset::class], + [SimpleTraitAsset::class] + ]; } }