1212use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
1313use Magento \Framework \Locale \ResolverInterface ;
1414use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
15+ use Magento \Framework \Stdlib \StringUtils ;
1516use PHPUnit \Framework \MockObject \MockObject ;
1617use PHPUnit \Framework \TestCase ;
1718use Psr \Log \LoggerInterface ;
@@ -43,6 +44,11 @@ class PostcodeTest extends TestCase
4344 */
4445 private $ loggerMock ;
4546
47+ /**
48+ * @var StringUtils|MockObject
49+ */
50+ private $ stringHelperMock ;
51+
4652 protected function setUp (): void
4753 {
4854 $ this ->localeMock = $ this ->getMockBuilder (TimezoneInterface::class)
@@ -54,9 +60,12 @@ protected function setUp(): void
5460 $ this ->directoryHelperMock = $ this ->getMockBuilder (\Magento \Directory \Helper \Data::class)
5561 ->disableOriginalConstructor ()
5662 ->getMock ();
63+ $ this ->stringHelperMock = $ this ->getMockBuilder (StringUtils::class)
64+ ->disableOriginalConstructor ()
65+ ->getMock ();
5766 $ this ->attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
5867 ->disableOriginalConstructor ()
59- ->addMethods (['getStoreLabel ' ])
68+ ->addMethods (['getStoreLabel ' , ' getValidateRules ' ])
6069 ->getMock ();
6170 }
6271
@@ -85,7 +94,8 @@ public function testValidateValue($value, $expected, $countryId, $isOptional)
8594 $ this ->localeMock ,
8695 $ this ->loggerMock ,
8796 $ this ->localeResolverMock ,
88- $ this ->directoryHelperMock
97+ $ this ->directoryHelperMock ,
98+ $ this ->stringHelperMock
8999 );
90100 $ object ->setAttribute ($ this ->attributeMock );
91101 $ object ->setExtractedData (['country_id ' => $ countryId ]);
@@ -106,4 +116,114 @@ public static function validateValueDataProvider()
106116 ['90034 ' , true , 'IE ' , true ],
107117 ];
108118 }
119+
120+ /**
121+ * Test validation of length and input rules
122+ *
123+ * @param string $value
124+ * @param bool|array $expected
125+ * @param array $validateRules
126+ * @param string $countryId
127+ * @param bool $isOptional
128+ *
129+ * @dataProvider validateValueWithRulesDataProvider
130+ */
131+ public function testValidateValueWithRules (
132+ string $ value ,
133+ bool |array $ expected ,
134+ array $ validateRules ,
135+ string $ countryId ,
136+ bool $ isOptional
137+ ) {
138+ $ storeLabel = 'Zip/Postal Code ' ;
139+ $ this ->attributeMock ->expects ($ this ->any ())
140+ ->method ('getStoreLabel ' )
141+ ->willReturn ($ storeLabel );
142+
143+ $ this ->attributeMock ->expects ($ this ->any ())
144+ ->method ('getValidateRules ' )
145+ ->willReturn ($ validateRules );
146+
147+ $ this ->directoryHelperMock ->expects ($ this ->once ())
148+ ->method ('isZipCodeOptional ' )
149+ ->willReturnMap ([
150+ [$ countryId , $ isOptional ],
151+ ]);
152+
153+ if (!empty ($ validateRules ['max_text_length ' ])) {
154+ $ this ->stringHelperMock ->expects ($ this ->any ())
155+ ->method ('strlen ' )
156+ ->willReturnCallback (function ($ str ) {
157+ return strlen (trim ($ str ));
158+ });
159+ }
160+
161+ $ object = new Postcode (
162+ $ this ->localeMock ,
163+ $ this ->loggerMock ,
164+ $ this ->localeResolverMock ,
165+ $ this ->directoryHelperMock ,
166+ $ this ->stringHelperMock
167+ );
168+ $ object ->setAttribute ($ this ->attributeMock );
169+ $ object ->setExtractedData (['country_id ' => $ countryId ]);
170+
171+ $ actual = $ object ->validateValue ($ value );
172+
173+ if (is_array ($ expected )) {
174+ $ this ->assertIsArray ($ actual );
175+ $ this ->assertCount (count ($ expected ), $ actual );
176+ foreach ($ expected as $ key => $ expectedMessage ) {
177+ $ actualMessage = $ actual [$ key ];
178+ // Convert Phrase to string if needed
179+ if ($ actualMessage instanceof \Magento \Framework \Phrase) {
180+ $ actualMessage = $ actualMessage ->__toString ();
181+ }
182+ $ this ->assertStringContainsString ($ expectedMessage , $ actualMessage );
183+ }
184+ } else {
185+ $ this ->assertEquals ($ expected , $ actual );
186+ }
187+ }
188+
189+ /**
190+ * @return array
191+ */
192+ public static function validateValueWithRulesDataProvider ()
193+ {
194+ return [
195+ // Test min length validation
196+ [
197+ '12 ' ,
198+ ['"Zip/Postal Code" length must be equal or greater than 5 characters. ' ],
199+ ['input_validation ' => 'alphanumeric ' , 'min_text_length ' => 5 ],
200+ 'US ' ,
201+ false
202+ ],
203+ // Test max length validation
204+ [
205+ '1234567890 ' ,
206+ ['"Zip/Postal Code" length must be equal or less than 6 characters. ' ],
207+ ['input_validation ' => 'alphanumeric ' , 'max_text_length ' => 6 ],
208+ 'US ' ,
209+ false
210+ ],
211+ // Test valid length
212+ [
213+ '12345 ' ,
214+ true ,
215+ ['input_validation ' => 'alphanumeric ' , 'min_text_length ' => 5 , 'max_text_length ' => 6 ],
216+ 'US ' ,
217+ false
218+ ],
219+ // Test no validation rules
220+ [
221+ '90034 ' ,
222+ true ,
223+ [],
224+ 'US ' ,
225+ false
226+ ],
227+ ];
228+ }
109229}
0 commit comments