shouldHydrateAnEntityWithServices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 29
rs 9.6333
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Tests\Hydration;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Hydration\EntityHydrator;
15
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
16
use Addiks\RDMBundle\Mapping\Annotation\Service;
17
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
18
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
19
use ErrorException;
20
use Addiks\RDMBundle\Mapping\ServiceMapping;
21
use Addiks\RDMBundle\Mapping\EntityMapping;
22
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
0 ignored issues
show
Bug introduced by
The type Addiks\RDMBundle\ValueRe...\ValueResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
24
use Addiks\RDMBundle\DataLoader\DataLoaderInterface;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
27
use Addiks\RDMBundle\Mapping\MappingInterface;
28
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
29
use Symfony\Component\DependencyInjection\ContainerInterface;
30
31
final class EntityHydratorTest extends TestCase
32
{
33
34
    /**
35
     * @var EntityHydrator
36
     */
37
    private $hydrator;
38
39
    /**
40
     * @var MappingDriverInterface
41
     */
42
    private $mappingDriver;
43
44
    /**
45
     * @var DataLoaderInterface
46
     */
47
    private $dbalDataLoader;
48
49
    /**
50
     * @var ContainerInterface
51
     */
52
    private $container;
53
54
    public function setUp(): void
55
    {
56
        $this->mappingDriver = $this->createMock(MappingDriverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...DriverInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping...\MappingDriverInterface of property $mappingDriver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->dbalDataLoader = $this->createMock(DataLoaderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...LoaderInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\DataLoader\DataLoaderInterface of property $dbalDataLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Depend...tion\ContainerInterface of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
60
        $this->hydrator = new EntityHydrator(
61
            $this->mappingDriver,
62
            $this->dbalDataLoader
63
        );
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function shouldHydrateAnEntityWithServices()
70
    {
71
        $fooMapping = $this->createMock(MappingInterface::class);
72
        $barMapping = $this->createMock(MappingInterface::class);
73
        $fazMapping = $this->createMock(MappingInterface::class);
74
75
        $this->mappingDriver->method("loadRDMMetadataForClass")->willReturn(
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Mapping...\MappingDriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        $this->mappingDriver->/** @scrutinizer ignore-call */ 
76
                              method("loadRDMMetadataForClass")->willReturn(

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
            new EntityMapping(EntityExample::class, [
77
                'foo' => $fooMapping,
78
                'bar' => $barMapping,
79
                'faz' => $fazMapping
80
            ])
81
        );
82
83
        $serviceA = new ServiceExample("SomeService", 123);
84
        $serviceB = new ServiceExample("AnotherService", 456);
85
        $serviceC = new ServiceExample("PrivateService", 789);
86
87
        $entity = new EntityExample();
88
89
        $fooMapping->method("resolveValue")->willReturn($serviceA);
90
        $barMapping->method("resolveValue")->willReturn($serviceB);
91
        $fazMapping->method("resolveValue")->willReturn($serviceC);
92
93
        $this->hydrator->hydrateEntity($entity, $this->createMock(EntityManagerInterface::class));
94
95
        $this->assertEquals($serviceA, $entity->foo);
96
        $this->assertEquals($serviceB, $entity->bar);
97
        $this->assertEquals($serviceC, $entity->getFaz());
98
    }
99
100
    /**
101
     * @test
102
     */
103
    public function shouldAssertHydration()
104
    {
105
        $fazMapping = $this->createMock(MappingInterface::class);
106
107
        /** @var ServiceExample $fooService */
108
        $fazService = $this->createMock(ServiceExample::class);
109
110
        $entity = new EntityExample(null, null, null, $fazService);
111
112
        $this->mappingDriver->method("loadRDMMetadataForClass")->willReturn(
113
            new EntityMapping(EntityExample::class, [
114
                'faz' => $fazMapping,
115
            ])
116
        );
117
118
        $fazMapping->expects($this->once())->method("assertValue")->with(
119
            $this->isInstanceOf(HydrationContextInterface::class),
120
            $this->equalTo([]),
121
            $this->equalTo($fazService)
122
        );
123
124
        $this->hydrator->assertHydrationOnEntity($entity, $this->createMock(EntityManagerInterface::class));
125
    }
126
127
}
128