|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP: Nelson Martell Library file |
|
4
|
|
|
* |
|
5
|
|
|
* Content: |
|
6
|
|
|
* - Test case for: [NelsonMartell\Extensions] String |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright © 2016 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 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 as NML; |
|
22
|
|
|
use NelsonMartell\Extensions\String; |
|
23
|
|
|
use NelsonMartell\Type; |
|
24
|
|
|
use \PHPUnit_Framework_TestCase as TestCase; |
|
25
|
|
|
use \InvalidArgumentException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Test case for `NelsonMartell\Extensions\String` class. |
|
29
|
|
|
* |
|
30
|
|
|
* @see String |
|
31
|
|
|
* @author Nelson Martell <[email protected]> |
|
32
|
|
|
* |
|
33
|
|
|
* @internal |
|
34
|
|
|
* */ |
|
35
|
|
|
class StringTest extends TestCase |
|
36
|
|
|
{ |
|
37
|
|
|
public function testPerformsFormatForStringWithIntegerPlaceholders() |
|
38
|
|
|
{ |
|
39
|
|
|
$expected = 'Mi nombre es Juan'; |
|
40
|
|
|
$actual = String::format('Mi nombre es {0}', ['Juan']); |
|
|
|
|
|
|
41
|
|
|
$this->assertEquals($expected, $actual); |
|
42
|
|
|
|
|
43
|
|
|
$actual = String::format("Mi nombre es {0}", 'Juan'); |
|
|
|
|
|
|
44
|
|
|
$this->assertEquals($expected, $actual); |
|
45
|
|
|
|
|
46
|
|
|
$name = 'Juan'; |
|
|
|
|
|
|
47
|
|
|
$actual = String::format("Mi nombre es {0}", $name); |
|
|
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $actual); |
|
49
|
|
|
|
|
50
|
|
|
$expected = 'Me llamo Nelson y tengo 29 años de edad.'; |
|
51
|
|
|
$name = 'Nelson'; |
|
|
|
|
|
|
52
|
|
|
$age = 29; |
|
|
|
|
|
|
53
|
|
|
$actual = String::format('Me llamo {0} y tengo {1} años de edad.', $name, $age); |
|
|
|
|
|
|
54
|
|
|
$this->assertEquals($expected, $actual); |
|
55
|
|
|
|
|
56
|
|
|
$actual = String::format('Me llamo {0} y tengo {1} años de edad.', [$name, $age]); |
|
57
|
|
|
$this->assertEquals($expected, $actual); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testPerformsFormatForStringsWithStringPlaceholders() |
|
61
|
|
|
{ |
|
62
|
|
|
$expected = 'Mi nombre es Juan'; |
|
63
|
|
|
$actual = String::format('Mi nombre es {name}', ["name" => "Juan"]); |
|
|
|
|
|
|
64
|
|
|
$this->assertEquals($expected, $actual); |
|
65
|
|
|
|
|
66
|
|
|
$expected = 'Tengo 20 años de edad.'; |
|
67
|
|
|
$actual = String::format('Tengo {age} años de edad.', ["name" => "Juan", 'age' => 20]); |
|
|
|
|
|
|
68
|
|
|
$this->assertEquals($expected, $actual); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testPerformsFormatWithManyData() |
|
72
|
|
|
{ |
|
73
|
|
|
$expected = 'Mi nombre es Juan y tengo 61 años de edad.'; |
|
74
|
|
|
$format = 'Mi nombre es {name} y tengo {age} años de edad.'; |
|
75
|
|
|
$actual = String::format($format, ['age' => 61, 'name' => 'Juan']); |
|
76
|
|
|
$this->assertEquals($expected, $actual); |
|
77
|
|
|
|
|
78
|
|
|
$format = 'Mi nombre es {1} y tengo {0} años de edad.'; |
|
79
|
|
|
$actual = String::format($format, [61, 'Juan']); |
|
80
|
|
|
$this->assertEquals($expected, $actual); |
|
81
|
|
|
|
|
82
|
|
|
$actual = String::format($format, 61, 'Juan'); |
|
83
|
|
|
$this->assertEquals($expected, $actual); |
|
84
|
|
|
|
|
85
|
|
|
$expected = 'Números: 1, 2, 3, 4, 5, 6, 7, 8, 9 y 10.'; |
|
86
|
|
|
$format = 'Números: '; |
|
87
|
|
|
for ($i = 0; $i < 8; $i++) { |
|
88
|
|
|
$format .= "{{$i}}, "; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
$format .= '{8} y {9}.'; |
|
91
|
|
|
$actual = String::format($format, range(1, 10)); |
|
|
|
|
|
|
92
|
|
|
$this->assertEquals($expected, $actual); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @depends testPerformsFormatWithManyData |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testPerformsFormatIgnoringNotMatchingData() |
|
99
|
|
|
{ |
|
100
|
|
|
$expected = 'Mi nombre es {name} y tengo {age} años de edad.'; |
|
101
|
|
|
$actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['fake_name' => 'Juan']); |
|
|
|
|
|
|
102
|
|
|
$this->assertEquals($expected, $actual); |
|
103
|
|
|
|
|
104
|
|
|
$actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', null); |
|
105
|
|
|
$this->assertEquals($expected, $actual); |
|
106
|
|
|
|
|
107
|
|
|
$actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', null, null, null); |
|
108
|
|
|
$this->assertEquals($expected, $actual); |
|
109
|
|
|
|
|
110
|
|
|
$expected = 'Mi nombre es Juan y tengo {age} años de edad.'; |
|
111
|
|
|
$actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['name' => 'Juan']); |
|
|
|
|
|
|
112
|
|
|
$this->assertEquals($expected, $actual); |
|
113
|
|
|
|
|
114
|
|
|
$expected = 'Mi nombre es {name} y tengo 54 años de edad.'; |
|
115
|
|
|
$actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['age' => 54]); |
|
|
|
|
|
|
116
|
|
|
$this->assertEquals($expected, $actual); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testPerformsFormatWithArgumentsOfDifferentTypesConvertiblesToString() |
|
120
|
|
|
{ |
|
121
|
|
|
$expected = 'Invalid argument type. "major" (position 0) must to be an instance of "integer"; "string" given.'; |
|
122
|
|
|
|
|
123
|
|
|
$major = 'Non Number'; |
|
124
|
|
|
$args = [ |
|
|
|
|
|
|
125
|
|
|
'name' => 'major', |
|
126
|
|
|
'expected' => NML\typeof(0), |
|
|
|
|
|
|
127
|
|
|
'pos' => 0, |
|
128
|
|
|
'actual' => NML\typeof($major), |
|
|
|
|
|
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
|
|
// Esto produce un error fatal debido a una debilidad en el uso de la función `asort` en el método |
|
132
|
|
|
// ``Cake\Utility\Text::.insert()``, ya que debería comparar como cadena usando el flag `SORT_STRING` |
|
133
|
|
|
// pero que se evita convirtiendo los valores a string en el método String::format(). |
|
134
|
|
|
|
|
135
|
|
|
$actual = String::format('Invalid argument type.', null); |
|
|
|
|
|
|
136
|
|
|
$actual .= String::format( |
|
137
|
|
|
' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.', |
|
138
|
|
|
$args |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertEquals($expected, $actual); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @expectedException InvalidArgumentException |
|
146
|
|
|
* @dataProvider nonStringObjectsProvider |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testDoNotPerformsFormatWithPlaceholdersValuesNotConvertiblesToString($obj) |
|
149
|
|
|
{ |
|
150
|
|
|
$format = PHP_EOL.'This test should throws an "{0}" with data: {testErrorData}.'.PHP_EOL; |
|
151
|
|
|
|
|
152
|
|
|
$str = String::format($format, [InvalidArgumentException::class, 'testErrorData' => $obj]); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function nonStringObjectsProvider() |
|
156
|
|
|
{ |
|
157
|
|
|
return [ |
|
158
|
|
|
'stdClass' => [new \stdClass], |
|
159
|
|
|
'int[]' => [[10, 20, 30, 40]], |
|
160
|
|
|
'string[]' => [['ten', '20', '30', '40']], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
|
|
|
|
|
164
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.