Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SampleTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class SampleTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $sample;
17 
20 
24  protected $request;
25 
29  protected $response;
30 
34  protected $objectManager;
35 
39  protected $messageManager;
40 
44  protected $redirect;
45 
49  protected $helperData;
50 
54  protected $downloadHelper;
55 
59  protected $product;
60 
64  protected $urlInterface;
65 
69  protected function setUp()
70  {
71  $this->objectManagerHelper = new ObjectManagerHelper($this);
72 
73  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
74  $this->response = $this->createPartialMock(
75  \Magento\Framework\App\ResponseInterface::class,
76  [
77  'setHttpResponseCode',
78  'clearBody',
79  'sendHeaders',
80  'sendResponse',
81  'setHeader',
82  'setRedirect'
83  ]
84  );
85 
86  $this->helperData = $this->createPartialMock(\Magento\Downloadable\Helper\Data::class, [
87  'getIsShareable'
88  ]);
89  $this->downloadHelper = $this->createPartialMock(\Magento\Downloadable\Helper\Download::class, [
90  'setResource',
91  'getFilename',
92  'getContentType',
93  'getFileSize',
94  'getContentDisposition',
95  'output'
96  ]);
97  $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
98  '_wakeup',
99  'load',
100  'getId',
101  'getProductUrl',
102  'getName'
103  ]);
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, [
108  'create',
109  'get'
110  ]);
111  $this->sample = $this->objectManagerHelper->getObject(
112  \Magento\Downloadable\Controller\Download\Sample::class,
113  [
114  'objectManager' => $this->objectManager,
115  'request' => $this->request,
116  'response' => $this->response,
117  'messageManager' => $this->messageManager,
118  'redirect' => $this->redirect
119  ]
120  );
121  }
122 
123  public function testExecuteLinkTypeUrl()
124  {
125  $sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
126  ->disableOriginalConstructor()
127  ->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl'])
128  ->getMock();
129 
130  $this->request->expects($this->once())->method('getParam')->with('sample_id', 0)->willReturn('some_sample_id');
131  $this->objectManager->expects($this->once())
132  ->method('create')
133  ->with(\Magento\Downloadable\Model\Sample::class)
134  ->willReturn($sampleMock);
135  $sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
136  $sampleMock->expects($this->once())->method('getId')->willReturn('some_link_id');
137  $sampleMock->expects($this->once())->method('getSampleType')->willReturn(
138  \Magento\Downloadable\Helper\Download::LINK_TYPE_URL
139  );
140  $sampleMock->expects($this->once())->method('getSampleUrl')->willReturn('sample_url');
141  $this->objectManager->expects($this->at(1))
142  ->method('get')
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())
149  ->method('addError')
150  ->with('Sorry, there was an error getting requested content. Please contact the store owner.')
151  ->willReturnSelf();
152  $this->redirect->expects($this->once())->method('getRedirectUrl')->willReturn('redirect_url');
153  $this->response->expects($this->once())->method('setRedirect')->with('redirect_url')->willReturnSelf();
154 
155  $this->assertEquals($this->response, $this->sample->execute());
156  }
157 
158  public function testExecuteLinkTypeFile()
159  {
160  $sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
161  ->disableOriginalConstructor()
162  ->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl', 'getBaseSamplePath'])
163  ->getMock();
164  $fileHelperMock = $this->getMockBuilder(\Magento\Downloadable\Helper\File::class)
165  ->disableOriginalConstructor()
166  ->setMethods(['getFilePath'])
167  ->getMock();
168 
169  $this->request->expects($this->once())->method('getParam')->with('sample_id', 0)->willReturn('some_sample_id');
170  $this->objectManager->expects($this->at(0))
171  ->method('create')
172  ->with(\Magento\Downloadable\Model\Sample::class)
173  ->willReturn($sampleMock);
174  $sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
175  $sampleMock->expects($this->once())->method('getId')->willReturn('some_sample_id');
176  $sampleMock->expects($this->any())->method('getSampleType')->willReturn(
177  \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
178  );
179  $this->objectManager->expects($this->at(1))
180  ->method('get')
181  ->with(\Magento\Downloadable\Helper\File::class)
182  ->willReturn($fileHelperMock);
183  $fileHelperMock->expects($this->once())->method('getFilePath')->willReturn('file_path');
184  $this->objectManager->expects($this->at(2))
185  ->method('get')
186  ->with(\Magento\Downloadable\Helper\Download::class)
187  ->willReturn($this->downloadHelper);
188  $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
189  $this->response->expects($this->any())->method('setHeader')->willReturnSelf();
190  $this->downloadHelper->expects($this->once())->method('output')->willThrowException(new \Exception());
191  $this->messageManager->expects($this->once())
192  ->method('addError')
193  ->with('Sorry, there was an error getting requested content. Please contact the store owner.')
194  ->willReturnSelf();
195  $this->redirect->expects($this->once())->method('getRedirectUrl')->willReturn('redirect_url');
196  $this->response->expects($this->once())->method('setRedirect')->with('redirect_url')->willReturnSelf();
197 
198  $this->assertEquals($this->response, $this->sample->execute());
199  }
200 }