|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Tests\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Compiler\FactoryReturnTypePass; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\factoryFunction; |
| 18 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy; |
| 19 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryParent; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Guilhem N. <egetick@gmail.com> |
| 23 | + */ |
| 24 | +class FactoryReturnTypePassTest extends \PHPUnit_Framework_TestCase |
| 25 | +{ |
| 26 | + public function testProcess() |
| 27 | + { |
| 28 | + $container = new ContainerBuilder(); |
| 29 | + |
| 30 | + $factory = $container->register('factory'); |
| 31 | + $factory->setFactory(array(FactoryDummy::class, 'createFactory')); |
| 32 | + |
| 33 | + $foo = $container->register('foo'); |
| 34 | + $foo->setFactory(array(new Reference('factory'), 'create')); |
| 35 | + |
| 36 | + $bar = $container->register('bar', __CLASS__); |
| 37 | + $bar->setFactory(array(new Reference('factory'), 'create')); |
| 38 | + |
| 39 | + $pass = new FactoryReturnTypePass(); |
| 40 | + $pass->process($container); |
| 41 | + |
| 42 | + if (method_exists(\ReflectionMethod::class, 'getReturnType')) { |
| 43 | + $this->assertEquals(FactoryDummy::class, $factory->getClass()); |
| 44 | + $this->assertEquals(\stdClass::class, $foo->getClass()); |
| 45 | + } else { |
| 46 | + $this->assertNull($factory->getClass()); |
| 47 | + $this->assertNull($foo->getClass()); |
| 48 | + } |
| 49 | + $this->assertEquals(__CLASS__, $bar->getClass()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider returnTypesProvider |
| 54 | + */ |
| 55 | + public function testReturnTypes($factory, $returnType, $hhvmSupport = true) |
| 56 | + { |
| 57 | + if (!$hhvmSupport && defined('HHVM_VERSION')) { |
| 58 | + $this->markTestSkipped('Scalar typehints not supported by hhvm.'); |
| 59 | + } |
| 60 | + |
| 61 | + $container = new ContainerBuilder(); |
| 62 | + |
| 63 | + $service = $container->register('service'); |
| 64 | + $service->setFactory($factory); |
| 65 | + |
| 66 | + $pass = new FactoryReturnTypePass(); |
| 67 | + $pass->process($container); |
| 68 | + |
| 69 | + if (method_exists(\ReflectionMethod::class, 'getReturnType')) { |
| 70 | + $this->assertEquals($returnType, $service->getClass()); |
| 71 | + } else { |
| 72 | + $this->assertNull($service->getClass()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public function returnTypesProvider() |
| 77 | + { |
| 78 | + return array( |
| 79 | + // must be loaded before the function as they are in the same file |
| 80 | + array(array(FactoryDummy::class, 'createBuiltin'), null, false), |
| 81 | + array(array(FactoryDummy::class, 'createParent'), FactoryParent::class), |
| 82 | + array(array(FactoryDummy::class, 'createSelf'), FactoryDummy::class), |
| 83 | + array(factoryFunction::class, FactoryDummy::class), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public function testCircularReference() |
| 88 | + { |
| 89 | + $container = new ContainerBuilder(); |
| 90 | + |
| 91 | + $factory = $container->register('factory'); |
| 92 | + $factory->setFactory(array(new Reference('factory2'), 'createSelf')); |
| 93 | + |
| 94 | + $factory2 = $container->register('factory2'); |
| 95 | + $factory2->setFactory(array(new Reference('factory'), 'create')); |
| 96 | + |
| 97 | + $pass = new FactoryReturnTypePass(); |
| 98 | + $pass->process($container); |
| 99 | + |
| 100 | + $this->assertNull($factory->getClass()); |
| 101 | + $this->assertNull($factory2->getClass()); |
| 102 | + } |
| 103 | +} |
0 commit comments