PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Wishlist/Test/Unit/Helper/DataTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 410 lines | 305 code | 79 blank | 26 comment | 0 complexity | 9b8d26510890dfe7b571ed0e18d1ac9a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\Helper;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\App\ActionInterface;
  9. use Magento\Framework\App\Helper\Context;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\Framework\Data\Helper\PostHelper;
  12. use Magento\Framework\Registry;
  13. use Magento\Framework\Url\EncoderInterface;
  14. use Magento\Framework\UrlInterface;
  15. use Magento\Store\Model\Store;
  16. use Magento\Store\Model\StoreManagerInterface;
  17. use Magento\Wishlist\Controller\WishlistProviderInterface;
  18. use Magento\Wishlist\Model\Item as WishlistItem;
  19. use Magento\Wishlist\Model\Wishlist;
  20. /**
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class DataTest extends \PHPUnit_Framework_TestCase
  24. {
  25. /** @var \Magento\Wishlist\Helper\Data */
  26. protected $model;
  27. /** @var WishlistProviderInterface |\PHPUnit_Framework_MockObject_MockObject */
  28. protected $wishlistProvider;
  29. /** @var Registry |\PHPUnit_Framework_MockObject_MockObject */
  30. protected $coreRegistry;
  31. /** @var PostHelper |\PHPUnit_Framework_MockObject_MockObject */
  32. protected $postDataHelper;
  33. /** @var WishlistItem |\PHPUnit_Framework_MockObject_MockObject */
  34. protected $wishlistItem;
  35. /** @var Product |\PHPUnit_Framework_MockObject_MockObject */
  36. protected $product;
  37. /** @var StoreManagerInterface |\PHPUnit_Framework_MockObject_MockObject */
  38. protected $storeManager;
  39. /** @var Store |\PHPUnit_Framework_MockObject_MockObject */
  40. protected $store;
  41. /** @var UrlInterface |\PHPUnit_Framework_MockObject_MockObject */
  42. protected $urlBuilder;
  43. /** @var Wishlist |\PHPUnit_Framework_MockObject_MockObject */
  44. protected $wishlist;
  45. /** @var EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $urlEncoderMock;
  47. /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  48. protected $requestMock;
  49. /** @var Context |\PHPUnit_Framework_MockObject_MockObject */
  50. protected $context;
  51. /**
  52. * Set up mock objects for tested class
  53. *
  54. * @return void
  55. */
  56. protected function setUp()
  57. {
  58. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->storeManager->expects($this->any())
  65. ->method('getStore')
  66. ->willReturn($this->store);
  67. $this->urlEncoderMock = $this->getMockBuilder(\Magento\Framework\Url\EncoderInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods(['getServer'])
  73. ->getMockForAbstractClass();
  74. $this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->context = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->context->expects($this->once())
  81. ->method('getUrlBuilder')
  82. ->willReturn($this->urlBuilder);
  83. $this->context->expects($this->once())
  84. ->method('getUrlEncoder')
  85. ->willReturn($this->urlEncoderMock);
  86. $this->context->expects($this->once())
  87. ->method('getRequest')
  88. ->willReturn($this->requestMock);
  89. $this->wishlistProvider = $this->getMockBuilder(\Magento\Wishlist\Controller\WishlistProviderInterface::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->coreRegistry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $this->postDataHelper = $this->getMockBuilder(\Magento\Framework\Data\Helper\PostHelper::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->wishlistItem = $this->getMockBuilder(\Magento\Wishlist\Model\Item::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods([
  101. 'getProduct',
  102. 'getWishlistItemId',
  103. ])
  104. ->getMock();
  105. $this->wishlist = $this->getMockBuilder(\Magento\Wishlist\Model\Wishlist::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  112. $this->model = $objectManager->getObject(
  113. \Magento\Wishlist\Helper\Data::class,
  114. [
  115. 'context' => $this->context,
  116. 'storeManager' => $this->storeManager,
  117. 'wishlistProvider' => $this->wishlistProvider,
  118. 'coreRegistry' => $this->coreRegistry,
  119. 'postDataHelper' => $this->postDataHelper
  120. ]
  121. );
  122. }
  123. public function testGetAddToCartUrl()
  124. {
  125. $url = 'http://magento.com/wishlist/index/index/wishlist_id/1/?___store=default';
  126. $this->store->expects($this->once())
  127. ->method('getUrl')
  128. ->with('wishlist/index/cart', ['item' => '%item%'])
  129. ->will($this->returnValue($url));
  130. $this->urlBuilder->expects($this->any())
  131. ->method('getUrl')
  132. ->with('wishlist/index/index', ['_current' => true, '_use_rewrite' => true, '_scope_to_url' => true])
  133. ->will($this->returnValue($url));
  134. $this->assertEquals($url, $this->model->getAddToCartUrl('%item%'));
  135. }
  136. public function testGetConfigureUrl()
  137. {
  138. $url = 'http://magento2ce/wishlist/index/configure/id/4/product_id/30/';
  139. /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $wishlistItem */
  140. $wishlistItem = $this->getMock(
  141. \Magento\Wishlist\Model\Item::class,
  142. ['getWishlistItemId', 'getProductId'],
  143. [],
  144. '',
  145. false
  146. );
  147. $wishlistItem
  148. ->expects($this->once())
  149. ->method('getWishlistItemId')
  150. ->will($this->returnValue(4));
  151. $wishlistItem
  152. ->expects($this->once())
  153. ->method('getProductId')
  154. ->will($this->returnValue(30));
  155. $this->urlBuilder->expects($this->once())
  156. ->method('getUrl')
  157. ->with('wishlist/index/configure', ['id' => 4, 'product_id' => 30])
  158. ->will($this->returnValue($url));
  159. $this->assertEquals($url, $this->model->getConfigureUrl($wishlistItem));
  160. }
  161. public function testGetWishlist()
  162. {
  163. $this->wishlistProvider->expects($this->once())
  164. ->method('getWishlist')
  165. ->will($this->returnValue($this->wishlist));
  166. $this->assertEquals($this->wishlist, $this->model->getWishlist());
  167. }
  168. public function testGetWishlistWithCoreRegistry()
  169. {
  170. $this->coreRegistry->expects($this->any())
  171. ->method('registry')
  172. ->willReturn($this->wishlist);
  173. $this->assertEquals($this->wishlist, $this->model->getWishlist());
  174. }
  175. public function testGetAddToCartParams()
  176. {
  177. $url = 'result url';
  178. $storeId = 1;
  179. $wishlistItemId = 1;
  180. $this->wishlistItem->expects($this->once())
  181. ->method('getProduct')
  182. ->willReturn($this->product);
  183. $this->wishlistItem->expects($this->once())
  184. ->method('getWishlistItemId')
  185. ->willReturn($wishlistItemId);
  186. $this->product->expects($this->once())
  187. ->method('isVisibleInSiteVisibility')
  188. ->willReturn(true);
  189. $this->product->expects($this->once())
  190. ->method('getStoreId')
  191. ->willReturn($storeId);
  192. $this->requestMock->expects($this->never())
  193. ->method('getServer');
  194. $this->urlEncoderMock->expects($this->never())
  195. ->method('encode');
  196. $this->store->expects($this->once())
  197. ->method('getUrl')
  198. ->with('wishlist/index/cart')
  199. ->willReturn($url);
  200. $this->postDataHelper->expects($this->once())
  201. ->method('getPostData')
  202. ->with($url, ['item' => $wishlistItemId])
  203. ->willReturn($url);
  204. $this->assertEquals($url, $this->model->getAddToCartParams($this->wishlistItem));
  205. }
  206. public function testGetAddToCartParamsWithReferer()
  207. {
  208. $url = 'result url';
  209. $storeId = 1;
  210. $wishlistItemId = 1;
  211. $referer = 'referer';
  212. $refererEncoded = 'referer_encoded';
  213. $this->wishlistItem->expects($this->once())
  214. ->method('getProduct')
  215. ->willReturn($this->product);
  216. $this->wishlistItem->expects($this->once())
  217. ->method('getWishlistItemId')
  218. ->willReturn($wishlistItemId);
  219. $this->product->expects($this->once())
  220. ->method('isVisibleInSiteVisibility')
  221. ->willReturn(true);
  222. $this->product->expects($this->once())
  223. ->method('getStoreId')
  224. ->willReturn($storeId);
  225. $this->requestMock->expects($this->once())
  226. ->method('getServer')
  227. ->with('HTTP_REFERER')
  228. ->willReturn($referer);
  229. $this->urlEncoderMock->expects($this->once())
  230. ->method('encode')
  231. ->with($referer)
  232. ->willReturn($refererEncoded);
  233. $this->store->expects($this->once())
  234. ->method('getUrl')
  235. ->with('wishlist/index/cart')
  236. ->willReturn($url);
  237. $this->postDataHelper->expects($this->once())
  238. ->method('getPostData')
  239. ->with($url, ['item' => $wishlistItemId, ActionInterface::PARAM_NAME_URL_ENCODED => $refererEncoded])
  240. ->willReturn($url);
  241. $this->assertEquals($url, $this->model->getAddToCartParams($this->wishlistItem, true));
  242. }
  243. public function testGetRemoveParams()
  244. {
  245. $url = 'result url';
  246. $wishlistItemId = 1;
  247. $this->wishlistItem->expects($this->once())
  248. ->method('getWishlistItemId')
  249. ->willReturn($wishlistItemId);
  250. $this->requestMock->expects($this->never())
  251. ->method('getServer');
  252. $this->urlEncoderMock->expects($this->never())
  253. ->method('encode');
  254. $this->urlBuilder->expects($this->once())
  255. ->method('getUrl')
  256. ->with('wishlist/index/remove', [])
  257. ->willReturn($url);
  258. $this->postDataHelper->expects($this->once())
  259. ->method('getPostData')
  260. ->with($url, ['item' => $wishlistItemId])
  261. ->willReturn($url);
  262. $this->assertEquals($url, $this->model->getRemoveParams($this->wishlistItem));
  263. }
  264. public function testGetRemoveParamsWithReferer()
  265. {
  266. $url = 'result url';
  267. $wishlistItemId = 1;
  268. $referer = 'referer';
  269. $refererEncoded = 'referer_encoded';
  270. $this->wishlistItem->expects($this->once())
  271. ->method('getWishlistItemId')
  272. ->willReturn($wishlistItemId);
  273. $this->requestMock->expects($this->once())
  274. ->method('getServer')
  275. ->with('HTTP_REFERER')
  276. ->willReturn($referer);
  277. $this->urlEncoderMock->expects($this->once())
  278. ->method('encode')
  279. ->with($referer)
  280. ->willReturn($refererEncoded);
  281. $this->urlBuilder->expects($this->once())
  282. ->method('getUrl')
  283. ->with('wishlist/index/remove', [])
  284. ->willReturn($url);
  285. $this->postDataHelper->expects($this->once())
  286. ->method('getPostData')
  287. ->with($url, ['item' => $wishlistItemId, ActionInterface::PARAM_NAME_URL_ENCODED => $refererEncoded])
  288. ->willReturn($url);
  289. $this->assertEquals($url, $this->model->getRemoveParams($this->wishlistItem, true));
  290. }
  291. public function testGetSharedAddToCartUrl()
  292. {
  293. $url = 'result url';
  294. $storeId = 1;
  295. $wishlistItemId = 1;
  296. $this->wishlistItem->expects($this->once())
  297. ->method('getProduct')
  298. ->willReturn($this->product);
  299. $this->wishlistItem->expects($this->once())
  300. ->method('getWishlistItemId')
  301. ->willReturn($wishlistItemId);
  302. $this->product->expects($this->once())
  303. ->method('isVisibleInSiteVisibility')
  304. ->willReturn(true);
  305. $this->product->expects($this->once())
  306. ->method('getStoreId')
  307. ->willReturn($storeId);
  308. $this->store->expects($this->once())
  309. ->method('getUrl')
  310. ->with('wishlist/shared/cart')
  311. ->willReturn($url);
  312. $this->postDataHelper->expects($this->once())
  313. ->method('getPostData')
  314. ->with($url, ['item' => $wishlistItemId])
  315. ->willReturn($url);
  316. $this->assertEquals($url, $this->model->getSharedAddToCartUrl($this->wishlistItem));
  317. }
  318. public function testGetSharedAddAllToCartUrl()
  319. {
  320. $url = 'result url';
  321. $this->store->expects($this->once())
  322. ->method('getUrl')
  323. ->with('*/*/allcart', ['_current' => true])
  324. ->willReturn($url);
  325. $this->postDataHelper->expects($this->once())
  326. ->method('getPostData')
  327. ->with($url)
  328. ->willReturn($url);
  329. $this->assertEquals($url, $this->model->getSharedAddAllToCartUrl());
  330. }
  331. }