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

AccessInterceptorValueHolderFactoryTest::testWillTryAutoGeneration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.6763
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\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 ProxyManagerTest\Assert;
19
use ProxyManagerTestAsset\AccessInterceptorValueHolderMock;
20
use ProxyManagerTestAsset\EmptyClass;
21
use stdClass;
22
23
/**
24
 * Tests for {@see \ProxyManager\Factory\AccessInterceptorValueHolderFactory}
25
 *
26
 * @group Coverage
27
 */
28
final class AccessInterceptorValueHolderFactoryTest extends TestCase
29
{
30
    /** @var ClassNameInflectorInterface&MockObject */
31
    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...
32
33
    /** @var SignatureCheckerInterface&MockObject */
34
    private SignatureCheckerInterface $signatureChecker;
35
36
    /** @var ClassSignatureGeneratorInterface&MockObject */
37
    private ClassSignatureGeneratorInterface $classSignatureGenerator;
38
39
    /** @var Configuration&MockObject */
40
    private Configuration $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
            ->method('getClassNameInflector')
55
            ->willReturn($this->inflector);
56
57
        $this
58
            ->config
59
            ->method('getSignatureChecker')
60
            ->willReturn($this->signatureChecker);
61
62
        $this
63
            ->config
64
            ->method('getClassSignatureGenerator')
65
            ->willReturn($this->classSignatureGenerator);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     *
71
     * @covers \ProxyManager\Factory\AccessInterceptorValueHolderFactory::__construct
72
     */
73
    public static function testWithOptionalFactory() : void
74
    {
75
        $factory       = new AccessInterceptorValueHolderFactory();
76
        $configuration = Assert::readAttribute($factory, 'configuration');
77
78
        self::assertNotEmpty($configuration);
79
        self::assertInstanceOf(Configuration::class, $configuration);
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
            ->willReturn(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->method('getGeneratorStrategy')->will(self::returnValue($generator));
135
        $this->config->method('getProxyAutoloader')->will(self::returnValue($autoloader));
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
            ->willReturn($proxyClassName);
168
169
        $this
170
            ->inflector
171
            ->expects(self::once())
172
            ->method('getUserClassName')
173
            ->with('stdClass')
174
            ->willReturn(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