Completed
Push — master ( 6a6c80...a2c1e8 )
by Marco
79:00 queued 52:38
created

AccessInterceptorScopeLocalizerFactoryTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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