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

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