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

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