Completed
Push — master ( 2689ec...25953d )
by Joschi
02:46
created

ObjectTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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