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

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