7
7
8
8
namespace Magento \Customer \Test \Unit \Model \Metadata \Form ;
9
9
10
+ use Magento \Customer \Api \Data \ValidationRuleInterface ;
10
11
use Magento \Customer \Model \Metadata \Form \Postcode ;
11
12
use Magento \Directory \Helper \Data as DirectoryHelper ;
13
+ use Magento \Framework \Phrase ;
12
14
use PHPUnit \Framework \MockObject \MockObject ;
15
+ use Magento \Framework \Stdlib \StringUtils ;
13
16
14
17
class PostcodeTest extends AbstractFormTestCase
15
18
{
19
+ /** @var StringUtils */
20
+ protected StringUtils $ stringHelper ;
21
+
16
22
/**
17
23
* @var DirectoryHelper|MockObject
18
24
*/
@@ -21,7 +27,7 @@ class PostcodeTest extends AbstractFormTestCase
21
27
protected function setUp (): void
22
28
{
23
29
parent ::setUp ();
24
-
30
+ $ this -> stringHelper = new StringUtils ();
25
31
$ this ->directoryHelper = $ this ->getMockBuilder (\Magento \Directory \Helper \Data::class)
26
32
->disableOriginalConstructor ()
27
33
->getMock ();
@@ -43,7 +49,8 @@ protected function getClass($value)
43
49
$ value ,
44
50
0 ,
45
51
false ,
46
- $ this ->directoryHelper
52
+ $ this ->directoryHelper ,
53
+ $ this ->stringHelper
47
54
);
48
55
}
49
56
@@ -87,4 +94,103 @@ public function validateValueDataProvider()
87
94
['90034 ' , true , 'IE ' , true ],
88
95
];
89
96
}
97
+
98
+ /**
99
+ * @param string|int|bool|null $value to assign to boolean
100
+ * @param string|bool $expected text output
101
+ * @dataProvider validateValueLengthDataProvider
102
+ */
103
+ public function testValidateValueLength ($ value , $ expected )
104
+ {
105
+ $ minTextLengthRule = $ this ->getMockBuilder (ValidationRuleInterface::class)
106
+ ->disableOriginalConstructor ()
107
+ ->setMethods (['getName ' , 'getValue ' ])
108
+ ->getMockForAbstractClass ();
109
+ $ minTextLengthRule ->expects ($ this ->any ())
110
+ ->method ('getName ' )
111
+ ->willReturn ('min_text_length ' );
112
+ $ minTextLengthRule ->expects ($ this ->any ())
113
+ ->method ('getValue ' )
114
+ ->willReturn (5 );
115
+
116
+ $ maxTextLengthRule = $ this ->getMockBuilder (ValidationRuleInterface::class)
117
+ ->disableOriginalConstructor ()
118
+ ->setMethods (['getName ' , 'getValue ' ])
119
+ ->getMockForAbstractClass ();
120
+ $ maxTextLengthRule ->expects ($ this ->any ())
121
+ ->method ('getName ' )
122
+ ->willReturn ('max_text_length ' );
123
+ $ maxTextLengthRule ->expects ($ this ->any ())
124
+ ->method ('getValue ' )
125
+ ->willReturn (6 );
126
+
127
+ $ inputValidationRule = $ this ->getMockBuilder (ValidationRuleInterface::class)
128
+ ->disableOriginalConstructor ()
129
+ ->setMethods (['getName ' , 'getValue ' ])
130
+ ->getMockForAbstractClass ();
131
+ $ inputValidationRule ->expects ($ this ->any ())
132
+ ->method ('getName ' )
133
+ ->willReturn ('input_validation ' );
134
+ $ inputValidationRule ->expects ($ this ->any ())
135
+ ->method ('getValue ' )
136
+ ->willReturn ('numeric ' );
137
+
138
+ $ validationRules = [
139
+ 'input_validation ' => $ inputValidationRule ,
140
+ 'min_text_length ' => $ minTextLengthRule ,
141
+ 'max_text_length ' => $ maxTextLengthRule ,
142
+ ];
143
+
144
+ $ this ->attributeMetadataMock ->expects (
145
+ $ this ->any ()
146
+ )->method (
147
+ 'getValidationRules '
148
+ )->willReturn (
149
+ $ validationRules
150
+ );
151
+
152
+ $ sut = $ this ->getClass ($ value );
153
+ $ actual = $ sut ->validateValue ($ value );
154
+
155
+ if (is_bool ($ actual )) {
156
+ $ this ->assertEquals ($ expected , $ actual );
157
+ } else {
158
+ if (is_array ($ actual )) {
159
+ $ actual = array_map (function ($ message ) {
160
+ return $ message instanceof Phrase ? $ message ->__toString () : $ message ;
161
+ }, $ actual );
162
+ }
163
+
164
+ if (is_array ($ expected )) {
165
+ foreach ($ expected as $ key => $ row ) {
166
+ $ this ->assertEquals ($ row , $ actual [$ key ]);
167
+ }
168
+ } else {
169
+ $ this ->assertContains ($ expected , $ actual );
170
+ }
171
+ }
172
+ }
173
+
174
+ /**
175
+ * @return array
176
+ */
177
+ public function validateValueLengthDataProvider (): array
178
+ {
179
+ return [
180
+ 'false ' => [false , ['"" is a required value. ' , '"" length must be equal or greater than 5 characters. ' ]],
181
+ 'empty ' => ['' , ['"" is a required value. ' , '"" length must be equal or greater than 5 characters. ' ]],
182
+ 'null ' => [null , ['"" is a required value. ' , '"" length must be equal or greater than 5 characters. ' ]],
183
+ 'one ' => [1 , '"" length must be equal or greater than 5 characters. ' ],
184
+ 'L1 ' => ['6 ' , '"" length must be equal or greater than 5 characters. ' ],
185
+ 'L2 ' => ['66 ' , '"" length must be equal or greater than 5 characters. ' ],
186
+ 'L5 ' => ['66666 ' , true ],
187
+ 'thousand ' => ['10000 ' , true ],
188
+ 'L6 ' => ['666666 ' , true ],
189
+ 'L7 ' => ['6666666 ' , '"" length must be equal or less than 6 characters. ' ],
190
+ 'S1 ' => ['s ' , ['"" length must be equal or greater than 5 characters. ' , "notDigits " => '"" contains non-numeric characters. ' ]],
191
+ 'S6 ' => ['string ' , ["notDigits " => '"" contains non-numeric characters. ' ]],
192
+ 'S7 ' => ['strings ' , ['"" length must be equal or less than 6 characters. ' , "notDigits " => '"" contains non-numeric characters. ' ]],
193
+ 'L6s ' => ['66666s ' , ["notDigits " => '"" contains non-numeric characters. ' ]],
194
+ ];
195
+ }
90
196
}
0 commit comments