Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\ResultFactory;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;

class ExportCouponsCsv extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
/**
* Export Coupons to csv file
*
* Class \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsCsv
*/
class ExportCouponsCsv extends Quote implements HttpGetActionInterface
{
/**
* Export coupon codes as CSV file
*
* @return \Magento\Framework\App\ResponseInterface|null
* @return ResponseInterface|null
*/
public function execute()
{
$this->_initRule();
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
if ($rule->getId()) {
$fileName = 'coupon_codes.csv';
$content = $this->_view->getLayout()->createBlock(
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
)->getCsvFile();
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
} else {
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
return;
}
$fileName = 'coupon_codes.csv';
/** @var Layout $resultLayout */
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getCsvFile();
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\ResultFactory;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;

class ExportCouponsXml extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
/**
* Export coupons to xml file
*
* Class \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsXml
*/
class ExportCouponsXml extends Quote implements HttpGetActionInterface
{
/**
* Export coupon codes as excel xml file
*
* @return \Magento\Framework\App\ResponseInterface|null
* @return ResponseInterface|null
*/
public function execute()
{
$this->_initRule();
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
if ($rule->getId()) {
$fileName = 'coupon_codes.xml';
$content = $this->_view->getLayout()->createBlock(
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
)->getExcelFile(
$fileName
);
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
} else {
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
return;
}
$fileName = 'coupon_codes.xml';
/** @var Layout $resultLayout */
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getExcelFile($fileName);
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsCsv;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use PHPUnit\Framework\TestCase;

class ExportCouponsCsvTest extends TestCase
{
/**
* @var ExportCouponsCsv
*/
private $controller;

/**
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileFactoryMock;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultFactoryMock;

/**
* Setup environment
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->fileFactoryMock = $this->createMock(FileFactory::class);
$this->resultFactoryMock = $this->createMock(ResultFactory::class);

$this->controller = $this->objectManagerHelper->getObject(
ExportCouponsCsv::class,
[
'fileFactory' => $this->fileFactoryMock,
'resultFactory' => $this->resultFactoryMock
]
);
}

/**
* Test execute function
*/
public function testExecute()
{
$fileName = 'coupon_codes.csv';

$resultLayoutMock = $this->createMock(Layout::class);
$layoutMock = $this->createMock(LayoutInterface::class);
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getCsvFile']);
$this->resultFactoryMock
->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
->willReturn($contentMock);
$contentMock->expects($this->once())->method('getCsvFile')->willReturn('csvFile');
$this->fileFactoryMock
->expects($this->once())
->method('create')
->with($fileName, 'csvFile', DirectoryList::VAR_DIR);

$this->controller->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsXml;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use PHPUnit\Framework\TestCase;

class ExportCouponsXmlTest extends TestCase
{
/**
* @var ExportCouponsXml
*/
private $controller;

/**
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileFactoryMock;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultFactoryMock;

/**
* Setup environment
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->fileFactoryMock = $this->createMock(FileFactory::class);
$this->resultFactoryMock = $this->createMock(ResultFactory::class);

$this->controller = $this->objectManagerHelper->getObject(
ExportCouponsXml::class,
[
'fileFactory' => $this->fileFactoryMock,
'resultFactory' => $this->resultFactoryMock
]
);
}

/**
* Test execute function
*/
public function testExecute()
{
$fileName = 'coupon_codes.xml';

$resultLayoutMock = $this->createMock(Layout::class);
$layoutMock = $this->createMock(LayoutInterface::class);
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getExcelFile']);
$this->resultFactoryMock
->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
->willReturn($contentMock);
$contentMock->expects($this->once())->method('getExcelFile')
->with($fileName)
->willReturn('xmlFile');
$this->fileFactoryMock
->expects($this->once())
->method('create')
->with($fileName, 'xmlFile', DirectoryList::VAR_DIR);

$this->controller->execute();
}
}