Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Backend\Model\Session\Quote as QuoteSession;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 
30 class AddressTest extends \PHPUnit\Framework\TestCase
31 {
35  private $objectManager;
36 
40  private $block;
41 
45  private $quoteSession;
46 
50  protected function setUp()
51  {
52  $this->objectManager = Bootstrap::getObjectManager();
53 
54  $this->quoteSession = $this->getMockBuilder(QuoteSession::class)
55  ->disableOriginalConstructor()
56  ->setMethods(['getCustomerId', 'getStore', 'getStoreId', 'getQuote'])
57  ->getMock();
58 
59  $this->block = $this->objectManager->create(
60  Address::class,
61  ['sessionQuote' => $this->quoteSession]
62  );
63  }
64 
70  public function testGetAddressCollection()
71  {
72  $website = $this->getWebsite('base');
73  $customer = $this->getCustomer('[email protected]', (int)$website->getId());
74  $addresses = $customer->getAddresses();
75  $this->quoteSession->method('getCustomerId')
76  ->willReturn($customer->getId());
77 
78  $actual = $this->block->getAddressCollection();
79  self::assertNotEmpty($actual);
80  self::assertEquals($addresses, $actual);
81  }
82 
88  public function testGetAddressCollectionJson()
89  {
90  $website = $this->getWebsite('test');
91  $customer = $this->getCustomer('[email protected]', (int)$website->getId());
92 
93  $store = $this->getStore('fixture_second_store');
94  $this->quoteSession->method('getStore')
95  ->willReturn($store);
96  $this->quoteSession->method('getCustomerId')
97  ->willReturn($customer->getId());
98  $addresses = $customer->getAddresses();
99  $expected = [
100  0 => [
101  'firstname' => false,
102  'lastname' => false,
103  'company' => false,
104  'street' => '',
105  'city' => false,
106  'country_id' => 'US',
107  'region' => false,
108  'region_id' => false,
109  'postcode' => false,
110  'telephone' => false,
111  'vat_id' => false,
112  ],
113  $addresses[0]->getId() => [
114  'firstname' => 'John',
115  'lastname' => 'Smith',
116  'company' => false,
117  'street' => 'Green str, 67',
118  'city' => 'Culver City',
119  'country_id' => 'US',
120  'region' => 'California',
121  'region_id' => 12,
122  'postcode' => '90230',
123  'telephone' => '3468676',
124  'vat_id' => false,
125  ],
126  $addresses[1]->getId() => [
127  'telephone' => '845454465',
128  'postcode' => '10178',
129  'country_id' => 'DE',
130  'city' => 'Berlin',
131  'street' => 'Tunnel Alexanderpl',
132  'firstname' => 'John',
133  'lastname' => 'Smith',
134  'company' => false,
135  'region' => false,
136  'region_id' => 0,
137  'vat_id' => false,
138  ]
139  ];
140 
141  $actual = json_decode($this->block->getAddressCollectionJson(), true);
142  self::assertEquals($expected, $actual);
143  }
144 
148  public function testGetAddressAsString()
149  {
150  $data = [
151  'firstname' => 'John',
152  'lastname' => 'Smith',
153  'company' => 'Test Company',
154  'street' => 'Green str, 67',
155  'city' => 'Culver City',
156  'country_id' => 'US',
157  'region' => 'California',
158  'region_id' => 12,
159  'postcode' => '90230',
160  'telephone' => '3468676',
161  ];
162  $address = $this->objectManager->create(AddressInterface::class, ['data' => $data]);
163  $expected = 'John Smith, Green str, 67, Culver City, California 90230, United States';
164  self::assertEquals($expected, $this->block->getAddressAsString($address));
165  }
166 
167  public function testGetForm()
168  {
169  $expectedFields = [
170  'prefix',
171  'firstname',
172  'middlename',
173  'lastname',
174  'suffix',
175  'company',
176  'street',
177  'city',
178  'country_id',
179  'region',
180  'region_id',
181  'postcode',
182  'telephone',
183  'fax',
184  'vat_id',
185  ];
186 
187  $form = $this->block->getForm();
188  self::assertEquals(1, $form->getElements()->count(), 'Form has invalid number of fieldsets');
189 
191  $fieldset = $form->getElements()[0];
192  self::assertEquals(
193  count($expectedFields),
194  $fieldset->getElements()->count(),
195  'Form has invalid number of fields'
196  );
197 
199  foreach ($fieldset->getElements() as $element) {
200  self::assertTrue(
201  in_array($element->getId(), $expectedFields),
202  sprintf('Unexpected field "%s" in form.', $element->getId())
203  );
204  }
205 
207  $countryIdField = $fieldset->getElements()->searchById('country_id');
208  $actual = Xpath::getElementsCountForXpath('//option', $countryIdField->getElementHtml());
209  self::assertEquals($this->getNumberOfCountryOptions(), $actual);
210  }
211 
221  private function getCustomer(string $email, int $websiteId): CustomerInterface
222  {
224  $repository = $this->objectManager->get(CustomerRepositoryInterface::class);
225  return $repository->get($email, $websiteId);
226  }
227 
235  private function getWebsite(string $code): WebsiteInterface
236  {
238  $repository = $this->objectManager->get(WebsiteRepositoryInterface::class);
239  return $repository->get($code);
240  }
241 
249  private function getStore(string $code): StoreInterface
250  {
252  $repository = $this->objectManager->get(StoreRepositoryInterface::class);
253  return $repository->get($code);
254  }
255 
259  private function getNumberOfCountryOptions()
260  {
262  $countryCollection = $this->objectManager->create(Collection::class);
263  return count($countryCollection->toOptionArray());
264  }
265 }
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
$addresses
Definition: address_list.php:7
$address
Definition: customer.php:38
$code
Definition: info.phtml:12
$element
Definition: element.phtml:12