DiscountsTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Block\Rss;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class DiscountsTest
  10. * @package Magento\SalesRule\Block\Rss
  11. */
  12. class DiscountsTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Block\Rss\Discounts
  16. */
  17. protected $block;
  18. /**
  19. * @var ObjectManagerHelper
  20. */
  21. protected $objectManagerHelper;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $storeManagerInterface;
  26. /**
  27. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $storeModel;
  30. /**
  31. * @var \Magento\SalesRule\Model\Rss\Discounts|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $discounts;
  34. /**
  35. * @var \Magento\Framework\App\Rss\UrlBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $rssBuilderInterface;
  38. /**
  39. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $urlBuilderInterface;
  42. /**
  43. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $requestInterface;
  46. /**
  47. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $scopeConfigInterface;
  50. /**
  51. * @var \Magento\SalesRule\Model\Rss\Discounts|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $rssModel;
  54. /**
  55. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $timezoneInterface;
  58. protected function setUp()
  59. {
  60. $this->storeManagerInterface = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  61. $this->requestInterface = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  62. $this->rssBuilderInterface = $this->createMock(\Magento\Framework\App\Rss\UrlBuilderInterface::class);
  63. $this->urlBuilderInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
  64. $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  65. $this->timezoneInterface = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  66. $this->discounts = $this->createMock(\Magento\SalesRule\Model\Rss\Discounts::class);
  67. $this->rssModel = $this->createPartialMock(\Magento\SalesRule\Model\Rss\Discounts::class, [
  68. '__wakeup',
  69. 'getDiscountCollection'
  70. ]);
  71. $this->storeModel = $this->createPartialMock(\Magento\Store\Model\Store::class, [
  72. '__wakeUp',
  73. 'getId',
  74. 'getWebsiteId',
  75. 'getName',
  76. 'getFrontendName'
  77. ]);
  78. $this->storeManagerInterface->expects($this->any())->method('getStore')
  79. ->will($this->returnValue($this->storeModel));
  80. $this->storeModel->expects($this->any())->method('getId')->will($this->returnValue(1));
  81. $this->objectManagerHelper = new ObjectManagerHelper($this);
  82. $this->block = $this->objectManagerHelper->getObject(
  83. \Magento\SalesRule\Block\Rss\Discounts::class,
  84. [
  85. 'storeManager' => $this->storeManagerInterface,
  86. 'rssModel' => $this->discounts,
  87. 'rssUrlBuilder' => $this->rssBuilderInterface,
  88. 'urlBuilder' => $this->urlBuilderInterface,
  89. 'request' => $this->requestInterface,
  90. 'scopeConfig' => $this->scopeConfigInterface,
  91. 'rssModel' => $this->rssModel,
  92. 'localeDate' => $this->timezoneInterface
  93. ]
  94. );
  95. }
  96. public function testGetRssData()
  97. {
  98. $ruleData = [
  99. 'to_date' => '12/12/14',
  100. 'from_date' => '12/12/14',
  101. 'coupon_code' => '1234567',
  102. 'description' => 'Rule Description',
  103. 'name' => 'Rule Name',
  104. ];
  105. $rssData = [
  106. 'title' => 'Frontend Name - Discounts and Coupons',
  107. 'description' => 'Frontend Name - Discounts and Coupons',
  108. 'link' => 'http://rss.magento.com/discount',
  109. 'charset' => 'UTF-8',
  110. 'language' => 'en_US',
  111. 'entries' => [
  112. 'title' => 'Rule Name',
  113. 'link' => 'http://rss.magento.com',
  114. 'description' => [
  115. 'description' => 'Rule Description',
  116. 'start_date' => '12/12/14',
  117. 'end_date' => '12/12/14',
  118. 'coupon_code' => '1234567',
  119. ],
  120. ],
  121. ];
  122. $rssUrl = 'http://rss.magento.com/discount';
  123. $url = 'http://rss.magento.com';
  124. $ruleModel = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, [
  125. '__wakeup',
  126. 'getCouponCode',
  127. 'getToDate',
  128. 'getFromDate',
  129. 'getDescription',
  130. 'getName'
  131. ]);
  132. $this->storeModel->expects($this->once())->method('getWebsiteId')->will($this->returnValue(1));
  133. $this->storeModel->expects($this->never())->method('getName');
  134. $this->storeModel->expects($this->atLeastOnce())->method('getFrontendName')->willReturn('Frontend Name');
  135. $this->requestInterface->expects($this->any())->method('getParam')->will($this->returnValue(1));
  136. $this->urlBuilderInterface->expects($this->any())->method('getUrl')->will($this->returnValue($url));
  137. $this->rssBuilderInterface->expects($this->any())->method('getUrl')->will($this->returnValue($rssUrl));
  138. $this->scopeConfigInterface->expects($this->any())->method('getValue')->will($this->returnValue('en_US'));
  139. $ruleModel->expects($this->any())->method('getCouponCode')->will($this->returnValue($ruleData['coupon_code']));
  140. $ruleModel->expects($this->any())->method('getToDate')->will($this->returnValue($ruleData['to_date']));
  141. $ruleModel->expects($this->once())->method('getFromDate')->will($this->returnValue($ruleData['from_date']));
  142. $ruleModel->expects($this->once())->method('getDescription')
  143. ->will($this->returnValue($ruleData['description']));
  144. $ruleModel->expects($this->once())->method('getName')->will($this->returnValue($ruleData['name']));
  145. $this->rssModel->expects($this->any())->method('getDiscountCollection')
  146. ->will($this->returnValue([$ruleModel]));
  147. $this->timezoneInterface->expects($this->any())->method('formatDateTime')->will($this->returnValue('12/12/14'));
  148. $data = $this->block->getRssData();
  149. $this->assertEquals($rssData['title'], $data['title']);
  150. $this->assertEquals($rssData['description'], $data['description']);
  151. $this->assertEquals($rssData['link'], $data['link']);
  152. $this->assertEquals($rssData['charset'], $data['charset']);
  153. $this->assertEquals($rssData['language'], $data['language']);
  154. $this->assertEquals($rssData['entries']['title'], $data['entries'][0]['title']);
  155. $this->assertEquals($rssData['entries']['link'], $data['entries'][0]['link']);
  156. $this->assertContains($rssData['entries']['description']['description'], $data['entries'][0]['description']);
  157. $this->assertContains($rssData['entries']['description']['start_date'], $data['entries'][0]['description']);
  158. $this->assertContains($rssData['entries']['description']['end_date'], $data['entries'][0]['description']);
  159. $this->assertContains($rssData['entries']['description']['coupon_code'], $data['entries'][0]['description']);
  160. }
  161. public function testGetCacheLifetime()
  162. {
  163. $this->assertEquals(0, $this->block->getCacheLifetime());
  164. }
  165. /**
  166. * @dataProvider isAllowedDataProvider
  167. * @param bool $isAllowed
  168. */
  169. public function testIsAllowed($isAllowed)
  170. {
  171. $this->scopeConfigInterface->expects($this->once())->method('isSetFlag')->will($this->returnValue($isAllowed));
  172. $this->assertEquals($isAllowed, $this->block->isAllowed());
  173. }
  174. /**
  175. * @return array
  176. */
  177. public function isAllowedDataProvider()
  178. {
  179. return [
  180. [true],
  181. [false]
  182. ];
  183. }
  184. public function testGetFeeds()
  185. {
  186. $feedData = [
  187. 'label' => 'Coupons/Discounts',
  188. 'link' => 'http://rss.magento.com/discount',
  189. ];
  190. $this->rssBuilderInterface->expects($this->any())
  191. ->method('getUrl')
  192. ->will($this->returnValue($feedData['link']));
  193. $this->scopeConfigInterface->expects($this->once())->method('isSetFlag')->will($this->returnValue(true));
  194. $this->assertEquals($feedData, $this->block->getFeeds());
  195. }
  196. }