Passed
Push — master ( 356755...14835f )
by Gerrit
04:25
created

shouldThrowExceptionWhenAccessUnknownRegistry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2017  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;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\RDMBundle\Hydration\HydrationContext;
15
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Addiks\RDMBundle\Exception\InvalidMappingException;
18
use Addiks\RDMBundle\Tests\ValueObjectExample;
19
use InvalidArgumentException;
20
21
final class HydrationContextTest extends TestCase
22
{
23
24
    /**
25
     * @var HydrationContext
26
     */
27
    private $context;
28
29
    /**
30
     * @var EntityExample
31
     */
32
    private $entity;
33
34
    /**
35
     * @var EntityManagerInterface
36
     */
37
    private $entityManager;
38
39
    public function setUp()
40
    {
41
        $this->entity = $this->createMock(EntityExample::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...n\EntityExample::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...ydration\EntityExample> of property $entity.

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...
42
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...anagerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManagerInterface> of property $entityManager.

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...
43
44
        $this->context = new HydrationContext(
45
            $this->entity,
46
            $this->entityManager
0 ignored issues
show
Documentation introduced by
$this->entityManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
        );
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function shouldStoreEntity()
54
    {
55
        $this->assertSame($this->entity, $this->context->getEntity());
56
        $this->assertTrue(is_subclass_of($this->context->getEntityClass(), EntityExample::class));
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Addiks\RDMBundle\Tests\...on\EntityExample::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function shouldStoreEntityManager()
63
    {
64
        $this->assertSame($this->entityManager, $this->context->getEntityManager());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function shouldHaveWorkingRegistry()
71
    {
72
        $this->assertFalse($this->context->hasRegisteredValue("some_registry_id"));
73
74
        $this->context->registerValue("some_registry_id", "Lorem ipsum");
75
76
        $this->assertTrue($this->context->hasRegisteredValue("some_registry_id"));
77
        $this->assertEquals("Lorem ipsum", $this->context->getRegisteredValue("some_registry_id"));
78
79
        $this->context->registerValue("some_registry_id", "dolor sit amet");
80
81
        $this->assertTrue($this->context->hasRegisteredValue("some_registry_id"));
82
        $this->assertEquals("dolor sit amet", $this->context->getRegisteredValue("some_registry_id"));
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function shouldMaintainObjectHydrationStack()
89
    {
90
        /** @var ValueObjectExample $object */
91
        $object = $this->createMock(ValueObjectExample::class);
92
93
        $this->assertEquals([$this->entity], $this->context->getObjectHydrationStack());
94
95
        $this->context->pushOnObjectHydrationStack($object);
96
97
        $this->assertEquals([$this->entity, $object], $this->context->getObjectHydrationStack());
98
99
        $this->assertSame($object, $this->context->popFromObjectHydrationStack());
100
101
        $this->assertEquals([$this->entity], $this->context->getObjectHydrationStack());
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function shouldThrowExceptionWhenPopFromEmptyObjectHydrationStack()
108
    {
109
        $this->expectException(InvalidArgumentException::class);
110
        $this->context->popFromObjectHydrationStack();
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function shouldThrowExceptionWhenAccessUnknownRegistry()
117
    {
118
        $this->expectException(InvalidMappingException::class);
119
        $this->context->getRegisteredValue("some_registry_id");
120
    }
121
122
}
123