77
88namespace Magento \Customer \Test \Unit \Model \Metadata \Form ;
99
10+ use Magento \Customer \Api \Data \ValidationRuleInterface ;
1011use Magento \Customer \Model \Metadata \Form \Postcode ;
1112use Magento \Directory \Helper \Data as DirectoryHelper ;
13+ use Magento \Framework \Phrase ;
1214use PHPUnit \Framework \MockObject \MockObject ;
15+ use Magento \Framework \Stdlib \StringUtils ;
1316
1417class PostcodeTest extends AbstractFormTestCase
1518{
19+ /** @var StringUtils */
20+ protected StringUtils $ stringHelper ;
21+
1622 /**
1723 * @var DirectoryHelper|MockObject
1824 */
@@ -21,7 +27,7 @@ class PostcodeTest extends AbstractFormTestCase
2127 protected function setUp (): void
2228 {
2329 parent ::setUp ();
24-
30+ $ this -> stringHelper = new StringUtils ();
2531 $ this ->directoryHelper = $ this ->getMockBuilder (\Magento \Directory \Helper \Data::class)
2632 ->disableOriginalConstructor ()
2733 ->getMock ();
@@ -43,7 +49,8 @@ protected function getClass($value)
4349 $ value ,
4450 0 ,
4551 false ,
46- $ this ->directoryHelper
52+ $ this ->directoryHelper ,
53+ $ this ->stringHelper
4754 );
4855 }
4956
@@ -87,4 +94,103 @@ public function validateValueDataProvider()
8794 ['90034 ' , true , 'IE ' , true ],
8895 ];
8996 }
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+ }
90196}
0 commit comments