1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Psi\Bridge\ObjectAgent\Doctrine\Collections\Store; |
9
|
|
|
|
10
|
|
|
class StoreTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* It should throw an exception if an object is not the correct class. |
14
|
|
|
* |
15
|
|
|
* @expectedException \InvalidArgumentException |
16
|
|
|
* @expectedExceptionMessage All objects in collection must be of class "Psi\Bridge\ObjectAgent\Doctrine\Collections\Tests\Unit\TestClass |
17
|
|
|
*/ |
18
|
|
|
public function testExceptionIntruder() |
19
|
|
|
{ |
20
|
|
|
new Store([ |
21
|
|
|
TestClass::class => [ |
22
|
|
|
new TestClass(), |
23
|
|
|
new \stdClass(), |
24
|
|
|
], |
25
|
|
|
]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* It should throw an exception if getting a collection for an unknown class. |
30
|
|
|
* |
31
|
|
|
* @expectedException \InvalidArgumentException |
32
|
|
|
* @expectedExceptionMessage No collections available of class "stdClass" |
33
|
|
|
*/ |
34
|
|
|
public function testGetCollectionException() |
35
|
|
|
{ |
36
|
|
|
$store = new Store([]); |
37
|
|
|
$store->getCollection(\stdClass::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* It should create a non-existing collection. |
42
|
|
|
*/ |
43
|
|
|
public function testGetOrCreateCollection() |
44
|
|
|
{ |
45
|
|
|
$store = new Store([]); |
46
|
|
|
$created = $store->getOrCreateCollection(\stdClass::class); |
47
|
|
|
$collection = $store->getOrCreateCollection(\stdClass::class); |
48
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $collection); |
49
|
|
|
$this->assertSame($created, $collection); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* It should remove an object. |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$store = new Store([ |
58
|
|
|
\stdClass::class => [ |
59
|
|
|
$target = new \stdClass(), |
60
|
|
|
new \stdClass(), |
61
|
|
|
new \stdClass(), |
62
|
|
|
], |
63
|
|
|
]); |
64
|
|
|
$store->remove($target); |
65
|
|
|
$collection = $store->getCollection(\stdClass::class); |
66
|
|
|
$this->assertCount(2, $collection); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* It should find an object. |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function testFind() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$store = new Store([ |
75
|
|
|
\stdClass::class => [ |
76
|
|
|
$target = new \stdClass(), |
77
|
|
|
new \stdClass(), |
78
|
|
|
new \stdClass(), |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
$object = $store->find(\stdClass::class, 0); |
82
|
|
|
$this->assertInstanceOf(\stdClass::class, $object); |
83
|
|
|
$this->assertSame($target, $object); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* It should say if it has a collection or not. |
88
|
|
|
*/ |
89
|
|
|
public function testHasCollection() |
90
|
|
|
{ |
91
|
|
|
$store = new Store([ |
92
|
|
|
\stdClass::class => [], |
93
|
|
|
]); |
94
|
|
|
$this->assertTrue($store->hasCollection(\stdClass::class)); |
95
|
|
|
$this->assertFalse($store->hasCollection(TestClass::class)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
class TestClass |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.