AddressDataBuilderTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Gateway\Request;
  7. use Magento\Braintree\Gateway\Request\AddressDataBuilder;
  8. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  9. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  10. use Magento\Payment\Gateway\Data\AddressAdapterInterface;
  11. use Magento\Braintree\Gateway\SubjectReader;
  12. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. /**
  14. * Tests \Magento\Braintree\Gateway\Request\AddressDataBuilder.
  15. */
  16. class AddressDataBuilderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var PaymentDataObjectInterface|MockObject
  20. */
  21. private $paymentDOMock;
  22. /**
  23. * @var OrderAdapterInterface|MockObject
  24. */
  25. private $orderMock;
  26. /**
  27. * @var AddressDataBuilder
  28. */
  29. private $builder;
  30. /**
  31. * @var SubjectReader|MockObject
  32. */
  33. private $subjectReaderMock;
  34. protected function setUp()
  35. {
  36. $this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
  37. $this->orderMock = $this->createMock(OrderAdapterInterface::class);
  38. $this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->builder = new AddressDataBuilder($this->subjectReaderMock);
  42. }
  43. /**
  44. * @expectedException \InvalidArgumentException
  45. */
  46. public function testBuildReadPaymentException()
  47. {
  48. $buildSubject = [
  49. 'payment' => null,
  50. ];
  51. $this->subjectReaderMock->expects(self::once())
  52. ->method('readPayment')
  53. ->with($buildSubject)
  54. ->willThrowException(new \InvalidArgumentException());
  55. $this->builder->build($buildSubject);
  56. }
  57. public function testBuildNoAddresses()
  58. {
  59. $this->paymentDOMock->expects(static::once())
  60. ->method('getOrder')
  61. ->willReturn($this->orderMock);
  62. $this->orderMock->expects(static::once())
  63. ->method('getShippingAddress')
  64. ->willReturn(null);
  65. $this->orderMock->expects(static::once())
  66. ->method('getBillingAddress')
  67. ->willReturn(null);
  68. $buildSubject = [
  69. 'payment' => $this->paymentDOMock,
  70. ];
  71. $this->subjectReaderMock->expects(self::once())
  72. ->method('readPayment')
  73. ->with($buildSubject)
  74. ->willReturn($this->paymentDOMock);
  75. static::assertEquals([], $this->builder->build($buildSubject));
  76. }
  77. /**
  78. * @param array $addressData
  79. * @param array $expectedResult
  80. *
  81. * @dataProvider dataProviderBuild
  82. */
  83. public function testBuild($addressData, $expectedResult)
  84. {
  85. $addressMock = $this->getAddressMock($addressData);
  86. $this->paymentDOMock->expects(static::once())
  87. ->method('getOrder')
  88. ->willReturn($this->orderMock);
  89. $this->orderMock->expects(static::once())
  90. ->method('getShippingAddress')
  91. ->willReturn($addressMock);
  92. $this->orderMock->expects(static::once())
  93. ->method('getBillingAddress')
  94. ->willReturn($addressMock);
  95. $buildSubject = [
  96. 'payment' => $this->paymentDOMock,
  97. ];
  98. $this->subjectReaderMock->expects(self::once())
  99. ->method('readPayment')
  100. ->with($buildSubject)
  101. ->willReturn($this->paymentDOMock);
  102. self::assertEquals($expectedResult, $this->builder->build($buildSubject));
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function dataProviderBuild()
  108. {
  109. return [
  110. [
  111. [
  112. 'first_name' => 'John',
  113. 'last_name' => 'Smith',
  114. 'company' => 'Magento',
  115. 'street_1' => 'street1',
  116. 'street_2' => 'street2',
  117. 'city' => 'Chicago',
  118. 'region_code' => 'IL',
  119. 'country_id' => 'US',
  120. 'post_code' => '00000',
  121. ],
  122. [
  123. AddressDataBuilder::SHIPPING_ADDRESS => [
  124. AddressDataBuilder::FIRST_NAME => 'John',
  125. AddressDataBuilder::LAST_NAME => 'Smith',
  126. AddressDataBuilder::COMPANY => 'Magento',
  127. AddressDataBuilder::STREET_ADDRESS => 'street1',
  128. AddressDataBuilder::EXTENDED_ADDRESS => 'street2',
  129. AddressDataBuilder::LOCALITY => 'Chicago',
  130. AddressDataBuilder::REGION => 'IL',
  131. AddressDataBuilder::POSTAL_CODE => '00000',
  132. AddressDataBuilder::COUNTRY_CODE => 'US',
  133. ],
  134. AddressDataBuilder::BILLING_ADDRESS => [
  135. AddressDataBuilder::FIRST_NAME => 'John',
  136. AddressDataBuilder::LAST_NAME => 'Smith',
  137. AddressDataBuilder::COMPANY => 'Magento',
  138. AddressDataBuilder::STREET_ADDRESS => 'street1',
  139. AddressDataBuilder::EXTENDED_ADDRESS => 'street2',
  140. AddressDataBuilder::LOCALITY => 'Chicago',
  141. AddressDataBuilder::REGION => 'IL',
  142. AddressDataBuilder::POSTAL_CODE => '00000',
  143. AddressDataBuilder::COUNTRY_CODE => 'US',
  144. ],
  145. ],
  146. ],
  147. ];
  148. }
  149. /**
  150. * @param array $addressData
  151. * @return AddressAdapterInterface|MockObject
  152. */
  153. private function getAddressMock($addressData)
  154. {
  155. $addressMock = $this->createMock(AddressAdapterInterface::class);
  156. $addressMock->expects(self::exactly(2))
  157. ->method('getFirstname')
  158. ->willReturn($addressData['first_name']);
  159. $addressMock->expects(self::exactly(2))
  160. ->method('getLastname')
  161. ->willReturn($addressData['last_name']);
  162. $addressMock->expects(self::exactly(2))
  163. ->method('getCompany')
  164. ->willReturn($addressData['company']);
  165. $addressMock->expects(self::exactly(2))
  166. ->method('getStreetLine1')
  167. ->willReturn($addressData['street_1']);
  168. $addressMock->expects(self::exactly(2))
  169. ->method('getStreetLine2')
  170. ->willReturn($addressData['street_2']);
  171. $addressMock->expects(self::exactly(2))
  172. ->method('getCity')
  173. ->willReturn($addressData['city']);
  174. $addressMock->expects(self::exactly(2))
  175. ->method('getRegionCode')
  176. ->willReturn($addressData['region_code']);
  177. $addressMock->expects(self::exactly(2))
  178. ->method('getPostcode')
  179. ->willReturn($addressData['post_code']);
  180. $addressMock->expects(self::exactly(2))
  181. ->method('getCountryId')
  182. ->willReturn($addressData['country_id']);
  183. return $addressMock;
  184. }
  185. }