DataWizard/tests/unit/Application/Model/ExportRenderer/CsvTest.php

175 lines
4.8 KiB
PHP
Raw Normal View History

2021-11-25 23:42:41 +01:00
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DataWizard\tests\unit\Application\Model\ExportRenderer;
use D3\DataWizard\Application\Model\Exceptions\RenderException;
use D3\DataWizard\Application\Model\ExportRenderer\Csv;
use League\Csv\Exception;
use League\Csv\Writer;
2021-11-26 23:16:32 +01:00
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
2021-11-25 23:42:41 +01:00
use PHPUnit\Framework\MockObject\MockObject;
2021-12-20 13:41:24 +01:00
use ReflectionException;
2021-11-25 23:42:41 +01:00
class CsvTest extends ExportRendererTest
{
/** @var Csv */
protected $_oModel;
2022-01-17 10:59:18 +01:00
public function setUp(): void
2021-11-25 23:42:41 +01:00
{
parent::setUp();
$this->_oModel = oxNew(Csv::class);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getContent
* @test
2021-12-20 13:41:24 +01:00
* @throws ReflectionException
2021-11-25 23:42:41 +01:00
* @dataProvider canGetContentDataProvider
*/
public function canGetContent($blThrowException)
{
$expected = 'expectedReturn';
$fieldList = ['field1', 'field2'];
$valueList = ['value1', 'value2'];
/** @var Writer|MockObject $csvMock */
2022-01-17 10:59:18 +01:00
$csvMockBuilder = $this->getMockBuilder(Writer::class);
2021-11-25 23:42:41 +01:00
$csvMockBuilder->disableOriginalConstructor();
2021-12-20 13:41:24 +01:00
$onlyMethods = ['__toString', 'insertOne', 'insertAll'];
2021-11-25 23:42:41 +01:00
$csvMockBuilder->onlyMethods($onlyMethods);
$csvMock = $csvMockBuilder->getMock();
2021-12-20 13:41:24 +01:00
$csvMock->method('insertOne')->willReturn(1);
$csvMock->method('insertAll')->willReturn(1);
2021-11-25 23:42:41 +01:00
if ($blThrowException) {
2021-12-20 13:41:24 +01:00
$csvMock->expects($this->atLeastOnce())->method('__toString')->willThrowException(oxNew(Exception::class));
2021-11-25 23:42:41 +01:00
$this->expectException(RenderException::class);
} else {
2021-12-20 13:41:24 +01:00
$csvMock->expects($this->atLeastOnce())->method('__toString')->willReturn($expected);
2021-11-25 23:42:41 +01:00
}
/** @var Csv|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Csv::class)
->onlyMethods(['getCsv'])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getCsv')->willReturn($csvMock);
$this->_oModel = $modelMock;
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'getContent',
[$valueList, $fieldList]
)
);
}
/**
* @return array
*/
public function canGetContentDataProvider(): array
{
return [
'exception' => [true],
2022-01-17 10:59:18 +01:00
'no exception' => [false],
2021-11-25 23:42:41 +01:00
];
}
2021-11-26 23:16:32 +01:00
2021-11-25 23:42:41 +01:00
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv
* @test
2021-12-20 13:41:24 +01:00
* @throws ReflectionException
2021-11-25 23:42:41 +01:00
*/
public function canGetCsv()
{
$this->assertInstanceOf(
Writer::class,
$this->callMethod(
$this->_oModel,
'getCsv'
)
);
}
2021-11-26 23:16:32 +01:00
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv
* @test
2021-12-20 13:41:24 +01:00
* @throws ReflectionException
2021-11-26 23:16:32 +01:00
*/
public function canGetCsvNoSettings()
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback(
function ($argName) {
2024-05-10 14:22:32 +02:00
return match ( $argName ) {
'sGiCsvFieldEncloser', 'sCSVSign' => null,
default => Registry::getConfig()->getConfigParam( $argName ),
};
2021-11-26 23:16:32 +01:00
}
);
$modelMock = $this->getMockBuilder(Csv::class)
->onlyMethods(['d3GetConfig'])
->getMock();
$modelMock->method('d3GetConfig')->willReturn($configMock);
$this->_oModel = $modelMock;
$csv = $this->callMethod(
$this->_oModel,
'getCsv'
);
$this->assertInstanceOf(
Writer::class,
$csv
);
$this->assertSame(
'"',
$csv->getEnclosure()
);
$this->assertSame(
';',
$csv->getDelimiter()
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::d3GetConfig
* @test
2021-12-20 13:41:24 +01:00
* @throws ReflectionException
2021-11-26 23:16:32 +01:00
*/
public function canGetConfig()
{
$this->assertInstanceOf(
Config::class,
$this->callMethod(
$this->_oModel,
'd3GetConfig'
)
);
}
2022-01-17 10:59:18 +01:00
}