Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

AccessInterceptorScopeLocalizerFactoryTest::testWillTryAutoGeneration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 8.6327
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ProxyManagerTest\Assert;
20
use ProxyManagerTestAsset\AccessInterceptorValueHolderMock;
21
use ProxyManagerTestAsset\LazyLoadingMock;
22
use stdClass;
23
24
/**
25
 * Tests for {@see \ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory}
26
 *
27
 * @group Coverage
28
 */
29
final class AccessInterceptorScopeLocalizerFactoryTest extends TestCase
30
{
31
    /** @var ClassNameInflectorInterface&MockObject */
32
    private ClassNameInflectorInterface $inflector;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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