Completed
Push — master ( 3bf76e...9f9c26 )
by Joschi
04:22
created

RelationTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Test
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\Kernel\Ports\Kernel;
40
use Apparat\Object\Application\Model\Object\Article;
41
use Apparat\Object\Domain\Factory\RelationFactory;
42
use Apparat\Object\Domain\Model\Path\Url;
43
use Apparat\Object\Domain\Model\Properties\Relations;
44
use Apparat\Object\Domain\Model\Relation\ContributedByRelation;
45
use Apparat\Object\Ports\Object;
46
use Apparat\Object\Ports\Relation;
47
48
/**
49
 * Object relation test
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Tests
53
 */
54
class RelationTest extends AbstractRepositoryEnabledTest
55
{
56
    /**
57
     * Example object path
58
     *
59
     * @var string
60
     */
61
    const OBJECT_PATH = '/2015/12/21/1.article/1';
62
63
    /**
64
     * Test the addition of an object relation
65
     *
66
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
67
     * @expectedExceptionCode 1462401333
68
     */
69
    public function testObjectAddRelation()
70
    {
71
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
72
        $this->assertInstanceOf(Article::class, $article);
73
        $article->addRelation('http://example.com <[email protected]> John Doe', Relation::EMBEDDED_BY);
74
        $this->assertEquals(2, count($article->findRelations([Relation::URL => 'example.com'])));
75
        foreach ($article->findRelations([Relation::EMAIL => 'tollwerk.de']) as $relation) {
76
            $article->deleteRelation($relation);
77
        }
78
        $this->assertEquals(2, count($article->getRelations()));
79
        $article->addRelation('http://example.com <[email protected]> John Doe', 'invalid');
80
    }
81
82
    /**
83
     * Test a relation deserialization with repeated email
84
     *
85
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
86
     * @expectedExceptionCode 1462395977
87
     */
88
    public function testInvalidRelationEmail()
89
    {
90
        RelationFactory::createFromString(Relation::CONTRIBUTED_BY, '<invalid', self::$repository);
91
    }
92
93
    /**
94
     * Test a relation deserialization with repeated email
95
     *
96
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
97
     * @expectedExceptionCode 1462394737
98
     */
99
    public function testRepeatedRelationEmail()
100
    {
101
        RelationFactory::createFromString(
102
            Relation::CONTRIBUTED_BY,
103
            '<[email protected]> <[email protected]>',
104
            self::$repository
105
        );
106
    }
107
108
    /**
109
     * Test a relation deserialization with repeated URL
110
     *
111
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
112
     * @expectedExceptionCode 1462394737
113
     */
114
    public function testRepeatedRelationUrl()
115
    {
116
        RelationFactory::createFromString(
117
            Relation::CONTRIBUTED_BY,
118
            'http://example.com http://example.com',
119
            self::$repository
120
        );
121
    }
122
123
    /**
124
     * Test a relation construction
125
     *
126
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
127
     * @expectedExceptionCode 1462703468
128
     */
129
    public function testRelationConstruction()
130
    {
131
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
132
133
        /** @var Relations $relations */
134
        $relations = Kernel::create(Relations::class, [[Relation::CONTRIBUTED_BY => []], $article]);
135
136
        // Multiple relation addition
137
        $relations = $relations->addRelation('http://example.org John Doe', Relation::CONTRIBUTED_BY);
138
        $relations = $relations->addRelation('http://example.org John Doe', Relation::CONTRIBUTED_BY);
139
140
        // Retrieve contributed-by relations
141
        $contributedByRels = $relations->getRelations(Relation::CONTRIBUTED_BY);
142
        $this->assertEquals(1, count($contributedByRels));
143
144
        // Multiple relation deletion
145
        $relations = $relations->deleteRelation($contributedByRels[0]);
146
        $relations = $relations->deleteRelation($contributedByRels[0]);
147
148
        // Add invalid relation
149
        $relations->addRelation(null);
150
    }
151
152
    /**
153
     * Test the filtering of relations
154
     */
155
    public function testRelationFiltering()
156
    {
157
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
158
159
        /** @var Relations $relations */
160
        $relations = Kernel::create(Relations::class, [[Relation::CONTRIBUTED_BY => []], $article]);
161
        $relations = $relations->addRelation(
162
            '!/repo/2016/01/08/2.contact/2 <[email protected]> John Doe',
163
            Relation::CONTRIBUTED_BY
164
        );
165
166
        // Filter by type
167
        $this->assertEquals(1, count($relations->findRelations([Relation::TYPE => Relation::CONTRIBUTED_BY])));
168
        $this->assertEquals(0, count($relations->findRelations([Relation::TYPE => Relation::CONTRIBUTES])));
169
170
        // Filter by URL
171
        $this->assertEquals(1, count($relations->findRelations([Relation::URL => 'repo'])));
172
        $this->assertEquals(0, count($relations->findRelations([Relation::URL => 'example.com'])));
173
174
        // Filter by email
175
        $this->assertEquals(1, count($relations->findRelations([Relation::EMAIL => '@example.com'])));
176
        $this->assertEquals(0, count($relations->findRelations([Relation::EMAIL => '@test.com'])));
177
178
        // Filter by label
179
        $this->assertEquals(1, count($relations->findRelations([Relation::LABEL => 'John'])));
180
        $this->assertEquals(0, count($relations->findRelations([Relation::LABEL => 'Jane'])));
181
182
        // Filter by coupling
183
        $this->assertEquals(1, count($relations->findRelations([Relation::COUPLING => true])));
184
        $this->assertEquals(0, count($relations->findRelations([Relation::COUPLING => false])));
185
186
        // Filter by invalid criteria
187
        $this->assertEquals(0, count($relations->findRelations(['invalid' => 'invalid'])));
188
    }
189
190
    /**
191
     * Test invalid relation coupling
192
     *
193
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
194
     * @expectedExceptionCode 1462311299
195
     */
196
    public function testInvalidRelationCoupling()
197
    {
198
        Kernel::create(ContributedByRelation::class, ['Label', '[email protected]', 'invalid-coupling']);
199
    }
200
201
    /**
202
     * Test relation getters & setters
203
     *
204
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
205
     * @expectedExceptionCode 1462311299
206
     */
207
    public function testRelationGetterSetters()
208
    {
209
        $url = Kernel::create(Url::class, [self::OBJECT_PATH]);
210
        $this->assertInstanceOf(Url::class, $url);
211
212
        /** @var ContributedByRelation $relation */
213
        $relation = Kernel::create(
214
            ContributedByRelation::class,
215
            [$url, 'label', '[email protected]', Relation::LOOSE_COUPLING]
216
        );
217
        $this->assertInstanceOf(ContributedByRelation::class, $relation);
218
219
        // Set the URL
220
        $url2 = Kernel::create(Url::class, ['http://example.com/test']);
221
        $this->assertInstanceOf(Url::class, $url2);
222
        $relation = $relation->setUrl($url2)
223
            ->setLabel('Modified label')
224
            ->setEmail('[email protected]')
225
            ->setCoupling(Relation::TIGHT_COUPLING);
226
        $relation->setCoupling('invalid-coupling');
227
    }
228
}
229