Completed
Push — master ( 5aa43e...28467c )
by Joschi
02:48
created

ObjectTest::testObjectFacadeRelative()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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 AbstractTest
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
                'type' => FileAdapterStrategy::TYPE,
83
                'root' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixture',
84
            ]
85
        );
86
87
        self::$repository = \Apparat\Object\Ports\Repository::instance(getenv('REPOSITORY_URL'));
88
89
        \date_default_timezone_set('UTC');
90
    }
91
92
    /**
93
     * Tears down the fixture
94
     */
95
    public function tearDown()
96
    {
97
        TestType::removeInvalidType();
98
        parent::tearDown();
99
    }
100
101
    /**
102
     * Test undefined object type
103
     *
104
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
105
     * @expectedExceptionCode 1450905868
106
     */
107
    public function testUndefinedObjectType()
108
    {
109
        $resource = $this->getMock(ResourceInterface::class);
110
        $resource->method('getPropertyData')->willReturn([]);
111
        $repositoryPath = $this->getMockBuilder(RepositoryPath::class)->disableOriginalConstructor()->getMock();
112
113
        /** @var ResourceInterface $resource */
114
        /** @var RepositoryPath $repositoryPath */
115
        ObjectFactory::createFromResource($resource, $repositoryPath);
116
    }
117
118
    /**
119
     * Test invalid object type
120
     *
121
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
122
     * @expectedExceptionCode 1449871242
123
     */
124
    public function testInvalidObjectType()
125
    {
126
        $resource = $this->getMock(ResourceInterface::class);
127
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
128
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
129
130
        /** @var ResourceInterface $resource */
131
        ObjectFactory::createFromResource($resource, $articleObjectPath);
132
    }
133
134
    /**
135
     * Load an article object and test basic properties
136
     */
137
    public function testLoadArticleObject()
138
    {
139
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
140
        $articleObject = self::$repository->loadObject($articleObjectPath);
141
        $this->assertEquals(getenv('APPARAT_BASE_URL') . getenv('REPOSITORY_URL') . self::OBJECT_PATH, $articleObject->getAbsoluteUrl());
142
    }
143
144
    /**
145
     * Load an article object and test its system properties
146
     */
147
    public function testLoadArticleObjectSystemProperties()
148
    {
149
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
150
        $articleObject = self::$repository->loadObject($articleObjectPath);
151
        $this->assertInstanceOf(Article::class, $articleObject);
152
        $this->assertEquals(new Id(1), $articleObject->getId());
153
        $this->assertEquals(new Type(Type::ARTICLE), $articleObject->getType());
154
        $this->assertEquals(new Revision(1), $articleObject->getRevision());
155
        $this->assertEquals(new \DateTimeImmutable('2015-12-21T22:30:00'), $articleObject->getCreated());
156
        $this->assertEquals(new \DateTimeImmutable('2015-12-21T22:45:00'), $articleObject->getPublished());
157
    }
158
159
    /**
160
     * Load an article object and test its meta properties
161
     */
162
    public function testLoadArticleObjectMetaProperties()
163
    {
164
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
165
        $articleObject = self::$repository->loadObject($articleObjectPath);
166
        $this->assertInstanceOf(Article::class, $articleObject);
167
        $this->assertEquals('Example article object', $articleObject->getDescription());
168
        $this->assertEquals('Article objects feature a Markdown payload along with some custom properties', $articleObject->getAbstract());
169
        $this->assertArrayEquals(['apparat', 'object', 'example', 'article'], $articleObject->getKeywords());
170
        $this->assertArrayEquals(['example', 'text'], $articleObject->getCategories());
171
172
        $authorCount = count($articleObject->getAuthors());
173
        $articleObject->addAuthor(AuthorFactory::createFromString(AuthorTest::GENERIC_AUTHOR));
174
        $this->assertEquals($authorCount + 1, count($articleObject->getAuthors()));
175
    }
176
177
    /**
178
     * Test the object facade with an absolute object URL
179
     */
180
    public function testObjectFacadeAbsolute()
181
    {
182
        $object = Object::instance(getenv('APPARAT_BASE_URL') . getenv('REPOSITORY_URL') . self::OBJECT_PATH);
183
        $this->assertInstanceOf(Article::class, $object);
184
    }
185
186
    /**
187
     * Test the object facade with a relative object URL
188
     */
189
    public function testObjectFacadeRelative()
190
    {
191
        $object = Object::instance(getenv('REPOSITORY_URL') . self::OBJECT_PATH);
192
        $this->assertInstanceOf(Article::class, $object);
193
        foreach ($object->getAuthors() as $author) {
194
            if ($author instanceof ApparatAuthor) {
195
//				echo $author->getId()->getId();
196
            }
197
        }
198
    }
199
200
    /**
201
     * Test the object facade with an invalid relative object URL
202
     *
203
     * @expectedException \Apparat\Resource\Infrastructure\Io\File\InvalidArgumentException
204
     * @expectedExceptionCode 1447616824
205
     */
206
    public function testObjectFacadeRelativeInvalid()
207
    {
208
        $object = Object::instance(getenv('REPOSITORY_URL') . '/2015/12/21/2.article/2');
209
        $this->assertInstanceOf(Article::class, $object);
210
    }
211
212
    /**
213
     * Test with a missing object type class
214
     *
215
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
216
     * @expectedExceptionCode 1450824842
217
     */
218
    public function testInvalidObjectTypeClass()
219
    {
220
        TestType::addInvalidType();
221
222
        $resource = $this->getMock(ResourceInterface::class);
223
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
224
        $articleObjectPath = new RepositoryPath(self::$repository, '/2016/02/16/5.invalid/5');
225
226
        /** @var ResourceInterface $resource */
227
        ObjectFactory::createFromResource($resource, $articleObjectPath);
228
    }
229
230
    /**
231
     * Test instantiation of object with invalid domain properties collection
232
     *
233
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
234
     * @expectedExceptionCode 1452288429
235
     */
236
    public function testInvalidDomainPropertyCollectionClass()
237
    {
238
        $this->getMockBuilder(AbstractObject::class)
239
            ->setConstructorArgs([new RepositoryPath(self::$repository, self::OBJECT_PATH)])
240
            ->getMock();
241
    }
242
}
243