71 $this->objectManagerHelper =
new ObjectManagerHelper($this);
73 $this->request = $this->createMock(\
Magento\Framework\
App\RequestInterface::class);
74 $this->response = $this->createPartialMock(
75 \
Magento\Framework\
App\ResponseInterface::class,
77 'setHttpResponseCode',
86 $this->helperData = $this->createPartialMock(\
Magento\Downloadable\Helper\Data::class, [
89 $this->downloadHelper = $this->createPartialMock(\
Magento\Downloadable\Helper\Download::class, [
94 'getContentDisposition',
97 $this->product = $this->createPartialMock(\
Magento\Catalog\Model\Product::class, [
104 $this->messageManager = $this->createMock(\
Magento\Framework\Message\ManagerInterface::class);
105 $this->redirect = $this->createMock(\
Magento\Framework\
App\
Response\RedirectInterface::class);
106 $this->urlInterface = $this->createMock(\
Magento\Framework\UrlInterface::class);
107 $this->objectManager = $this->createPartialMock(\
Magento\Framework\ObjectManager\ObjectManager::class, [
111 $this->linkSample = $this->objectManagerHelper->getObject(
112 \
Magento\Downloadable\Controller\Download\LinkSample::class,
114 'objectManager' => $this->objectManager,
115 'request' => $this->request,
116 'response' => $this->response,
117 'messageManager' => $this->messageManager,
118 'redirect' => $this->redirect
125 $linkMock = $this->getMockBuilder(\
Magento\Downloadable\Model\Link::class)
126 ->disableOriginalConstructor()
127 ->setMethods([
'getId',
'load',
'getSampleType',
'getSampleUrl'])
130 $this->request->expects($this->once())->method(
'getParam')->with(
'link_id', 0)->willReturn(
'some_link_id');
131 $this->objectManager->expects($this->once())
133 ->with(\
Magento\Downloadable\Model\Link::class)
134 ->willReturn($linkMock);
135 $linkMock->expects($this->once())->method(
'load')->with(
'some_link_id')->willReturnSelf();
136 $linkMock->expects($this->once())->method(
'getId')->willReturn(
'some_link_id');
137 $linkMock->expects($this->once())->method(
'getSampleType')->willReturn(
138 \
Magento\Downloadable\Helper\Download::LINK_TYPE_URL
140 $linkMock->expects($this->once())->method(
'getSampleUrl')->willReturn(
'sample_url');
141 $this->objectManager->expects($this->at(1))
143 ->with(\
Magento\Downloadable\Helper\Download::class)
144 ->willReturn($this->downloadHelper);
145 $this->response->expects($this->once())->method(
'setHttpResponseCode')->with(200)->willReturnSelf();
146 $this->response->expects($this->any())->method(
'setHeader')->willReturnSelf();
147 $this->downloadHelper->expects($this->once())->method(
'output')->willThrowException(
new \Exception());
148 $this->messageManager->expects($this->once())
150 ->with(
'Sorry, there was an error getting requested content. Please contact the store owner.')
152 $this->redirect->expects($this->once())->method(
'getRedirectUrl')->willReturn(
'redirect_url');
153 $this->response->expects($this->once())->method(
'setRedirect')->with(
'redirect_url')->willReturnSelf();
155 $this->assertEquals($this->response, $this->linkSample->execute());
160 $linkMock = $this->getMockBuilder(\
Magento\Downloadable\Model\Link::class)
161 ->disableOriginalConstructor()
162 ->setMethods([
'getId',
'load',
'getSampleType',
'getSampleUrl',
'getBaseSamplePath'])
164 $fileMock = $this->getMockBuilder(\
Magento\Downloadable\Helper\File::class)
165 ->disableOriginalConstructor()
166 ->setMethods([
'getFilePath',
'load',
'getSampleType',
'getSampleUrl'])
169 $this->request->expects($this->once())->method(
'getParam')->with(
'link_id', 0)->willReturn(
'some_link_id');
170 $this->objectManager->expects($this->at(0))
172 ->with(\
Magento\Downloadable\Model\Link::class)
173 ->willReturn($linkMock);
174 $linkMock->expects($this->once())->method(
'load')->with(
'some_link_id')->willReturnSelf();
175 $linkMock->expects($this->once())->method(
'getId')->willReturn(
'some_link_id');
176 $linkMock->expects($this->any())->method(
'getSampleType')->willReturn(
177 \
Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
179 $this->objectManager->expects($this->at(1))
181 ->with(\
Magento\Downloadable\Helper\File::class)
182 ->willReturn($fileMock);
183 $this->objectManager->expects($this->at(2))
185 ->with(\
Magento\Downloadable\Model\Link::class)
186 ->willReturn($linkMock);
187 $linkMock->expects($this->once())->method(
'getBaseSamplePath')->willReturn(
'downloadable/files/link_samples');
188 $this->objectManager->expects($this->at(3))
190 ->with(\
Magento\Downloadable\Helper\Download::class)
191 ->willReturn($this->downloadHelper);
192 $this->response->expects($this->once())->method(
'setHttpResponseCode')->with(200)->willReturnSelf();
193 $this->response->expects($this->any())->method(
'setHeader')->willReturnSelf();
194 $this->downloadHelper->expects($this->once())->method(
'output')->willThrowException(
new \Exception());
195 $this->messageManager->expects($this->once())
197 ->with(
'Sorry, there was an error getting requested content. Please contact the store owner.')
199 $this->redirect->expects($this->once())->method(
'getRedirectUrl')->willReturn(
'redirect_url');
200 $this->response->expects($this->once())->method(
'setRedirect')->with(
'redirect_url')->willReturnSelf();
202 $this->assertEquals($this->response, $this->linkSample->execute());
testExecuteLinkTypeFile()