Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BookTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class BookTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_block;
17 
21  protected $currentCustomer;
22 
23  protected function setUp()
24  {
26  $blockMock = $this->getMockBuilder(
27  \Magento\Framework\View\Element\BlockInterface::class
28  )->disableOriginalConstructor()->setMethods(
29  ['setTitle', 'toHtml']
30  )->getMock();
31  $blockMock->expects($this->any())->method('setTitle');
32 
34  ->get(\Magento\Customer\Helper\Session\CurrentCustomer::class);
36  $layout = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class);
37  $layout->setBlock('head', $blockMock);
38  $this->_block = $layout
39  ->createBlock(
40  \Magento\Customer\Block\Address\Book::class,
41  '',
42  ['currentCustomer' => $this->currentCustomer]
43  );
44  }
45 
46  protected function tearDown()
47  {
50  $customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
51  // Cleanup customer from registry
52  $customerRegistry->remove(1);
53  }
54 
55  public function testGetAddressEditUrl()
56  {
57  $this->assertEquals(
58  'http://localhost/index.php/customer/address/edit/id/1/',
59  $this->_block->getAddressEditUrl(1)
60  );
61  }
62 
69  public function testHasPrimaryAddress($customerId, $expected)
70  {
71  if (!empty($customerId)) {
72  $this->currentCustomer->setCustomerId($customerId);
73  }
74  $this->assertEquals($expected, $this->_block->hasPrimaryAddress());
75  }
76 
78  {
79  return ['0' => [0, false], '1' => [1, true], '5' => [5, false]];
80  }
81 
86  public function testGetAdditionalAddresses()
87  {
88  $this->currentCustomer->setCustomerId(1);
89  $this->assertNotNull($this->_block->getAdditionalAddresses());
90  $this->assertCount(1, $this->_block->getAdditionalAddresses());
91  $this->assertInstanceOf(
92  \Magento\Customer\Api\Data\AddressInterface::class,
93  $this->_block->getAdditionalAddresses()[0]
94  );
95  $this->assertEquals(2, $this->_block->getAdditionalAddresses()[0]->getId());
96  }
97 
103  {
104  if (!empty($customerId)) {
105  $this->currentCustomer->setCustomerId($customerId);
106  }
107  $this->assertEquals($expected, $this->_block->getAdditionalAddresses());
108  }
109 
111  {
112  return ['0' => [0, false], '5' => [5, false]];
113  }
114 
119  public function testGetAddressHtml()
120  {
121  $expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />" .
122  "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
124  \Magento\Customer\Api\AddressRepositoryInterface::class
125  )->getById(1);
126  $html = $this->_block->getAddressHtml($address);
127  $this->assertEquals($expected, $html);
128  }
129 
131  {
132  $this->assertEquals('', $this->_block->getAddressHtml(null));
133  }
134 
138  public function testGetCustomer()
139  {
142  \Magento\Customer\Api\CustomerRepositoryInterface::class
143  );
144  $customer = $customerRepository->getById(1);
145 
146  $this->currentCustomer->setCustomerId(1);
147  $object = $this->_block->getCustomer();
148  $this->assertEquals($customer, $object);
149  }
150 
152  {
153  $this->assertNull($this->_block->getCustomer());
154  }
155 
162  public function testGetDefaultBilling($customerId, $expected)
163  {
164  $this->currentCustomer->setCustomerId($customerId);
165  $this->assertEquals($expected, $this->_block->getDefaultBilling());
166  }
167 
169  {
170  return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
171  }
172 
179  public function testGetDefaultShipping($customerId, $expected)
180  {
181  if (!empty($customerId)) {
182  $this->currentCustomer->setCustomerId($customerId);
183  }
184  $this->assertEquals($expected, $this->_block->getDefaultShipping());
185  }
186 
188  {
189  return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
190  }
191 
196  public function testGetAddressById()
197  {
198  $this->assertInstanceOf(\Magento\Customer\Api\Data\AddressInterface::class, $this->_block->getAddressById(1));
199  $this->assertNull($this->_block->getAddressById(5));
200  }
201 }
$objectManager
Definition: bootstrap.php:17
$customer
Definition: customers.php:11
testHasPrimaryAddress($customerId, $expected)
Definition: BookTest.php:69
$customerRepository
testGetDefaultShipping($customerId, $expected)
Definition: BookTest.php:179
$address
Definition: customer.php:38
$customerRegistry
Definition: customers.php:12
testGetDefaultBilling($customerId, $expected)
Definition: BookTest.php:162
testGetAdditionalAddressesNegative($customerId, $expected)
Definition: BookTest.php:102