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

RemoteObjectFactoryTest::testWillTryAutoGeneration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.9381
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\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
final class RemoteObjectFactoryTest 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\RemoteObjectFactory::__construct
72
     * @covers \ProxyManager\Factory\RemoteObjectFactory::createProxy
73
     * @covers \ProxyManager\Factory\RemoteObjectFactory::getGenerator
74
     */
75
    public function testWillSkipAutoGeneration() : void
76
    {
77
        $this
78
            ->inflector
79
            ->expects(self::once())
80
            ->method('getProxyClassName')
81
            ->with(BaseInterface::class)
82
            ->willReturn(RemoteObjectMock::class);
83
84
        /** @var AdapterInterface&MockObject $adapter */
85
        $adapter = $this->createMock(AdapterInterface::class);
86
        $factory = new RemoteObjectFactory($adapter, $this->config);
87
        /** @var stdClass|RemoteObjectMock $proxy */
88
        $proxy = $factory->createProxy(BaseInterface::class);
89
90
        self::assertInstanceOf(RemoteObjectMock::class, $proxy);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @covers \ProxyManager\Factory\RemoteObjectFactory::__construct
97
     * @covers \ProxyManager\Factory\RemoteObjectFactory::createProxy
98
     * @covers \ProxyManager\Factory\RemoteObjectFactory::getGenerator
99
     *
100
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
101
     */
102
    public function testWillTryAutoGeneration() : void
103
    {
104
        $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
105
        $generator      = $this->createMock(GeneratorStrategyInterface::class);
106
        $autoloader     = $this->createMock(AutoloaderInterface::class);
107
108
        $this->config->method('getGeneratorStrategy')->willReturn($generator);
109
        $this->config->method('getProxyAutoloader')->willReturn($autoloader);
110
111
        $generator
112
            ->expects(self::once())
113
            ->method('generate')
114
            ->with(
115
                self::callback(
116
                    static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
117
                        return $targetClass->getName() === $proxyClassName;
118
                    }
119
                )
120
            );
121
122
        // simulate autoloading
123
        $autoloader
124
            ->expects(self::once())
125
            ->method('__invoke')
126
            ->with($proxyClassName)
127
            ->willReturnCallback(static function () use ($proxyClassName) : bool {
128
                eval(
129
                    'class ' . $proxyClassName . ' implements \ProxyManager\Proxy\RemoteObjectInterface {'
130
                    . 'public static function staticProxyConstructor() : self { return new static(); }'
131
                    . '}'
132
                );
133
134
                return true;
135
            });
136
137
        $this
138
            ->inflector
139
            ->expects(self::once())
140
            ->method('getProxyClassName')
141
            ->with(BaseInterface::class)
142
            ->willReturn($proxyClassName);
143
144
        $this
145
            ->inflector
146
            ->expects(self::once())
147
            ->method('getUserClassName')
148
            ->with(BaseInterface::class)
149
            ->willReturn('stdClass');
150
151
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
152
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
153
154
        /** @var AdapterInterface $adapter */
155
        $adapter = $this->createMock(AdapterInterface::class);
156
        $factory = new RemoteObjectFactory($adapter, $this->config);
157
        $factory->createProxy(BaseInterface::class);
158
    }
159
}
160