Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WishlistProviderTest.php
Go to the documentation of this file.
1 <?php
7 
8 class WishlistProviderTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $wishlistProvider;
14 
18  protected $request;
19 
23  protected $wishlistFactory;
24 
28  protected $customerSession;
29 
33  protected $messageManager;
34 
40  protected function setUp()
41  {
42  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43 
44  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
45 
46  $this->wishlistFactory = $this->createPartialMock(\Magento\Wishlist\Model\WishlistFactory::class, ['create']);
47 
48  $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
49 
50  $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
51 
52  $this->wishlistProvider = $objectManager->getObject(
53  \Magento\Wishlist\Controller\WishlistProvider::class,
54  [
55  'request' => $this->request,
56  'wishlistFactory' => $this->wishlistFactory,
57  'customerSession' => $this->customerSession,
58  'messageManager' => $this->messageManager
59  ]
60  );
61  }
62 
63  public function testGetWishlist()
64  {
65  $wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
66 
67  $this->wishlistFactory->expects($this->once())
68  ->method('create')
69  ->will($this->returnValue($wishlist));
70 
71  $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
72  }
73 
74  public function testGetWishlistWithCustomer()
75  {
76  $wishlist = $this->createPartialMock(
77  \Magento\Wishlist\Model\Wishlist::class,
78  ['loadByCustomerId', 'getId', 'getCustomerId', '__wakeup']
79  );
80  $wishlist->expects($this->once())
81  ->method('loadByCustomerId')
82  ->will($this->returnSelf());
83  $wishlist->expects($this->once())
84  ->method('getId')
85  ->will($this->returnValue(1));
86  $wishlist->expects($this->once())
87  ->method('getCustomerId')
88  ->will($this->returnValue(1));
89 
90  $this->wishlistFactory->expects($this->once())
91  ->method('create')
92  ->will($this->returnValue($wishlist));
93 
94  $this->customerSession->expects($this->once())
95  ->method('getCustomerId')
96  ->will($this->returnValue(1));
97 
98  $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
99  }
100 
102  {
103  $wishlist = $this->createPartialMock(
104  \Magento\Wishlist\Model\Wishlist::class,
105  ['loadByCustomerId', 'load', 'getId', 'getCustomerId', '__wakeup']
106  );
107 
108  $wishlist->expects($this->once())
109  ->method('load')
110  ->will($this->returnSelf());
111  $wishlist->expects($this->any())
112  ->method('getId')
113  ->will($this->returnValue(1));
114  $wishlist->expects($this->once())
115  ->method('getCustomerId')
116  ->will($this->returnValue(1));
117 
118  $this->wishlistFactory->expects($this->once())
119  ->method('create')
120  ->will($this->returnValue($wishlist));
121 
122  $this->request->expects($this->once())
123  ->method('getParam')
124  ->will($this->returnValue(1));
125 
126  $this->customerSession->expects($this->once())
127  ->method('getCustomerId')
128  ->will($this->returnValue(1));
129 
130  $this->assertEquals($wishlist, $this->wishlistProvider->getWishlist());
131  }
132 
134  {
135  $wishlist = $this->createPartialMock(
136  \Magento\Wishlist\Model\Wishlist::class,
137  ['loadByCustomerId', 'load', 'getId', 'getCustomerId', '__wakeup']
138  );
139 
140  $wishlist->expects($this->once())
141  ->method('load')
142  ->will($this->returnSelf());
143  $wishlist->expects($this->any())
144  ->method('getId')
145  ->will($this->returnValue(1));
146  $wishlist->expects($this->once())
147  ->method('getCustomerId')
148  ->will($this->returnValue(1));
149 
150  $this->wishlistFactory->expects($this->once())
151  ->method('create')
152  ->will($this->returnValue($wishlist));
153 
154  $this->request->expects($this->once())
155  ->method('getParam')
156  ->will($this->returnValue(1));
157 
158  $this->assertEquals(false, $this->wishlistProvider->getWishlist());
159  }
160 }
$objectManager
Definition: bootstrap.php:17
$wishlist
Definition: wishlist.php:10