Completed
Pull Request — master (#50)
by Jonathan
04:15 queued 01:55
created

ObjectManagerDecoratorTest::testGetRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Persistence;
4
5
use Doctrine\Persistence\Mapping\ClassMetadata;
6
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
7
use Doctrine\Persistence\ObjectManager;
8
use Doctrine\Persistence\ObjectManagerDecorator;
9
use Doctrine\Persistence\ObjectRepository;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
13
class ObjectManagerDecoratorTest extends TestCase
14
{
15
    /** @var ObjectManager|MockObject */
16
    private $wrapped;
17
18
    /** @var NullObjectManagerDecorator */
19
    private $decorated;
20
21
    protected function setUp() : void
22
    {
23
        $this->wrapped   = $this->createMock(ObjectManager::class);
24
        $this->decorated = new NullObjectManagerDecorator($this->wrapped);
25
    }
26
27
    public function testFind() : void
28
    {
29
        $object = new TestObject();
30
31
        $this->wrapped->expects(self::once())
32
            ->method('find')
33
            ->with(TestObject::class, 1)
34
            ->willReturn($object);
35
36
        self::assertSame($object, $this->decorated->find(TestObject::class, 1));
37
    }
38
39
    public function testPersist() : void
40
    {
41
        $object = new TestObject();
42
43
        $this->wrapped->expects(self::once())
44
            ->method('persist')
45
            ->with($object);
46
47
        $this->decorated->persist($object);
48
    }
49
50
    public function testRemove() : void
51
    {
52
        $object = new TestObject();
53
54
        $this->wrapped->expects(self::once())
55
            ->method('remove')
56
            ->with($object);
57
58
        $this->decorated->remove($object);
59
    }
60
61
    public function testMerge() : void
62
    {
63
        $object1 = new TestObject();
64
        $object2 = new TestObject();
65
66
        $this->wrapped->expects(self::once())
67
            ->method('merge')
68
            ->with($object1)
69
            ->willReturn($object2);
70
71
        self::assertSame($object2, $this->decorated->merge($object1));
72
    }
73
74
    public function testClearWithNoArgument() : void
75
    {
76
        $this->wrapped->expects(self::once())
77
            ->method('clear');
78
79
        $this->decorated->clear();
80
    }
81
82
    public function testClearWithArgument() : void
83
    {
84
        $this->wrapped->expects(self::once())
85
            ->method('clear')
86
            ->with(TestObject::class);
87
88
        $this->decorated->clear(TestObject::class);
89
    }
90
91
    public function testDetach() : void
92
    {
93
        $object = new TestObject();
94
95
        $this->wrapped->expects(self::once())
96
            ->method('detach')
97
            ->with($object);
98
99
        $this->decorated->detach($object);
100
    }
101
102
    public function testRefresh() : void
103
    {
104
        $object = new TestObject();
105
106
        $this->wrapped->expects(self::once())
107
            ->method('refresh')
108
            ->with($object);
109
110
        $this->decorated->refresh($object);
111
    }
112
113
    public function testFlush() : void
114
    {
115
        $this->wrapped->expects(self::once())
116
            ->method('flush');
117
118
        $this->decorated->flush();
119
    }
120
121
    public function testGetRepository() : void
122
    {
123
        $repository = $this->createMock(ObjectRepository::class);
124
125
        $this->wrapped->expects(self::once())
126
            ->method('getRepository')
127
            ->with(TestObject::class)
128
            ->willReturn($repository);
129
130
        self::assertSame($repository, $this->decorated->getRepository(TestObject::class));
131
    }
132
133
    public function testGetClassMetadata() : void
134
    {
135
        $classMetadata = $this->createMock(ClassMetadata::class);
136
137
        $this->wrapped->expects(self::once())
138
            ->method('getClassMetadata')
139
            ->with(TestObject::class)
140
            ->willReturn($classMetadata);
141
142
        self::assertSame($classMetadata, $this->decorated->getClassMetadata(TestObject::class));
143
    }
144
145
    public function testGetClassMetadataFactory() : void
146
    {
147
        $classMetadataFactory = $this->createMock(ClassMetadataFactory::class);
148
149
        $this->wrapped->expects(self::once())
150
            ->method('getMetadataFactory')
151
            ->willReturn($classMetadataFactory);
152
153
        self::assertSame($classMetadataFactory, $this->decorated->getMetadataFactory());
154
    }
155
156
    public function testInitializeObject() : void
157
    {
158
        $object = new TestObject();
159
160
        $this->wrapped->expects(self::once())
161
            ->method('initializeObject')
162
            ->with($object);
163
164
        $this->decorated->initializeObject($object);
165
    }
166
167
    public function testContains() : void
168
    {
169
        $object = new TestObject();
170
171
        $this->wrapped->expects(self::once())
172
            ->method('contains')
173
            ->with($object)
174
            ->willReturn(true);
175
176
        self::assertTrue($this->decorated->contains($object));
177
    }
178
}
179
180
class NullObjectManagerDecorator extends ObjectManagerDecorator
181
{
182
    /**
183
     * @param ObjectManager|MockObject $wrapped
184
     */
185
    public function __construct($wrapped)
186
    {
187
        $this->wrapped = $wrapped;
0 ignored issues
show
Documentation Bug introduced by
It seems like $wrapped can also be of type PHPUnit\Framework\MockObject\MockObject. However, the property $wrapped is declared as type Doctrine\Persistence\ObjectManager. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
188
    }
189
}
190