Skip to content

Commit 41e15fd

Browse files
author
Yuri Kovsher
committed
MAGETWO-2204: "HEADERS ALREADY SENT" When Controller Action Outputs Directly
1 parent 8733b0b commit 41e15fd

File tree

1 file changed

+194
-107
lines changed
  • dev/tests/unit/testsuite/Magento/Backup/Controller/Adminhtml/Index

1 file changed

+194
-107
lines changed

dev/tests/unit/testsuite/Magento/Backup/Controller/Adminhtml/Index/DownloadTest.php

Lines changed: 194 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,144 @@
55
*/
66
namespace Magento\Backup\Controller\Adminhtml\Index;
77

8+
use Magento\TestFramework\Helper\ObjectManager;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
11+
/**
12+
* @covers \Magento\Backup\Controller\Adminhtml\Index\Download
13+
*/
814
class DownloadTest extends \PHPUnit_Framework_TestCase
915
{
16+
/**
17+
* @var \Magento\TestFramework\Helper\ObjectManager
18+
*/
19+
protected $objectManager;
20+
21+
/**
22+
* @var \Magento\Backend\App\Action\Context
23+
*/
24+
protected $context;
25+
26+
/**
27+
* @var \Magento\Backup\Controller\Adminhtml\Index\Download
28+
*/
29+
protected $downloadController;
30+
31+
/**
32+
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $objectManagerMock;
35+
36+
/**
37+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $requestMock;
40+
1041
/**
1142
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
1243
*/
13-
protected $response;
44+
protected $responseMock;
1445

1546
/**
1647
* @var \Magento\Backup\Model\BackupFactory|\PHPUnit_Framework_MockObject_MockObject
1748
*/
18-
protected $backupModelFactory;
49+
protected $backupModelFactoryMock;
1950

2051
/**
2152
* @var \Magento\Backup\Model\Backup|\PHPUnit_Framework_MockObject_MockObject
2253
*/
23-
protected $backup;
54+
protected $backupModelMock;
2455

2556
/**
26-
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
57+
* @var \Magento\Backup\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
protected $dataHelperMock;
60+
61+
/**
62+
* @var \Magento\Framework\App\Response\Http\FileFactory|\PHPUnit_Framework_MockObject_MockObject
63+
*/
64+
protected $fileFactoryMock;
65+
66+
/**
67+
* @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
2768
*/
28-
protected $request;
69+
protected $resultRawFactoryMock;
70+
71+
/**
72+
* @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
73+
*/
74+
protected $resultRedirectFactoryMock;
75+
76+
/**
77+
* @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
78+
*/
79+
protected $resultRawMock;
80+
81+
/**
82+
* @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
83+
*/
84+
protected $resultRedirectMock;
2985

3086
public function setUp()
3187
{
32-
$this->backup = $this->getMock(
33-
'\Magento\Backup\Model\Backup',
34-
['getTime', 'exists', 'getSize', 'output'],
35-
[],
36-
'',
37-
false
88+
$this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
89+
->getMock();
90+
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
91+
->getMock();
92+
$this->responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface')
93+
->getMock();
94+
$this->backupModelFactoryMock = $this->getMockBuilder('Magento\Backup\Model\BackupFactory')
95+
->disableOriginalConstructor()
96+
->getMock();
97+
$this->backupModelMock = $this->getMockBuilder('Magento\Backup\Model\Backup')
98+
->disableOriginalConstructor()
99+
->setMethods(['getTime', 'exists', 'getSize', 'output'])
100+
->getMock();
101+
$this->dataHelperMock = $this->getMockBuilder('Magento\Backup\Helper\Data')
102+
->disableOriginalConstructor()
103+
->getMock();
104+
$this->fileFactoryMock = $this->getMockBuilder('Magento\Framework\App\Response\Http\FileFactory')
105+
->disableOriginalConstructor()
106+
->getMock();
107+
$this->resultRawFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RawFactory')
108+
->disableOriginalConstructor()
109+
->setMethods(['create'])
110+
->getMock();
111+
$this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory')
112+
->disableOriginalConstructor()
113+
->setMethods(['create'])
114+
->getMock();
115+
$this->resultRawMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Raw')
116+
->disableOriginalConstructor()
117+
->getMock();
118+
$this->resultRedirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
119+
->disableOriginalConstructor()
120+
->getMock();
121+
122+
$this->objectManager = new ObjectManager($this);
123+
$this->context = $this->objectManager->getObject(
124+
'Magento\Backend\App\Action\Context',
125+
[
126+
'objectManager' => $this->objectManagerMock,
127+
'request' => $this->requestMock,
128+
'response' => $this->responseMock
129+
]
130+
);
131+
$this->downloadController = $this->objectManager->getObject(
132+
'Magento\Backup\Controller\Adminhtml\Index\Download',
133+
[
134+
'context' => $this->context,
135+
'backupModelFactory' => $this->backupModelFactoryMock,
136+
'fileFactory' => $this->fileFactoryMock,
137+
'resultRawFactory' => $this->resultRawFactoryMock,
138+
'resultRedirectFactory' => $this->resultRedirectFactoryMock
139+
]
38140
);
39-
$this->request = $this->getMock('\Magento\Framework\App\RequestInterface', [], [], '', false);
40-
$this->backupModelFactory = $this->getMock('\Magento\Backup\Model\BackupFactory', [], [], '', false);
41-
$this->response = $this->getMock('\Magento\Framework\App\ResponseInterface', [], [], '', false);
42141
}
43142

143+
/**
144+
* @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
145+
*/
44146
public function testExecuteBackupFound()
45147
{
46148
$time = 1;
@@ -49,109 +151,94 @@ public function testExecuteBackupFound()
49151
$size = 10;
50152
$output = 'test';
51153

52-
$this->backup->expects($this->once())->method('getTime')->willReturn($time);
53-
$this->backup->expects($this->once())->method('exists')->willReturn(true);
54-
$this->backup->expects($this->once())->method('getSize')->willReturn($size);
55-
$this->backup->expects($this->once())->method('output')->willReturn($output);
56-
57-
$this->request->expects($this->any())->method('getParam')
58-
->will($this->returnValueMap([['time', null, $time], ['type', null, $type]]));
59-
60-
$this->backupModelFactory->expects($this->once())->method('create')->with($time, $type)
61-
->willReturn($this->backup);
62-
63-
$helper = $this->getMock('Magento\Backup\Helper\Data', [], [], '', false);
64-
$helper->expects($this->once())->method('generateBackupDownloadName')->with($this->backup)
154+
$this->backupModelMock->expects($this->atLeastOnce())
155+
->method('getTime')
156+
->willReturn($time);
157+
$this->backupModelMock->expects($this->atLeastOnce())
158+
->method('exists')
159+
->willReturn(true);
160+
$this->backupModelMock->expects($this->atLeastOnce())
161+
->method('getSize')
162+
->willReturn($size);
163+
$this->backupModelMock->expects($this->atLeastOnce())
164+
->method('output')
165+
->willReturn($output);
166+
$this->requestMock->expects($this->any())
167+
->method('getParam')
168+
->willReturnMap(
169+
[
170+
['time', null, $time],
171+
['type', null, $type]
172+
]
173+
);
174+
$this->backupModelFactoryMock->expects($this->once())
175+
->method('create')
176+
->with($time, $type)
177+
->willReturn($this->backupModelMock);
178+
$this->dataHelperMock->expects($this->once())
179+
->method('generateBackupDownloadName')
180+
->with($this->backupModelMock)
65181
->willReturn($filename);
66-
67-
$objectManager = $this->getMock('\Magento\Framework\ObjectManagerInterface', [], [], '', false);
68-
$objectManager->expects($this->once())->method('get')->with('Magento\Backup\Helper\Data')
69-
->willReturn($helper);
70-
71-
$fileFactory = $this->getMock('\Magento\Framework\App\Response\Http\FileFactory', [], [], '', false);
72-
$fileFactory->expects($this->once())->method('create')->with(
73-
$filename,
74-
null,
75-
\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
76-
'application/octet-stream',
77-
$size
78-
)->willReturn($this->response);
79-
80-
$resultRaw = $this->getMock('\Magento\Framework\Controller\Result\Raw', [], [], '', false);
81-
$resultRaw->expects($this->once())->method('setContents')->with($output);
82-
83-
$resultRawFactory = $this->getMock(
84-
'\Magento\Framework\Controller\Result\RawFactory',
85-
['create'],
86-
[],
87-
'',
88-
false
89-
);
90-
$resultRawFactory->expects($this->once())->method('create')->willReturn($resultRaw);
91-
92-
$context = $this->getMock('\Magento\Backend\App\Action\Context', [], [], '', false);
93-
$context->expects($this->once())->method('getRequest')->willReturn($this->request);
94-
$context->expects($this->once())->method('getObjectManager')->willReturn($objectManager);
95-
$context->expects($this->once())->method('getResponse')->willReturn($this->response);
96-
97-
/** @var Download|\PHPUnit_Framework_MockObject_MockObject $controller */
98-
$controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject(
99-
'Magento\Backup\Controller\Adminhtml\Index\Download',
100-
[
101-
'backupModelFactory' => $this->backupModelFactory,
102-
'context' => $context,
103-
'fileFactory' => $fileFactory,
104-
'resultRawFactory' => $resultRawFactory
105-
]
106-
);
107-
$this->assertSame($resultRaw, $controller->execute());
182+
$this->objectManagerMock->expects($this->once())
183+
->method('get')
184+
->with('Magento\Backup\Helper\Data')
185+
->willReturn($this->dataHelperMock);
186+
$this->fileFactoryMock->expects($this->once())
187+
->method('create')->with(
188+
$filename,
189+
null,
190+
DirectoryList::VAR_DIR,
191+
'application/octet-stream',
192+
$size
193+
)
194+
->willReturn($this->responseMock);
195+
$this->resultRawMock->expects($this->once())
196+
->method('setContents')
197+
->with($output);
198+
$this->resultRawFactoryMock->expects($this->once())
199+
->method('create')
200+
->willReturn($this->resultRawMock);
201+
202+
$this->assertSame($this->resultRawMock, $this->downloadController->execute());
108203
}
109204

110205
/**
111-
* @dataProvider executeBackupNotFoundDataProvider
112-
* @param string $time
206+
* @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
207+
* @param int $time
113208
* @param bool $exists
114209
* @param int $existsCount
210+
* @dataProvider executeBackupNotFoundDataProvider
115211
*/
116212
public function testExecuteBackupNotFound($time, $exists, $existsCount)
117213
{
118214
$type = 'db';
119215

120-
$this->backup->expects($this->once())->method('getTime')->willReturn($time);
121-
$this->backup->expects($this->exactly($existsCount))->method('exists')->willReturn($exists);
122-
123-
$this->request->expects($this->any())->method('getParam')
124-
->will($this->returnValueMap([['time', null, $time], ['type', null, $type]]));
125-
126-
$context = $this->getMock('\Magento\Backend\App\Action\Context', [], [], '', false);
127-
$context->expects($this->once())->method('getRequest')->willReturn($this->request);
128-
$context->expects($this->once())->method('getResponse')->willReturn($this->response);
129-
130-
$this->backupModelFactory->expects($this->once())->method('create')->with($time, $type)
131-
->willReturn($this->backup);
132-
133-
$resultRedirect = $this->getMock('Magento\Backend\Model\View\Result\Redirect', [], [], '', false);
134-
$resultRedirect->expects($this->once())->method('setPath')->with('backup/*');
135-
136-
$resultRedirectFactory = $this->getMock(
137-
'Magento\Backend\Model\View\Result\RedirectFactory',
138-
['create'],
139-
[],
140-
'',
141-
false
142-
);
143-
$resultRedirectFactory->expects($this->once())->method('create')->willReturn($resultRedirect);
144-
145-
/** @var Download|\PHPUnit_Framework_MockObject_MockObject $controller */
146-
$controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject(
147-
'Magento\Backup\Controller\Adminhtml\Index\Download',
148-
[
149-
'context' => $context,
150-
'backupModelFactory' => $this->backupModelFactory,
151-
'resultRedirectFactory' => $resultRedirectFactory
152-
]
153-
);
154-
$this->assertSame($resultRedirect, $controller->execute());
216+
$this->backupModelMock->expects($this->atLeastOnce())
217+
->method('getTime')
218+
->willReturn($time);
219+
$this->backupModelMock->expects($this->exactly($existsCount))
220+
->method('exists')
221+
->willReturn($exists);
222+
$this->requestMock->expects($this->any())
223+
->method('getParam')
224+
->willReturnMap(
225+
[
226+
['time', null, $time],
227+
['type', null, $type]
228+
]
229+
);
230+
$this->backupModelFactoryMock->expects($this->once())
231+
->method('create')
232+
->with($time, $type)
233+
->willReturn($this->backupModelMock);
234+
$this->resultRedirectMock->expects($this->once())
235+
->method('setPath')
236+
->with('backup/*');
237+
$this->resultRedirectFactoryMock->expects($this->once())
238+
->method('create')
239+
->willReturn($this->resultRedirectMock);
240+
241+
$this->assertSame($this->resultRedirectMock, $this->downloadController->execute());
155242
}
156243

157244
/**
@@ -165,4 +252,4 @@ public function executeBackupNotFoundDataProvider()
165252
[0, false, 0]
166253
];
167254
}
168-
}
255+
}

0 commit comments

Comments
 (0)