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

Factory/LazyLoadingValueHolderFactoryTest.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\LazyLoadingValueHolderFactory;
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\EmptyClass;
19
use ProxyManagerTestAsset\LazyLoadingMock;
20
use function get_class;
21
22
/**
23
 * Tests for {@see \ProxyManager\Factory\LazyLoadingValueHolderFactory}
24
 *
25
 * @group Coverage
26
 */
27
class LazyLoadingValueHolderFactoryTest 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\LazyLoadingValueHolderFactory::__construct
74
     */
75
    public function testWithOptionalFactory() : void
76
    {
77
        $factory = new LazyLoadingValueHolderFactory();
78
        self::assertAttributeNotEmpty('configuration', $factory);
79
        self::assertAttributeInstanceOf(Configuration::class, 'configuration', $factory);
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     *
85
     * @covers \ProxyManager\Factory\LazyLoadingValueHolderFactory::__construct
86
     * @covers \ProxyManager\Factory\LazyLoadingValueHolderFactory::createProxy
87
     */
88
    public function testWillSkipAutoGeneration() : void
89
    {
90
        $className = UniqueIdentifierGenerator::getIdentifier('foo');
91
92
        $this
93
            ->inflector
94
            ->expects(self::once())
95
            ->method('getProxyClassName')
96
            ->with($className)
97
            ->will(self::returnValue(LazyLoadingMock::class));
98
99
        $factory     = new LazyLoadingValueHolderFactory($this->config);
100
        $initializer = static function () : void {
101
        };
102
        /** @var LazyLoadingMock $proxy */
103
        $proxy = $factory->createProxy($className, $initializer);
104
105
        self::assertInstanceOf(LazyLoadingMock::class, $proxy);
106
        self::assertSame($initializer, $proxy->initializer);
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     *
112
     * @covers \ProxyManager\Factory\LazyLoadingValueHolderFactory::__construct
113
     * @covers \ProxyManager\Factory\LazyLoadingValueHolderFactory::createProxy
114
     * @covers \ProxyManager\Factory\LazyLoadingValueHolderFactory::getGenerator
115
     *
116
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
117
     */
118
    public function testWillTryAutoGeneration() : void
119
    {
120
        $className      = UniqueIdentifierGenerator::getIdentifier('foo');
121
        $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
122
        $generator      = $this->createMock(GeneratorStrategyInterface::class);
123
        $autoloader     = $this->createMock(AutoloaderInterface::class);
124
125
        $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...
126
        $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...
127
128
        $generator
129
            ->expects(self::once())
130
            ->method('generate')
131
            ->with(
132
                self::callback(
133
                    static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
134
                        return $targetClass->getName() === $proxyClassName;
135
                    }
136
                )
137
            );
138
139
        // simulate autoloading
140
        $autoloader
141
            ->expects(self::once())
142
            ->method('__invoke')
143
            ->with($proxyClassName)
144
            ->willReturnCallback(static function () use ($proxyClassName) : bool {
145
                eval('class ' . $proxyClassName . ' extends \\ProxyManagerTestAsset\\LazyLoadingMock {}');
146
147
                return true;
148
            });
149
150
        $this
151
            ->inflector
152
            ->expects(self::once())
153
            ->method('getProxyClassName')
154
            ->with($className)
155
            ->will(self::returnValue($proxyClassName));
156
157
        $this
158
            ->inflector
159
            ->expects(self::once())
160
            ->method('getUserClassName')
161
            ->with($className)
162
            ->will(self::returnValue(EmptyClass::class));
163
164
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
165
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
166
167
        $factory     = new LazyLoadingValueHolderFactory($this->config);
168
        $initializer = static function () : void {
169
        };
170
        /** @var LazyLoadingMock $proxy */
171
        $proxy = $factory->createProxy($className, $initializer);
172
173
        self::assertInstanceOf($proxyClassName, $proxy);
174
        self::assertSame($proxyClassName, get_class($proxy));
175
        self::assertSame($initializer, $proxy->initializer);
176
    }
177
}
178