1 | <?php |
||
26 | final class NullObjectFactoryTest extends TestCase |
||
27 | { |
||
28 | /** @var ClassNameInflectorInterface&MockObject */ |
||
29 | private ClassNameInflectorInterface $inflector; |
||
|
|||
30 | |||
31 | /** @var SignatureCheckerInterface&MockObject */ |
||
32 | private SignatureCheckerInterface $signatureChecker; |
||
33 | |||
34 | /** @var ClassSignatureGeneratorInterface&MockObject */ |
||
35 | private ClassSignatureGeneratorInterface $classSignatureGenerator; |
||
36 | |||
37 | /** @var Configuration&MockObject */ |
||
38 | private Configuration $config; |
||
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | protected function setUp() : void |
||
44 | { |
||
45 | $this->config = $this->createMock(Configuration::class); |
||
46 | $this->inflector = $this->createMock(ClassNameInflectorInterface::class); |
||
47 | $this->signatureChecker = $this->createMock(SignatureCheckerInterface::class); |
||
48 | $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class); |
||
49 | |||
50 | $this |
||
51 | ->config |
||
52 | ->method('getClassNameInflector') |
||
53 | ->willReturn($this->inflector); |
||
54 | |||
55 | $this |
||
56 | ->config |
||
57 | ->method('getSignatureChecker') |
||
58 | ->willReturn($this->signatureChecker); |
||
59 | |||
60 | $this |
||
61 | ->config |
||
62 | ->method('getClassSignatureGenerator') |
||
63 | ->willReturn($this->classSignatureGenerator); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | * |
||
69 | * @covers \ProxyManager\Factory\NullObjectFactory::__construct |
||
70 | * @covers \ProxyManager\Factory\NullObjectFactory::createProxy |
||
71 | * @covers \ProxyManager\Factory\NullObjectFactory::getGenerator |
||
72 | */ |
||
73 | public function testWillSkipAutoGeneration() : void |
||
74 | { |
||
75 | $instance = new stdClass(); |
||
76 | |||
77 | $this |
||
78 | ->inflector |
||
79 | ->expects(self::once()) |
||
80 | ->method('getProxyClassName') |
||
81 | ->with('stdClass') |
||
82 | ->willReturn(NullObjectMock::class); |
||
83 | |||
84 | $factory = new NullObjectFactory($this->config); |
||
85 | /** @var NullObjectMock $proxy */ |
||
86 | $proxy = $factory->createProxy($instance); |
||
87 | |||
88 | self::assertInstanceOf(NullObjectMock::class, $proxy); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | * |
||
94 | * @covers \ProxyManager\Factory\NullObjectFactory::__construct |
||
95 | * @covers \ProxyManager\Factory\NullObjectFactory::createProxy |
||
96 | * @covers \ProxyManager\Factory\NullObjectFactory::getGenerator |
||
97 | * |
||
98 | * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful |
||
99 | */ |
||
100 | public function testWillTryAutoGeneration() : void |
||
101 | { |
||
102 | $instance = new stdClass(); |
||
103 | $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar'); |
||
104 | $generator = $this->createMock(GeneratorStrategyInterface::class); |
||
105 | $autoloader = $this->createMock(AutoloaderInterface::class); |
||
106 | |||
107 | $this->config->method('getGeneratorStrategy')->will(self::returnValue($generator)); |
||
108 | $this->config->method('getProxyAutoloader')->will(self::returnValue($autoloader)); |
||
109 | |||
110 | $generator |
||
111 | ->expects(self::once()) |
||
112 | ->method('generate') |
||
113 | ->with( |
||
114 | self::callback( |
||
115 | static function (ClassGenerator $targetClass) use ($proxyClassName) : bool { |
||
116 | return $targetClass->getName() === $proxyClassName; |
||
117 | } |
||
118 | ) |
||
119 | ); |
||
120 | |||
121 | // simulate autoloading |
||
122 | $autoloader |
||
123 | ->expects(self::once()) |
||
124 | ->method('__invoke') |
||
125 | ->with($proxyClassName) |
||
126 | ->willReturnCallback(static function () use ($proxyClassName) : bool { |
||
127 | eval('class ' . $proxyClassName . ' extends \\ProxyManagerTestAsset\\NullObjectMock {}'); |
||
128 | |||
129 | return true; |
||
130 | }); |
||
131 | |||
132 | $this |
||
133 | ->inflector |
||
134 | ->expects(self::once()) |
||
135 | ->method('getProxyClassName') |
||
136 | ->with('stdClass') |
||
137 | ->willReturn($proxyClassName); |
||
138 | |||
139 | $this |
||
140 | ->inflector |
||
141 | ->expects(self::once()) |
||
142 | ->method('getUserClassName') |
||
143 | ->with('stdClass') |
||
144 | ->willReturn(NullObjectMock::class); |
||
145 | |||
146 | $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature'); |
||
147 | $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0)); |
||
148 | |||
149 | $factory = new NullObjectFactory($this->config); |
||
150 | $factory->createProxy($instance); |
||
151 | } |
||
152 | } |
||
153 |