1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Copyright © 2016-2019 Nelson Martell (http://nelson6e65.github.io) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License (MIT) |
8
|
|
|
* For full copyright and license information, please see the LICENSE |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016-2019 Nelson Martell |
12
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
13
|
|
|
* @since v0.6.0 |
14
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
15
|
|
|
* */ |
16
|
|
|
namespace NelsonMartell\Test\TestCase\Extensions; |
17
|
|
|
|
18
|
|
|
use stdClass; |
19
|
|
|
use ReflectionClass; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
|
22
|
|
|
use NelsonMartell\Extensions\Text; |
23
|
|
|
|
24
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\ToString as ClassString; |
25
|
|
|
|
26
|
|
|
use NelsonMartell\Test\Helpers\ExporterPlugin; |
27
|
|
|
use NelsonMartell\Test\Helpers\IComparerTester; |
28
|
|
|
|
29
|
|
|
use PHPUnit\Framework\TestCase; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test case for `NelsonMartell\Extensions\Text` class. |
33
|
|
|
* |
34
|
|
|
* @see Text |
35
|
|
|
* @author Nelson Martell <[email protected]> |
36
|
|
|
* |
37
|
|
|
* @internal |
38
|
|
|
* */ |
39
|
|
|
class TextTest extends TestCase |
40
|
|
|
{ |
41
|
|
|
use IComparerTester; |
42
|
|
|
use ExporterPlugin; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @dataProvider validPositionalArgsListProvider |
46
|
|
|
* @dataProvider validNamedArgsListProvider |
47
|
|
|
*/ |
48
|
|
|
public function testPerformsFormatWithSecuentialAndNotSecuentialData($expected, $format, $data, $positional = false) |
49
|
|
|
{ |
50
|
|
|
$actual = Text::format($format, $data); |
51
|
|
|
$this->assertEquals($expected, $actual); |
52
|
|
|
|
53
|
|
|
if ($positional) { |
54
|
|
|
$actual = Text::format($format, ...$data); |
55
|
|
|
$this->assertEquals($expected, $actual); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @expectedException InvalidArgumentException |
62
|
|
|
* @dataProvider nonStringObjectsProvider |
63
|
|
|
*/ |
64
|
|
|
public function testDoNotPerformsFormatWithPlaceholdersValuesNotConvertiblesToString($obj) |
65
|
|
|
{ |
66
|
|
|
Text::format('{0}: {1}', InvalidArgumentException::class, $obj); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function nonStringObjectsProvider() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
'stdClass' => [new \stdClass], |
73
|
|
|
'int[]' => [[10, 20, 30, 40]], |
74
|
|
|
'string[]' => [['ten', '20', '30', '40']], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* expected, format, data, secuential |
80
|
|
|
**/ |
81
|
|
|
public function validPositionalArgsListProvider() |
82
|
|
|
{ |
83
|
|
|
$secuential = true; |
84
|
|
|
|
85
|
|
|
return [ |
86
|
|
|
's: complete: basic array' => [ |
87
|
|
|
'Bob is 65 years old and has 101 cats.', |
88
|
|
|
'{0} is {1} years old and has {2} cats.', |
89
|
|
|
['Bob', 65, 101], |
90
|
|
|
$secuential, |
91
|
|
|
], |
92
|
|
|
's: complete: basic array with extra data not in placeholder' => [ |
93
|
|
|
'Bob is 65 years old and has 101 cats.', |
94
|
|
|
'{0} is {1} years old and has {2} cats.', |
95
|
|
|
['Bob', 65, 101, 'I am not here'], |
96
|
|
|
$secuential, |
97
|
|
|
], |
98
|
|
|
's: missing value for placeholder' => [ |
99
|
|
|
'Bob is 65 years old and has {2} cats.', |
100
|
|
|
'{0} is {1} years old and has {2} cats.', |
101
|
|
|
['Bob', 65], |
102
|
|
|
$secuential, |
103
|
|
|
], |
104
|
|
|
's: with some empty data' => [ |
105
|
|
|
'Bob is 65 years old and has cats.', |
106
|
|
|
'{0} is {1} years old and has {2} cats.', |
107
|
|
|
['Bob', 65, ''], |
108
|
|
|
$secuential, |
109
|
|
|
], |
110
|
|
|
's: with some null data' => [ |
111
|
|
|
'Bob is 65 years old and has cats.', |
112
|
|
|
'{0} is {1} years old and has {2} cats.', |
113
|
|
|
['Bob', 65, null], |
114
|
|
|
$secuential, |
115
|
|
|
], |
116
|
|
|
's: only 1 argument null' => [ |
117
|
|
|
'Null is .', |
118
|
|
|
'Null is {0}.', |
119
|
|
|
[null], |
120
|
|
|
$secuential, |
121
|
|
|
], |
122
|
|
|
's: class implementing IConvertibleToString' => [ |
123
|
|
|
'x = (-1, 1)', |
124
|
|
|
'{0} = ({1})', |
125
|
|
|
['x', new ClassString()], |
126
|
|
|
$secuential |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* expected, format, data, secuential = false |
133
|
|
|
**/ |
134
|
|
|
public function validNamedArgsListProvider() |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
'n: complete' => [ |
138
|
|
|
'Bob is 65 years old and has 101 cats.', |
139
|
|
|
'{name} is {age} years old and has {n} cats.', |
140
|
|
|
[ |
141
|
|
|
'name' => 'Bob', |
142
|
|
|
'age' => 65, |
143
|
|
|
'n' => 101 |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
'n: complete with numeric index' => [ |
147
|
|
|
'Bob is 65 years old and has 101 cats.', |
148
|
|
|
'{name} is {age} years old and has {7} cats.', |
149
|
|
|
[ |
150
|
|
|
'name' => 'Bob', |
151
|
|
|
'age' => 65, |
152
|
|
|
7 => 101 |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
'n: missing value for placeholder' => [ |
156
|
|
|
'Bob is 65 years old and has {n} cats.', |
157
|
|
|
'{name} is {age} years old and has {n} cats.', |
158
|
|
|
[ |
159
|
|
|
'name' => 'Bob', |
160
|
|
|
'age' => 65, |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
'n: complete with some empty value' => [ |
164
|
|
|
'Bob is 65 years old and has cats.', |
165
|
|
|
'{name} is {age} years old and has {n} cats.', |
166
|
|
|
[ |
167
|
|
|
'name' => 'Bob', |
168
|
|
|
'age' => 65, |
169
|
|
|
'n' => '' |
170
|
|
|
], |
171
|
|
|
], |
172
|
|
|
'n: complete with some null values' => [ |
173
|
|
|
'Bob is 65 years old and has cats.', |
174
|
|
|
'{name} is {age} years old and has {n} cats.', |
175
|
|
|
[ |
176
|
|
|
'name' => 'Bob', |
177
|
|
|
'age' => 65, |
178
|
|
|
'n' => null |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
'n: class implementing IConvertibleToString' => [ |
182
|
|
|
'x = (-1, 1)', |
183
|
|
|
'{var} = ({coords})', |
184
|
|
|
[ |
185
|
|
|
'var' => 'x', |
186
|
|
|
'coords' => new ClassString(), |
187
|
|
|
], |
188
|
|
|
], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// IComparerTester ============================================================================== |
193
|
|
|
|
194
|
|
|
public function getTargetClassName() |
195
|
|
|
{ |
196
|
|
|
return Text::class; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getTargetClassReflection() |
200
|
|
|
{ |
201
|
|
|
return new ReflectionClass($this->getTargetClassName()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
public function compareMethodArgumentsProvider() |
207
|
|
|
{ |
208
|
|
|
return [ |
209
|
|
|
'stdClass > string' => [1, new stdClass, 'stdClass'], |
210
|
|
|
'string < stdClass' => [-1, 'stdClass', new stdClass], |
211
|
|
|
'string > null' => [1, 's', null], |
212
|
|
|
'null < string' => [-1, null, 's'], |
213
|
|
|
'string (empty) == null' => [0, '', null], |
214
|
|
|
'null == string (empty)' => [0, null, ''], |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function compareMethodArraysProvider() |
219
|
|
|
{ |
220
|
|
|
return [ |
221
|
|
|
[[-1, 0, 1, 'b', 'c', 'd', 'z', 'z1', new stdClass]], |
222
|
|
|
]; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|