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

NullObjectFactoryTest::testWillTryAutoGeneration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.0472
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\NullObjectFactory;
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\NullObjectMock;
19
use stdClass;
20
21
/**
22
 * Tests for {@see \ProxyManager\Factory\NullObjectFactory}
23
 *
24
 * @group Coverage
25
 */
26
class NullObjectFactoryTest extends TestCase
27
{
28
    /** @var MockObject */
29
    protected $inflector;
30
31
    /** @var MockObject */
32
    protected $signatureChecker;
33
34
    /** @var ClassSignatureGeneratorInterface|MockObject */
35
    private $classSignatureGenerator;
36
37
    /** @var Configuration|MockObject */
38
    protected $config;
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    protected function setUp() : void
44
    {
45
        $this->config                  = $this->createMock(Configuration::class);
46
        $this->inflector               = $this->createMock(ClassNameInflectorInterface::class);
47
        $this->signatureChecker        = $this->createMock(SignatureCheckerInterface::class);
48
        $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
49
50
        $this
51
            ->config
52
            ->method('getClassNameInflector')
53
            ->willReturn($this->inflector);
54
55
        $this
56
            ->config
57
            ->method('getSignatureChecker')
58
            ->willReturn($this->signatureChecker);
59
60
        $this
61
            ->config
62
            ->method('getClassSignatureGenerator')
63
            ->willReturn($this->classSignatureGenerator);
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     *
69
     * @covers \ProxyManager\Factory\NullObjectFactory::__construct
70
     * @covers \ProxyManager\Factory\NullObjectFactory::createProxy
71
     * @covers \ProxyManager\Factory\NullObjectFactory::getGenerator
72
     */
73
    public function testWillSkipAutoGeneration() : void
74
    {
75
        $instance = new stdClass();
76
77
        $this
78
            ->inflector
79
            ->expects(self::once())
80
            ->method('getProxyClassName')
81
            ->with('stdClass')
82
            ->willReturn(NullObjectMock::class);
83
84
        $factory = new NullObjectFactory($this->config);
85
        /** @var NullObjectMock $proxy */
86
        $proxy = $factory->createProxy($instance);
87
88
        self::assertInstanceOf(NullObjectMock::class, $proxy);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     *
94
     * @covers \ProxyManager\Factory\NullObjectFactory::__construct
95
     * @covers \ProxyManager\Factory\NullObjectFactory::createProxy
96
     * @covers \ProxyManager\Factory\NullObjectFactory::getGenerator
97
     *
98
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
99
     */
100
    public function testWillTryAutoGeneration() : void
101
    {
102
        $instance       = new stdClass();
103
        $proxyClassName = UniqueIdentifierGenerator::getIdentifier('bar');
104
        $generator      = $this->createMock(GeneratorStrategyInterface::class);
105
        $autoloader     = $this->createMock(AutoloaderInterface::class);
106
107
        $this->config->method('getGeneratorStrategy')->will(self::returnValue($generator));
0 ignored issues
show
Bug introduced by
The method method() 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...
108
        $this->config->method('getProxyAutoloader')->will(self::returnValue($autoloader));
0 ignored issues
show
Bug introduced by
The method method() 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...
109
110
        $generator
111
            ->expects(self::once())
112
            ->method('generate')
113
            ->with(
114
                self::callback(
115
                    static function (ClassGenerator $targetClass) use ($proxyClassName) : bool {
116
                        return $targetClass->getName() === $proxyClassName;
117
                    }
118
                )
119
            );
120
121
        // simulate autoloading
122
        $autoloader
123
            ->expects(self::once())
124
            ->method('__invoke')
125
            ->with($proxyClassName)
126
            ->willReturnCallback(static function () use ($proxyClassName) : bool {
127
                eval('class ' . $proxyClassName . ' extends \\ProxyManagerTestAsset\\NullObjectMock {}');
128
129
                return true;
130
            });
131
132
        $this
133
            ->inflector
134
            ->expects(self::once())
135
            ->method('getProxyClassName')
136
            ->with('stdClass')
137
            ->willReturn($proxyClassName);
138
139
        $this
140
            ->inflector
141
            ->expects(self::once())
142
            ->method('getUserClassName')
143
            ->with('stdClass')
144
            ->willReturn(NullObjectMock::class);
145
146
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
147
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<ProxyManager\Sign...tureGeneratorInterface>.

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...
148
149
        $factory = new NullObjectFactory($this->config);
150
        $factory->createProxy($instance);
151
    }
152
}
153