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