Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PrintActionTest.php
Go to the documentation of this file.
1 <?php
7 
12 class PrintActionTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $printAction;
18 
22  protected $context;
23 
27  protected $requestMock;
28 
33 
38 
42  protected $objectManagerMock;
43 
47  protected $creditmemoMock;
48 
52  protected $creditmemoPdfMock;
53 
57  protected $pdfMock;
58 
62  protected $dateTimeMock;
63 
67  protected $fileFactoryMock;
68 
72  protected $responseMock;
73 
78 
82  protected $resultForwardMock;
83 
87  protected function setUp()
88  {
89  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
90  ->getMock();
91  $this->creditmemoLoaderMock = $this->getMockBuilder(
92  \Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader::class
93  )->disableOriginalConstructor()
94  ->setMethods(
95  [
96  'setOrderId',
97  'setCreditmemoId',
98  'setCreditmemo',
99  'setInvoiceId',
100  'load'
101  ]
102  )
103  ->getMock();
104  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
105  ->getMock();
106  $this->creditmemoRepositoryMock = $this->createMock(\Magento\Sales\Api\CreditmemoRepositoryInterface::class);
107  $this->creditmemoMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110  $this->creditmemoPdfMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Pdf\Creditmemo::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113  $this->pdfMock = $this->getMockBuilder(\Zend_Pdf::class)
114  ->disableOriginalConstructor()
115  ->getMock();
116  $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $this->fileFactoryMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
120  ->disableOriginalConstructor()
121  ->getMock();
122  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
123  ->getMock();
124  $this->resultForwardFactoryMock = $this->getMockBuilder(
125  \Magento\Backend\Model\View\Result\ForwardFactory::class
126  )->disableOriginalConstructor()
127  ->setMethods(['create'])
128  ->getMock();
129  $this->resultForwardMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
130  ->disableOriginalConstructor()
131  ->getMock();
132 
133  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
134  $this->context = $objectManager->getObject(
135  \Magento\Backend\App\Action\Context::class,
136  [
137  'request' => $this->requestMock,
138  'objectManager' => $this->objectManagerMock
139  ]
140  );
141  $this->printAction = $objectManager->getObject(
142  \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\PrintAction::class,
143  [
144  'context' => $this->context,
145  'fileFactory' => $this->fileFactoryMock,
146  'resultForwardFactory' => $this->resultForwardFactoryMock,
147  'creditmemoLoader' => $this->creditmemoLoaderMock,
148  'creditmemoRepository' => $this->creditmemoRepositoryMock,
149  ]
150  );
151  }
152 
156  public function testExecute()
157  {
158  $creditmemoId = 2;
159  $date = '2015-01-19_13-03-45';
160  $fileName = 'creditmemo2015-01-19_13-03-45.pdf';
161  $pdfContent = 'pdf0123456789';
162  $fileData = ['type' => 'string', 'value' => $pdfContent, 'rm' => true];
163  $this->prepareTestExecute($creditmemoId);
164 
165  $this->objectManagerMock->expects($this->any())
166  ->method('create')
167  ->willReturnMap(
168  [
169  [\Magento\Sales\Model\Order\Creditmemo::class, [], $this->creditmemoMock],
170  [\Magento\Sales\Model\Order\Pdf\Creditmemo::class, [], $this->creditmemoPdfMock]
171  ]
172  );
173  $this->creditmemoRepositoryMock->expects($this->once())
174  ->method('get')
175  ->with($creditmemoId)
176  ->willReturn($this->creditmemoMock);
177  $this->creditmemoPdfMock->expects($this->once())
178  ->method('getPdf')
179  ->with([$this->creditmemoMock])
180  ->willReturn($this->pdfMock);
181  $this->objectManagerMock->expects($this->once())
182  ->method('get')
183  ->with(\Magento\Framework\Stdlib\DateTime\DateTime::class)
184  ->willReturn($this->dateTimeMock);
185  $this->dateTimeMock->expects($this->once())
186  ->method('date')
187  ->with('Y-m-d_H-i-s')
188  ->willReturn($date);
189  $this->pdfMock->expects($this->once())
190  ->method('render')
191  ->willReturn($pdfContent);
192  $this->fileFactoryMock->expects($this->once())
193  ->method('create')
194  ->with(
195  $fileName,
196  $fileData,
197  \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
198  'application/pdf'
199  )
200  ->willReturn($this->responseMock);
201 
202  $this->assertInstanceOf(
203  \Magento\Framework\App\ResponseInterface::class,
204  $this->printAction->execute()
205  );
206  }
207 
211  public function testExecuteNoCreditmemoId()
212  {
213  $this->prepareTestExecute();
214 
215  $this->resultForwardFactoryMock->expects($this->once())
216  ->method('create')
217  ->willReturn($this->resultForwardMock);
218  $this->resultForwardMock->expects($this->once())
219  ->method('forward')
220  ->with('noroute')
221  ->willReturnSelf();
222 
223  $this->assertInstanceOf(
224  \Magento\Backend\Model\View\Result\Forward::class,
225  $this->printAction->execute()
226  );
227  }
228 
232  protected function prepareTestExecute($creditmemoId = null)
233  {
234  $orderId = 1;
235  $creditmemo = 3;
236  $invoiceId = 4;
237 
238  $this->requestMock->expects($this->any())
239  ->method('getParam')
240  ->willReturnMap(
241  [
242  ['order_id', null, $orderId],
243  ['creditmemo_id', null, $creditmemoId],
244  ['creditmemo', null, $creditmemo],
245  ['invoice_id', null, $invoiceId]
246  ]
247  );
248  $this->creditmemoLoaderMock->expects($this->once())
249  ->method('setOrderId')
250  ->with($orderId)
251  ->willReturnSelf();
252  $this->creditmemoLoaderMock->expects($this->once())
253  ->method('setCreditmemoId')
254  ->with($creditmemoId)
255  ->willReturnSelf();
256  $this->creditmemoLoaderMock->expects($this->once())
257  ->method('setCreditmemo')
258  ->with($creditmemo)
259  ->willReturnSelf();
260  $this->creditmemoLoaderMock->expects($this->once())
261  ->method('setInvoiceId')
262  ->with($invoiceId)
263  ->willReturnSelf();
264  $this->creditmemoLoaderMock->expects($this->once())
265  ->method('load');
266  }
267 }
$objectManager
Definition: bootstrap.php:17
$fileName
Definition: translate.phtml:15