|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-object |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Object |
|
8
|
|
|
* @subpackage Apparat\Object\Tests |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Object\Tests; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Object\Application\Model\Object\Article; |
|
40
|
|
|
use Apparat\Object\Application\Model\Object\Contact; |
|
41
|
|
|
use Apparat\Object\Domain\Factory\SelectorFactory; |
|
42
|
|
|
use Apparat\Object\Domain\Model\Object\Collection; |
|
43
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Object collection tests |
|
47
|
|
|
* |
|
48
|
|
|
* @package Apparat\Kernel |
|
49
|
|
|
* @subpackage Apparat\Object\Tests |
|
50
|
|
|
*/ |
|
51
|
|
|
class CollectionTest extends AbstractRepositoryEnabledTest |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Test an object collection |
|
55
|
|
|
* |
|
56
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException |
|
57
|
|
|
* @expectedExceptionCode 1456530074 |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testObjectCollection() |
|
60
|
|
|
{ |
|
61
|
|
|
$selector = SelectorFactory::createFromString('/2015/*/*/*.article'); |
|
62
|
|
|
$collection = self::$repository->findObjects($selector); |
|
63
|
|
|
$this->assertInstanceOf(Collection::class, $collection); |
|
64
|
|
|
$this->assertTrue(is_int(count($collection))); |
|
65
|
|
|
$this->assertTrue(count($collection) == 1); |
|
66
|
|
|
|
|
67
|
|
|
$uid = 1; |
|
68
|
|
|
foreach ($collection as $uid => $article) { |
|
69
|
|
|
$this->assertInstanceOf(Article::class, $article); |
|
70
|
|
|
$this->assertEquals($uid, $article->getId()->getId()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->assertTrue(isset($collection[$uid])); |
|
74
|
|
|
$this->assertInstanceOf(Article::class, $collection[$uid]); |
|
75
|
|
|
|
|
76
|
|
|
// Load a contact object |
|
77
|
|
|
$contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2'); |
|
78
|
|
|
$contactObject = self::$repository->loadObject($contactObjectPath); |
|
79
|
|
|
$this->assertInstanceOf(Contact::class, $contactObject); |
|
80
|
|
|
$collection[$uid] = $contactObject; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Test unsetting an object from a collection by index |
|
85
|
|
|
* |
|
86
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException |
|
87
|
|
|
* @expectedExceptionCode 1456530074 |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testObjectCollectionUnset() |
|
90
|
|
|
{ |
|
91
|
|
|
$selector = SelectorFactory::createFromString('/2015/*/*/*.article'); |
|
92
|
|
|
$collection = self::$repository->findObjects($selector); |
|
93
|
|
|
unset($collection[0]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Test adding an object to a collection |
|
98
|
|
|
* |
|
99
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException |
|
100
|
|
|
* @expectedExceptionCode 1450131914 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testObjectCollectionAdd() |
|
103
|
|
|
{ |
|
104
|
|
|
// Load a contact object |
|
105
|
|
|
$contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2'); |
|
106
|
|
|
$contactObject = self::$repository->loadObject($contactObjectPath); |
|
107
|
|
|
$this->assertInstanceOf(Contact::class, $contactObject); |
|
108
|
|
|
|
|
109
|
|
|
// Load a collection |
|
110
|
|
|
$selector = SelectorFactory::createFromString('/2015/*/*/*.article'); |
|
111
|
|
|
$collection = self::$repository->findObjects($selector); |
|
112
|
|
|
$this->assertInstanceOf(Collection::class, $collection); |
|
113
|
|
|
$articleCount = count($collection); |
|
114
|
|
|
|
|
115
|
|
|
// Add the contact to the collection |
|
116
|
|
|
$collection = $collection->add($contactObject); |
|
117
|
|
|
$this->assertEquals($articleCount + 1, count($collection)); |
|
118
|
|
|
|
|
119
|
|
|
// Add another object by repository path |
|
120
|
|
|
/** @var Collection $collection */ |
|
121
|
|
|
$collection = $collection->add(new RepositoryPath(self::$repository, '/2016/02/07/3.article/3')); |
|
122
|
|
|
$this->assertEquals($articleCount + 2, count($collection)); |
|
123
|
|
|
|
|
124
|
|
|
// Add invalid object |
|
125
|
|
|
$collection->add(null); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Test removing an object from a collection |
|
130
|
|
|
* |
|
131
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException |
|
132
|
|
|
* @expectedExceptionCode 1448737190 |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testObjectCollectionRemove() |
|
135
|
|
|
{ |
|
136
|
|
|
// Load a contact object |
|
137
|
|
|
$contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2'); |
|
138
|
|
|
$contactObject = self::$repository->loadObject($contactObjectPath); |
|
139
|
|
|
$this->assertInstanceOf(Contact::class, $contactObject); |
|
140
|
|
|
|
|
141
|
|
|
// Load a collection |
|
142
|
|
|
$selector = SelectorFactory::createFromString('/2015/*/*/*.article'); |
|
143
|
|
|
$collection = self::$repository->findObjects($selector); |
|
144
|
|
|
$this->assertInstanceOf(Collection::class, $collection); |
|
145
|
|
|
$articleCount = count($collection); |
|
146
|
|
|
|
|
147
|
|
|
$article = null; |
|
148
|
|
|
foreach ($collection as $article) { |
|
149
|
|
|
break; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($article instanceof Article) { |
|
153
|
|
|
// Add the contact to the collection |
|
154
|
|
|
/** @var Collection $collection */ |
|
155
|
|
|
$collection = $collection->add($contactObject); |
|
156
|
|
|
$this->assertEquals($articleCount + 1, count($collection)); |
|
157
|
|
|
|
|
158
|
|
|
// Remove the article |
|
159
|
|
|
$collection = $collection->remove($article); |
|
160
|
|
|
$this->assertEquals($articleCount, count($collection)); |
|
161
|
|
|
|
|
162
|
|
|
// Remove the contact by ID |
|
163
|
|
|
$collection = $collection->remove(2); |
|
164
|
|
|
$this->assertEquals($articleCount - 1, count($collection)); |
|
165
|
|
|
|
|
166
|
|
|
// Remove invalid object |
|
167
|
|
|
$collection->remove('invalid'); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Test appending two object collections |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testObjectCollectionAppend() |
|
175
|
|
|
{ |
|
176
|
|
|
// Load an article collection |
|
177
|
|
|
$articles = self::$repository->findObjects(SelectorFactory::createFromString('/2015/*/*/*.article')); |
|
178
|
|
|
$articleCount = count($articles); |
|
179
|
|
|
$article = null; |
|
180
|
|
|
foreach ($articles as $article) { |
|
181
|
|
|
break; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// Load a contact collection |
|
185
|
|
|
$contacts = self::$repository->findObjects(SelectorFactory::createFromString('/2016/01')); |
|
186
|
|
|
$contactCount = count($contacts); |
|
187
|
|
|
$contact = null; |
|
188
|
|
|
foreach ($contacts as $contact) { |
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Append the contacts collection to the articles collection |
|
193
|
|
|
$combined = $articles->append($contacts); |
|
194
|
|
|
$this->assertEquals($articleCount + $contactCount, count($combined)); |
|
195
|
|
|
$this->assertInstanceOf(Article::class, $combined[$article->getId()->getId()]); |
|
196
|
|
|
$this->assertInstanceOf(Contact::class, $combined[$contact->getId()->getId()]); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|