Completed
Pull Request — master (#381)
by Marco
08:41
created

testWillSkipAutoGeneration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\Factory;
6
7
use PHPUnit\Framework\TestCase;
8
use ProxyManager\Autoloader\AutoloaderInterface;
9
use ProxyManager\Configuration;
10
use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory;
11
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
12
use ProxyManager\Generator\ClassGenerator;
13
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
14
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
15
use ProxyManager\Inflector\ClassNameInflectorInterface;
16
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
17
use ProxyManager\Signature\SignatureCheckerInterface;
18
use ProxyManagerTestAsset\AccessInterceptorValueHolderMock;
19
use ProxyManagerTestAsset\LazyLoadingMock;
20
use stdClass;
21
22
/**
23
 * Tests for {@see \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory}
24
 *
25
 * @author Marco Pivetta <[email protected]>
26
 * @license MIT
27
 *
28
 * @group Coverage
29
 */
30
class AccessInterceptorScopeLocalizerFactoryTest extends TestCase
31
{
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockObject
34
     */
35
    protected $inflector;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject
39
     */
40
    protected $signatureChecker;
41
42
    /**
43
     * @var ClassSignatureGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $classSignatureGenerator;
46
47
    /**
48
     * @var Configuration|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    protected $config;
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function setUp()
56
    {
57
        $this->config                  = $this->createMock(Configuration::class);
58
        $this->inflector               = $this->createMock(ClassNameInflectorInterface::class);
59
        $this->signatureChecker        = $this->createMock(SignatureCheckerInterface::class);
60
        $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
61
62
        $this
63
            ->config
64
            ->expects(self::any())
65
            ->method('getClassNameInflector')
66
            ->will(self::returnValue($this->inflector));
67
68
        $this
69
            ->config
70
            ->expects(self::any())
71
            ->method('getSignatureChecker')
72
            ->will(self::returnValue($this->signatureChecker));
73
74
        $this
75
            ->config
76
            ->expects(self::any())
77
            ->method('getClassSignatureGenerator')
78
            ->will(self::returnValue($this->classSignatureGenerator));
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     *
84
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::__construct
85
     */
86
    public function testWithOptionalFactory() : void
87
    {
88
        $factory = new AccessInterceptorValueHolderFactory();
89
        self::assertAttributeNotEmpty('configuration', $factory);
90
        self::assertAttributeInstanceOf(Configuration::class, 'configuration', $factory);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::__construct
97
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::createProxy
98
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::getGenerator
99
     */
100
    public function testWillSkipAutoGeneration() : void
101
    {
102
        $instance = new stdClass();
103
104
        $this
105
            ->inflector
106
            ->expects(self::once())
107
            ->method('getProxyClassName')
108
            ->with('stdClass')
109
            ->will(self::returnValue(AccessInterceptorValueHolderMock::class));
110
111
        $factory            = new AccessInterceptorScopeLocalizerFactory($this->config);
112
        $prefixInterceptors = [function () {
113
            self::fail('Not supposed to be called');
114
        }];
115
        $suffixInterceptors = [function () {
116
            self::fail('Not supposed to be called');
117
        }];
118
        /* @var $proxy AccessInterceptorValueHolderMock */
119
        $proxy              = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);
120
121
        self::assertInstanceOf(AccessInterceptorValueHolderMock::class, $proxy);
122
        self::assertSame($instance, $proxy->instance);
123
        self::assertSame($prefixInterceptors, $proxy->prefixInterceptors);
124
        self::assertSame($suffixInterceptors, $proxy->suffixInterceptors);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     *
130
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::__construct
131
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::createProxy
132
     * @covers \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory::getGenerator
133
     *
134
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
135
     */
136
    public function testWillTryAutoGeneration() : void
137
    {
138
        $instance       = new stdClass();
139
        $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
140
        $generator      = $this->createMock(GeneratorStrategyInterface::class);
141
        $autoloader     = $this->createMock(AutoloaderInterface::class);
142
143
        $this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<ProxyManager\Configuration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $this->config->expects(self::any())->method('getProxyAutoloader')->will(self::returnValue($autoloader));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<ProxyManager\Configuration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
146
        $generator
147
            ->expects(self::once())
148
            ->method('generate')
149
            ->with(
150
                self::callback(
151
                    function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
152
                        return $targetClass->getName() === $proxyClassName;
153
                    }
154
                )
155
            );
156
157
        // simulate autoloading
158
        $autoloader
159
            ->expects(self::once())
160
            ->method('__invoke')
161
            ->with($proxyClassName)
162
            ->willReturnCallback(function () use ($proxyClassName) : bool {
163
                eval(
164
                    'class ' . $proxyClassName
165
                    . ' extends \\ProxyManagerTestAsset\\AccessInterceptorValueHolderMock {}'
166
                );
167
168
                return true;
169
            });
170
171
        $this
172
            ->inflector
173
            ->expects(self::once())
174
            ->method('getProxyClassName')
175
            ->with('stdClass')
176
            ->will(self::returnValue($proxyClassName));
177
178
        $this
179
            ->inflector
180
            ->expects(self::once())
181
            ->method('getUserClassName')
182
            ->with('stdClass')
183
            ->will(self::returnValue(LazyLoadingMock::class));
184
185
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
186
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<ProxyManager\Sign...tureGeneratorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
188
        $factory            = new AccessInterceptorScopeLocalizerFactory($this->config);
189
        $prefixInterceptors = [function () {
190
            self::fail('Not supposed to be called');
191
        }];
192
        $suffixInterceptors = [function () {
193
            self::fail('Not supposed to be called');
194
        }];
195
        /* @var $proxy AccessInterceptorValueHolderMock */
196
        $proxy              = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);
197
198
        self::assertInstanceOf($proxyClassName, $proxy);
199
        self::assertSame($instance, $proxy->instance);
200
        self::assertSame($prefixInterceptors, $proxy->prefixInterceptors);
201
        self::assertSame($suffixInterceptors, $proxy->suffixInterceptors);
202
    }
203
}
204