ReviewTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Block\Express;
  7. use Magento\Paypal\Block\Express\Review;
  8. use Magento\Quote\Model\Quote\Address\Rate;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ReviewTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $request;
  18. /**
  19. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $assetRepo;
  22. /**
  23. * @var Review
  24. */
  25. protected $model;
  26. /**
  27. * @inheritdoc
  28. */
  29. protected function setUp()
  30. {
  31. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  33. $eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  34. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  35. $scopeConfig->expects($this->any())
  36. ->method('getValue')
  37. ->with(
  38. $this->stringContains('advanced/modules_disable_output/'),
  39. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  40. )->will($this->returnValue(false));
  41. $urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
  42. $urlBuilder->expects($this->any())->method('getUrl')->will($this->returnArgument(0));
  43. $context = $this->createPartialMock(
  44. \Magento\Framework\View\Element\Template\Context::class,
  45. ['getLayout', 'getEventManager', 'getScopeConfig', 'getRequest', 'getAssetRepository', 'getUrlBuilder']
  46. );
  47. $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  48. $this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  49. $context->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
  50. $context->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
  51. $context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
  52. $context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
  53. $context->expects($this->any())->method('getAssetRepository')->will($this->returnValue($this->assetRepo));
  54. $context->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($urlBuilder));
  55. $this->model = $helper->getObject(\Magento\Paypal\Block\Express\Review::class, ['context' => $context]);
  56. }
  57. /**
  58. * @param bool $isSecure
  59. * @dataProvider getViewFileUrlDataProvider
  60. */
  61. public function testGetViewFileUrl($isSecure)
  62. {
  63. $this->request->expects($this->once())->method('isSecure')->will($this->returnValue($isSecure));
  64. $this->assetRepo->expects($this->once())
  65. ->method('getUrlWithParams')
  66. ->with('some file', $this->callback(function ($value) use ($isSecure) {
  67. return isset($value['_secure']) && $value['_secure'] === $isSecure;
  68. }))
  69. ->will($this->returnValue('result url'));
  70. $this->assertEquals('result url', $this->model->getViewFileUrl('some file'));
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getViewFileUrlDataProvider()
  76. {
  77. return [[true], [false]];
  78. }
  79. public function testBeforeToHtmlWhenQuoteIsNotVirtual()
  80. {
  81. $quote = $this->_getQuoteMock();
  82. $quote->expects($this->any())->method('getIsVirtual')->will($this->returnValue(false));
  83. $quote->setMayEditShippingMethod('MayEditShippingMethod');
  84. $shippingRate = new \Magento\Framework\DataObject(['code' => 'Rate 1']);
  85. $shippingRates = [
  86. [$shippingRate],
  87. ];
  88. $quote->getShippingAddress()
  89. ->expects($this->any())
  90. ->method('getGroupedAllShippingRates')
  91. ->will($this->returnValue($shippingRates));
  92. $quote->getShippingAddress()
  93. ->expects($this->any())
  94. ->method('getShippingMethod')
  95. ->will($this->returnValue($shippingRate->getCode()));
  96. $this->model->setQuote($quote);
  97. $this->model->toHtml();
  98. $this->assertEquals(
  99. $this->model->getPaymentMethodTitle(),
  100. $quote->getPayment()->getMethodInstance()->getTitle()
  101. );
  102. $this->assertTrue($this->model->getShippingRateRequired());
  103. $this->assertSame($shippingRates, $this->model->getShippingRateGroups());
  104. $this->assertSame($shippingRate, $this->model->getCurrentShippingRate());
  105. $this->assertNotNull($this->model->getCanEditShippingAddress());
  106. $this->assertEquals($quote->getMayEditShippingMethod(), $this->model->getCanEditShippingMethod());
  107. $this->assertContains('paypal/express/saveShippingMethod', $this->model->getShippingMethodSubmitUrl());
  108. $this->assertContains('paypal/express/edit', $this->model->getEditUrl());
  109. $this->assertContains('paypal/express/placeOrder', $this->model->getPlaceOrderUrl());
  110. }
  111. public function testBeforeToHtmlWhenQuoteIsVirtual()
  112. {
  113. $quote = $this->_getQuoteMock();
  114. $quote->expects($this->any())->method('getIsVirtual')->will($this->returnValue(true));
  115. $this->model->setQuote($quote);
  116. $this->model->toHtml();
  117. $this->assertEquals(
  118. $this->model->getPaymentMethodTitle(),
  119. $quote->getPayment()->getMethodInstance()->getTitle()
  120. );
  121. $this->assertFalse($this->model->getShippingRateRequired());
  122. $this->assertContains('paypal/express/edit', $this->model->getEditUrl());
  123. $this->assertContains('paypal/express/placeOrder', $this->model->getPlaceOrderUrl());
  124. }
  125. /**
  126. * Create mock of sales quote model
  127. *
  128. * @return \PHPUnit_Framework_MockObject_MockObject
  129. */
  130. protected function _getQuoteMock()
  131. {
  132. $methodInstance = new \Magento\Framework\DataObject(['title' => 'Payment Method']);
  133. $payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
  134. $payment->expects($this->any())->method('getMethodInstance')->will($this->returnValue($methodInstance));
  135. $quote = $this->createMock(\Magento\Quote\Model\Quote::class);
  136. $quote->expects($this->any())->method('getPayment')->will($this->returnValue($payment));
  137. $quote->setPayment($payment);
  138. $address = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address::class)
  139. ->disableOriginalConstructor()
  140. ->setMethods(['getShippingMethod', 'getGroupedAllShippingRates', '__wakeup'])
  141. ->getMock();
  142. $quote->expects($this->any())->method('getShippingAddress')->will($this->returnValue($address));
  143. return $quote;
  144. }
  145. public function testGetEmail()
  146. {
  147. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  148. $billingAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  149. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddressMock);
  150. $billingAddressMock->expects($this->once())->method('getEmail')->willReturn('test@example.com');
  151. $this->model->setQuote($quoteMock);
  152. $this->assertEquals('test@example.com', $this->model->getEmail());
  153. }
  154. public function testGetEmailWhenBillingAddressNotExist()
  155. {
  156. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  157. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn(null);
  158. $this->model->setQuote($quoteMock);
  159. $this->assertEquals('', $this->model->getEmail());
  160. }
  161. public function testCanEditShippingMethod()
  162. {
  163. $this->model->setData('can_edit_shipping_method', true);
  164. static::assertTrue($this->model->canEditShippingMethod());
  165. $this->model->setData('can_edit_shipping_method', false);
  166. static::assertTrue($this->model->canEditShippingMethod());
  167. }
  168. }