1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-2016 Ryan Parman. |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* http://opensource.org/licenses/MIT |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Skyzyx\Tests\StrongTypes; |
27
|
|
|
|
28
|
|
|
use Skyzyx\StrongTypes\StringType; |
29
|
|
|
use Skyzyx\StrongTypes\StringType\Utf8String; |
30
|
|
|
use Skyzyx\StrongTypes\Util; |
31
|
|
|
|
32
|
|
|
class StringTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
public function testStringType() |
35
|
|
|
{ |
36
|
|
|
$type = new StringType('abc'); |
37
|
|
|
|
38
|
|
|
$this->assertEquals('Skyzyx\StrongTypes\StringType', get_class($type)); |
39
|
|
|
$this->assertEquals('abc', (string) $type); |
40
|
|
|
$this->assertEquals('abc', $type->getValue()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @expectedException UnexpectedValueException |
45
|
|
|
* @expectedExceptionMessage The Skyzyx\StrongTypes\StringType class expects a value of type string. |
46
|
|
|
* Received a value of type integer instead. |
47
|
|
|
*/ |
48
|
|
|
public function testStringException() |
49
|
|
|
{ |
50
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
51
|
|
|
new StringType(123); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testClassOrType() |
55
|
|
|
{ |
56
|
|
|
$type = new StringType('abc'); |
57
|
|
|
$this->assertEquals('string', Util::getClassOrType('abc')); |
58
|
|
|
$this->assertEquals('Skyzyx\StrongTypes\StringType', Util::getClassOrType($type)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testExactLengthOk() |
62
|
|
|
{ |
63
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
64
|
|
|
new TestString2('abcde'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException InvalidArgumentException |
69
|
|
|
* @expectedExceptionMessage The parameter was expecting an integer, but instead received a value of type string. |
70
|
|
|
*/ |
71
|
|
|
public function testExactLengthFail() |
72
|
|
|
{ |
73
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
74
|
|
|
new TestString3('abcde'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException InvalidArgumentException |
79
|
|
|
* @expectedExceptionMessage The parameter was expecting an integer, but instead received a value of type |
80
|
|
|
* Skyzyx\StrongTypes\String. |
81
|
|
|
*/ |
82
|
|
|
public function testExactLengthFail2() |
83
|
|
|
{ |
84
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
85
|
|
|
new TestString4('abcde'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testLengthOk() |
89
|
|
|
{ |
90
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
91
|
|
|
new TestString('abcdefghijkl'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testLengthOk2() |
95
|
|
|
{ |
96
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
97
|
|
|
new TestString('ಠ_ಠ'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @expectedException LengthException |
102
|
|
|
* @expectedExceptionMessage The length of the Skyzyx\Tests\StrongTypes\TestString object is 3, |
103
|
|
|
* but MUST be between 5 and 20. |
104
|
|
|
*/ |
105
|
|
|
public function testMinLengthFail() |
106
|
|
|
{ |
107
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
108
|
|
|
new TestString('abc'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @expectedException LengthException |
113
|
|
|
* @expectedExceptionMessage The length of the Skyzyx\Tests\StrongTypes\TestString object is 26, |
114
|
|
|
* but MUST be between 5 and 20. |
115
|
|
|
*/ |
116
|
|
|
public function testMaxLengthFail() |
117
|
|
|
{ |
118
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
119
|
|
|
new TestString('abcdefghijklmnopqrstuvwxyz'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @expectedException LengthException |
124
|
|
|
* @expectedExceptionMessage The length of the Skyzyx\Tests\StrongTypes\TestString object is 50, |
125
|
|
|
* but MUST be between 5 and 20. |
126
|
|
|
*/ |
127
|
|
|
public function testMaxLengthFail2() |
128
|
|
|
{ |
129
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
130
|
|
|
new TestString('ಠ_ಠ ♪♫℃℉αβΔΣ‼︎⁈…æœ℅␛'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @expectedException LengthException |
135
|
|
|
* @expectedExceptionMessage The length of the Skyzyx\Tests\StrongTypes\TestUtf8String object is 3, |
136
|
|
|
* but MUST be between 5 and 20. |
137
|
|
|
*/ |
138
|
|
|
public function testMinUtf8LengthOk() |
139
|
|
|
{ |
140
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
141
|
|
|
new TestUtf8String('æœ℅'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testMaxUtf8LengthOk() |
145
|
|
|
{ |
146
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
147
|
|
|
new TestUtf8String('ಠ_ಠ ♪♫℃℉αβΔΣ‼︎⁈…æœ℅␛'); |
148
|
|
|
new TestUtf8String('나는 유리를 먹을 수 있어요. 그래도'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testAsciiFromUnicode() |
152
|
|
|
{ |
153
|
|
|
$ascii = StringType::fromUnicode('\u0041\u0042\u0043'); |
154
|
|
|
$this->assertEquals('ABC', $ascii->getValue()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testAsciiFromUnicode2() |
158
|
|
|
{ |
159
|
|
|
$ascii = StringType::fromUnicode('\u1F60A'); |
160
|
|
|
$this->assertEquals('😊', $ascii->getValue()); |
161
|
|
|
$this->assertEquals(4, $ascii->getLength()); // String will always get this wrong for multibyte. |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testUtf8FromUnicode() |
165
|
|
|
{ |
166
|
|
|
$utf8 = Utf8String::fromUnicode('\u1F60A'); |
167
|
|
|
$this->assertEquals('😊', $utf8->getValue()); |
168
|
|
|
$this->assertEquals(1, $utf8->getLength()); // Utf8String should always get this right. |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testAsciiFromBytes() |
172
|
|
|
{ |
173
|
|
|
$ascii = StringType::fromBytes('\xF0\x9F\x98\x8A'); |
174
|
|
|
$this->assertEquals('😊', $ascii->getValue()); |
175
|
|
|
$this->assertEquals(4, $ascii->getLength()); // String will always get this wrong for multibyte. |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testUtf8FromBytes() |
179
|
|
|
{ |
180
|
|
|
$utf8 = Utf8String::fromBytes('\xF0\x9F\x98\x8A'); |
181
|
|
|
$this->assertEquals('😊', $utf8->getValue()); |
182
|
|
|
$this->assertEquals(1, $utf8->getLength()); // Utf8String should always get this right. |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testUtf8FromBytes2() |
186
|
|
|
{ |
187
|
|
|
$utf8 = Utf8String::fromBytes('Look at me smile! \xF0\x9F\x98\x8A'); |
188
|
|
|
$this->assertEquals('Look at me smile! 😊', $utf8->getValue()); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|