NullObjectGeneratorTest::testGeneratesValidCode()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.1575
c 0
b 0
f 0
cc 7
nc 24
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\ProxyGenerator;
6
7
use ProxyManager\Generator\ClassGenerator;
8
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
9
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
10
use ProxyManager\Proxy\NullObjectInterface;
11
use ProxyManager\ProxyGenerator\NullObjectGenerator;
12
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
13
use ProxyManager\ProxyGenerator\Util\Properties;
14
use ProxyManagerTestAsset\BaseClass;
15
use ProxyManagerTestAsset\BaseInterface;
16
use ProxyManagerTestAsset\ClassWithByRefMagicMethods;
17
use ProxyManagerTestAsset\ClassWithMagicMethods;
18
use ProxyManagerTestAsset\ClassWithMixedProperties;
19
use ProxyManagerTestAsset\ClassWithMixedReferenceableTypedProperties;
20
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
21
use ReflectionClass;
22
use ReflectionMethod;
23
24
/**
25
 * Tests for {@see \ProxyManager\ProxyGenerator\NullObjectGenerator}
26
 *
27
 * @covers \ProxyManager\ProxyGenerator\NullObjectGenerator
28
 * @group Coverage
29
 */
30
final class NullObjectGeneratorTest extends AbstractProxyGeneratorTest
31
{
32
    /**
33
     * @dataProvider getTestedImplementations
34
     *
35
     * Verifies that generated code is valid and implements expected interfaces
36
     * @psalm-param class-string $className
37
     * @psalm-suppress MoreSpecificImplementedParamType
38
     */
39
    public function testGeneratesValidCode(string $className) : void
40
    {
41
        $generator          = $this->getProxyGenerator();
42
        $generatedClassName = UniqueIdentifierGenerator::getIdentifier('AbstractProxyGeneratorTest');
43
        $generatedClass     = new ClassGenerator($generatedClassName);
44
        $originalClass      = new ReflectionClass($className);
45
        $generatorStrategy  = new EvaluatingGeneratorStrategy();
46
47
        $generator->generate($originalClass, $generatedClass);
48
        $generatorStrategy->generate($generatedClass);
49
50
        $generatedReflection = new ReflectionClass($generatedClassName);
51
52
        if ($originalClass->isInterface()) {
53
            self::assertTrue($generatedReflection->implementsInterface($className));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
54
        }
55
56
        self::assertSame($generatedClassName, $generatedReflection->getName());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
57
58
        foreach ($this->getExpectedImplementedInterfaces() as $interface) {
59
            self::assertTrue($generatedReflection->implementsInterface($interface));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
60
        }
61
62
        /**
63
         * @psalm-suppress InvalidStringClass
64
         * @psalm-suppress MixedAssignment
65
         * @psalm-suppress MixedMethodCall
66
         */
67
        $proxy = $generatedClassName::staticProxyConstructor();
68
69
        self::assertInstanceOf($className, $proxy);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
70
71
        foreach (Properties::fromReflectionClass($generatedReflection)
72
                ->onlyNullableProperties()
73
                ->getPublicProperties() as $property) {
74
            /** @psalm-suppress MixedPropertyFetch */
75
            self::assertNull($proxy->{$property->getName()});
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
76
        }
77
78
        foreach ($generatedReflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
79
            if ($method->getNumberOfParameters() || $method->isStatic()) {
80
                continue;
81
            }
82
83
            $callback = [$proxy, $method->getName()];
84
85
            self::assertIsCallable($callback);
0 ignored issues
show
Bug introduced by
The method assertIsCallable() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
86
            self::assertNull($callback());
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<ProxyManagerTest\...ullObjectGeneratorTest>.

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...
87
        }
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    protected function getProxyGenerator() : ProxyGeneratorInterface
94
    {
95
        return new NullObjectGenerator();
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    protected function getExpectedImplementedInterfaces() : array
102
    {
103
        return [
104
            NullObjectInterface::class,
105
        ];
106
    }
107
108
    /**
109
     * @psalm-return array<int, array<int, class-string>>
110
     */
111
    public function getTestedImplementations() : array
112
    {
113
        return [
114
            [BaseClass::class],
115
            [ClassWithMagicMethods::class],
116
            [ClassWithByRefMagicMethods::class],
117
            [ClassWithMixedProperties::class],
118
            [ClassWithMixedTypedProperties::class],
119
            [ClassWithMixedReferenceableTypedProperties::class],
120
            [BaseInterface::class],
121
        ];
122
    }
123
}
124