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

AbstractBaseFactoryTest::testGeneratesClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.0254
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\AbstractBaseFactory;
12
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
13
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
14
use ProxyManager\Inflector\ClassNameInflectorInterface;
15
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
16
use ProxyManager\Signature\ClassSignatureGeneratorInterface;
17
use ProxyManager\Signature\SignatureCheckerInterface;
18
use ReflectionClass;
19
use ReflectionMethod;
20
use stdClass;
21
use Zend\Code\Generator\ClassGenerator;
22
use function class_exists;
23
24
/**
25
 * Tests for {@see \ProxyManager\Factory\AbstractBaseFactory}
26
 *
27
 * @covers \ProxyManager\Factory\AbstractBaseFactory
28
 * @group Coverage
29
 */
30
final class AbstractBaseFactoryTest extends TestCase
31
{
32
    /**
33
     * Note: we mock the class in order to assert on the abstract method usage
34
     *
35
     * @var AbstractBaseFactory|MockObject
36
     */
37
    private AbstractBaseFactory $factory;
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...
38
39
    /** @var ProxyGeneratorInterface&MockObject */
40
    private ProxyGeneratorInterface $generator;
41
42
    /** @var ClassNameInflectorInterface&MockObject */
43
    private ClassNameInflectorInterface $classNameInflector;
44
45
    /** @var GeneratorStrategyInterface&MockObject */
46
    private GeneratorStrategyInterface $generatorStrategy;
47
48
    /** @var AutoloaderInterface&MockObject */
49
    private AutoloaderInterface $proxyAutoloader;
50
51
    /** @var SignatureCheckerInterface&MockObject */
52
    private SignatureCheckerInterface $signatureChecker;
53
54
    /** @var ClassSignatureGeneratorInterface&MockObject */
55
    private ClassSignatureGeneratorInterface $classSignatureGenerator;
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    protected function setUp() : void
61
    {
62
        $configuration                 = $this->createMock(Configuration::class);
63
        $this->generator               = $this->createMock(ProxyGeneratorInterface::class);
64
        $this->classNameInflector      = $this->createMock(ClassNameInflectorInterface::class);
65
        $this->generatorStrategy       = $this->createMock(GeneratorStrategyInterface::class);
66
        $this->proxyAutoloader         = $this->createMock(AutoloaderInterface::class);
67
        $this->signatureChecker        = $this->createMock(SignatureCheckerInterface::class);
68
        $this->classSignatureGenerator = $this->createMock(ClassSignatureGeneratorInterface::class);
69
70
        $configuration
71
            ->method('getClassNameInflector')
72
            ->willReturn($this->classNameInflector);
73
74
        $configuration
75
            ->method('getGeneratorStrategy')
76
            ->willReturn($this->generatorStrategy);
77
78
        $configuration
79
            ->method('getProxyAutoloader')
80
            ->willReturn($this->proxyAutoloader);
81
82
        $configuration
83
            ->method('getSignatureChecker')
84
            ->willReturn($this->signatureChecker);
85
86
        $configuration
87
            ->method('getClassSignatureGenerator')
88
            ->willReturn($this->classSignatureGenerator);
89
90
        $this
91
            ->classNameInflector
92
            ->method('getUserClassName')
93
            ->willReturn('stdClass');
94
95
        $this->factory = $this->getMockForAbstractClass(AbstractBaseFactory::class, [$configuration]);
96
97
        $this->factory->method('getGenerator')->willReturn($this->generator);
98
    }
99
100
    public function testGeneratesClass() : void
101
    {
102
        $generateProxy = new ReflectionMethod($this->factory, 'generateProxy');
103
104
        $generateProxy->setAccessible(true);
105
        $generatedClass = UniqueIdentifierGenerator::getIdentifier('fooBar');
106
107
        $this
108
            ->classNameInflector
109
            ->method('getProxyClassName')
110
            ->with('stdClass')
111
            ->willReturn($generatedClass);
112
113
        $this
114
            ->generatorStrategy
115
            ->expects(self::once())
116
            ->method('generate')
117
            ->with(self::isInstanceOf(ClassGenerator::class));
118
        $this
119
            ->proxyAutoloader
120
            ->expects(self::once())
121
            ->method('__invoke')
122
            ->with($generatedClass)
123
            ->will(self::returnCallback(static function ($className) : bool {
124
                eval('class ' . $className . ' {}');
125
126
                return true;
127
            }));
128
129
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
130
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
131
        $this
132
            ->generator
133
            ->expects(self::once())
134
            ->method('generate')
135
            ->with(
136
                self::callback(static function (ReflectionClass $reflectionClass) : bool {
137
                    return $reflectionClass->getName() === 'stdClass';
138
                }),
139
                self::isInstanceOf(ClassGenerator::class),
140
                ['some' => 'proxy', 'options' => 'here']
141
            );
142
143
        self::assertSame(
144
            $generatedClass,
145
            $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
146
        );
147
        self::assertTrue(class_exists($generatedClass, false));
148
        self::assertSame(
149
            $generatedClass,
150
            $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
151
        );
152
    }
153
}
154