Completed
Push — master ( 91d196...1cc552 )
by Joschi
02:38
created

ObjectTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 17
Bugs 1 Features 5
Metric Value
wmc 18
c 17
b 1
f 5
lcom 1
cbo 11
dl 0
loc 242
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 14 1
A tearDown() 0 5 1
A testUndefinedObjectType() 0 10 1
A testInvalidObjectType() 0 9 1
A testLoadArticleObject() 0 9 1
A testLoadArticleObjectSystemProperties() 0 16 1
A testLoadArticleObjectMetaProperties() 0 17 1
A testLoadArticleObjectDomainProperties() 0 8 1
A testLoadArticleObjectDomainEmptyProperty() 0 6 1
A testObjectFacadeAbsolute() 0 5 1
A testObjectFacadeRelative() 0 10 3
A testObjectFacadeRelativeInvalid() 0 5 1
A testInvalidObjectTypeClass() 0 11 1
A testInvalidDomainPropertyCollectionClass() 0 6 1
A testObjectPropertyData() 0 8 1
A testCreateArticleObject() 0 3 1
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\Factory\AuthorFactory;
42
use Apparat\Object\Domain\Model\Author\ApparatAuthor;
43
use Apparat\Object\Domain\Model\Object\AbstractObject;
44
use Apparat\Object\Domain\Model\Object\Id;
45
use Apparat\Object\Domain\Model\Object\ResourceInterface;
46
use Apparat\Object\Domain\Model\Object\Revision;
47
use Apparat\Object\Domain\Model\Object\Type;
48
use Apparat\Object\Domain\Model\Path\RepositoryPath;
49
use Apparat\Object\Domain\Model\Properties\SystemProperties;
50
use Apparat\Object\Domain\Repository\Repository;
51
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
52
use Apparat\Object\Ports\Object;
53
54
/**
55
 * Object tests
56
 *
57
 * @package Apparat\Object
58
 * @subpackage ApparatTest
59
 */
