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