123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backup\Test\Unit\Controller\Adminhtml\Index;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\App\Filesystem\DirectoryList;
- /**
- * @covers \Magento\Backup\Controller\Adminhtml\Index\Download
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DownloadTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * @var \Magento\Backend\App\Action\Context
- */
- protected $context;
- /**
- * @var \Magento\Backup\Controller\Adminhtml\Index\Download
- */
- protected $downloadController;
- /**
- * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectManagerMock;
- /**
- * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $responseMock;
- /**
- * @var \Magento\Backup\Model\BackupFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $backupModelFactoryMock;
- /**
- * @var \Magento\Backup\Model\Backup|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $backupModelMock;
- /**
- * @var \Magento\Backup\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataHelperMock;
- /**
- * @var \Magento\Framework\App\Response\Http\FileFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $fileFactoryMock;
- /**
- * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRawFactoryMock;
- /**
- * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRedirectFactoryMock;
- /**
- * @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRawMock;
- /**
- * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRedirectMock;
- protected function setUp()
- {
- $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
- ->getMock();
- $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
- ->getMock();
- $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
- ->getMock();
- $this->backupModelFactoryMock = $this->getMockBuilder(\Magento\Backup\Model\BackupFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->backupModelMock = $this->getMockBuilder(\Magento\Backup\Model\Backup::class)
- ->disableOriginalConstructor()
- ->setMethods(['getTime', 'exists', 'getSize', 'output'])
- ->getMock();
- $this->dataHelperMock = $this->getMockBuilder(\Magento\Backup\Helper\Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->fileFactoryMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultRawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->resultRedirectFactoryMock = $this->getMockBuilder(
- \Magento\Backend\Model\View\Result\RedirectFactory::class
- )->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->resultRawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->objectManager = new ObjectManager($this);
- $this->context = $this->objectManager->getObject(
- \Magento\Backend\App\Action\Context::class,
- [
- 'objectManager' => $this->objectManagerMock,
- 'request' => $this->requestMock,
- 'response' => $this->responseMock,
- 'resultRedirectFactory' => $this->resultRedirectFactoryMock
- ]
- );
- $this->downloadController = $this->objectManager->getObject(
- \Magento\Backup\Controller\Adminhtml\Index\Download::class,
- [
- 'context' => $this->context,
- 'backupModelFactory' => $this->backupModelFactoryMock,
- 'fileFactory' => $this->fileFactoryMock,
- 'resultRawFactory' => $this->resultRawFactoryMock,
- ]
- );
- }
- /**
- * @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
- */
- public function testExecuteBackupFound()
- {
- $time = 1;
- $type = 'db';
- $filename = 'filename';
- $size = 10;
- $output = 'test';
- $this->backupModelMock->expects($this->atLeastOnce())
- ->method('getTime')
- ->willReturn($time);
- $this->backupModelMock->expects($this->atLeastOnce())
- ->method('exists')
- ->willReturn(true);
- $this->backupModelMock->expects($this->atLeastOnce())
- ->method('getSize')
- ->willReturn($size);
- $this->backupModelMock->expects($this->atLeastOnce())
- ->method('output')
- ->willReturn($output);
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap(
- [
- ['time', null, $time],
- ['type', null, $type]
- ]
- );
- $this->backupModelFactoryMock->expects($this->once())
- ->method('create')
- ->with($time, $type)
- ->willReturn($this->backupModelMock);
- $this->dataHelperMock->expects($this->once())
- ->method('generateBackupDownloadName')
- ->with($this->backupModelMock)
- ->willReturn($filename);
- $this->objectManagerMock->expects($this->once())
- ->method('get')
- ->with(\Magento\Backup\Helper\Data::class)
- ->willReturn($this->dataHelperMock);
- $this->fileFactoryMock->expects($this->once())
- ->method('create')->with(
- $filename,
- null,
- DirectoryList::VAR_DIR,
- 'application/octet-stream',
- $size
- )
- ->willReturn($this->responseMock);
- $this->resultRawMock->expects($this->once())
- ->method('setContents')
- ->with($output);
- $this->resultRawFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->resultRawMock);
- $this->assertSame($this->resultRawMock, $this->downloadController->execute());
- }
- /**
- * @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
- * @param int $time
- * @param bool $exists
- * @param int $existsCount
- * @dataProvider executeBackupNotFoundDataProvider
- */
- public function testExecuteBackupNotFound($time, $exists, $existsCount)
- {
- $type = 'db';
- $this->backupModelMock->expects($this->atLeastOnce())
- ->method('getTime')
- ->willReturn($time);
- $this->backupModelMock->expects($this->exactly($existsCount))
- ->method('exists')
- ->willReturn($exists);
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap(
- [
- ['time', null, $time],
- ['type', null, $type]
- ]
- );
- $this->backupModelFactoryMock->expects($this->once())
- ->method('create')
- ->with($time, $type)
- ->willReturn($this->backupModelMock);
- $this->resultRedirectMock->expects($this->once())
- ->method('setPath')
- ->with('backup/*');
- $this->resultRedirectFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->resultRedirectMock);
- $this->assertSame($this->resultRedirectMock, $this->downloadController->execute());
- }
- /**
- * @return array
- */
- public function executeBackupNotFoundDataProvider()
- {
- return [
- [1, false, 1],
- [0, true, 0],
- [0, false, 0]
- ];
- }
- }
|