HydratorFactoryTest::testWillSkipAutoGeneration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydratorTest\Factory;
6
7
use CodeGenerationUtils\Autoloader\AutoloaderInterface;
8
use CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface;
9
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface;
10
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;
11
use GeneratedHydrator\ClassGenerator\DefaultHydratorGenerator;
12
use GeneratedHydrator\Configuration;
13
use GeneratedHydrator\Factory\HydratorFactory;
14
use GeneratedHydratorTestAsset\LazyLoadingMock;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Tests for {@see \GeneratedHydrator\Factory\HydratorFactory}
20
 */
21
class HydratorFactoryTest extends TestCase
22
{
23
    /** @var MockObject */
24
    protected $inflector;
25
26
    /** @var Configuration|MockObject */
27
    protected $config;
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function setUp() : void
33
    {
34
        $this->inflector = $this->createMock(ClassNameInflectorInterface::class);
35
        $this->config    = $this
36
            ->getMockBuilder('GeneratedHydrator\Configuration')
37
            ->disableOriginalConstructor()
38
            ->getMock();
39
40
        $this
41
            ->config
42
            ->expects(self::any())
43
            ->method('getClassNameInflector')
44
            ->will(self::returnValue($this->inflector));
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @covers \GeneratedHydrator\Factory\HydratorFactory::__construct
51
     * @covers \GeneratedHydrator\Factory\HydratorFactory::getHydratorClass
52
     */
53
    public function testWillSkipAutoGeneration()
54
    {
55
        $className = UniqueIdentifierGenerator::getIdentifier('foo');
56
57
        $this->config->expects(self::any())->method('getHydratedClassName')->will(self::returnValue($className));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in GeneratedHydrator\Configuration.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
        $this->config->expects(self::any())->method('doesAutoGenerateProxies')->will(self::returnValue(false));
59
        $this
60
            ->inflector
61
            ->expects(self::any())
62
            ->method('getUserClassName')
63
            ->with($className)
64
            ->will(self::returnValue('GeneratedHydratorTestAsset\BaseClass'));
65
66
        $this
67
            ->inflector
68
            ->expects(self::once())
69
            ->method('getGeneratedClassName')
70
            ->with('GeneratedHydratorTestAsset\BaseClass')
71
            ->will(self::returnValue('GeneratedHydratorTestAsset\EmptyClass'));
72
73
        $factory        = new HydratorFactory($this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, GeneratedHydrator\Factor...rFactory::__construct() does only seem to accept object<GeneratedHydrator\Configuration>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
        $generatedClass = $factory->getHydratorClass();
75
76
        self::assertInstanceOf('GeneratedHydratorTestAsset\EmptyClass', new $generatedClass());
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     *
82
     * @covers \GeneratedHydrator\Factory\HydratorFactory::__construct
83
     * @covers \GeneratedHydrator\Factory\HydratorFactory::getHydratorClass
84
     *
85
     * NOTE: serious mocking going on in here (a class is generated on-the-fly) - careful
86
     */
87
    public function testWillTryAutoGeneration()
88
    {
89
        $className          = UniqueIdentifierGenerator::getIdentifier('foo');
90
        $generatedClassName = UniqueIdentifierGenerator::getIdentifier('bar');
91
        $generator          = $this->createMock(GeneratorStrategyInterface::class);
92
        $autoloader         = $this->createMock(AutoloaderInterface::class);
93
94
        $this->config->expects(self::any())->method('getHydratedClassName')->will(self::returnValue($className));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in GeneratedHydrator\Configuration.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
        $this->config->expects(self::any())->method('doesAutoGenerateProxies')->will(self::returnValue(true));
96
        $this->config->expects(self::any())->method('getGeneratorStrategy')->will(self::returnValue($generator));
97
        $this->config->expects(self::any())->method('getHydratorGenerator')->willReturn(new DefaultHydratorGenerator());
98
        $this
99
            ->config
100
            ->expects(self::any())
101
            ->method('getGeneratedClassAutoloader')
102
            ->will(self::returnValue($autoloader));
103
104
        $generator
105
            ->expects(self::once())
106
            ->method('generate')
107
            ->with(self::isType('array'));
108
109
        // simulate autoloading
110
        $autoloader
111
            ->expects(self::once())
112
            ->method('__invoke')
113
            ->with($generatedClassName)
114
            ->willReturnCallback(static function () use ($generatedClassName) : bool {
115
                eval('class ' . $generatedClassName . ' {}');
116
117
                return true;
118
            });
119
120
            $this
121
            ->inflector
122
            ->expects(self::once())
123
            ->method('getGeneratedClassName')
124
            ->with('GeneratedHydratorTestAsset\BaseClass')
125
            ->will(self::returnValue($generatedClassName));
126
127
            $this
128
            ->inflector
129
            ->expects(self::once())
130
            ->method('getUserClassName')
131
            ->with($className)
132
            ->will(self::returnValue('GeneratedHydratorTestAsset\BaseClass'));
133
134
            $factory = new HydratorFactory($this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, GeneratedHydrator\Factor...rFactory::__construct() does only seem to accept object<GeneratedHydrator\Configuration>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
135
        /** @var LazyLoadingMock $generatedClass */
136
            $generatedClass = $factory->getHydratorClass();
137
138
            self::assertInstanceOf($generatedClassName, new $generatedClass());
139
    }
140
}
141