1 | <?php |
||
30 | final class AbstractBaseFactoryTest extends TestCase |
||
31 | { |
||
32 | /** |
||
33 | * Note: we mock the class in order to assert on the abstract method usage |
||
34 | * |
||
35 | * @var AbstractBaseFactory|MockObject |
||
36 | */ |
||
37 | private AbstractBaseFactory $factory; |
||
|
|||
38 | |||
39 | /** @var ProxyGeneratorInterface&MockObject */ |
||
40 | private ProxyGeneratorInterface $generator; |
||
41 | |||
42 | /** @var ClassNameInflectorInterface&MockObject */ |
||
43 | private ClassNameInflectorInterface $classNameInflector; |
||
44 | |||
45 | /** @var GeneratorStrategyInterface&MockObject */ |
||
46 | private GeneratorStrategyInterface $generatorStrategy; |
||
47 | |||
48 | /** @var AutoloaderInterface&MockObject */ |
||
49 | private AutoloaderInterface $proxyAutoloader; |
||
50 | |||
51 | /** @var SignatureCheckerInterface&MockObject */ |
||
52 | private SignatureCheckerInterface $signatureChecker; |
||
53 | |||
54 | /** @var ClassSignatureGeneratorInterface&MockObject */ |
||
55 | private ClassSignatureGeneratorInterface $classSignatureGenerator; |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | */ |
||
60 | protected function setUp() : void |
||
61 | { |
||
62 | $configuration = $this->createMock(Configuration::class); |
||
63 | $this->generator = $this->createMock(ProxyGeneratorInterface::class); |
||
64 | $this->classNameInflector = $this->createMock(ClassNameInflectorInterface::class); |
||
65 | $this->generatorStrategy = $this->createMock(GeneratorStrategyInterface::class); |
||
66 | $this->proxyAutoloader = $this->createMock(AutoloaderInterface::class); |
||
67 | $this->signatureChecker = $this->createMock(SignatureCheckerInterface::class); |
||
68 | $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class); |
||
69 | |||
70 | $configuration |
||
71 | ->method('getClassNameInflector') |
||
72 | ->willReturn($this->classNameInflector); |
||
73 | |||
74 | $configuration |
||
75 | ->method('getGeneratorStrategy') |
||
76 | ->willReturn($this->generatorStrategy); |
||
77 | |||
78 | $configuration |
||
79 | ->method('getProxyAutoloader') |
||
80 | ->willReturn($this->proxyAutoloader); |
||
81 | |||
82 | $configuration |
||
83 | ->method('getSignatureChecker') |
||
84 | ->willReturn($this->signatureChecker); |
||
85 | |||
86 | $configuration |
||
87 | ->method('getClassSignatureGenerator') |
||
88 | ->willReturn($this->classSignatureGenerator); |
||
89 | |||
90 | $this |
||
91 | ->classNameInflector |
||
92 | ->method('getUserClassName') |
||
93 | ->willReturn('stdClass'); |
||
94 | |||
95 | $this->factory = $this->getMockForAbstractClass(AbstractBaseFactory::class, [$configuration]); |
||
96 | |||
97 | $this->factory->method('getGenerator')->willReturn($this->generator); |
||
98 | } |
||
99 | |||
100 | public function testGeneratesClass() : void |
||
101 | { |
||
102 | $generateProxy = new ReflectionMethod($this->factory, 'generateProxy'); |
||
103 | |||
104 | $generateProxy->setAccessible(true); |
||
105 | $generatedClass = UniqueIdentifierGenerator::getIdentifier('fooBar'); |
||
106 | |||
107 | $this |
||
108 | ->classNameInflector |
||
109 | ->method('getProxyClassName') |
||
110 | ->with('stdClass') |
||
111 | ->willReturn($generatedClass); |
||
112 | |||
113 | $this |
||
114 | ->generatorStrategy |
||
115 | ->expects(self::once()) |
||
116 | ->method('generate') |
||
117 | ->with(self::isInstanceOf(ClassGenerator::class)); |
||
118 | $this |
||
119 | ->proxyAutoloader |
||
120 | ->expects(self::once()) |
||
121 | ->method('__invoke') |
||
122 | ->with($generatedClass) |
||
123 | ->will(self::returnCallback(static function ($className) : bool { |
||
124 | eval('class ' . $className . ' {}'); |
||
125 | |||
126 | return true; |
||
127 | })); |
||
128 | |||
129 | $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature'); |
||
130 | $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0)); |
||
131 | $this |
||
132 | ->generator |
||
133 | ->expects(self::once()) |
||
134 | ->method('generate') |
||
135 | ->with( |
||
136 | self::callback(static function (ReflectionClass $reflectionClass) : bool { |
||
137 | return $reflectionClass->getName() === 'stdClass'; |
||
138 | }), |
||
139 | self::isInstanceOf(ClassGenerator::class), |
||
140 | ['some' => 'proxy', 'options' => 'here'] |
||
141 | ); |
||
142 | |||
143 | self::assertSame( |
||
144 | $generatedClass, |
||
145 | $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here']) |
||
146 | ); |
||
147 | self::assertTrue(class_exists($generatedClass, false)); |
||
148 | self::assertSame( |
||
149 | $generatedClass, |
||
150 | $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here']) |
||
151 | ); |
||
152 | } |
||
153 | } |
||
154 |