/
RemoteObjectFactoryTest.php
173 lines (149 loc) Β· 5.9 KB
/
RemoteObjectFactoryTest.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace ProxyManagerTest\Factory;
use PHPUnit\Framework\TestCase;
use ProxyManager\Autoloader\AutoloaderInterface;
use ProxyManager\Configuration;
use ProxyManager\Factory\RemoteObject\AdapterInterface;
use ProxyManager\Factory\RemoteObjectFactory;
use ProxyManager\Generator\ClassGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
use ProxyManager\Inflector\ClassNameInflectorInterface;
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
use ProxyManager\Signature\SignatureCheckerInterface;
use ProxyManagerTestAsset\BaseInterface;
use ProxyManagerTestAsset\RemoteProxy\RemoteObjectMock;
/**
* Tests for {@see \ProxyManager\Factory\RemoteObjectFactory}
*
* @author Vincent Blanchon <blanchon.vincent@gmail.com>
* @license MIT
*
* @group Coverage
*/
class RemoteObjectFactoryTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $inflector;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $signatureChecker;
/**
* @var ClassSignatureGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $classSignatureGenerator;
/**
* @var Configuration|\PHPUnit_Framework_MockObject_MockObject
*/
protected $config;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->config = $this->createMock(Configuration::class);
$this->inflector = $this->createMock(ClassNameInflectorInterface::class);
$this->signatureChecker = $this->createMock(SignatureCheckerInterface::class);
$this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
$this
->config
->expects(self::any())
->method('getClassNameInflector')
->will(self::returnValue($this->inflector));
$this
->config
->expects(self::any())
->method('getSignatureChecker')
->will(self::returnValue($this->signatureChecker));
$this
->config
->expects(self::any())
->method('getClassSignatureGenerator')
->will(self::returnValue($this->classSignatureGenerator));
}
/**
* {@inheritDoc}
*
* @covers \ProxyManager\Factory\RemoteObjectFactory::__construct
* @covers \ProxyManager\Factory\RemoteObjectFactory::createProxy
* @covers \ProxyManager\Factory\RemoteObjectFactory::getGenerator
*/
public function testWillSkipAutoGeneration() : void
{
$this
->inflector
->expects(self::once())
->method('getProxyClassName')
->with(BaseInterface::class)
->will(self::returnValue(RemoteObjectMock::class));
/* @var $adapter AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
$adapter = $this->createMock(AdapterInterface::class);
$factory = new RemoteObjectFactory($adapter, $this->config);
/* @var $proxy \stdClass */
$proxy = $factory->createProxy(BaseInterface::class);
self::assertInstanceOf(RemoteObjectMock::class, $proxy);
}
/**
* {@inheritDoc}
*
* @covers \ProxyManager\Factory\RemoteObjectFactory::__construct
* @covers \ProxyManager\Factory\RemoteObjectFactory::createProxy
* @covers \ProxyManager\Factory\RemoteObjectFactory::getGenerator
*
* NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
*/
public function testWillTryAutoGeneration() : void
{
$proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
$generator = $this->createMock(GeneratorStrategyInterface::class);
$autoloader = $this->createMock(AutoloaderInterface::class);
$this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
$this->config->expects(self::any())->method('getProxyAutoloader')->will(self::returnValue($autoloader));
$generator
->expects(self::once())
->method('generate')
->with(
self::callback(
function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
return $targetClass->getName() === $proxyClassName;
}
)
);
// simulate autoloading
$autoloader
->expects(self::once())
->method('__invoke')
->with($proxyClassName)
->willReturnCallback(function () use ($proxyClassName) : bool {
eval(
'class ' . $proxyClassName . ' implements \ProxyManager\Proxy\RemoteObjectInterface {'
. 'public static function staticProxyConstructor() : self { return new static(); }'
. '}'
);
return true;
});
$this
->inflector
->expects(self::once())
->method('getProxyClassName')
->with(BaseInterface::class)
->will(self::returnValue($proxyClassName));
$this
->inflector
->expects(self::once())
->method('getUserClassName')
->with(BaseInterface::class)
->will(self::returnValue('stdClass'));
$this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
$this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
/* @var $adapter AdapterInterface */
$adapter = $this->createMock(AdapterInterface::class);
$factory = new RemoteObjectFactory($adapter, $this->config);
$proxy = $factory->createProxy(BaseInterface::class);
self::assertInstanceOf($proxyClassName, $proxy);
}
}