CustomerDataBuilderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\CustomerDataBuilder;
  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\CustomerDataBuilder.
  15. */
  16. class CustomerDataBuilderTest 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 CustomerDataBuilder
  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 CustomerDataBuilder($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. /**
  58. * @param array $billingData
  59. * @param array $expectedResult
  60. *
  61. * @dataProvider dataProviderBuild
  62. */
  63. public function testBuild($billingData, $expectedResult)
  64. {
  65. $billingMock = $this->getBillingMock($billingData);
  66. $this->paymentDOMock->expects(static::once())
  67. ->method('getOrder')
  68. ->willReturn($this->orderMock);
  69. $this->orderMock->expects(static::once())
  70. ->method('getBillingAddress')
  71. ->willReturn($billingMock);
  72. $buildSubject = [
  73. 'payment' => $this->paymentDOMock,
  74. ];
  75. $this->subjectReaderMock->expects(self::once())
  76. ->method('readPayment')
  77. ->with($buildSubject)
  78. ->willReturn($this->paymentDOMock);
  79. self::assertEquals($expectedResult, $this->builder->build($buildSubject));
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function dataProviderBuild()
  85. {
  86. return [
  87. [
  88. [
  89. 'first_name' => 'John',
  90. 'last_name' => 'Smith',
  91. 'company' => 'Magento',
  92. 'phone' => '555-555-555',
  93. 'email' => 'john@magento.com',
  94. ],
  95. [
  96. CustomerDataBuilder::CUSTOMER => [
  97. CustomerDataBuilder::FIRST_NAME => 'John',
  98. CustomerDataBuilder::LAST_NAME => 'Smith',
  99. CustomerDataBuilder::COMPANY => 'Magento',
  100. CustomerDataBuilder::PHONE => '555-555-555',
  101. CustomerDataBuilder::EMAIL => 'john@magento.com',
  102. ],
  103. ],
  104. ],
  105. ];
  106. }
  107. /**
  108. * @param array $billingData
  109. * @return AddressAdapterInterface|MockObject
  110. */
  111. private function getBillingMock($billingData)
  112. {
  113. $addressMock = $this->createMock(AddressAdapterInterface::class);
  114. $addressMock->expects(static::once())
  115. ->method('getFirstname')
  116. ->willReturn($billingData['first_name']);
  117. $addressMock->expects(static::once())
  118. ->method('getLastname')
  119. ->willReturn($billingData['last_name']);
  120. $addressMock->expects(static::once())
  121. ->method('getCompany')
  122. ->willReturn($billingData['company']);
  123. $addressMock->expects(static::once())
  124. ->method('getTelephone')
  125. ->willReturn($billingData['phone']);
  126. $addressMock->expects(static::once())
  127. ->method('getEmail')
  128. ->willReturn($billingData['email']);
  129. return $addressMock;
  130. }
  131. }