pax_global_header00006660000000000000000000000064125373675150014530gustar00rootroot0000000000000052 comment=8e884e78f9f0eb1329e445619e04456e64d8051d instantiator-1.0.5/000077500000000000000000000000001253736751500142525ustar00rootroot00000000000000instantiator-1.0.5/.gitignore000066400000000000000000000000671253736751500162450ustar00rootroot00000000000000phpunit.xml composer.lock build vendor coverage.clover instantiator-1.0.5/.scrutinizer.yml000066400000000000000000000022211253736751500174310ustar00rootroot00000000000000before_commands: - "composer install --prefer-source" tools: external_code_coverage: timeout: 600 php_code_coverage: enabled: true test_command: ./vendor/bin/phpunit php_code_sniffer: enabled: true config: standard: PSR2 filter: paths: ["src/*", "tests/*"] php_cpd: enabled: true excluded_dirs: ["build/*", "tests", "vendor"] php_cs_fixer: enabled: true config: level: all filter: paths: ["src/*", "tests/*"] php_loc: enabled: true excluded_dirs: ["build", "tests", "vendor"] php_mess_detector: enabled: true config: ruleset: phpmd.xml.dist design_rules: { eval_expression: false } filter: paths: ["src/*"] php_pdepend: enabled: true excluded_dirs: ["build", "tests", "vendor"] php_analyzer: enabled: true filter: paths: ["src/*", "tests/*"] php_hhvm: enabled: true filter: paths: ["src/*", "tests/*"] sensiolabs_security_checker: true instantiator-1.0.5/.travis.install.sh000077500000000000000000000010421253736751500176410ustar00rootroot00000000000000#!/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 elif [ "$TRAVIS_PHP_VERSION" = '5.3.3' ] ; then composer self-update composer update --prefer-source --no-dev composer dump-autoload else composer self-update composer update --prefer-source fi instantiator-1.0.5/.travis.yml000066400000000000000000000016011253736751500163610ustar00rootroot00000000000000language: php php: - 5.3.3 - 5.3 - 5.4 - 5.5 - 5.6 - hhvm before_script: - ./.travis.install.sh - if [ $TRAVIS_PHP_VERSION = '5.6' ]; then PHPUNIT_FLAGS="--coverage-clover coverage.clover"; else PHPUNIT_FLAGS=""; fi script: - if [ $TRAVIS_PHP_VERSION = '5.3.3' ]; then phpunit; fi - if [ $TRAVIS_PHP_VERSION != '5.3.3' ]; then ./vendor/bin/phpunit $PHPUNIT_FLAGS; fi - if [ $TRAVIS_PHP_VERSION != '5.3.3' ]; then ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests/; fi - if [[ $TRAVIS_PHP_VERSION != '5.3.3' && $TRAVIS_PHP_VERSION != '5.4.29' && $TRAVIS_PHP_VERSION != '5.5.13' ]]; then php -n ./vendor/bin/athletic -p ./tests/DoctrineTest/InstantiatorPerformance/ -f GroupedFormatter; fi 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 instantiator-1.0.5/CONTRIBUTING.md000066400000000000000000000020731253736751500165050ustar00rootroot00000000000000# Contributing * Coding standard for the project is [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) * The project will follow strict [object calisthenics](http://www.slideshare.net/guilhermeblanco/object-calisthenics-applied-to-php) * Any contribution must provide tests for additional introduced conditions * Any un-confirmed issue needs a failing test case before being accepted * Pull requests must be sent from a new hotfix/feature branch, not from `master`. ## Installation To install the project and run the tests, you need to clone it first: ```sh $ git clone git://github.com/doctrine/instantiator.git ``` You will then need to run a composer installation: ```sh $ cd Instantiator $ curl -s https://getcomposer.org/installer | php $ php composer.phar update ``` ## Testing The PHPUnit version to be used is the one installed as a dev- dependency via composer: ```sh $ ./vendor/bin/phpunit ``` Accepted coverage for new contributions is 80%. Any contribution not satisfying this requirement won't be merged. instantiator-1.0.5/LICENSE000066400000000000000000000020441253736751500152570ustar00rootroot00000000000000Copyright (c) 2014 Doctrine Project Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. instantiator-1.0.5/README.md000066400000000000000000000035671253736751500155440ustar00rootroot00000000000000# Instantiator This library provides a way of avoiding usage of constructors when instantiating PHP classes. [![Build Status](https://travis-ci.org/doctrine/instantiator.svg?branch=master)](https://travis-ci.org/doctrine/instantiator) [![Code Coverage](https://scrutinizer-ci.com/g/doctrine/instantiator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/doctrine/instantiator/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/doctrine/instantiator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/doctrine/instantiator/?branch=master) [![Dependency Status](https://www.versioneye.com/package/php--doctrine--instantiator/badge.svg)](https://www.versioneye.com/package/php--doctrine--instantiator) [![HHVM Status](http://hhvm.h4cc.de/badge/doctrine/instantiator.png)](http://hhvm.h4cc.de/package/doctrine/instantiator) [![Latest Stable Version](https://poser.pugx.org/doctrine/instantiator/v/stable.png)](https://packagist.org/packages/doctrine/instantiator) [![Latest Unstable Version](https://poser.pugx.org/doctrine/instantiator/v/unstable.png)](https://packagist.org/packages/doctrine/instantiator) ## Installation The suggested installation method is via [composer](https://getcomposer.org/): ```sh php composer.phar require "doctrine/instantiator:~1.0.3" ``` ## Usage The instantiator is able to create new instances of any class without using the constructor or any API of the class itself: ```php $instantiator = new \Doctrine\Instantiator\Instantiator(); $instance = $instantiator->instantiate('My\\ClassName\\Here'); ``` ## Contributing Please read the [CONTRIBUTING.md](CONTRIBUTING.md) contents if you wish to help out! ## Credits This library was migrated from [ocramius/instantiator](https://github.com/Ocramius/Instantiator), which has been donated to the doctrine organization, and which is now deprecated in favour of this package. instantiator-1.0.5/composer.json000066400000000000000000000024741253736751500170030ustar00rootroot00000000000000{ "name": "doctrine/instantiator", "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", "type": "library", "license": "MIT", "homepage": "https://github.com/doctrine/instantiator", "keywords": [ "instantiate", "constructor" ], "authors": [ { "name": "Marco Pivetta", "email": "ocramius@gmail.com", "homepage": "http://ocramius.github.com/" } ], "require": { "php": ">=5.3,<8.0-DEV" }, "require-dev": { "ext-phar": "*", "ext-pdo": "*", "phpunit/phpunit": "~4.0", "squizlabs/php_codesniffer": "~2.0", "athletic/athletic": "~0.1.8" }, "autoload": { "psr-4": { "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" } }, "autoload-dev": { "psr-0": { "DoctrineTest\\InstantiatorPerformance\\": "tests", "DoctrineTest\\InstantiatorTest\\": "tests", "DoctrineTest\\InstantiatorTestAsset\\": "tests" } }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } } } instantiator-1.0.5/phpmd.xml.dist000066400000000000000000000020751253736751500170520ustar00rootroot00000000000000 instantiator-1.0.5/phpunit.xml.dist000066400000000000000000000011731253736751500174270ustar00rootroot00000000000000 ./tests/DoctrineTest/InstantiatorTest ./src instantiator-1.0.5/src/000077500000000000000000000000001253736751500150415ustar00rootroot00000000000000instantiator-1.0.5/src/Doctrine/000077500000000000000000000000001253736751500166105ustar00rootroot00000000000000instantiator-1.0.5/src/Doctrine/Instantiator/000077500000000000000000000000001253736751500212675ustar00rootroot00000000000000instantiator-1.0.5/src/Doctrine/Instantiator/Exception/000077500000000000000000000000001253736751500232255ustar00rootroot00000000000000instantiator-1.0.5/src/Doctrine/Instantiator/Exception/ExceptionInterface.php000066400000000000000000000022451253736751500275200ustar00rootroot00000000000000. */ namespace Doctrine\Instantiator\Exception; /** * Base exception marker interface for the instantiator component * * @author Marco Pivetta */ interface ExceptionInterface { } instantiator-1.0.5/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php000066400000000000000000000043641253736751500307150ustar00rootroot00000000000000. */ namespace Doctrine\Instantiator\Exception; use InvalidArgumentException as BaseInvalidArgumentException; use ReflectionClass; /** * Exception for invalid arguments provided to the instantiator * * @author Marco Pivetta */ class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface { /** * @param string $className * * @return self */ public static function fromNonExistingClass($className) { if (interface_exists($className)) { return new self(sprintf('The provided type "%s" is an interface, and can not be instantiated', $className)); } if (PHP_VERSION_ID >= 50400 && trait_exists($className)) { return new self(sprintf('The provided type "%s" is a trait, and can not be instantiated', $className)); } return new self(sprintf('The provided class "%s" does not exist', $className)); } /** * @param ReflectionClass $reflectionClass * * @return self */ public static function fromAbstractClass(ReflectionClass $reflectionClass) { return new self(sprintf( 'The provided class "%s" is abstract, and can not be instantiated', $reflectionClass->getName() )); } } instantiator-1.0.5/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php000066400000000000000000000053161253736751500307230ustar00rootroot00000000000000. */ namespace Doctrine\Instantiator\Exception; use Exception; use ReflectionClass; use UnexpectedValueException as BaseUnexpectedValueException; /** * Exception for given parameters causing invalid/unexpected state on instantiation * * @author Marco Pivetta */ class UnexpectedValueException extends BaseUnexpectedValueException implements ExceptionInterface { /** * @param ReflectionClass $reflectionClass * @param Exception $exception * * @return self */ public static function fromSerializationTriggeredException(ReflectionClass $reflectionClass, Exception $exception) { return new self( sprintf( 'An exception was raised while trying to instantiate an instance of "%s" via un-serialization', $reflectionClass->getName() ), 0, $exception ); } /** * @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 ) { return new self( sprintf( 'Could not produce an instance of "%s" via un-serialization, since an error was triggered ' . 'in file "%s" at line "%d"', $reflectionClass->getName(), $errorFile, $errorLine ), 0, new Exception($errorString, $errorCode) ); } } instantiator-1.0.5/src/Doctrine/Instantiator/Instantiator.php000066400000000000000000000203031253736751500244550ustar00rootroot00000000000000. */ namespace Doctrine\Instantiator; use Closure; use Doctrine\Instantiator\Exception\InvalidArgumentException; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Exception; use ReflectionClass; /** * {@inheritDoc} * * @author Marco Pivetta */ final class Instantiator implements InstantiatorInterface { /** * Markers used internally by PHP to define whether {@see \unserialize} should invoke * the method {@see \Serializable::unserialize()} when dealing with classes implementing * the {@see \Serializable} interface. */ const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C'; const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O'; /** * @var \Closure[] of {@see \Closure} instances used to instantiate specific classes */ private static $cachedInstantiators = array(); /** * @var object[] of objects that can directly be cloned */ private static $cachedCloneables = array(); /** * {@inheritDoc} */ public function instantiate($className) { if (isset(self::$cachedCloneables[$className])) { return clone self::$cachedCloneables[$className]; } if (isset(self::$cachedInstantiators[$className])) { $factory = self::$cachedInstantiators[$className]; return $factory(); } return $this->buildAndCacheFromFactory($className); } /** * Builds the requested object and caches it in static properties for performance * * @param string $className * * @return object */ private function buildAndCacheFromFactory($className) { $factory = self::$cachedInstantiators[$className] = $this->buildFactory($className); $instance = $factory(); if ($this->isSafeToClone(new ReflectionClass($instance))) { self::$cachedCloneables[$className] = clone $instance; } return $instance; } /** * Builds a {@see \Closure} capable of instantiating the given $className without * invoking its constructor. * * @param string $className * * @return Closure */ private function buildFactory($className) { $reflectionClass = $this->getReflectionClass($className); if ($this->isInstantiableViaReflection($reflectionClass)) { return function () use ($reflectionClass) { return $reflectionClass->newInstanceWithoutConstructor(); }; } $serializedString = sprintf( '%s:%d:"%s":0:{}', $this->getSerializationFormat($reflectionClass), strlen($className), $className ); $this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString); return function () use ($serializedString) { return unserialize($serializedString); }; } /** * @param string $className * * @return ReflectionClass * * @throws InvalidArgumentException */ private function getReflectionClass($className) { if (! class_exists($className)) { throw InvalidArgumentException::fromNonExistingClass($className); } $reflection = new ReflectionClass($className); if ($reflection->isAbstract()) { throw InvalidArgumentException::fromAbstractClass($reflection); } return $reflection; } /** * @param ReflectionClass $reflectionClass * @param string $serializedString * * @throws UnexpectedValueException * * @return void */ private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString) { set_error_handler(function ($code, $message, $file, $line) use ($reflectionClass, & $error) { $error = UnexpectedValueException::fromUncleanUnSerialization( $reflectionClass, $message, $code, $file, $line ); }); $this->attemptInstantiationViaUnSerialization($reflectionClass, $serializedString); restore_error_handler(); if ($error) { throw $error; } } /** * @param ReflectionClass $reflectionClass * @param string $serializedString * * @throws UnexpectedValueException * * @return void */ private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) { try { unserialize($serializedString); } catch (Exception $exception) { restore_error_handler(); throw UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $exception); } } /** * @param ReflectionClass $reflectionClass * * @return bool */ private function isInstantiableViaReflection(ReflectionClass $reflectionClass) { if (\PHP_VERSION_ID >= 50600) { return ! ($this->hasInternalAncestors($reflectionClass) && $reflectionClass->isFinal()); } return \PHP_VERSION_ID >= 50400 && ! $this->hasInternalAncestors($reflectionClass); } /** * Verifies whether the given class is to be considered internal * * @param ReflectionClass $reflectionClass * * @return bool */ private function hasInternalAncestors(ReflectionClass $reflectionClass) { do { if ($reflectionClass->isInternal()) { return true; } } while ($reflectionClass = $reflectionClass->getParentClass()); 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 */ private function isSafeToClone(ReflectionClass $reflection) { 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'); } } instantiator-1.0.5/src/Doctrine/Instantiator/InstantiatorInterface.php000066400000000000000000000025761253736751500263120ustar00rootroot00000000000000. */ namespace Doctrine\Instantiator; /** * Instantiator provides utility methods to build objects without invoking their constructors * * @author Marco Pivetta */ interface InstantiatorInterface { /** * @param string $className * * @return object * * @throws \Doctrine\Instantiator\Exception\ExceptionInterface */ public function instantiate($className); } instantiator-1.0.5/tests/000077500000000000000000000000001253736751500154145ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/000077500000000000000000000000001253736751500200235ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/InstantiatorPerformance/000077500000000000000000000000001253736751500246645ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/InstantiatorPerformance/InstantiatorPerformanceEvent.php000066400000000000000000000060021253736751500332360ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorPerformance; use Athletic\AthleticEvent; use Doctrine\Instantiator\Instantiator; /** * Performance tests for {@see \Doctrine\Instantiator\Instantiator} * * @author Marco Pivetta */ class InstantiatorPerformanceEvent extends AthleticEvent { /** * @var \Doctrine\Instantiator\Instantiator */ private $instantiator; /** * {@inheritDoc} */ protected function setUp() { $this->instantiator = new Instantiator(); $this->instantiator->instantiate(__CLASS__); $this->instantiator->instantiate('ArrayObject'); $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'); $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'); $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'); } /** * @iterations 20000 * @baseline * @group instantiation */ public function testInstantiateSelf() { $this->instantiator->instantiate(__CLASS__); } /** * @iterations 20000 * @group instantiation */ public function testInstantiateInternalClass() { $this->instantiator->instantiate('ArrayObject'); } /** * @iterations 20000 * @group instantiation */ public function testInstantiateSimpleSerializableAssetClass() { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'); } /** * @iterations 20000 * @group instantiation */ public function testInstantiateSerializableArrayObjectAsset() { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'); } /** * @iterations 20000 * @group instantiation */ public function testInstantiateUnCloneableAsset() { $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTest/000077500000000000000000000000001253736751500233425ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/InstantiatorTest/Exception/000077500000000000000000000000001253736751500253005ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php000066400000000000000000000064011253736751500336220ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTest\Exception; use Doctrine\Instantiator\Exception\InvalidArgumentException; use PHPUnit_Framework_TestCase; use ReflectionClass; /** * Tests for {@see \Doctrine\Instantiator\Exception\InvalidArgumentException} * * @author Marco Pivetta * * @covers \Doctrine\Instantiator\Exception\InvalidArgumentException */ class InvalidArgumentExceptionTest extends PHPUnit_Framework_TestCase { public function testFromNonExistingTypeWithNonExistingClass() { $className = __CLASS__ . uniqid(); $exception = InvalidArgumentException::fromNonExistingClass($className); $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\InvalidArgumentException', $exception); $this->assertSame('The provided class "' . $className . '" does not exist', $exception->getMessage()); } public function testFromNonExistingTypeWithTrait() { 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' ); $this->assertSame( 'The provided type "DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset" is a trait, ' . 'and can not be instantiated', $exception->getMessage() ); } public function testFromNonExistingTypeWithInterface() { $exception = InvalidArgumentException::fromNonExistingClass('Doctrine\\Instantiator\\InstantiatorInterface'); $this->assertSame( 'The provided type "Doctrine\\Instantiator\\InstantiatorInterface" is an interface, ' . 'and can not be instantiated', $exception->getMessage() ); } public function testFromAbstractClass() { $reflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'); $exception = InvalidArgumentException::fromAbstractClass($reflection); $this->assertSame( 'The provided class "DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset" is abstract, ' . 'and can not be instantiated', $exception->getMessage() ); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php000066400000000000000000000057101253736751500336340ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTest\Exception; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Exception; use PHPUnit_Framework_TestCase; use ReflectionClass; /** * Tests for {@see \Doctrine\Instantiator\Exception\UnexpectedValueException} * * @author Marco Pivetta * * @covers \Doctrine\Instantiator\Exception\UnexpectedValueException */ class UnexpectedValueExceptionTest extends PHPUnit_Framework_TestCase { public function testFromSerializationTriggeredException() { $reflectionClass = new ReflectionClass($this); $previous = new Exception(); $exception = UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $previous); $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\UnexpectedValueException', $exception); $this->assertSame($previous, $exception->getPrevious()); $this->assertSame( 'An exception was raised while trying to instantiate an instance of "' . __CLASS__ . '" via un-serialization', $exception->getMessage() ); } public function testFromUncleanUnSerialization() { $reflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'); $exception = UnexpectedValueException::fromUncleanUnSerialization($reflection, 'foo', 123, 'bar', 456); $this->assertInstanceOf('Doctrine\\Instantiator\\Exception\\UnexpectedValueException', $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"', $exception->getMessage() ); $previous = $exception->getPrevious(); $this->assertInstanceOf('Exception', $previous); $this->assertSame('foo', $previous->getMessage()); $this->assertSame(123, $previous->getCode()); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php000066400000000000000000000167341253736751500274050ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTest; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Doctrine\Instantiator\Instantiator; use PHPUnit_Framework_TestCase; use ReflectionClass; /** * Tests for {@see \Doctrine\Instantiator\Instantiator} * * @author Marco Pivetta * * @covers \Doctrine\Instantiator\Instantiator */ class InstantiatorTest extends PHPUnit_Framework_TestCase { /** * @var Instantiator */ private $instantiator; /** * {@inheritDoc} */ protected function setUp() { $this->instantiator = new Instantiator(); } /** * @param string $className * * @dataProvider getInstantiableClasses */ public function testCanInstantiate($className) { $this->assertInstanceOf($className, $this->instantiator->instantiate($className)); } /** * @param string $className * * @dataProvider getInstantiableClasses */ public function testInstantiatesSeparateInstances($className) { $instance1 = $this->instantiator->instantiate($className); $instance2 = $this->instantiator->instantiate($className); $this->assertEquals($instance1, $instance2); $this->assertNotSame($instance1, $instance2); } public function testExceptionOnUnSerializationException() { if (defined('HHVM_VERSION')) { $this->markTestSkipped( 'As of facebook/hhvm#3432, HHVM has no PDORow, and therefore ' . ' no internal final classes that cannot be instantiated' ); } $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->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()); } } } /** * @param string $invalidClassName * * @dataProvider getInvalidClassNames */ public function testInstantiationFromNonExistingClass($invalidClassName) { $this->setExpectedException('Doctrine\\Instantiator\\Exception\\InvalidArgumentException'); $this->instantiator->instantiate($invalidClassName); } public function testInstancesAreNotCloned() { $className = 'TemporaryClass' . uniqid(); eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}'); $instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); $instance->foo = 'bar'; $instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); $this->assertObjectNotHasAttribute('foo', $instance2); } /** * Provides a list of instantiable classes (existing) * * @return string[][] */ public function getInstantiableClasses() { $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; } /** * Provides a list of instantiable classes (existing) * * @return string[][] */ public function getInvalidClassNames() { $classNames = array( array(__CLASS__ . uniqid()), array('Doctrine\\Instantiator\\InstantiatorInterface'), array('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'), ); if (\PHP_VERSION_ID >= 50400) { $classNames[] = array('DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset'); } return $classNames; } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/000077500000000000000000000000001253736751500243425ustar00rootroot00000000000000instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/AbstractClassAsset.php000066400000000000000000000022231253736751500306030ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; /** * A simple asset for an abstract class * * @author Marco Pivetta */ abstract class AbstractClassAsset { } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/ArrayObjectAsset.php000066400000000000000000000026601253736751500302640ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use ArrayObject; use BadMethodCallException; /** * Test asset that extends an internal PHP class * * @author Marco Pivetta */ class ArrayObjectAsset extends ArrayObject { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/ExceptionAsset.php000066400000000000000000000026631253736751500300200ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use Exception; /** * Test asset that extends an internal PHP base exception * * @author Marco Pivetta */ class ExceptionAsset extends Exception { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/FinalExceptionAsset.php000066400000000000000000000026761253736751500307760ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use Exception; /** * Test asset that extends an internal PHP base exception * * @author Marco Pivetta */ final class FinalExceptionAsset extends Exception { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/PharAsset.php000066400000000000000000000026331253736751500267510ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use Phar; /** * Test asset that extends an internal PHP class * * @author Marco Pivetta */ class PharAsset extends Phar { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/PharExceptionAsset.php000066400000000000000000000030721253736751500306260ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use PharException; /** * Test asset that extends an internal PHP class * This class should be serializable without problems * and without getting the "Erroneous data format for unserializing" * error * * @author Marco Pivetta */ class PharExceptionAsset extends PharException { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/SerializableArrayObjectAsset.php000066400000000000000000000035201253736751500326070ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use ArrayObject; use BadMethodCallException; use Serializable; /** * Serializable test asset that also extends an internal class * * @author Marco Pivetta */ class SerializableArrayObjectAsset extends ArrayObject implements Serializable { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } /** * {@inheritDoc} */ public function serialize() { return ''; } /** * {@inheritDoc} * * Should not be called * * @throws BadMethodCallException */ public function unserialize($serialized) { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/SimpleSerializableAsset.php000066400000000000000000000034071253736751500316370ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use Serializable; /** * Base serializable test asset * * @author Marco Pivetta */ class SimpleSerializableAsset implements Serializable { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } /** * {@inheritDoc} */ public function serialize() { return ''; } /** * {@inheritDoc} * * Should not be called * * @throws BadMethodCallException */ public function unserialize($serialized) { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/SimpleTraitAsset.php000066400000000000000000000022111253736751500303040ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; /** * A simple trait with no attached logic * * @author Marco Pivetta */ trait SimpleTraitAsset { } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/UnCloneableAsset.php000066400000000000000000000031241253736751500302420ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; /** * Base un-cloneable asset * * @author Marco Pivetta */ class UnCloneableAsset { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } /** * Magic `__clone` - should not be invoked * * @throws BadMethodCallException */ public function __clone() { throw new BadMethodCallException('Not supposed to be called!'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/UnserializeExceptionArrayObjectAsset.php000066400000000000000000000025321253736751500343540ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use ArrayObject; use BadMethodCallException; /** * A simple asset for an abstract class * * @author Marco Pivetta */ class UnserializeExceptionArrayObjectAsset extends ArrayObject { /** * {@inheritDoc} */ public function __wakeup() { throw new BadMethodCallException(); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/WakeUpNoticesAsset.php000066400000000000000000000025621253736751500306010ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use ArrayObject; /** * A simple asset for an abstract class * * @author Marco Pivetta */ class WakeUpNoticesAsset extends ArrayObject { /** * Wakeup method called after un-serialization */ public function __wakeup() { trigger_error('Something went bananas while un-serializing this instance'); } } instantiator-1.0.5/tests/DoctrineTest/InstantiatorTestAsset/XMLReaderAsset.php000066400000000000000000000026441253736751500276440ustar00rootroot00000000000000. */ namespace DoctrineTest\InstantiatorTestAsset; use BadMethodCallException; use XMLReader; /** * Test asset that extends an internal PHP class * * @author Dave Marshall */ class XMLReaderAsset extends XMLReader { /** * Constructor - should not be called * * @throws BadMethodCallException */ public function __construct() { throw new BadMethodCallException('Not supposed to be called!'); } }