DatatypeTest::testUrlDataTypeWithInvalidUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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\Properties\Datatype\ApparatUrl;
41
use Apparat\Object\Application\Model\Properties\Datatype\Datetime;
42
use Apparat\Object\Application\Model\Properties\Datatype\Email;
43
use Apparat\Object\Application\Model\Properties\Datatype\Geo;
44
use Apparat\Object\Application\Model\Properties\Datatype\Integer;
45
use Apparat\Object\Application\Model\Properties\Datatype\Number;
46
use Apparat\Object\Application\Model\Properties\Datatype\Sentence;
47
use Apparat\Object\Application\Model\Properties\Datatype\Text;
48
use Apparat\Object\Application\Model\Properties\Datatype\Token;
49
use Apparat\Object\Application\Model\Properties\Datatype\Url;
50
use Apparat\Object\Domain\Model\Object\ObjectInterface;
51
use Apparat\Object\Domain\Model\Uri\GeoUri;
52
use Apparat\Object\Infrastructure\Model\Object\Object;
53
54
/**
55
 * Property model tests
56
 *
57
 * @package Apparat\Object
58
 * @subpackage Apparat\Object\Tests
59
 */
60
class DatatypeTest extends AbstractRepositoryEnabledTest
61
{
62
    /**
63
     * Object
64
     *
65
     * @var ObjectInterface
66
     */
67
    protected static $object;
68
    /**
69
     * Example object locator
70
     *
71
     * @var string
72
     */
73
    const ARTICLE_LOCATOR = '/2015/12/21/1-article/1';
74
    /**
75
     * Example Url
76
     *
77
     * @var string
78
     */
79
    const URL = 'http://example.com/path/to/resource?var=val';
80
81
    /**
82
     * Setup
83
     */
84
    public static function setUpBeforeClass()
85
    {
86
        parent::setUpBeforeClass();
87
        self::$object = Object::load(getenv('REPOSITORY_URL').self::ARTICLE_LOCATOR);
88
    }
89
90
    /**
91
     * Test the apparat URL data type without valid filters
92
     *
93
     * @expectedException \InvalidArgumentException
94
     */
95
    public function testApparatUrlDataTypeWithoutFilter()
96
    {
97
        /** @var ApparatUrl $apparatUrlDatatype */
98
        $apparatUrlDatatype = Kernel::create(ApparatUrl::class, [self::$object, ['invalid']]);
99
        $this->assertInstanceOf(ApparatUrl::class, $apparatUrlDatatype);
100
        $apparatUrlDatatype->match(self::ARTICLE_LOCATOR);
101
    }
102
103
    /**
104
     * Test the URL data type with an invalid value
105
     *
106
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
107
     */
108
    public function testUrlDataTypeWithInvalidUrl()
109
    {
110
        /** @var Url $urlDatatype */
111
        $urlDatatype = Kernel::create(Url::class, [self::$object, []]);
112
        $this->assertInstanceOf(Url::class, $urlDatatype);
113
        $this->assertInstanceOf(\Apparat\Object\Domain\Model\Uri\Url::class, $urlDatatype->match(self::URL));
114
        $urlDatatype->match('http://');
115
    }
116
117
    /**
118
     * Test the Sentence data type with a multiline text
119
     *
120
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
121
     */
122
    public function testSentenceDataTypeWithMultilineText()
123
    {
124
        /** @var Sentence $sentenceDatatype */
125
        $sentenceDatatype = Kernel::create(Sentence::class, [self::$object, []]);
126
        $this->assertInstanceOf(Sentence::class, $sentenceDatatype);
127
        $this->assertEquals('Test sentence', $sentenceDatatype->match('Test sentence'));
128
        $sentenceDatatype->match("Test\nsentence");
129
    }
130
131
    /**
132
     * Test the Email data type with an invalid email address
133
     *
134
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
135
     */
136
    public function testEmailDataTypeWithInvalidEmail()
137
    {
138
        /** @var Email $emailDatatype */
139
        $emailDatatype = Kernel::create(Email::class, [self::$object, []]);
140
        $this->assertInstanceOf(Email::class, $emailDatatype);
141
        $this->assertEquals('[email protected]', $emailDatatype->match('[email protected]'));
142
        $emailDatatype->match('invalid-email');
143
    }
144
145
    /**
146
     * Test the Token data type with whitespace
147
     *
148
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
149
     */
150
    public function testTokenDataTypeWithWhitespace()
151
    {
152
        /** @var Token $tokenDatatype */
153
        $tokenDatatype = Kernel::create(Token::class, [self::$object, []]);
154
        $this->assertInstanceOf(Token::class, $tokenDatatype);
155
        $this->assertEquals('token', $tokenDatatype->match('token'));
156
        $tokenDatatype->match('invalid token');
157
    }
158
159
    /**
160
     * Test the Text data type
161
     */
162
    public function testTextDataType()
163
    {
164
        /** @var Text $textDatatype */
165
        $textDatatype = Kernel::create(Text::class, [self::$object, []]);
166
        $this->assertInstanceOf(Text::class, $textDatatype);
167
        $this->assertEquals("Text\nwith\nseveral\nlines", $textDatatype->match("Text\nwith\nseveral\nlines"));
168
    }
169
170
    /**
171
     * Test the Datetime data type with a timestamp
172
     *
173
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
174
     */
175
    public function testDatetimeDataType()
176
    {
177
        $now = new \DateTimeImmutable();
178
        $datetimeDatatype = Kernel::create(Datetime::class, [self::$object, []]);
179
        $this->assertInstanceOf(Datetime::class, $datetimeDatatype);
180
        $this->assertEquals($now, $datetimeDatatype->match($now->format('c')));
181
        $datetimeDatatype->match(time());
182
    }
183
184
    /**
185
     * Test the Geo data type with an invalid Geo URI
186
     *
187
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
188
     */
189
    public function testGeoDataTypeWithInvalidGeoUrl()
190
    {
191
        $geoUriStr = 'geo:1.23,-4.56,678.9';
192
        /** @var GeoUri $geoUri */
193
        $geoUri = Kernel::create(GeoUri::class, [$geoUriStr]);
194
        $geoApparatUrlStr = '/2016/06/05/1-geo';
195
        /** @var \Apparat\Object\Domain\Model\Uri\ApparatUrl $geoApparatUrl */
196
        $geoApparatUrl = Kernel::create(
197
            \Apparat\Object\Domain\Model\Uri\ApparatUrl::class,
198
            [$geoApparatUrlStr, false, self::$object->getRepositoryLocator()->getRepository()]
199
        );
200
201
        /** @var Geo $geoDatatype */
202
        $geoDatatype = Kernel::create(Geo::class, [self::$object, []]);
203
        $this->assertInstanceOf(Geo::class, $geoDatatype);
204
        $this->assertEquals($geoUri, $geoDatatype->match($geoUriStr));
205
        $this->assertTrue($geoApparatUrl->matches($geoDatatype->match($geoApparatUrlStr)));
206
        $geoDatatype->match('Invalid Geo URI');
207
    }
208
209
    /**
210
     * Test the Integer data type with a string
211
     *
212
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
213
     */
214
    public function testIntegerDataTypeWithString()
215
    {
216
        /** @var \Apparat\Object\Application\Model\Properties\Datatype\Integer $integerDatatype */
217
        $integerDatatype = Kernel::create(Integer::class, [self::$object, []]);
218
        $this->assertInstanceOf(Integer::class, $integerDatatype);
219
        $this->assertEquals(100, $integerDatatype->match('100'));
220
        $integerDatatype->match('string');
221
    }
222
223
    /**
224
     * Test the Number data type with a string
225
     *
226
     * @expectedException \Apparat\Object\Application\Model\Properties\Domain\DomainException
227
     */
228
    public function testNumberDataTypeWithString()
229
    {
230
        /** @var \Apparat\Object\Application\Model\Properties\Datatype\Number $numberDatatype */
231
        $numberDatatype = Kernel::create(Number::class, [self::$object, []]);
232
        $this->assertInstanceOf(Number::class, $numberDatatype);
233
        $this->assertEquals(123.45, $numberDatatype->match('123.45'));
234
        $numberDatatype->match('string');
235
    }
236
}
237