Completed
Push — master ( a738a2...ae9dae )
by Joschi
02:37
created

CollectionTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 7
dl 0
loc 169
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 13 1
A testObjectCollection() 0 23 2
A testObjectCollectionUnset() 0 6 1
B testObjectCollectionAdd() 0 25 1
B testObjectCollectionRemove() 0 34 2
A testObjectCollectionAppend() 0 23 3
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Tests
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\Object\Application\Model\Object\Article;
40
use Apparat\Object\Application\Model\Object\Contact;
41
use Apparat\Object\Domain\Factory\SelectorFactory;
42
use Apparat\Object\Domain\Model\Object\Collection;
43
use Apparat\Object\Domain\Model\Path\RepositoryPath;
44
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
45
use Apparat\Object\Ports\Repository;
46
47
/**
48
 * Object collection tests
49
 *
50
 * @package Apparat\Kernel
51
 * @subpackage Apparat\Object\Tests
52
 */
53
class CollectionTest extends AbstractTest
54
{
55
    /**
56
     * Test repository
57
     *
58
     * @var \Apparat\Object\Domain\Repository\Repository
59
     */
60
    protected static $repository = null;
61
62
    /**
63
     * Setup
64
     */
65
    public static function setUpBeforeClass()
66
    {
67
        Repository::register(
68
            getenv('REPOSITORY_URL'), [
69
                'type' => FileAdapterStrategy::TYPE,
70
                'root' => __DIR__ . DIRECTORY_SEPARATOR . 'Fixture',
71
            ]
72
        );
73
74
        self::$repository = Repository::instance(getenv('REPOSITORY_URL'));
75
76
        \date_default_timezone_set('UTC');
77
    }
78
79
    /**
80
     * Test an object collection
81
     *
82
     * @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException
83
     * @expectedExceptionCode 1456530074
84
     */
85
    public function testObjectCollection()
86
    {
87
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
88
        $collection = self::$repository->findObjects($selector);
89
        $this->assertInstanceOf(Collection::class, $collection);
90
        $this->assertTrue(is_int(count($collection)));
91
        $this->assertTrue(count($collection) == 1);
92
93
        $id = 1;
94
        foreach ($collection as $id => $article) {
95
            $this->assertInstanceOf(Article::class, $article);
96
            $this->assertEquals($id, $article->getId()->getId());
97
        }
98
99
        $this->assertTrue(isset($collection[$id]));
100
        $this->assertInstanceOf(Article::class, $collection[$id]);
101
102
        // Load a contact object
103
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
104
        $contactObject = self::$repository->loadObject($contactObjectPath);
105
        $this->assertInstanceOf(Contact::class, $contactObject);
106
        $collection[$id] = $contactObject;
107
    }
108
109
    /**
110
     * Test unsetting an object from a collection by index
111
     *
112
     * @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException
113
     * @expectedExceptionCode 1456530074
114
     */
115
    public function testObjectCollectionUnset()
116
    {
117
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
118
        $collection = self::$repository->findObjects($selector);
119
        unset($collection[0]);
120
    }
121
122
    /**
123
     * Test adding an object to a collection
124
     *
125
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
126
     * @expectedExceptionCode 1450131914
127
     */
128
    public function testObjectCollectionAdd()
129
    {
130
        // Load a contact object
131
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
132
        $contactObject = self::$repository->loadObject($contactObjectPath);
133
        $this->assertInstanceOf(Contact::class, $contactObject);
134
135
        // Load a collection
136
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
137
        $collection = self::$repository->findObjects($selector);
138
        $this->assertInstanceOf(Collection::class, $collection);
139
        $articleCount = count($collection);
140
141
        // Add the contact to the collection
142
        $collection = $collection->add($contactObject);
143
        $this->assertEquals($articleCount + 1, count($collection));
144
145
        // Add another object by repository path
146
        /** @var Collection $collection */
147
        $collection = $collection->add(new RepositoryPath(self::$repository, '/2016/02/07/3.article/3'));
148
        $this->assertEquals($articleCount + 2, count($collection));
149
150
        // Add invalid object
151
        $collection->add(null);
152
    }
153
154
    /**
155
     * Test removing an object from a collection
156
     *
157
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
158
     * @expectedExceptionCode 1448737190
159
     */
160
    public function testObjectCollectionRemove()
161
    {
162
        // Load a contact object
163
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
164
        $contactObject = self::$repository->loadObject($contactObjectPath);
165
        $this->assertInstanceOf(Contact::class, $contactObject);
166
167
        // Load a collection
168
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
169
        $collection = self::$repository->findObjects($selector);
170
        $this->assertInstanceOf(Collection::class, $collection);
171
        $articleCount = count($collection);
172
173
        $article = null;
174
        foreach ($collection as $article) {
175
            break;
176
        }
177
178
        // Add the contact to the collection
179
        /** @var Collection $collection */
180
        $collection = $collection->add($contactObject);
181
        $this->assertEquals($articleCount + 1, count($collection));
182
183
        // Remove the article
184
        $collection = $collection->remove($article);
0 ignored issues
show
Bug introduced by
It seems like $article defined by null on line 173 can be null; however, Apparat\Object\Domain\Mo...ct\Collection::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
185
        $this->assertEquals($articleCount, count($collection));
186
187
        // Remove the contact by ID
188
        $collection = $collection->remove(2);
189
        $this->assertEquals($articleCount - 1, count($collection));
190
191
        // Remove invalid object
192
        $collection->remove('invalid');
193
    }
194
195
    /**
196
     * Test appending two object collections
197
     */
198
    public function testObjectCollectionAppend() {
199
        // Load an article collection
200
        $articles = self::$repository->findObjects(SelectorFactory::createFromString('/2015/*/*/*.article'));
201
        $articleCount = count($articles);
202
        $article = null;
203
        foreach ($articles as $article) {
204
            break;
205
        }
206
207
        // Load a contact collection
208
        $contacts = self::$repository->findObjects(SelectorFactory::createFromString('/2016/01'));
209
        $contactCount = count($contacts);
210
        $contact = null;
211
        foreach ($contacts as $contact) {
212
            break;
213
        }
214
215
        // Append the contacts collection to the articles collection
216
        $combined = $articles->append($contacts);
217
        $this->assertEquals($articleCount + $contactCount, count($combined));
218
        $this->assertInstanceOf(Article::class, $combined[$article->getId()->getId()]);
219
        $this->assertInstanceOf(Contact::class, $combined[$contact->getId()->getId()]);
220
    }
221
}
222