RssTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\Helper;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class RssTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Wishlist\Helper\Rss
  14. */
  15. protected $model;
  16. /**
  17. * @var \Magento\Wishlist\Model\WishlistFactory|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $wishlistFactoryMock;
  20. /**
  21. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $requestMock;
  24. /**
  25. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $urlDecoderMock;
  28. /**
  29. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $customerFactoryMock;
  32. /**
  33. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $customerSessionMock;
  36. /**
  37. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $customerRepositoryMock;
  40. /**
  41. * @var \Magento\Framework\Module\Manager|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $moduleManagerMock;
  44. /**
  45. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $scopeConfigMock;
  48. protected function setUp()
  49. {
  50. $this->wishlistFactoryMock = $this->getMockBuilder(\Magento\Wishlist\Model\WishlistFactory::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods(['create'])
  53. ->getMock();
  54. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  55. ->getMock();
  56. $this->urlDecoderMock = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)
  57. ->getMock();
  58. $this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['create'])
  61. ->getMock();
  62. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  66. ->getMock();
  67. $this->moduleManagerMock = $this->getMockBuilder(\Magento\Framework\Module\Manager::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  71. ->getMock();
  72. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  73. $this->model = $objectManager->getObject(
  74. \Magento\Wishlist\Helper\Rss::class,
  75. [
  76. 'wishlistFactory' => $this->wishlistFactoryMock,
  77. 'httpRequest' => $this->requestMock,
  78. 'urlDecoder' => $this->urlDecoderMock,
  79. 'customerFactory' => $this->customerFactoryMock,
  80. 'customerSession' => $this->customerSessionMock,
  81. 'customerRepository' => $this->customerRepositoryMock,
  82. 'moduleManager' => $this->moduleManagerMock,
  83. 'scopeConfig' => $this->scopeConfigMock,
  84. ]
  85. );
  86. }
  87. public function testGetWishlistWithWishlistId()
  88. {
  89. $wishlistId = 1;
  90. $wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->wishlistFactoryMock->expects($this->once())
  94. ->method('create')
  95. ->willReturn($wishlist);
  96. $this->requestMock->expects($this->once())
  97. ->method('getParam')
  98. ->with('wishlist_id', null)
  99. ->willReturn($wishlistId);
  100. $wishlist->expects($this->once())
  101. ->method('load')
  102. ->with($wishlistId, null)
  103. ->willReturnSelf();
  104. $this->assertEquals($wishlist, $this->model->getWishlist());
  105. // Check that wishlist is cached
  106. $this->assertSame($wishlist, $this->model->getWishlist());
  107. }
  108. public function testGetWishlistWithCustomerId()
  109. {
  110. $customerId = 1;
  111. $data = $customerId . ',2';
  112. $wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->wishlistFactoryMock->expects($this->once())
  116. ->method('create')
  117. ->willReturn($wishlist);
  118. $this->requestMock->expects($this->at(0))
  119. ->method('getParam')
  120. ->with('wishlist_id', null)
  121. ->willReturn('');
  122. $this->urlDecoderMock->expects($this->any())
  123. ->method('decode')
  124. ->willReturnArgument(0);
  125. $this->requestMock->expects($this->at(1))
  126. ->method('getParam')
  127. ->with('data', null)
  128. ->willReturn($data);
  129. $this->customerSessionMock->expects($this->once())
  130. ->method('getCustomerId')
  131. ->willReturn(0);
  132. $customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $this->customerFactoryMock->expects($this->once())
  136. ->method('create')
  137. ->willReturn($customer);
  138. $this->customerRepositoryMock->expects($this->never())
  139. ->method('getById');
  140. $customer->expects($this->exactly(2))
  141. ->method('getId')
  142. ->willReturn($customerId);
  143. $wishlist->expects($this->once())
  144. ->method('loadByCustomerId')
  145. ->with($customerId, false)
  146. ->willReturnSelf();
  147. $this->assertEquals($wishlist, $this->model->getWishlist());
  148. }
  149. public function testGetCustomerWithSession()
  150. {
  151. $customerId = 1;
  152. $data = $customerId . ',2';
  153. $this->urlDecoderMock->expects($this->any())
  154. ->method('decode')
  155. ->willReturnArgument(0);
  156. $this->requestMock->expects($this->once())
  157. ->method('getParam')
  158. ->with('data', null)
  159. ->willReturn($data);
  160. $this->customerSessionMock->expects($this->once())
  161. ->method('getCustomerId')
  162. ->willReturn($customerId);
  163. $customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $this->customerRepositoryMock->expects($this->once())
  167. ->method('getById')
  168. ->with($customerId)
  169. ->willReturn($customer);
  170. $this->customerFactoryMock->expects($this->never())
  171. ->method('create');
  172. $this->assertEquals($customer, $this->model->getCustomer());
  173. // Check that customer is cached
  174. $this->assertSame($customer, $this->model->getCustomer());
  175. }
  176. /**
  177. * @param bool $isModuleEnabled
  178. * @param bool $isWishlistActive
  179. * @param bool $result
  180. * @dataProvider dataProviderIsRssAllow
  181. */
  182. public function testIsRssAllow($isModuleEnabled, $isWishlistActive, $result)
  183. {
  184. $this->moduleManagerMock->expects($this->once())
  185. ->method('isEnabled')
  186. ->with('Magento_Rss')
  187. ->willReturn($isModuleEnabled);
  188. $this->scopeConfigMock->expects($this->any())
  189. ->method('isSetFlag')
  190. ->with('rss/wishlist/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  191. ->willReturn($isWishlistActive);
  192. $this->assertEquals($result, $this->model->isRssAllow());
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function dataProviderIsRssAllow()
  198. {
  199. return [
  200. [false, false, false],
  201. [true, false, false],
  202. [false, true, false],
  203. [true, true, true],
  204. ];
  205. }
  206. }