Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ValidatorTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $validator;
16 
17  protected function setUp()
18  {
20  $this->validator = $objectManager->create(\Magento\Directory\Model\Country\Postcode\ValidatorInterface::class);
21  }
22 
26  public function testPostCodes($countryId, $validPostcode)
27  {
28  try {
29  $this->assertTrue($this->validator->validate($validPostcode, $countryId));
30  $this->assertFalse($this->validator->validate('INVALID-100', $countryId));
31  } catch (\InvalidArgumentException $ex) {
32  //skip validation test for none existing countryId
33  }
34  }
35 
41  {
42  $this->validator->validate('12345', 'INVALID-CODE');
43  }
44 
48  public function testInvalidCanadaZipCode($countryId, $invalidPostCode)
49  {
50  $this->assertFalse($this->validator->validate($invalidPostCode, $countryId));
51  }
52 
56  public function testValidCanadaZipCode($countryId, $validPostCode)
57  {
58  $this->assertTrue($this->validator->validate($validPostCode, $countryId));
59  }
60 
64  public function getCanadaInvalidPostCodes()
65  {
66  return [
67  ['countryId' => 'CA', 'postcode' => '12345'],
68  ['countryId' => 'CA', 'postcode' => 'A1B2C3D'],
69  ['countryId' => 'CA', 'postcode' => 'A1B2C'],
70  ['countryId' => 'CA', 'postcode' => 'A1B 2C3'],
71  ];
72  }
73 
77  public function getCanadaValidPostCodes()
78  {
79  return [
80  ['countryId' => 'CA', 'postcode' => 'A1B2C3'],
81  ['countryId' => 'CA', 'postcode' => 'A1B 2C3'],
82  ['countryId' => 'CA', 'postcode' => 'Z9Y 8X7'],
83  ['countryId' => 'CA', 'postcode' => 'Z9Y8X7'],
84  ];
85  }
86 
91  public function getPostcodesDataProvider()
92  {
93  return [
94  ['countryId' => 'DZ', 'postcode' => '12345'],
95  ['countryId' => 'AS', 'postcode' => '12345'],
96  ['countryId' => 'AR', 'postcode' => '1234'],
97  ['countryId' => 'AM', 'postcode' => '123456'],
98  ['countryId' => 'AU', 'postcode' => '1234'],
99  ['countryId' => 'AT', 'postcode' => '1234'],
100  ['countryId' => 'AZ', 'postcode' => '1234'],
101  ['countryId' => 'AZ', 'postcode' => '123456'],
102  ['countryId' => 'BD', 'postcode' => '1234'],
103  ['countryId' => 'BY', 'postcode' => '123456'],
104  ['countryId' => 'BE', 'postcode' => '1234'],
105  ['countryId' => 'BA', 'postcode' => '12345'],
106  ['countryId' => 'BR', 'postcode' => '12345'],
107  ['countryId' => 'BR', 'postcode' => '12345-678'],
108  ['countryId' => 'BN', 'postcode' => 'PS1234'],
109  ['countryId' => 'BG', 'postcode' => '1234'],
110  ['countryId' => 'CA', 'postcode' => 'P9M 3T6'],
111  ['countryId' => 'IC', 'postcode' => '12345'],
112  ['countryId' => 'CN', 'postcode' => '123456'],
113  ['countryId' => 'HR', 'postcode' => '12345'],
114  ['countryId' => 'CU', 'postcode' => '12345'],
115  ['countryId' => 'CY', 'postcode' => '1234'],
116  ['countryId' => 'CZ', 'postcode' => '123 45'],
117  ['countryId' => 'DK', 'postcode' => '1234'],
118  ['countryId' => 'EE', 'postcode' => '12345'],
119  ['countryId' => 'FI', 'postcode' => '12345'],
120  ['countryId' => 'FR', 'postcode' => '12345'],
121  ['countryId' => 'GF', 'postcode' => '12345'],
122  ['countryId' => 'GE', 'postcode' => '1234'],
123  ['countryId' => 'DE', 'postcode' => '12345'],
124  ['countryId' => 'GR', 'postcode' => '123 45'],
125  ['countryId' => 'GL', 'postcode' => '1234'],
126  ['countryId' => 'GP', 'postcode' => '12345'],
127  ['countryId' => 'GU', 'postcode' => '12345'],
128  ['countryId' => 'GG', 'postcode' => 'PL5 7TH'],
129  ['countryId' => 'HU', 'postcode' => '1234'],
130  ['countryId' => 'IS', 'postcode' => '123'],
131  ['countryId' => 'IN', 'postcode' => '123456'],
132  ['countryId' => 'ID', 'postcode' => '12345'],
133  ['countryId' => 'IL', 'postcode' => '1234567'],
134  ['countryId' => 'IT', 'postcode' => '12345'],
135  ['countryId' => 'JP', 'postcode' => '123-4567'],
136  ['countryId' => 'JP', 'postcode' => '1234567'],
137  ['countryId' => 'JE', 'postcode' => 'TY8 9PL'],
138  ['countryId' => 'KZ', 'postcode' => '123456'],
139  ['countryId' => 'KE', 'postcode' => '12345'],
140  ['countryId' => 'KR', 'postcode' => '123-456'],
141  ['countryId' => 'KG', 'postcode' => '123456'],
142  ['countryId' => 'LV', 'postcode' => '1234'],
143  ['countryId' => 'LI', 'postcode' => '1234'],
144  ['countryId' => 'LT', 'postcode' => '12345'],
145  ['countryId' => 'LU', 'postcode' => '1234'],
146  ['countryId' => 'MK', 'postcode' => '1234'],
147  ['countryId' => 'MG', 'postcode' => '123'],
148  ['countryId' => 'MY', 'postcode' => '12345'],
149  ['countryId' => 'MV', 'postcode' => '12345'],
150  ['countryId' => 'MV', 'postcode' => '1234'],
151  ['countryId' => 'MT', 'postcode' => 'WRT 123'],
152  ['countryId' => 'MT', 'postcode' => 'WRT 45'],
153  ['countryId' => 'MH', 'postcode' => '12345'],
154  ['countryId' => 'MQ', 'postcode' => '12345'],
155  ['countryId' => 'MX', 'postcode' => '12345'],
156  ['countryId' => 'MD', 'postcode' => '1234'],
157  ['countryId' => 'MC', 'postcode' => '12345'],
158  ['countryId' => 'MN', 'postcode' => '123456'],
159  ['countryId' => 'MA', 'postcode' => '12345'],
160  ['countryId' => 'NL', 'postcode' => '1234 TR'],
161  ['countryId' => 'NO', 'postcode' => '1234'],
162  ['countryId' => 'PK', 'postcode' => '12345'],
163  ['countryId' => 'PH', 'postcode' => '1234'],
164  ['countryId' => 'PL', 'postcode' => '12-345'],
165  ['countryId' => 'PT', 'postcode' => '1234'],
166  ['countryId' => 'PT', 'postcode' => '1234-567'],
167  ['countryId' => 'PR', 'postcode' => '12345'],
168  ['countryId' => 'RE', 'postcode' => '12345'],
169  ['countryId' => 'RO', 'postcode' => '123456'],
170  ['countryId' => 'RU', 'postcode' => '123456'],
171  ['countryId' => 'MP', 'postcode' => '12345'],
172  ['countryId' => 'CS', 'postcode' => '12345'],
173  ['countryId' => 'SG', 'postcode' => '123456'],
174  ['countryId' => 'SK', 'postcode' => '123 45'],
175  ['countryId' => 'SI', 'postcode' => '1234'],
176  ['countryId' => 'ZA', 'postcode' => '1234'],
177  ['countryId' => 'ES', 'postcode' => '12345'],
178  ['countryId' => 'XY', 'postcode' => '12345'],
179  ['countryId' => 'SZ', 'postcode' => 'R123'],
180  ['countryId' => 'SE', 'postcode' => '123 45'],
181  ['countryId' => 'CH', 'postcode' => '1234'],
182  ['countryId' => 'TW', 'postcode' => '123'],
183  ['countryId' => 'TW', 'postcode' => '12345'],
184  ['countryId' => 'TJ', 'postcode' => '123456'],
185  ['countryId' => 'TH', 'postcode' => '12345'],
186  ['countryId' => 'TR', 'postcode' => '12345'],
187  ['countryId' => 'TM', 'postcode' => '123456'],
188  ['countryId' => 'UA', 'postcode' => '12345'],
189  ['countryId' => 'GB', 'postcode' => 'PL12 3RT'],
190  ['countryId' => 'GB', 'postcode' => 'P1L 2RT'],
191  ['countryId' => 'GB', 'postcode' => 'QW1 2RT'],
192  ['countryId' => 'GB', 'postcode' => 'QW1R 2TG'],
193  ['countryId' => 'GB', 'postcode' => 'L12 3PL'],
194  ['countryId' => 'GB', 'postcode' => 'Q1 2PL'],
195  ['countryId' => 'US', 'postcode' => '12345-6789'],
196  ['countryId' => 'US', 'postcode' => '12345'],
197  ['countryId' => 'UY', 'postcode' => '12345'],
198  ['countryId' => 'UZ', 'postcode' => '123456'],
199  ['countryId' => 'VI', 'postcode' => '12345'],
200  ];
201  }
202 }
$objectManager
Definition: bootstrap.php:17
testInvalidCanadaZipCode($countryId, $invalidPostCode)