-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractBaseFactoryTest.php
153 lines (127 loc) Β· 5.18 KB
/
AbstractBaseFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace ProxyManagerTest\Factory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ProxyManager\Autoloader\AutoloaderInterface;
use ProxyManager\Configuration;
use ProxyManager\Factory\AbstractBaseFactory;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
use ProxyManager\Inflector\ClassNameInflectorInterface;
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
use ProxyManager\Signature\SignatureCheckerInterface;
use ReflectionClass;
use ReflectionMethod;
use stdClass;
use Zend\Code\Generator\ClassGenerator;
use function class_exists;
/**
* Tests for {@see \ProxyManager\Factory\AbstractBaseFactory}
*
* @covers \ProxyManager\Factory\AbstractBaseFactory
* @group Coverage
*/
class AbstractBaseFactoryTest extends TestCase
{
/**
* Note: we mock the class in order to assert on the abstract method usage
*
* @var AbstractBaseFactory|MockObject
*/
private $factory;
/** @var ProxyGeneratorInterface|MockObject */
private $generator;
/** @var ClassNameInflectorInterface|MockObject */
private $classNameInflector;
/** @var GeneratorStrategyInterface|MockObject */
private $generatorStrategy;
/** @var AutoloaderInterface|MockObject */
private $proxyAutoloader;
/** @var SignatureCheckerInterface|MockObject */
private $signatureChecker;
/** @var ClassSignatureGeneratorInterface|MockObject */
private $classSignatureGenerator;
/**
* {@inheritDoc}
*/
protected function setUp() : void
{
$configuration = $this->createMock(Configuration::class);
$this->generator = $this->createMock(ProxyGeneratorInterface::class);
$this->classNameInflector = $this->createMock(ClassNameInflectorInterface::class);
$this->generatorStrategy = $this->createMock(GeneratorStrategyInterface::class);
$this->proxyAutoloader = $this->createMock(AutoloaderInterface::class);
$this->signatureChecker = $this->createMock(SignatureCheckerInterface::class);
$this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
$configuration
->method('getClassNameInflector')
->willReturn($this->classNameInflector);
$configuration
->method('getGeneratorStrategy')
->willReturn($this->generatorStrategy);
$configuration
->method('getProxyAutoloader')
->willReturn($this->proxyAutoloader);
$configuration
->method('getSignatureChecker')
->willReturn($this->signatureChecker);
$configuration
->method('getClassSignatureGenerator')
->willReturn($this->classSignatureGenerator);
$this
->classNameInflector
->method('getUserClassName')
->willReturn('stdClass');
$this->factory = $this->getMockForAbstractClass(AbstractBaseFactory::class, [$configuration]);
$this->factory->expects(self::any())->method('getGenerator')->willReturn($this->generator);
}
public function testGeneratesClass() : void
{
$generateProxy = new ReflectionMethod($this->factory, 'generateProxy');
$generateProxy->setAccessible(true);
$generatedClass = UniqueIdentifierGenerator::getIdentifier('fooBar');
$this
->classNameInflector
->method('getProxyClassName')
->with('stdClass')
->willReturn($generatedClass);
$this
->generatorStrategy
->expects(self::once())
->method('generate')
->with(self::isInstanceOf(ClassGenerator::class));
$this
->proxyAutoloader
->expects(self::once())
->method('__invoke')
->with($generatedClass)
->will(self::returnCallback(static function ($className) : bool {
eval('class ' . $className . ' {}');
return true;
}));
$this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
$this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
$this
->generator
->expects(self::once())
->method('generate')
->with(
self::callback(static function (ReflectionClass $reflectionClass) : bool {
return $reflectionClass->getName() === 'stdClass';
}),
self::isInstanceOf(ClassGenerator::class),
['some' => 'proxy', 'options' => 'here']
);
self::assertSame(
$generatedClass,
$generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
);
self::assertTrue(class_exists($generatedClass, false));
self::assertSame(
$generatedClass,
$generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
);
}
}