AgreementsTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Billing;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class AgreementsTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Element\Context|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $context;
  16. /**
  17. * @codingStandardsIgnoreStart
  18. * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  19. * @codingStandardsIgnoreEnd
  20. */
  21. private $agreementCollection;
  22. /**
  23. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $urlBuilder;
  26. /**
  27. * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $escaper;
  30. /**
  31. * @var \Magento\Paypal\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $helper;
  34. /**
  35. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $layout;
  38. /**
  39. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $eventManager;
  42. /**
  43. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $scopeConfig;
  46. /**
  47. * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $cache;
  50. /**
  51. * @var \Magento\Paypal\Block\Billing\Agreements
  52. */
  53. private $block;
  54. /**
  55. * @inheritdoc
  56. */
  57. protected function setUp()
  58. {
  59. $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  60. $this->escaper = $this->createMock(\Magento\Framework\Escaper::class);
  61. $this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
  62. $localeDate = $this->getMockForAbstractClass(
  63. \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class,
  64. [],
  65. '',
  66. false
  67. );
  68. $this->context->expects($this->once())->method('getLocaleDate')->willReturn($localeDate);
  69. $this->urlBuilder = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class, [], '', false);
  70. $this->context->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilder);
  71. $this->layout = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class, [], '', false);
  72. $this->context->expects($this->once())->method('getLayout')->willReturn($this->layout);
  73. $this->eventManager = $this->getMockForAbstractClass(
  74. \Magento\Framework\Event\ManagerInterface::class,
  75. [],
  76. '',
  77. false
  78. );
  79. $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
  80. $this->scopeConfig = $this->getMockForAbstractClass(
  81. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  82. [],
  83. '',
  84. false
  85. );
  86. $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfig);
  87. $this->cache = $this->getMockForAbstractClass(\Magento\Framework\App\CacheInterface::class, [], '', false);
  88. $this->context->expects($this->once())->method('getCache')->willReturn($this->cache);
  89. $this->agreementCollection = $this->getMockBuilder(
  90. \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory::class
  91. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  92. $this->helper = $this->createMock(\Magento\Paypal\Helper\Data::class);
  93. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  94. $this->block = $objectManager->getObject(
  95. \Magento\Paypal\Block\Billing\Agreements::class,
  96. [
  97. 'context' => $this->context,
  98. 'agreementCollection' => $this->agreementCollection,
  99. 'helper' => $this->helper,
  100. ]
  101. );
  102. }
  103. public function testGetBillingAgreements()
  104. {
  105. $collection = $this->createMock(\Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection::class);
  106. $this->agreementCollection->expects($this->once())->method('create')->willReturn($collection);
  107. $collection->expects($this->once())->method('addFieldToFilter')->willReturn($collection);
  108. $collection->expects($this->once())->method('setOrder')->willReturn($collection);
  109. $this->assertSame($collection, $this->block->getBillingAgreements());
  110. // call second time to make sure mock only called once
  111. $this->block->getBillingAgreements();
  112. }
  113. public function testGetItemValueCreatedAt()
  114. {
  115. $this->escaper->expects($this->once())->method('escapeHtml');
  116. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  117. $item->expects($this->exactly(2))->method('getData')->with('created_at')->willReturn('03/10/2014');
  118. $this->block->getItemValue($item, 'created_at');
  119. }
  120. public function testGetItemValueCreatedAtNoData()
  121. {
  122. $this->escaper->expects($this->once())->method('escapeHtml');
  123. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  124. $item->expects($this->once())->method('getData')->with('created_at')->willReturn(false);
  125. $this->block->getItemValue($item, 'created_at');
  126. }
  127. public function testGetItemValueUpdatedAt()
  128. {
  129. $this->escaper->expects($this->once())->method('escapeHtml');
  130. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  131. $item->expects($this->exactly(2))->method('getData')->with('updated_at')->willReturn('03/10/2014');
  132. $this->block->getItemValue($item, 'updated_at');
  133. }
  134. public function testGetItemValueUpdatedAtNoData()
  135. {
  136. $this->escaper->expects($this->once())->method('escapeHtml');
  137. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  138. $item->expects($this->once())->method('getData')->with('updated_at')->willReturn(false);
  139. $this->block->getItemValue($item, 'updated_at');
  140. }
  141. public function testGetItemValueEditUrl()
  142. {
  143. $this->escaper->expects($this->once())->method('escapeHtml');
  144. $item = $this->createPartialMock(\Magento\Paypal\Model\Billing\Agreement::class, ['getAgreementId']);
  145. $item->expects($this->once())->method('getAgreementId')->willReturn(1);
  146. $this->urlBuilder
  147. ->expects($this->once())
  148. ->method('getUrl')
  149. ->with('paypal/billing_agreement/view', ['agreement' => 1]);
  150. $this->block->getItemValue($item, 'edit_url');
  151. }
  152. public function testGetItemPaymentMethodLabel()
  153. {
  154. $this->escaper->expects($this->once())->method('escapeHtml')->with('label', null);
  155. $item = $this->createPartialMock(\Magento\Paypal\Model\Billing\Agreement::class, ['getAgreementLabel']);
  156. $item->expects($this->once())->method('getAgreementLabel')->willReturn('label');
  157. $this->block->getItemValue($item, 'payment_method_label');
  158. }
  159. public function testGetItemStatus()
  160. {
  161. $this->escaper->expects($this->once())->method('escapeHtml')->with('status', null);
  162. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  163. $item->expects($this->once())->method('getStatusLabel')->willReturn('status');
  164. $this->block->getItemValue($item, 'status');
  165. }
  166. public function testGetItemDefault()
  167. {
  168. $this->escaper->expects($this->once())->method('escapeHtml')->with('value', null);
  169. $item = $this->createMock(\Magento\Paypal\Model\Billing\Agreement::class);
  170. $item->expects($this->exactly(2))->method('getData')->with('default')->willReturn('value');
  171. $this->block->getItemValue($item, 'default');
  172. }
  173. public function testGetWizardPaymentMethodOptions()
  174. {
  175. $method1 = $this->createPartialMock(
  176. \Magento\Paypal\Model\Method\Agreement::class,
  177. ['getConfigData', 'getCode', 'getTitle']
  178. );
  179. $method2 = $this->createPartialMock(
  180. \Magento\Paypal\Model\Method\Agreement::class,
  181. ['getConfigData', 'getCode', 'getTitle']
  182. );
  183. $method3 = $this->createPartialMock(
  184. \Magento\Paypal\Model\Method\Agreement::class,
  185. ['getConfigData', 'getCode', 'getTitle']
  186. );
  187. $method1->expects($this->once())->method('getCode')->willReturn('code1');
  188. $method2->expects($this->never())->method('getCode');
  189. $method3->expects($this->once())->method('getCode')->willReturn('code3');
  190. $method1->expects($this->once())->method('getTitle')->willReturn('title1');
  191. $method2->expects($this->never())->method('getTitle');
  192. $method3->expects($this->once())->method('getTitle')->willReturn('title3');
  193. $method1->expects($this->any())->method('getConfigData')->willReturn(1);
  194. $method2->expects($this->any())->method('getConfigData')->willReturn(0);
  195. $method3->expects($this->any())->method('getConfigData')->willReturn(1);
  196. $paymentMethods = [$method1, $method2, $method3];
  197. $this->helper->expects($this->once())->method('getBillingAgreementMethods')->willReturn($paymentMethods);
  198. $this->assertEquals(['code1' => 'title1', 'code3' => 'title3'], $this->block->getWizardPaymentMethodOptions());
  199. }
  200. public function testToHtml()
  201. {
  202. $this->eventManager
  203. ->expects($this->at(0))
  204. ->method('dispatch')
  205. ->with('view_block_abstract_to_html_before', ['block' => $this->block]);
  206. $transport = new \Magento\Framework\DataObject(['html' => '']);
  207. $this->eventManager
  208. ->expects($this->at(1))
  209. ->method('dispatch')
  210. ->with('view_block_abstract_to_html_after', ['block' => $this->block, 'transport' => $transport]);
  211. $this->scopeConfig
  212. ->expects($this->once())
  213. ->method('getValue')
  214. ->willReturn(false);
  215. $this->urlBuilder->expects($this->once())->method('getUrl')->with('paypal/billing_agreement/startWizard', []);
  216. $this->block->toHtml();
  217. }
  218. }