Completed
Push — master ( e2e6d9...5aa43e )
by Joschi
02:56
created

ObjectTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
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\Model\Author\ApparatAuthor;
42
use Apparat\Object\Domain\Model\Object\ResourceInterface;
43
use Apparat\Object\Domain\Model\Path\RepositoryPath;
44
use Apparat\Object\Domain\Model\Properties\SystemProperties;
45
use Apparat\Object\Domain\Repository\Repository;
46
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
47
use Apparat\Object\Ports\Object;
48
49
/**
50
 * Object tests
51
 *
52
 * @package Apparat\Object
53
 * @subpackage ApparatTest
54
 */
55
class ObjectTest extends AbstractTest
56
{
57
    /**
58
     * Example object path
59
     *
60
     * @var string
61
     */
62
    const OBJECT_PATH = '/2015/12/21/1.article/1';
63
    /**
64
     * Test repository
65
     *
66
     * @var Repository
67
     */
68
    protected static $repository = null;
69
70
    /**
71
     * Setup
72
     */
73
    public static function setUpBeforeClass()
74
    {
75
        \Apparat\Object\Ports\Repository::register(
76
            getenv('REPOSITORY_URL'), [
77
                'type' => FileAdapterStrategy::TYPE,
78
                'root' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixture',
79
            ]
80
        );
81
82
        self::$repository = \Apparat\Object\Ports\Repository::instance(getenv('REPOSITORY_URL'));
83
    }
84
85
    /**
86
     * Tears down the fixture
87
     */
88
    public function tearDown()
89
    {
90
        TestType::removeInvalidType();
91
        parent::tearDown();
92
    }
93
94
    /**
95
     * Test undefined object type
96
     *
97
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
98
     * @expectedExceptionCode 1450905868
99
     */
100
    public function testUndefinedObjectType()
101
    {
102
        $resource = $this->getMock(ResourceInterface::class);
103
        $resource->method('getPropertyData')->willReturn([]);
104
        $repositoryPath = $this->getMockBuilder(RepositoryPath::class)->disableOriginalConstructor()->getMock();
105
106
        /** @var ResourceInterface $resource */
107
        /** @var RepositoryPath $repositoryPath */
108
        ObjectFactory::createFromResource($resource, $repositoryPath);
109
    }
110
111
    /**
112
     * Test invalid object type
113
     *
114
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
115
     * @expectedExceptionCode 1449871242
116
     */
117
    public function testInvalidObjectType()
118
    {
119
        $resource = $this->getMock(ResourceInterface::class);
120
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
121
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
122
123
        /** @var ResourceInterface $resource */
124
        ObjectFactory::createFromResource($resource, $articleObjectPath);
125
    }
126
127
    /**
128
     * Load an article object and test its meta properties
129
     */
130
    public function testLoadArticleObjectMetaProperties()
131
    {
132
        $articleObjectPath = new RepositoryPath(self::$repository, self::OBJECT_PATH);
133
        $articleObject = self::$repository->loadObject($articleObjectPath);
134
        $this->assertInstanceOf(Article::class, $articleObject);
135
        $this->assertEquals('Example article object', $articleObject->getDescription());
136
        $this->assertEquals('Article objects feature a Markdown payload along with some custom properties', $articleObject->getAbstract());
137
        $this->assertArrayEquals(['apparat', 'object', 'example', 'article'], $articleObject->getKeywords());
138
        $this->assertArrayEquals(['example', 'text'], $articleObject->getCategories());
139
    }
140
141
    /**
142
     * Test the object facade with an absolute object URL
143
     */
144
    public function testObjectFacadeAbsolute()
145
    {
146
        $object = Object::instance(getenv('APPARAT_BASE_URL') . getenv('REPOSITORY_URL') . self::OBJECT_PATH);
147
        $this->assertInstanceOf(Article::class, $object);
148
    }
149
150
    /**
151
     * Test the object facade with a relative object URL
152
     */
153
    public function testObjectFacadeRelative()
154
    {
155
        $object = Object::instance(getenv('REPOSITORY_URL') . self::OBJECT_PATH);
156
        $this->assertInstanceOf(Article::class, $object);
157
        foreach ($object->getAuthors() as $author) {
158
            if ($author instanceof ApparatAuthor) {
159
//				echo $author->getId()->getId();
160
            }
161
        }
162
    }
163
164
    /**
165
     * Test the object facade with an invalid relative object URL
166
     *
167
     * @expectedException \Apparat\Resource\Infrastructure\Io\File\InvalidArgumentException
168
     * @expectedExceptionCode 1447616824
169
     */
170
    public function testObjectFacadeRelativeInvalid()
171
    {
172
        $object = Object::instance(getenv('REPOSITORY_URL') . '/2015/12/21/2.article/2');
173
        $this->assertInstanceOf(Article::class, $object);
174
    }
175
176
    /**
177
     * Test with a missing object type class
178
     *
179
     * @expectedException \Apparat\Object\Application\Factory\InvalidArgumentException
180
     * @expectedExceptionCode 1450824842
181
     */
182
    public function testInvalidObjectTypeClass()
183
    {
184
        TestType::addInvalidType();
185
186
        $resource = $this->getMock(ResourceInterface::class);
187
        $resource->method('getPropertyData')->willReturn([SystemProperties::COLLECTION => ['type' => 'invalid']]);
188
        $articleObjectPath = new RepositoryPath(self::$repository, '/2016/02/16/5.invalid/5');
189
190
        /** @var ResourceInterface $resource */
191
        ObjectFactory::createFromResource($resource, $articleObjectPath);
192
    }
193
}
194