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 
10 
16 class PrintActionTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $requestMock;
22 
26  protected $responseMock;
27 
31  protected $fileFactory;
32 
36  protected $actionFlagMock;
37 
41  protected $sessionMock;
42 
46  protected $objectManagerMock;
47 
51  protected $controller;
52 
53  protected function setUp()
54  {
55  $objectManager = new ObjectManager($this);
56 
57  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
58  ->disableOriginalConstructor()
59  ->setMethods([])
60  ->getMock();
61  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
62  ->disableOriginalConstructor()
63  ->setMethods([])
64  ->getMock();
65 
66  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
67  ->disableOriginalConstructor()
68  ->setMethods([])
69  ->getMock();
70 
71  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
72  ->disableOriginalConstructor()
73  ->setMethods([])
74  ->getMock();
75 
76  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
77 
78  $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
79  ->disableOriginalConstructor()
80  ->setMethods([])
81  ->getMock();
82  $contextMock->expects($this->any())
83  ->method('getRequest')
84  ->will($this->returnValue($this->requestMock));
85  $contextMock->expects($this->any())
86  ->method('getResponse')
87  ->will($this->returnValue($this->responseMock));
88  $contextMock->expects($this->any())
89  ->method('getSession')
90  ->will($this->returnValue($this->sessionMock));
91  $contextMock->expects($this->any())
92  ->method('getActionFlag')
93  ->will($this->returnValue($this->actionFlagMock));
94  $contextMock->expects($this->any())
95  ->method('getObjectManager')
96  ->will($this->returnValue($this->objectManagerMock));
97 
98  $this->fileFactory = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
99  ->disableOriginalConstructor()
100  ->setMethods([])
101  ->getMock();
102 
103  $this->controller = $objectManager->getObject(
104  \Magento\Sales\Controller\Adminhtml\Order\Invoice\PrintAction::class,
105  [
106  'context' => $contextMock,
107  'fileFactory' => $this->fileFactory
108  ]
109  );
110  }
111 
112  public function testExecute()
113  {
114  $invoiceId = 2;
115 
116  $this->requestMock->expects($this->once())
117  ->method('getParam')
118  ->with('invoice_id')
119  ->will($this->returnValue($invoiceId));
120 
121  $invoiceMock = $this->createMock(\Magento\Sales\Model\Order\Invoice::class);
122 
123  $pdfMock = $this->createPartialMock(\Magento\Sales\Model\Order\Pdf\Invoice::class, ['render', 'getPdf']);
124  $pdfMock->expects($this->once())
125  ->method('getPdf')
126  ->willReturnSelf();
127  $pdfMock->expects($this->once())
128  ->method('render');
129  $dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\DateTime::class);
130 
131  $invoiceRepository = $this->getMockBuilder(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
132  ->disableOriginalConstructor()
133  ->getMock();
134  $invoiceRepository->expects($this->any())
135  ->method('get')
136  ->willReturn($invoiceMock);
137 
138  $this->objectManagerMock->expects($this->at(0))
139  ->method('create')
140  ->with(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
141  ->willReturn($invoiceRepository);
142  $this->objectManagerMock->expects($this->at(1))
143  ->method('create')
144  ->with(\Magento\Sales\Model\Order\Pdf\Invoice::class)
145  ->willReturn($pdfMock);
146  $this->objectManagerMock->expects($this->at(2))
147  ->method('get')
148  ->with(\Magento\Framework\Stdlib\DateTime\DateTime::class)
149  ->willReturn($dateTimeMock);
150 
151  $this->assertNull($this->controller->execute());
152  }
153 }
$invoiceRepository
$objectManager
Definition: bootstrap.php:17