Completed
Push — master ( 9f8061...f01068 )
by Marco
13s
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\AccessInterceptorValueHolderFactory;
11
use ProxyManager\Generator\ClassGenerator;
12
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
13
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
14
use ProxyManager\Inflector\ClassNameInflectorInterface;
15
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
16
use ProxyManager\Signature\SignatureCheckerInterface;
17
use ProxyManagerTestAsset\AccessInterceptorValueHolderMock;
18
use ProxyManagerTestAsset\EmptyClass;
19
use stdClass;
20
21
/**
22
 * Tests for {@see \ProxyManager\Factory\AccessInterceptorValueHolderFactory}
23
 *
24
 * @author Marco Pivetta <[email protected]>
25
 * @license MIT
26
 *
27
 * @group Coverage
28
 */
29
class AccessInterceptorValueHolderFactoryTest extends TestCase
30
{
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject
33
     */
34
    protected $inflector;
35
36
    /**
37
     * @var \PHPUnit_Framework_MockObject_MockObject
38
     */
39
    protected $signatureChecker;
40
41
    /**
42
     * @var ClassSignatureGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
43
     */
44
    private $classSignatureGenerator;
45
46
    /**
47
     * @var Configuration|\PHPUnit_Framework_MockObject_MockObject
48
     */
49
    protected $config;
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function setUp()
55
    {
56
        $this->config                  = $this->createMock(Configuration::class);
57
        $this->inflector               = $this->createMock(ClassNameInflectorInterface::class);
58
        $this->signatureChecker        = $this->createMock(SignatureCheckerInterface::class);
59
        $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
60
61
        $this
62
            ->config
63
            ->expects(self::any())
64
            ->method('getClassNameInflector')
65
            ->will(self::returnValue($this->inflector));
66
67
        $this
68
            ->config
69
            ->expects(self::any())
70
            ->method('getSignatureChecker')
71
            ->will(self::returnValue($this->signatureChecker));
72
73
        $this
74
            ->config
75
            ->expects(self::any())
76
            ->method('getClassSignatureGenerator')
77
            ->will(self::returnValue($this->classSignatureGenerator));
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     *
83
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::__construct
84
     */
85
    public function testWithOptionalFactory() : void
86
    {
87
        $factory = new AccessInterceptorValueHolderFactory();
88
        self::assertAttributeNotEmpty('configuration', $factory);
89
        self::assertAttributeInstanceOf(Configuration::class, 'configuration', $factory);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     *
95
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::__construct
96
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::createProxy
97
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::getGenerator
98
     */
99
    public function testWillSkipAutoGeneration() : void
100
    {
101
        $instance = new stdClass();
102
103
        $this
104
            ->inflector
105
            ->expects(self::once())
106
            ->method('getProxyClassName')
107
            ->with('stdClass')
108
            ->will(self::returnValue(AccessInterceptorValueHolderMock::class));
109
110
        $factory            = new AccessInterceptorValueHolderFactory($this->config);
111
        $prefixInterceptors = [function () {
112
            self::fail('Not supposed to be called');
113
        }];
114
        $suffixInterceptors = [function () {
115
            self::fail('Not supposed to be called');
116
        }];
117
        /* @var $proxy AccessInterceptorValueHolderMock */
118
        $proxy              = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);
119
120
        self::assertInstanceOf(AccessInterceptorValueHolderMock::class, $proxy);
121
        self::assertSame($instance, $proxy->instance);
122
        self::assertSame($prefixInterceptors, $proxy->prefixInterceptors);
123
        self::assertSame($suffixInterceptors, $proxy->suffixInterceptors);
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     *
129
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::__construct
130
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::createProxy
131
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::getGenerator
132
     *
133
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
134
     */
135
    public function testWillTryAutoGeneration() : void
136
    {
137
        $instance       = new stdClass();
138
        $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
139
        $generator      = $this->createMock(GeneratorStrategyInterface::class);
140
        $autoloader     = $this->createMock(AutoloaderInterface::class);
141
142
        $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...
143
        $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...
144
145
        $generator
146
            ->expects(self::once())
147
            ->method('generate')
148
            ->with(
149
                self::callback(
150
                    function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
151
                        return $targetClass->getName() === $proxyClassName;
152
                    }
153
                )
154
            );
155
156
        // simulate autoloading
157
        $autoloader
158
            ->expects(self::once())
159
            ->method('__invoke')
160
            ->with($proxyClassName)
161
            ->willReturnCallback(function () use ($proxyClassName) : bool {
162
                eval(
163
                    'class ' . $proxyClassName
164
                    . ' extends \\ProxyManagerTestAsset\\AccessInterceptorValueHolderMock {}'
165
                );
166
167
                return true;
168
            });
169
170
        $this
171
            ->inflector
172
            ->expects(self::once())
173
            ->method('getProxyClassName')
174
            ->with('stdClass')
175
            ->will(self::returnValue($proxyClassName));
176
177
        $this
178
            ->inflector
179
            ->expects(self::once())
180
            ->method('getUserClassName')
181
            ->with('stdClass')
182
            ->will(self::returnValue(EmptyClass::class));
183
184
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
185
        $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...
186
187
        $factory            = new AccessInterceptorValueHolderFactory($this->config);
188
        $prefixInterceptors = [function () {
189
            self::fail('Not supposed to be called');
190
        }];
191
        $suffixInterceptors = [function () {
192
            self::fail('Not supposed to be called');
193
        }];
194
        /* @var $proxy AccessInterceptorValueHolderMock */
195
        $proxy              = $factory->createProxy($instance, $prefixInterceptors, $suffixInterceptors);
196
197
        self::assertInstanceOf($proxyClassName, $proxy);
198
        self::assertSame($instance, $proxy->instance);
199
        self::assertSame($prefixInterceptors, $proxy->prefixInterceptors);
200
        self::assertSame($suffixInterceptors, $proxy->suffixInterceptors);
201
    }
202
}
203