WishlistProviderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Test\Unit\Controller;
  7. class WishlistProviderTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Wishlist\Controller\WishlistProvider
  11. */
  12. protected $wishlistProvider;
  13. /**
  14. * @var \Magento\Framework\App\RequestInterface
  15. */
  16. protected $request;
  17. /**
  18. * @var \Magento\Wishlist\Model\WishlistFactory
  19. */
  20. protected $wishlistFactory;
  21. /**
  22. * @var \Magento\Customer\Model\Session
  23. */
  24. protected $customerSession;
  25. /**
  26. * @var \Magento\Framework\Message\ManagerInterface
  27. */
  28. protected $messageManager;
  29. /**
  30. * Set up mock objects for tested class
  31. *
  32. * @return void
  33. */
  34. protected function setUp()
  35. {
  36. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  37. $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  38. $this->wishlistFactory = $this->createPartialMock(\Magento\Wishlist\Model\WishlistFactory::class, ['create']);
  39. $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
  40. $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  41. $this->wishlistProvider = $objectManager->getObject(
  42. \Magento\Wishlist\Controller\WishlistProvider::class,
  43. [
  44. 'request' => $this->request,
  45. 'wishlistFactory' => $this->wishlistFactory,
  46. 'customerSession' => $this->customerSession,
  47. 'messageManager' => $this->messageManager
  48. ]
  49. );
  50. }
  51. public function testGetWishlist()
  52. {
  53. $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
  54. $this->wishlistFactory->expects($this->once())
  55. ->method('create')
  56. ->will($this->returnValue($wishlist));
  57. $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
  58. }
  59. public function testGetWishlistWithCustomer()
  60. {
  61. $wishlist = $this->createPartialMock(
  62. \Magento\Wishlist\Model\Wishlist::class,
  63. ['loadByCustomerId', 'getId', 'getCustomerId', '__wakeup']
  64. );
  65. $wishlist->expects($this->once())
  66. ->method('loadByCustomerId')
  67. ->will($this->returnSelf());
  68. $wishlist->expects($this->once())
  69. ->method('getId')
  70. ->will($this->returnValue(1));
  71. $wishlist->expects($this->once())
  72. ->method('getCustomerId')
  73. ->will($this->returnValue(1));
  74. $this->wishlistFactory->expects($this->once())
  75. ->method('create')
  76. ->will($this->returnValue($wishlist));
  77. $this->customerSession->expects($this->once())
  78. ->method('getCustomerId')
  79. ->will($this->returnValue(1));
  80. $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
  81. }
  82. public function testGetWishlistWithIdAndCustomer()
  83. {
  84. $wishlist = $this->createPartialMock(
  85. \Magento\Wishlist\Model\Wishlist::class,
  86. ['loadByCustomerId', 'load', 'getId', 'getCustomerId', '__wakeup']
  87. );
  88. $wishlist->expects($this->once())
  89. ->method('load')
  90. ->will($this->returnSelf());
  91. $wishlist->expects($this->any())
  92. ->method('getId')
  93. ->will($this->returnValue(1));
  94. $wishlist->expects($this->once())
  95. ->method('getCustomerId')
  96. ->will($this->returnValue(1));
  97. $this->wishlistFactory->expects($this->once())
  98. ->method('create')
  99. ->will($this->returnValue($wishlist));
  100. $this->request->expects($this->once())
  101. ->method('getParam')
  102. ->will($this->returnValue(1));
  103. $this->customerSession->expects($this->once())
  104. ->method('getCustomerId')
  105. ->will($this->returnValue(1));
  106. $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
  107. }
  108. public function testGetWishlistWithIdWithoutCustomer()
  109. {
  110. $wishlist = $this->createPartialMock(
  111. \Magento\Wishlist\Model\Wishlist::class,
  112. ['loadByCustomerId', 'load', 'getId', 'getCustomerId', '__wakeup']
  113. );
  114. $wishlist->expects($this->once())
  115. ->method('load')
  116. ->will($this->returnSelf());
  117. $wishlist->expects($this->any())
  118. ->method('getId')
  119. ->will($this->returnValue(1));
  120. $wishlist->expects($this->once())
  121. ->method('getCustomerId')
  122. ->will($this->returnValue(1));
  123. $this->wishlistFactory->expects($this->once())
  124. ->method('create')
  125. ->will($this->returnValue($wishlist));
  126. $this->request->expects($this->once())
  127. ->method('getParam')
  128. ->will($this->returnValue(1));
  129. $this->assertEquals(false, $this->wishlistProvider->getWishlist());
  130. }
  131. }