Completed
Push — master ( f4221b...caffc5 )
by Joschi
06:49
created

RelationTest::testRelationConstruction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.2
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\Domain\Repository\Repository;
46
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
47
use Apparat\Object\Ports\Object;
48
use Apparat\Object\Ports\Relation;
49
50
/**
51
 * Object relation test
52
 *
53
 * @package Apparat\Object
54
 * @subpackage Apparat\Object\Tests
55
 */
56
class RelationTest extends AbstractDisabledAutoconnectorTest
57
{
58
    /**
59
     * Example object path
60
     *
61
     * @var string
62
     */
63
    const OBJECT_PATH = '/2015/12/21/1.article/1';
64
65
    /**
66
     * Test repository
67
     *
68
     * @var Repository
69
     */
70
    protected static $repository = null;
71
72
    /**
73
     * Setup
74
     */
75
    public static function setUpBeforeClass()
76
    {
77
        \Apparat\Object\Ports\Repository::register(
78
            getenv('REPOSITORY_URL'),
79
            [
80
                'type' => FileAdapterStrategy::TYPE,
81
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
82
            ]
83
        );
84
85
        self::$repository = \Apparat\Object\Ports\Repository::instance(getenv('REPOSITORY_URL'));
86
87
        \date_default_timezone_set('UTC');
88
    }
89
90
    /**
91
     * Test the addition of an object relation
92
     *
93
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
94
     * @expectedExceptionCode 1462401333
95
     */
96
    public function testObjectAddRelation()
97
    {
98
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
99
        $this->assertInstanceOf(Article::class, $article);
100
        $article->addRelation('http://example.com <[email protected]> John Doe', Relation::EMBEDDED_BY);
101
        $this->assertEquals(2, count($article->findRelations([Relation::URL => 'example.com'])));
102
        foreach ($article->findRelations([Relation::EMAIL => 'tollwerk.de']) as $relation) {
103
            $article->deleteRelation($relation);
104
        }
105
        $this->assertEquals(2, count($article->getRelations()));
106
        $article->addRelation('http://example.com <[email protected]> John Doe', 'invalid');
107
    }
108
109
    /**
110
     * Test a relation deserialization with repeated email
111
     *
112
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
113
     * @expectedExceptionCode 1462395977
114
     */
115
    public function testInvalidRelationEmail()
116
    {
117
        RelationFactory::createFromString(Relation::CONTRIBUTED_BY, '<invalid', self::$repository);
118
    }
119
120
    /**
121
     * Test a relation deserialization with repeated email
122
     *
123
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
124
     * @expectedExceptionCode 1462394737
125
     */
126
    public function testRepeatedRelationEmail()
127
    {
128
        RelationFactory::createFromString(
129
            Relation::CONTRIBUTED_BY,
130
            '<[email protected]> <[email protected]>',
131
            self::$repository
132
        );
133
    }
134
135
    /**
136
     * Test a relation deserialization with repeated URL
137
     *
138
     * @expectedException \Apparat\Object\Domain\Model\Relation\InvalidArgumentException
139
     * @expectedExceptionCode 1462394737
140
     */
141
    public function testRepeatedRelationUrl()
142
    {
143
        RelationFactory::createFromString(
144
            Relation::CONTRIBUTED_BY,
145
            'http://example.com http://example.com',
146
            self::$repository
147
        );
148
    }
149
150
    /**
151
     * Test a relation construction
152
     *
153
     * @expectedException \Apparat\Object\Domain\Model\Properties\InvalidArgumentException
154
     * @expectedExceptionCode 1462703468
155
     */
156
    public function testRelationConstruction()
157
    {
158
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
159
160
        /** @var Relations $relations */
161
        $relations = Kernel::create(Relations::class, [[Relation::CONTRIBUTED_BY => []], $article]);
162
163
        // Multiple relation addition
164
        $relations = $relations->addRelation('http://example.org John Doe', Relation::CONTRIBUTED_BY);
165
        $relations = $relations->addRelation('http://example.org John Doe', Relation::CONTRIBUTED_BY);
166
167
        // Retrieve contributed-by relations
168
        $contributedByRelations = $relations->getRelations(Relation::CONTRIBUTED_BY);
169
        $this->assertEquals(1, count($contributedByRelations));
170
171
        // Multiple relation deletion
172
        $relations = $relations->deleteRelation($contributedByRelations[0]);
173
        $relations = $relations->deleteRelation($contributedByRelations[0]);
174
175
        // Add invalid relation
176
        $relations->addRelation(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string|object<Apparat\Ob...tion\RelationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
    }
178
179
    /**
180
     * Test the filtering of relations
181
     */
182
    public function testRelationFiltering()
183
    {
184
        $article = Object::instance(getenv('REPOSITORY_URL').self::OBJECT_PATH);
185
186
        /** @var Relations $relations */
187
        $relations = Kernel::create(Relations::class, [[Relation::CONTRIBUTED_BY => []], $article]);
188
        $relations = $relations->addRelation(
189
            '!/repo/2016/01/08/2.contact/2 <[email protected]> John Doe',
190
            Relation::CONTRIBUTED_BY
191
        );
192
193
        // Filter by type
194
        $this->assertEquals(1, count($relations->findRelations([Relation::TYPE => Relation::CONTRIBUTED_BY])));
195
        $this->assertEquals(0, count($relations->findRelations([Relation::TYPE => Relation::CONTRIBUTES])));
196
197
        // Filter by URL
198
        $this->assertEquals(1, count($relations->findRelations([Relation::URL => 'repo'])));
199
        $this->assertEquals(0, count($relations->findRelations([Relation::URL => 'example.com'])));
200
201
        // Filter by email
202
        $this->assertEquals(1, count($relations->findRelations([Relation::EMAIL => '@example.com'])));
203
        $this->assertEquals(0, count($relations->findRelations([Relation::EMAIL => '@test.com'])));
204
205
        // Filter by label
206
        $this->assertEquals(1, count($relations->findRelations([Relation::LABEL => 'John'])));
207
        $this->assertEquals(0, count($relations->findRelations([Relation::LABEL => 'Jane'])));
208
209
        // Filter by coupling
210
        $this->assertEquals(1, count($relations->findRelations([Relation::COUPLING => true])));
211
        $this->assertEquals(0, count($relations->findRelations([Relation::COUPLING => false])));
212
213
        // Filter by invalid criteria
214
        $this->assertEquals(0, count($relations->findRelations(['invalid' => 'invalid'])));
215
    }
216
217
    /**
218
     * Test invalid relation coupling
219
     *
220
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
221
     * @expectedExceptionCode 1462311299
222
     */
223
    public function testInvalidRelationCoupling()
224
    {
225
        Kernel::create(ContributedByRelation::class, ['Label', '[email protected]', 'invalid-coupling']);
226
    }
227
228
    /**
229
     * Test relation getters & setters
230
     *
231
     * @expectedException \Apparat\Object\Domain\Model\Relation\OutOfBoundsException
232
     * @expectedExceptionCode 1462311299
233
     */
234
    public function testRelationGetterSetters()
235
    {
236
        $url = Kernel::create(Url::class, [self::OBJECT_PATH]);
237
        $this->assertInstanceOf(Url::class, $url);
238
239
        /** @var ContributedByRelation $relation */
240
        $relation = Kernel::create(
241
            ContributedByRelation::class,
242
            [$url, 'label', '[email protected]', Relation::LOOSE_COUPLING]
243
        );
244
        $this->assertInstanceOf(ContributedByRelation::class, $relation);
245
246
        // Set the URL
247
        $url2 = Kernel::create(Url::class, ['http://example.com/test']);
248
        $this->assertInstanceOf(Url::class, $url2);
249
        $relation = $relation->setUrl($url2)
250
            ->setLabel('Modified label')
251
            ->setEmail('[email protected]')
252
            ->setCoupling(Relation::TIGHT_COUPLING);
253
        $relation->setCoupling('invalid-coupling');
254
    }
255
}
256