1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Infrastructure |
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\Factory\ObjectFactory; |
40
|
|
|
use Apparat\Object\Application\Model\Object\Article; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\AbstractObject; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\ResourceInterface; |
43
|
|
|
use Apparat\Object\Domain\Model\Properties\SystemProperties; |
44
|
|
|
use Apparat\Object\Domain\Model\Uri\RepositoryLocator; |
45
|
|
|
use Apparat\Object\Infrastructure\Factory\AdapterStrategyFactory; |
46
|
|
|
use Apparat\Object\Infrastructure\Model\Object\Object; |
47
|
|
|
use Apparat\Object\Infrastructure\Repository\Repository as InfrastructureRepository; |
48
|
|
|
use Apparat\Object\Ports\Types\Object as ObjectTypes; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Object tests |
52
|
|
|
* |
53
|
|
|
* @package Apparat\Object |
54
|
|
|
* @subpackage Apparat\Object\Test |
55
|
|
|
*/ |
56
|
|
|
class ObjectTest extends AbstractRepositoryEnabledTest |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Example object locator |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
const OBJECT_LOCATOR = '/2015/12/21/1-article/1'; |
64
|
|
|
/** |
65
|
|
|
* Example hidden object locator |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
const HIDDEN_OBJECT_LOCATOR = '/2016/05/26/6-article/6'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Tears down the fixture |
73
|
|
|
*/ |
74
|
|
|
public function tearDown() |
75
|
|
|
{ |
76
|
|
|
TestTypeService::removeInvalidType(); |
77
|
|
|
parent::tearDown(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Test undefined object type |
82
|
|
|
* |
83
|
|
|
* @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException |
84
|
|
|
* @expectedExceptionCode 1450905868 |
85
|
|
|
*/ |
86
|
|
|
public function testUndefinedObjectType() |
87
|
|
|
{ |
88
|
|
|
$resource = $this->createMock(ResourceInterface::class); |
89
|
|
|
$resource->method('getPropertyData')->willReturn([]); |
90
|
|
|
$repositoryLocator = |
91
|
|
|
$this->getMockBuilder(RepositoryLocator::class)->disableOriginalConstructor()->getMock(); |
92
|
|
|
|
93
|
|
|
/** @var ResourceInterface $resource */ |
94
|
|
|
/** @var RepositoryLocator $repositoryLocator */ |
95
|
|
|
ObjectFactory::createFromResource($repositoryLocator, $resource); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test invalid object type |
100
|
|
|
* |
101
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException |
102
|
|
|
* @expectedExceptionCode 1449871242 |
103
|
|
|
*/ |
104
|
|
|
public function testInvalidObjectType() |
105
|
|
|
{ |
106
|
|
|
$resource = $this->createMock(ResourceInterface::class); |
107
|
|
|
$resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]); |
108
|
|
|
$articleObjectLocator = new RepositoryLocator(self::$repository, self::OBJECT_LOCATOR); |
109
|
|
|
|
110
|
|
|
/** @var ResourceInterface $resource */ |
111
|
|
|
ObjectFactory::createFromResource($articleObjectLocator, $resource); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Load an article object with an invalid visibility requirement |
116
|
|
|
* |
117
|
|
|
* @expectedException \Apparat\Object\Domain\Repository\InvalidArgumentException |
118
|
|
|
* @expectedExceptionCode 1449999646 |
119
|
|
|
*/ |
120
|
|
|
public function testLoadObjectInvalidVisibility() |
121
|
|
|
{ |
122
|
|
|
$articleObjectLocator = new RepositoryLocator(self::$repository, self::OBJECT_LOCATOR); |
123
|
|
|
self::$repository->loadObject($articleObjectLocator, 0); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Load a non-existing object |
128
|
|
|
* |
129
|
|
|
* @expectedException \Apparat\Object\Application\Model\Object\InvalidArgumentException |
130
|
|
|
* @expectedExceptionCode 1466882391 |
131
|
|
|
*/ |
132
|
|
|
public function testLoadNonExistingObject() |
133
|
|
|
{ |
134
|
|
|
AdapterStrategyFactory::setAdapterStrategyTypeClass( |
135
|
|
|
TestFileAdapterStrategy::TYPE, |
136
|
|
|
TestFileAdapterStrategy::class |
137
|
|
|
); |
138
|
|
|
$repository = InfrastructureRepository::register( |
139
|
|
|
'non-'.getenv('REPOSITORY_URL'), |
140
|
|
|
[ |
141
|
|
|
'type' => TestFileAdapterStrategy::TYPE, |
142
|
|
|
'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture', |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
$objectLocator = new RepositoryLocator($repository, self::OBJECT_LOCATOR); |
146
|
|
|
$repository->loadObject($objectLocator); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Test the object facade with an absolute object URL |
151
|
|
|
*/ |
152
|
|
|
public function testObjectAbsolute() |
153
|
|
|
{ |
154
|
|
|
$object = Object::load(getenv('APPARAT_BASE_URL').getenv('REPOSITORY_URL').self::OBJECT_LOCATOR); |
155
|
|
|
$this->assertInstanceOf(Article::class, $object); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Test the object facade with a relative object URL |
160
|
|
|
*/ |
161
|
|
|
public function testObjectRelative() |
162
|
|
|
{ |
163
|
|
|
$object = Object::load(getenv('REPOSITORY_URL').self::OBJECT_LOCATOR); |
164
|
|
|
$this->assertInstanceOf(Article::class, $object); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test the object facade with an invalid relative object URL |
169
|
|
|
* |
170
|
|
|
* @expectedException \Apparat\Resource\Ports\InvalidReaderArgumentException |
171
|
|
|
* @expectedExceptionCode 1447616824 |
172
|
|
|
*/ |
173
|
|
|
public function testObjectRelativeInvalid() |
174
|
|
|
{ |
175
|
|
|
$object = Object::load(getenv('REPOSITORY_URL').'/2015/12/21/2-article/2'); |
176
|
|
|
$this->assertInstanceOf(Article::class, $object); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Test with a missing object type class |
181
|
|
|
* |
182
|
|
|
* @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException |
183
|
|
|
* @expectedExceptionCode 1450824842 |
184
|
|
|
*/ |
185
|
|
|
public function testInvalidObjectTypeClass() |
186
|
|
|
{ |
187
|
|
|
TestTypeService::addInvalidType(); |
188
|
|
|
|
189
|
|
|
$resource = $this->createMock(ResourceInterface::class); |
190
|
|
|
$resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]); |
191
|
|
|
$articleObjectLocator = new RepositoryLocator(self::$repository, '/2016/02/16/5-invalid/5'); |
192
|
|
|
|
193
|
|
|
/** @var ResourceInterface $resource */ |
194
|
|
|
ObjectFactory::createFromResource($articleObjectLocator, $resource); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Test instantiation of object with invalid domain properties collection |
199
|
|
|
* |
200
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException |
201
|
|
|
* @expectedExceptionCode 1452288429 |
202
|
|
|
*/ |
203
|
|
|
public function testInvalidDomainPropertyCollectionClass() |
204
|
|
|
{ |
205
|
|
|
$this->getMockBuilder(AbstractObject::class) |
206
|
|
|
->setConstructorArgs([new RepositoryLocator(self::$repository, self::OBJECT_LOCATOR)]) |
207
|
|
|
->getMock(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Test the property data |
212
|
|
|
*/ |
213
|
|
|
public function testObjectPropertyData() |
214
|
|
|
{ |
215
|
|
|
// $frontMarkResource = |
216
|
|
|
// Resource::frontMark('file://'.__DIR__.DIRECTORY_SEPARATOR.'Fixture'.self::OBJECT_LOCATOR.'.md'); |
217
|
|
|
$object = Object::load(getenv('REPOSITORY_URL').self::OBJECT_LOCATOR); |
218
|
|
|
$this->assertTrue(is_array($object->getPropertyData())); |
219
|
|
|
// print_r($frontMarkResource->getData()); |
220
|
|
|
// print_r($object->getPropertyData()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Test mutation by altering metadata |
225
|
|
|
* |
226
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Properties\OutOfBoundsException |
227
|
|
|
* @expectedExceptionCode 1462632083 |
228
|
|
|
*/ |
229
|
|
|
public function testMetaDataMutation() |
230
|
|
|
{ |
231
|
|
|
$object = Object::load(getenv('REPOSITORY_URL').self::OBJECT_LOCATOR); |
232
|
|
|
$this->assertTrue(is_array($object->getPropertyData())); |
233
|
|
|
$objectUrl = $object->getAbsoluteUrl(); |
234
|
|
|
$objectRevision = $object->getRevision(); |
235
|
|
|
$object->setTitle($object->getTitle().' (mutated)'); |
236
|
|
|
$object->setSlug($object->getSlug().'-mutated'); |
237
|
|
|
$object->setDescription($object->getDescription().' (mutated)'); |
238
|
|
|
$object->setAbstract($object->getAbstract()); |
239
|
|
|
$object->setLicense(ltrim($object->getLicense().', ', ', ').'MIT'); |
240
|
|
|
$object->setKeywords(array_merge($object->getKeywords(), ['mutated'])); |
241
|
|
|
$object->setCategories($object->getCategories()); |
242
|
|
|
$this->assertEquals(preg_replace('%\/(.?+)$%', '/.$1-2', $objectUrl), $object->getAbsoluteUrl()); |
243
|
|
|
$this->assertEquals($objectRevision->getRevision() + 1, $object->getRevision()->getRevision()); |
244
|
|
|
$this->assertTrue($object->hasBeenModified()); |
245
|
|
|
$this->assertTrue($object->hasBeenMutated()); |
246
|
|
|
$this->assertEquals('MIT', $object->getLicense()); |
247
|
|
|
$this->assertEquals(ObjectTypes::PRIVACY_PRIVATE, $object->getPrivacy()); |
248
|
|
|
$this->assertEquals( |
249
|
|
|
ObjectTypes::PRIVACY_PUBLIC, |
250
|
|
|
$object->setPrivacy(ObjectTypes::PRIVACY_PUBLIC)->getPrivacy() |
251
|
|
|
); |
252
|
|
|
$object->setPrivacy('invalid'); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Test change by altering relations |
257
|
|
|
*/ |
258
|
|
|
public function testRelationChange() |
259
|
|
|
{ |
260
|
|
|
// TODO: Implement |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Test to persist an earlier revision |
265
|
|
|
*/ |
266
|
|
|
public function testPersistEarlierRevision() |
267
|
|
|
{ |
268
|
|
|
// TODO |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|