65722f4fde98711eebfdedd8e0a11d77337a2bdd
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / FactoryReturnTypePassTest.php
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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\FactoryReturnTypePass;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\Tests\Fixtures\factoryFunction;
19 use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy;
20 use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryParent;
21
22 /**
23  * @author Guilhem N. <egetick@gmail.com>
24  */
25 class FactoryReturnTypePassTest extends TestCase
26 {
27     public function testProcess()
28     {
29         $container = new ContainerBuilder();
30
31         $factory = $container->register('factory');
32         $factory->setFactory(array(FactoryDummy::class, 'createFactory'));
33
34         $container->setAlias('alias_factory', 'factory');
35
36         $foo = $container->register('foo');
37         $foo->setFactory(array(new Reference('alias_factory'), 'create'));
38
39         $bar = $container->register('bar', __CLASS__);
40         $bar->setFactory(array(new Reference('factory'), 'create'));
41
42         $pass = new FactoryReturnTypePass();
43         $pass->process($container);
44
45         if (method_exists(\ReflectionMethod::class, 'getReturnType')) {
46             $this->assertEquals(FactoryDummy::class, $factory->getClass());
47             $this->assertEquals(\stdClass::class, $foo->getClass());
48         } else {
49             $this->assertNull($factory->getClass());
50             $this->assertNull($foo->getClass());
51         }
52         $this->assertEquals(__CLASS__, $bar->getClass());
53     }
54
55     /**
56      * @dataProvider returnTypesProvider
57      */
58     public function testReturnTypes($factory, $returnType, $hhvmSupport = true)
59     {
60         if (!$hhvmSupport && defined('HHVM_VERSION')) {
61             $this->markTestSkipped('Scalar typehints not supported by hhvm.');
62         }
63
64         $container = new ContainerBuilder();
65
66         $service = $container->register('service');
67         $service->setFactory($factory);
68
69         $pass = new FactoryReturnTypePass();
70         $pass->process($container);
71
72         if (method_exists(\ReflectionMethod::class, 'getReturnType')) {
73             $this->assertEquals($returnType, $service->getClass());
74         } else {
75             $this->assertNull($service->getClass());
76         }
77     }
78
79     public function returnTypesProvider()
80     {
81         return array(
82             // must be loaded before the function as they are in the same file
83             array(array(FactoryDummy::class, 'createBuiltin'), null, false),
84             array(array(FactoryDummy::class, 'createParent'), FactoryParent::class),
85             array(array(FactoryDummy::class, 'createSelf'), FactoryDummy::class),
86             array(factoryFunction::class, FactoryDummy::class),
87         );
88     }
89
90     public function testCircularReference()
91     {
92         $container = new ContainerBuilder();
93
94         $factory = $container->register('factory');
95         $factory->setFactory(array(new Reference('factory2'), 'createSelf'));
96
97         $factory2 = $container->register('factory2');
98         $factory2->setFactory(array(new Reference('factory'), 'create'));
99
100         $pass = new FactoryReturnTypePass();
101         $pass->process($container);
102
103         $this->assertNull($factory->getClass());
104         $this->assertNull($factory2->getClass());
105     }
106
107     public function testCompile()
108     {
109         $container = new ContainerBuilder();
110
111         $factory = $container->register('factory');
112         $factory->setFactory(array(FactoryDummy::class, 'createFactory'));
113
114         if (!method_exists(\ReflectionMethod::class, 'getReturnType')) {
115             if (method_exists($this, 'expectException')) {
116                 $this->expectException(\RuntimeException::class);
117                 $this->expectExceptionMessage('Please add the class to service "factory" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.');
118             } else {
119                 $this->setExpectedException(\RuntimeException::class, 'Please add the class to service "factory" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.');
120             }
121         }
122
123         $container->compile();
124
125         $this->assertEquals(FactoryDummy::class, $container->getDefinition('factory')->getClass());
126     }
127 }