60
class ObjectTest extends AbstractDisabledAutoconnectorTest
61
{
62
    /**
63
     * Example object path
64
     *
65
     * @var string
66
     */
67
    const OBJECT_PATH = '/2015/12/21/1.article/1';
68
    /**
69
     * Test repository
70
     *
71
     * @var Repository
72
     */
73
    protected static $repository = null;
74
75
    /**
76
     * Setup
77
     */
78
    public static function setUpBeforeClass()
79
    {
80
        \Apparat\Object\Ports\Repository::register(
81
            getenv('REPOSITORY_URL'),
82
            [
83
                'type' => FileAdapterStrategy::TYPE,
84
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
85
            ]
86
        );
87
88
        self::$repository = \Apparat\Object\Ports\Repository::instance(getenv('REPOSITORY_URL'));
89
90
        \date_default_timezone_set('UTC');
91
    }
92
93
    /**
94
     * Tears down the fixture
95
     */
96
    public function tearDown()
97
    {
98
        TestType::removeInvalidType();
99
        parent::tearDown();
100
    }
101
102
    /**
103
     * Test undefined object type
104
     *
105
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
106
     * @expectedExceptionCode 1450905868
107
     */
108
    public function testUndefinedObjectType()
109
    {
110
        $resource = $this->getMock(ResourceInterface::class);
111
        $resource->method('getPropertyData')->willReturn([]);
112
        $repositoryPath = $this->getMockBuilder(RepositoryPath::class)->disableOriginalConstructor()->getMock();
113
114
        /** @var ResourceInterface $resource */
115
        /** @var RepositoryPath $repositoryPath */
116
        ObjectFactory::createFromResource($resource, $repositoryPath);
117
    }
118
119
    /**
120
     * Test invalid object type
121
     *
122
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
123
     * @expectedExceptionCode 1449871242
124
     */
125
    public function testInvalidObjectType()
126
    {
127
        $resource = $this->getMock(ResourceInterface::class);
128
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
129
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
130
131
        /** @var ResourceInterface $resource */
132
        ObjectFactory::createFromResource($resource, $articleObjectPath);
133
    }
134
135
    /**
136
     * Load an article object and test basic properties
137
     */
138
    public function testLoadArticleObject()
139
    {
140
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
141
        $articleObject = self::$repository->loadObject($articleObjectPath);
142
        $this->assertEquals(
143
            getenv('APPARAT_BASE_URL').getenv('REPOSITORY_URL').self::OBJECT_PATH,
144
            $articleObject->getAbsoluteUrl()
145
        );
146
    }
147
148
    /**
149
     * Load an article object and test its system properties
150
     */
151
    public function testLoadArticleObjectSystemProperties()
152
    {
153
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
154
        $articleObject = self::$repository->loadObject($articleObjectPath);
155
        $this->assertInstanceOf(Article::class, $articleObject);
156
        $this->assertEquals(new Id(1), $articleObject->getId());
157
        $this->assertEquals(new Type(Type::ARTICLE), $articleObject->getType());
158
        $this->assertEquals(new Revision(1), $articleObject->getRevision());
159
        $this->assertEquals(new \DateTimeImmutable('2015-12-21T22:30:00'), $articleObject->getCreated());
160
        $this->assertEquals(new \DateTimeImmutable('2015-12-21T22:45:00'), $articleObject->getPublished());
161
        $this->assertEquals('a123456789012345678901234567890123456789', $articleObject->getHash());
162
        $this->assertEquals(
163
            "# Example article object\n\nThis file is an example for an object of type `\"article\"`.\n",
164
            $articleObject->getPayload()
165
        );
166
    }
167
168
    /**
169
     * Load an article object and test its meta properties
170
     */
171
    public function testLoadArticleObjectMetaProperties()
172
    {
173
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
174
        $articleObject = self::$repository->loadObject($articleObjectPath);
175
        $this->assertInstanceOf(Article::class, $articleObject);
176
        $this->assertEquals('Example article object', $articleObject->getDescription());
177
        $this->assertEquals(
178
            'Article objects feature a Markdown payload along with some custom properties',
179
            $articleObject->getAbstract()
180
        );
181
        $this->assertArrayEquals(['apparat', 'object', 'example', 'article'], $articleObject->getKeywords());
182
        $this->assertArrayEquals(['example', 'text'], $articleObject->getCategories());
183
184
        $authorCount = count($articleObject->getAuthors());
185
        $articleObject->addAuthor(AuthorFactory::createFromString(AuthorTest::GENERIC_AUTHOR));
186
        $this->assertEquals($authorCount + 1, count($articleObject->getAuthors()));
187
    }
188
189
    /**
190
     * Load an article object and test its domain properties
191
     *
192
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
193
     * @expectedExceptionCode 1450818168
194
     */
195
    public function testLoadArticleObjectDomainProperties()
196
    {
197
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
198
        $articleObject = self::$repository->loadObject($articleObjectPath);
199
        $this->assertEquals('/system/url', $articleObject->getDomainProperty('uid'));
200
        $this->assertEquals('value', $articleObject->getDomainProperty('group:single'));
201
        $articleObject->getDomainProperty('group:invalid');
202
    }
203
204
    /**
205
     * Load an article object and test an empty domain property name
206
     *
207
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
208
     * @expectedExceptionCode 1450817720
209
     */
210
    public function testLoadArticleObjectDomainEmptyProperty()
211
    {
212
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
213
        $articleObject = self::$repository->loadObject($articleObjectPath);
214
        $articleObject->getDomainProperty('');
215
    }
216
217
    /**
218
     * Test the object facade with an absolute object URL
219
     */
220
    public function testObjectFacadeAbsolute()
221
    {
222
        $object = Object::instance(getenv('APPARAT_BASE_URL').getenv('REPOSITORY_URL').self::OBJECT_PATH);
223
        $this->assertInstanceOf(Article::class, $object);
224
    }
225
226
    /**
227
     * Test the object facade with a relative object URL
228
     */
229
    public function testObjectFacadeRelative()
230
    {
231
        $object = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
232
        $this->assertInstanceOf(Article::class, $object);
233
        foreach ($object->getAuthors() as $author) {
234
            if ($author instanceof ApparatAuthor) {
235
//				echo $author->getId()->getId();
236
            }
237
        }
238
    }
239
240
    /**
241
     * Test the object facade with an invalid relative object URL
242
     *
243
     * @expectedException \Apparat\Resource\Infrastructure\Io\File\InvalidArgumentException
244
     * @expectedExceptionCode 1447616824
245
     */
246
    public function testObjectFacadeRelativeInvalid()
247
    {
248
        $object = Object::instance(getenv('REPOSITORY_URL').'/2015/12/21/2.article/2');
249
        $this->assertInstanceOf(Article::class, $object);
250
    }
251
252
    /**
253
     * Test with a missing object type class
254
     *
255
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
256
     * @expectedExceptionCode 1450824842
257
     */
258
    public function testInvalidObjectTypeClass()
259
    {
260
        TestType::addInvalidType();
261
262
        $resource = $this->getMock(ResourceInterface::class);
263
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
264
        $articleObjectPath = new RepositoryPath(self::$repository, '/2016/02/16/5.invalid/5');
265
266
        /** @var ResourceInterface $resource */
267
        ObjectFactory::createFromResource($resource, $articleObjectPath);
268
    }
269
270
    /**
271
     * Test instantiation of object with invalid domain properties collection
272
     *
273
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
274
     * @expectedExceptionCode 1452288429
275
     */
276
    public function testInvalidDomainPropertyCollectionClass()
277
    {
278
        $this->getMockBuilder(AbstractObject::class)
279
            ->setConstructorArgs([new RepositoryPath(self::$repository, self::OBJECT_PATH)])
280
            ->getMock();
281
    }
282
283
    /**
284
     * Test the property data
285
     */
286
    public function testObjectPropertyData()
287
    {
288
//  $frontMarkResource = Resource::frontMark('file://'.__DIR__.DIRECTORY_SEPARATOR.'Fixture'.self::OBJECT_PATH.'.md');
289
        $object = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
290
        $this->assertTrue(is_array($object->getPropertyData()));
291
//        print_r($frontMarkResource->getData());
292
//        print_r($object->getPropertyData());
293
    }
294
295
    /**
296
     * Test the creation of an article object
297
     */
298
    public function testCreateArticleObject() {
299
//        $object = Object::create(Type::ARTICLE);
300
    }
301
}
302