Completed
Push — master ( a373f3...4d04c4 )
by Joschi
03:00
created

testArticleApparatObjectUnvalidUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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)
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 as ApplicationArticle;
41
use Apparat\Object\Infrastructure\Model\Object\Object;
42
use Apparat\Object\Ports\Facades\RepositoryFacade;
43
use Apparat\Object\Ports\Factory\SelectorFactory;
44
use Apparat\Object\Ports\Object\Article;
45
use Apparat\Object\Ports\Object\Contact;
46
47
/**
48
 * Object URL tests
49
 *
50
 * @package Apparat\Object
51
 * @subpackage Apparat\Object\Test
52
 */
53
class ApparatObjectTest extends AbstractRepositoryEnabledTest
54
{
55
    /**
56
     * Example article locator
57
     *
58
     * @var string
59
     */
60
    const ARTICLE_LOCATOR = '/repo/2015/12/21/1-article/1';
61
    /**
62
     * Example contact locator
63
     *
64
     * @var string
65
     */
66
    const CONTACT_LOCATOR = '/repo/2016/01/08/2-contact/2';
67
68
    /**
69
     * Test the apparat object factory with an invalid object type
70
     *
71
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
72
     * @expectedExceptionCode 1465368597
73
     */
74
    public function testApparatObjectFactoryInvalidType()
75
    {
76
        /** @var ApplicationArticle $articleObj */
77
        $articleObj = Object::load(self::ARTICLE_LOCATOR);
78
        TestApparatObjectFactory::create($articleObj);
79
    }
80
81
    /**
82
     * Test the selection of apparat objects via the repository facade
83
     */
84
    public function testApparatObjectFindCollection()
85
    {
86
        $selector = SelectorFactory::createFromString('/2015/*/*/*-article');
87
        $apparatObjects = RepositoryFacade::instance(getenv('REPOSITORY_URL'))->findObjects($selector);
88
        $this->assertTrue(is_array($apparatObjects));
89
        foreach ($apparatObjects as $apparatObject) {
90
            $this->assertInstanceOf(Article::class, $apparatObject);
91
        }
92
    }
93
94
    /**
95
     * Test the article apparat object with an illegal setter
96
     *
97
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
98
     * @expectedExceptionCode 1466804125
99
     */
100
    public function testArticleApparatObjectIllegalSetter()
101
    {
102
        /** @var Article $articleApparatObj */
103
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
104
        $this->assertInstanceOf(Article::class, $articleApparatObj);
105
106
        $this->assertTrue(isset($articleApparatObj['name']));
107
        $this->assertEquals('First repository article', $articleApparatObj['name']);
108
        $this->assertEquals('First repository article', $articleApparatObj->getName());
109
        $articleApparatObj['name'] = null;
110
    }
111
112
    /**
113
     * Test the article apparat object with an invalid getter
114
     *
115
     * @expectedException \BadMethodCallException
116
     */
117
    public function testArticleApparatObjectInvalidGetter()
118
    {
119
        /** @var Article $articleApparatObj */
120
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
121
        $this->assertInstanceOf(Article::class, $articleApparatObj);
122
123
        // Test serialization / deserialization
124
        $serialized = serialize($articleApparatObj);
125
        $unserializedArticle = unserialize($serialized);
126
        $this->assertEquals($unserializedArticle['name'], $articleApparatObj['name']);
127
128
        // Test iteration
129
        $articleArray = $articleApparatObj->getArrayCopy();
130
        $this->assertEquals(count($articleArray), count($articleApparatObj));
131
        foreach ($articleApparatObj as $property => $value) {
132
            $this->assertTrue(array_key_exists($property, $articleArray));
133
            $this->assertEquals($articleArray[$property], $value);
134
            unset($articleArray[$property]);
135
        }
136
        $this->assertEquals(0, count($articleArray));
137
138
        /** @noinspection PhpUndefinedMethodInspection */
139
        $articleApparatObj->getInvalid();
0 ignored issues
show
Documentation Bug introduced by
The method getInvalid does not exist on object<Apparat\Object\Ports\Object\Article>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
140
    }
141
142
    /**
143
     * Test the article apparat object with an invalid array getter
144
     *
145
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
146
     * @expectedExceptionCode 1465330399
147
     */
148
    public function testArticleApparatObjectInvalidArrayGetter()
149
    {
150
        /** @var Article $articleApparatObj */
151
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
152
        $this->assertInstanceOf(Article::class, $articleApparatObj);
153
154
        $articleApparatObj['invalid'];
155
    }
156
157
    /**
158
     * Test the article apparat object with an invalid array append
159
     *
160
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
161
     * @expectedExceptionCode 1466804193
162
     */
163
    public function testArticleApparatObjectInvalidArrayAppend()
164
    {
165
        /** @var Article $articleApparatObj */
166
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
167
        $this->assertInstanceOf(Article::class, $articleApparatObj);
168
169
        $articleApparatObj->append('invalid');
170
    }
171
172
    /**
173
     * Test sorting and exchange methods of an article apparat object
174
     *
175
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
176
     * @expectedExceptionCode 1466805183
177
     */
178
    public function testArticleApparatObjectSortingExchange()
179
    {
180
        /** @var Article $articleApparatObj */
181
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
182
        $this->assertInstanceOf(Article::class, $articleApparatObj);
183
184
        $articleApparatObj->asort();
185
        $articleApparatObj->ksort();
186
        $articleApparatObj->uasort(function () {
0 ignored issues
show
Documentation introduced by
function () { } is of type object<Closure>, but the function expects a object<Callable>.

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...
187
        });
188
        $articleApparatObj->uksort(function () {
0 ignored issues
show
Documentation introduced by
function () { } is of type object<Closure>, but the function expects a object<Callable>.

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...
189
        });
190
        $articleApparatObj->natsort();
191
        $articleApparatObj->natcasesort();
192
193
        /** @var ApplicationArticle $articleObj */
194
        $articleObj = Object::load(self::ARTICLE_LOCATOR);
195
        $articleApparatObj->exchangeArray($articleObj);
196
        $articleApparatObj->exchangeArray(new \stdClass());
197
    }
198
199
    /**
200
     * Test illegal unsetting of apparat object property
201
     *
202
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
203
     * @expectedExceptionCode 1465330565
204
     */
205
    public function testArticleApparatObjectUnvalidUnset()
206
    {
207
        /** @var Article $articleApparatObj */
208
        $articleApparatObj = RepositoryFacade::instance('repo')->loadObject(self::ARTICLE_LOCATOR);
209
        unset($articleApparatObj['name']);
210
    }
211
212
    /**
213
     * Test invalid apparat object getter
214
     *
215
     * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
216
     * @expectedExceptionCode 1465330399
217
     */
218
    public function testInvalidApparatObjectGetter()
219
    {
220
        /** @var ApplicationArticle $articleObj */
221
        $articleObj = Object::load(self::ARTICLE_LOCATOR);
222
        $articleApparatObj = Kernel::create(TestApparatObject::class, [$articleObj]);
223
        $articleApparatObj['invalid'];
224
    }
225
226
    /**
227
     * Test the contact object
228
     */
229
    public function testContactObject()
230
    {
231
        $contactApparatObject = RepositoryFacade::instance('repo')->loadObject(self::CONTACT_LOCATOR);
232
        $this->assertInstanceOf(Contact::class, $contactApparatObject);
233
    }
234
}
235