Completed
Push — master ( 60c983...53e735 )
by Joschi
02:42
created

CollectionTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 14 1
A testObjectCollection() 0 23 2
A testObjectCollectionUnset() 0 6 1
B testObjectCollectionAdd() 0 25 1
B testObjectCollectionRemove() 0 34 2
B testObjectCollectionAppend() 0 24 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 AbstractDisabledAutoconnectorTest
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
            [
70
                'type' => FileAdapterStrategy::TYPE,
71
                'root' => __DIR__.DIRECTORY_SEPARATOR.'Fixture',
72
            ]
73
        );
74
75
        self::$repository = Repository::instance(getenv('REPOSITORY_URL'));
76
77
        \date_default_timezone_set('UTC');
78
    }
79
80
    /**
81
     * Test an object collection
82
     *
83
     * @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException
84
     * @expectedExceptionCode 1456530074
85
     */
86
    public function testObjectCollection()
87
    {
88
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
89
        $collection = self::$repository->findObjects($selector);
90
        $this->assertInstanceOf(Collection::class, $collection);
91
        $this->assertTrue(is_int(count($collection)));
92
        $this->assertTrue(count($collection) == 1);
93
94
        $uid = 1;
95
        foreach ($collection as $uid => $article) {
96
            $this->assertInstanceOf(Article::class, $article);
97
            $this->assertEquals($uid, $article->getId()->getId());
98
        }
99
100
        $this->assertTrue(isset($collection[$uid]));
101
        $this->assertInstanceOf(Article::class, $collection[$uid]);
102
103
        // Load a contact object
104
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
105
        $contactObject = self::$repository->loadObject($contactObjectPath);
106
        $this->assertInstanceOf(Contact::class, $contactObject);
107
        $collection[$uid] = $contactObject;
108
    }
109
110
    /**
111
     * Test unsetting an object from a collection by index
112
     *
113
     * @expectedException \Apparat\Object\Domain\Model\Object\RuntimeException
114
     * @expectedExceptionCode 1456530074
115
     */
116
    public function testObjectCollectionUnset()
117
    {
118
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
119
        $collection = self::$repository->findObjects($selector);
120
        unset($collection[0]);
121
    }
122
123
    /**
124
     * Test adding an object to a collection
125
     *
126
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
127
     * @expectedExceptionCode 1450131914
128
     */
129
    public function testObjectCollectionAdd()
130
    {
131
        // Load a contact object
132
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
133
        $contactObject = self::$repository->loadObject($contactObjectPath);
134
        $this->assertInstanceOf(Contact::class, $contactObject);
135
136
        // Load a collection
137
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
138
        $collection = self::$repository->findObjects($selector);
139
        $this->assertInstanceOf(Collection::class, $collection);
140
        $articleCount = count($collection);
141
142
        // Add the contact to the collection
143
        $collection = $collection->add($contactObject);
144
        $this->assertEquals($articleCount + 1, count($collection));
145
146
        // Add another object by repository path
147
        /** @var Collection $collection */
148
        $collection = $collection->add(new RepositoryPath(self::$repository, '/2016/02/07/3.article/3'));
149
        $this->assertEquals($articleCount + 2, count($collection));
150
151
        // Add invalid object
152
        $collection->add(null);
153
    }
154
155
    /**
156
     * Test removing an object from a collection
157
     *
158
     * @expectedException \Apparat\Object\Domain\Model\Object\InvalidArgumentException
159
     * @expectedExceptionCode 1448737190
160
     */
161
    public function testObjectCollectionRemove()
162
    {
163
        // Load a contact object
164
        $contactObjectPath = new RepositoryPath(self::$repository, '/2016/01/08/2.contact/2');
165
        $contactObject = self::$repository->loadObject($contactObjectPath);
166
        $this->assertInstanceOf(Contact::class, $contactObject);
167
168
        // Load a collection
169
        $selector = SelectorFactory::createFromString('/2015/*/*/*.article');
170
        $collection = self::$repository->findObjects($selector);
171
        $this->assertInstanceOf(Collection::class, $collection);
172
        $articleCount = count($collection);
173
174
        $article = null;
175
        foreach ($collection as $article) {
176
            break;
177
        }
178
179
        // Add the contact to the collection
180
        /** @var Collection $collection */
181
        $collection = $collection->add($contactObject);
182
        $this->assertEquals($articleCount + 1, count($collection));
183
184
        // Remove the article
185
        $collection = $collection->remove($article);
0 ignored issues
show
Bug introduced by
It seems like $article defined by null on line 174 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...
186
        $this->assertEquals($articleCount, count($collection));
187
188
        // Remove the contact by ID
189
        $collection = $collection->remove(2);
190
        $this->assertEquals($articleCount - 1, count($collection));
191
192
        // Remove invalid object
193
        $collection->remove('invalid');
194
    }
195
196
    /**
197
     * Test appending two object collections
198
     */
199
    public function testObjectCollectionAppend()
200
    {
201
        // Load an article collection
202
        $articles = self::$repository->findObjects(SelectorFactory::createFromString('/2015/*/*/*.article'));
203
        $articleCount = count($articles);
204
        $article = null;
205
        foreach ($articles as $article) {
206
            break;
207
        }
208
209
        // Load a contact collection
210
        $contacts = self::$repository->findObjects(SelectorFactory::createFromString('/2016/01'));
211
        $contactCount = count($contacts);
212
        $contact = null;
213
        foreach ($contacts as $contact) {
214
            break;
215
        }
216
217
        // Append the contacts collection to the articles collection
218
        $combined = $articles->append($contacts);
219
        $this->assertEquals($articleCount + $contactCount, count($combined));
220
        $this->assertInstanceOf(Article::class, $combined[$article->getId()->getId()]);
221
        $this->assertInstanceOf(Contact::class, $combined[$contact->getId()->getId()]);
222
    }
223
}
224