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

Factory/AbstractBaseFactoryTest.php (1 issue)

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\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
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 $factory;
38
39
    /** @var ProxyGeneratorInterface|MockObject */
40
    private $generator;
41
42
    /** @var ClassNameInflectorInterface|MockObject */
43
    private $classNameInflector;
44
45
    /** @var GeneratorStrategyInterface|MockObject */
46
    private $generatorStrategy;
47
48
    /** @var AutoloaderInterface|MockObject */
49
    private $proxyAutoloader;
50
51
    /** @var SignatureCheckerInterface|MockObject */
52
    private $signatureChecker;
53
54
    /** @var ClassSignatureGeneratorInterface|MockObject */
55
    private $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
            ->expects(self::any())
72
            ->method('getClassNameInflector')
73
            ->will(self::returnValue($this->classNameInflector));
74
75
        $configuration
76
            ->expects(self::any())
77
            ->method('getGeneratorStrategy')
78
            ->will(self::returnValue($this->generatorStrategy));
79
80
        $configuration
81
            ->expects(self::any())
82
            ->method('getProxyAutoloader')
83
            ->will(self::returnValue($this->proxyAutoloader));
84
85
        $configuration
86
            ->expects(self::any())
87
            ->method('getSignatureChecker')
88
            ->will(self::returnValue($this->signatureChecker));
89
90
        $configuration
91
            ->expects(self::any())
92
            ->method('getClassSignatureGenerator')
93
            ->will(self::returnValue($this->classSignatureGenerator));
94
95
        $this
96
            ->classNameInflector
97
            ->expects(self::any())
98
            ->method('getUserClassName')
99
            ->will(self::returnValue('stdClass'));
100
101
        $this->factory = $this->getMockForAbstractClass(AbstractBaseFactory::class, [$configuration]);
102
103
        $this->factory->expects(self::any())->method('getGenerator')->will(self::returnValue($this->generator));
104
    }
105
106
    public function testGeneratesClass() : void
107
    {
108
        $generateProxy = new ReflectionMethod($this->factory, 'generateProxy');
109
110
        $generateProxy->setAccessible(true);
111
        $generatedClass = UniqueIdentifierGenerator::getIdentifier('fooBar');
112
113
        $this
0 ignored issues
show
The method expects() does not seem to exist on object<ProxyManager\Infl...NameInflectorInterface>.

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...
114
            ->classNameInflector
115
            ->expects(self::any())
116
            ->method('getProxyClassName')
117
            ->with('stdClass')
118
            ->will(self::returnValue($generatedClass));
119
120
        $this
121
            ->generatorStrategy
122
            ->expects(self::once())
123
            ->method('generate')
124
            ->with(self::isInstanceOf(ClassGenerator::class));
125
        $this
126
            ->proxyAutoloader
127
            ->expects(self::once())
128
            ->method('__invoke')
129
            ->with($generatedClass)
130
            ->will(self::returnCallback(static function ($className) : bool {
131
                eval('class ' . $className . ' {}');
132
133
                return true;
134
            }));
135
136
        $this->signatureChecker->expects(self::atLeastOnce())->method('checkSignature');
137
        $this->classSignatureGenerator->expects(self::once())->method('addSignature')->will(self::returnArgument(0));
138
        $this
139
            ->generator
140
            ->expects(self::once())
141
            ->method('generate')
142
            ->with(
143
                self::callback(static function (ReflectionClass $reflectionClass) : bool {
144
                    return $reflectionClass->getName() === 'stdClass';
145
                }),
146
                self::isInstanceOf(ClassGenerator::class),
147
                ['some' => 'proxy', 'options' => 'here']
148
            );
149
150
        self::assertSame(
151
            $generatedClass,
152
            $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
153
        );
154
        self::assertTrue(class_exists($generatedClass, false));
155
        self::assertSame(
156
            $generatedClass,
157
            $generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
158
        );
159
    }
160
}
161