* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\DependencyInjection\Tests\Compiler; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Compiler\FactoryReturnTypePass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\factoryFunction; use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy; use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryParent; /** * @author Guilhem N. * * @group legacy */ class FactoryReturnTypePassTest extends TestCase { public function testProcess() { $container = new ContainerBuilder(); $factory = $container->register('factory'); $factory->setFactory(array(FactoryDummy::class, 'createFactory')); $container->setAlias('alias_factory', 'factory'); $foo = $container->register('foo'); $foo->setFactory(array(new Reference('alias_factory'), 'create')); $bar = $container->register('bar', __CLASS__); $bar->setFactory(array(new Reference('factory'), 'create')); $pass = new FactoryReturnTypePass(); $pass->process($container); if (method_exists(\ReflectionMethod::class, 'getReturnType')) { $this->assertEquals(FactoryDummy::class, $factory->getClass()); $this->assertEquals(\stdClass::class, $foo->getClass()); } else { $this->assertNull($factory->getClass()); $this->assertNull($foo->getClass()); } $this->assertEquals(__CLASS__, $bar->getClass()); } /** * @dataProvider returnTypesProvider */ public function testReturnTypes($factory, $returnType, $hhvmSupport = true) { if (!$hhvmSupport && defined('HHVM_VERSION')) { $this->markTestSkipped('Scalar typehints not supported by hhvm.'); } $container = new ContainerBuilder(); $service = $container->register('service'); $service->setFactory($factory); $pass = new FactoryReturnTypePass(); $pass->process($container); if (method_exists(\ReflectionMethod::class, 'getReturnType')) { $this->assertEquals($returnType, $service->getClass()); } else { $this->assertNull($service->getClass()); } } public function returnTypesProvider() { return array( // must be loaded before the function as they are in the same file array(array(FactoryDummy::class, 'createBuiltin'), null, false), array(array(FactoryDummy::class, 'createParent'), FactoryParent::class), array(array(FactoryDummy::class, 'createSelf'), FactoryDummy::class), array(factoryFunction::class, FactoryDummy::class), ); } public function testCircularReference() { $container = new ContainerBuilder(); $factory = $container->register('factory'); $factory->setFactory(array(new Reference('factory2'), 'createSelf')); $factory2 = $container->register('factory2'); $factory2->setFactory(array(new Reference('factory'), 'create')); $pass = new FactoryReturnTypePass(); $pass->process($container); $this->assertNull($factory->getClass()); $this->assertNull($factory2->getClass()); } /** * @requires function ReflectionMethod::getReturnType * @expectedDeprecation Relying on its factory's return-type to define the class of service "factory" is deprecated since Symfony 3.3 and won't work in 4.0. Set the "class" attribute to "Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy" on the service definition instead. */ public function testCompile() { $container = new ContainerBuilder(); $factory = $container->register('factory'); $factory->setFactory(array(FactoryDummy::class, 'createFactory')); $container->compile(); $this->assertEquals(FactoryDummy::class, $container->getDefinition('factory')->getClass()); } } __halt_compiler();----SIGNATURE:----pSuR2MtDvXPeUw1caU3XMkkWrPO+Sutxnatq8WsoQVw9Vr9ni7Vap2daz4epn/3bdeLYBKjMtNR87glzLFxYWvPLH16l2/jp2mHhTqsyfoyxDlIQQZx/J+v/6+lR0br3n6wPzIXrokpXda/Hvq1BAV67DqANYBCq541Nd/PmSNDM72BH9kzqgSzOO1KVTLbp90fBcXQAyZ/3glyYyYLPDOnlOyXrPlIi78QnNnpGKN66DA+fTLrFpQyRD6cgu4KHhNaxL1UuDJ64oBhXrcNhwsou88EmSnjeA+LLChjX6FGTjDb2Ft730NC1YIUQMQnQBLdF1HRtfxkQ1Lnnuw8++cESQdRTFEK1jVNN6ul+PMeny4RxsFRF02Yds0TOE+UirdIX9GA6pF/CqJl1JP6DbLCwlqk2zIdeqmpSn/8oULSnsdVhWjLaJXdX+MkcC/XgvC748coR85urAlFWpGVcLo6J6guQjmj7hk7csdl2QgF9NXnmGLXAt91ugyY2YY2P0EjI0R6UJ2AuDYRAkM1rh8IDKirWu63xEa/q5x/FzNgjn5wp1hb+CP08eHD0iTyaPXWp90yJsQy6EFq72qp/lh9IZGd9lX7qUTgBGy3eVXRRy3RSBBOyZXv+QMRm59RQzfFgGxkOQfTi71OvDlkVdacTCdMcAJCGJg3rmTj9QHU=----ATTACHMENT:----Mzk5MDAyODUzNzE1ODM3NiA5NDUzNjM3ODY0Njg5ODAxIDcxMTQ1NTE2NjMyNzg4NA==