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