|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Wishlist\Controller\Index; |
| 8 | + |
| 9 | +class IndexTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 13 | + */ |
| 14 | + protected $context; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject |
| 18 | + */ |
| 19 | + protected $request; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $response; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Wishlist\Controller\WishlistProvider|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $wishlistProvider; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $customerSession; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Framework\App\View|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $view; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Store\App\Response\Redirect|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + protected $redirect; |
| 45 | + |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $this->context = $this->getMock('Magento\Framework\App\Action\Context', [], [], '', false); |
| 49 | + $this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false); |
| 50 | + $this->response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false); |
| 51 | + $this->customerSession = $this->getMock('Magento\Customer\Model\Session', [], [], '', false); |
| 52 | + $this->wishlistProvider = $this->getMock('Magento\Wishlist\Controller\WishlistProvider', [], [], '', false); |
| 53 | + $this->view = $this->getMock('Magento\Framework\App\View', [], [], '', false); |
| 54 | + $this->redirect = $this->getMock('\Magento\Store\App\Response\Redirect', [], [], '', false); |
| 55 | + } |
| 56 | + |
| 57 | + protected function prepareContext() |
| 58 | + { |
| 59 | + $om = $this->getMock('Magento\Framework\App\ObjectManager', [], [], '', false); |
| 60 | + $eventManager = $this->getMock('Magento\Framework\Event\Manager', null, [], '', false); |
| 61 | + $url = $this->getMock('Magento\Framework\Url', [], [], '', false); |
| 62 | + $actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', [], [], '', false); |
| 63 | + $messageManager = $this->getMock('Magento\Framework\Message\Manager', [], [], '', false); |
| 64 | + |
| 65 | + $this->context |
| 66 | + ->expects($this->any()) |
| 67 | + ->method('getObjectManager') |
| 68 | + ->willReturn($om); |
| 69 | + $this->context |
| 70 | + ->expects($this->any()) |
| 71 | + ->method('getRequest') |
| 72 | + ->willReturn($this->request); |
| 73 | + $this->context |
| 74 | + ->expects($this->any()) |
| 75 | + ->method('getResponse') |
| 76 | + ->willReturn($this->response); |
| 77 | + $this->context |
| 78 | + ->expects($this->any()) |
| 79 | + ->method('getEventManager') |
| 80 | + ->willReturn($eventManager); |
| 81 | + $this->context |
| 82 | + ->expects($this->any()) |
| 83 | + ->method('getUrl') |
| 84 | + ->willReturn($url); |
| 85 | + $this->context |
| 86 | + ->expects($this->any()) |
| 87 | + ->method('getActionFlag') |
| 88 | + ->willReturn($actionFlag); |
| 89 | + $this->context |
| 90 | + ->expects($this->any()) |
| 91 | + ->method('getRedirect') |
| 92 | + ->willReturn($this->redirect); |
| 93 | + $this->context |
| 94 | + ->expects($this->any()) |
| 95 | + ->method('getView') |
| 96 | + ->willReturn($this->view); |
| 97 | + $this->context |
| 98 | + ->expects($this->any()) |
| 99 | + ->method('getMessageManager') |
| 100 | + ->willReturn($messageManager); |
| 101 | + } |
| 102 | + |
| 103 | + public function getController() |
| 104 | + { |
| 105 | + $this->prepareContext(); |
| 106 | + return new \Magento\Wishlist\Controller\Index\Index( |
| 107 | + $this->context, |
| 108 | + $this->customerSession, |
| 109 | + $this->wishlistProvider |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + public function testExecuteWithoutWishlist() |
| 114 | + { |
| 115 | + $this->setExpectedException('Magento\Framework\App\Action\NotFoundException'); |
| 116 | + |
| 117 | + $this->wishlistProvider |
| 118 | + ->expects($this->once()) |
| 119 | + ->method('getWishlist') |
| 120 | + ->willReturn(null); |
| 121 | + |
| 122 | + $this->getController()->execute(); |
| 123 | + } |
| 124 | + |
| 125 | + public function testExecutePassed() |
| 126 | + { |
| 127 | + $wishlist = $this->getMock('Magento\Wishlist\Model\Wishlist', [], [], '', false); |
| 128 | + |
| 129 | + $block = $this->getMock('Magento\Ui\Component\Form\Element\Input', [], [], '', false); |
| 130 | + $block |
| 131 | + ->expects($this->at(0)) |
| 132 | + ->method('__call') |
| 133 | + ->with('setRefererUrl', ['http://referer-url-test.com']) |
| 134 | + ->willReturn(true); |
| 135 | + $block |
| 136 | + ->expects($this->at(1)) |
| 137 | + ->method('__call') |
| 138 | + ->with('setRefererUrl', ['http://referer-url.com']) |
| 139 | + ->willReturn(true); |
| 140 | + |
| 141 | + $this->redirect |
| 142 | + ->expects($this->once()) |
| 143 | + ->method('getRefererUrl') |
| 144 | + ->willReturn('http://referer-url-test.com'); |
| 145 | + |
| 146 | + $layout = $this->getMock('Magento\Framework\View\Layout', [], [], '', false); |
| 147 | + $layout |
| 148 | + ->expects($this->once()) |
| 149 | + ->method('getBlock') |
| 150 | + ->with('customer.wishlist') |
| 151 | + ->willReturn($block); |
| 152 | + $layout |
| 153 | + ->expects($this->once()) |
| 154 | + ->method('initMessages') |
| 155 | + ->willReturn(true); |
| 156 | + |
| 157 | + $this->wishlistProvider |
| 158 | + ->expects($this->once()) |
| 159 | + ->method('getWishlist') |
| 160 | + ->willReturn($wishlist); |
| 161 | + |
| 162 | + $this->view |
| 163 | + ->expects($this->once()) |
| 164 | + ->method('loadLayout') |
| 165 | + ->willReturn(true); |
| 166 | + $this->view |
| 167 | + ->expects($this->exactly(2)) |
| 168 | + ->method('getLayout') |
| 169 | + ->willReturn($layout); |
| 170 | + $this->view |
| 171 | + ->expects($this->once()) |
| 172 | + ->method('renderLayout') |
| 173 | + ->willReturn(true); |
| 174 | + |
| 175 | + $this->customerSession |
| 176 | + ->expects($this->once()) |
| 177 | + ->method('__call') |
| 178 | + ->with('getAddActionReferer', [true]) |
| 179 | + ->willReturn('http://referer-url.com'); |
| 180 | + |
| 181 | + $this->getController()->execute(); |
| 182 | + } |
| 183 | +} |
0 commit comments