21 Commits

Author SHA1 Message Date
175bcdd956 adjust version informations 2021-12-21 13:56:16 +01:00
532e5f6960 extract content rendering into separate method 2021-12-21 13:52:47 +01:00
744efd5d59 adjust version informations 2021-12-20 13:53:10 +01:00
675b18aaa8 define test dependencies 2021-11-28 23:05:04 +01:00
a0788ba666 add missing tests 2021-11-28 23:02:15 +01:00
3aa64f8f63 add tests for configuration class 2021-11-28 01:04:46 +01:00
b1946c4aae improve tests for EE 2021-11-27 00:59:02 +01:00
61827e7825 add tests for renderers 2021-11-25 23:42:41 +01:00
5fda1baecb add tests for admin controller classes 2021-11-24 23:38:28 +01:00
f5d35021f5 fix composer.json 2021-11-11 11:15:23 +01:00
e134a4c14a adjust version informations 2021-11-11 09:35:35 +01:00
b5d6936074 change deprecated CSV export code 2021-11-11 09:34:30 +01:00
ac7ae90310 update READMEs 2021-11-10 15:29:30 +01:00
067326e704 add data wizard link suggestion 2021-11-10 14:09:19 +01:00
ae09cf88a5 enable controller based exception handling 2021-11-10 13:50:22 +01:00
3b47f4da05 add extendable tpl blocks for submit elements on tasks 2021-11-10 13:50:21 +01:00
903d178124 add tpl block for easy extension 2021-11-10 13:50:20 +01:00
bd90f18a87 force get translations from admin 2021-11-10 13:50:20 +01:00
74edceafc8 add JSON export renderer 2021-11-10 13:49:55 +01:00
ea77239510 change deprecated CSV export code 2021-10-29 11:38:29 +02:00
e1502b274e show registered export renderers in admin submit without required tpl block extension 2021-10-29 11:37:51 +02:00
44 changed files with 3563 additions and 159 deletions

View File

@ -23,6 +23,7 @@ use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException;
use Doctrine\DBAL\DBALException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Exception\StandardException;
@ -61,24 +62,42 @@ class d3ActionWizard extends AdminDetailsController
public function runTask()
{
try {
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$action = $this->configuration->getActionById($id);
[ $queryString, $parameters ] = $action->getQuery();
if (Registry::getConfig()->getConfigParam('d3datawizard_debug')) {
throw oxNew(
DebugException::class,
d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters)
);
}
$action->run();
$this->execute();
} catch (DataWizardException|DBALException|DatabaseErrorException $e) {
Registry::getLogger()->error($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
/**
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
protected function execute()
{
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$action = $this->configuration->getActionById($id);
[ $queryString, $parameters ] = $action->getQuery();
if ($this->d3GetConfig()->getConfigParam('d3datawizard_debug')) {
throw oxNew(
DebugException::class,
d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters)
);
}
$action->run();
}
/**
* @return Config
*/
public function d3GetConfig()
{
return Registry::getConfig();
}
public function getUserMessages()
{
return null;

View File

@ -18,11 +18,14 @@ namespace D3\DataWizard\Application\Controller\Admin;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\ModCfg\Application\Model\d3database;
use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException;
use Doctrine\DBAL\DBALException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Exception\StandardException;
@ -61,24 +64,48 @@ class d3ExportWizard extends AdminDetailsController
public function runTask()
{
try {
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$export = $this->configuration->getExportById($id);
[ $queryString, $parameters ] = $export->getQuery();
if (Registry::getConfig()->getConfigParam('d3datawizard_debug')) {
throw oxNew(
DebugException::class,
d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters)
);
}
$export->run(Registry::getRequest()->getRequestEscapedParameter('format'));
$this->execute();
} catch (DataWizardException|DBALException|DatabaseErrorException $e) {
Registry::getLogger()->error($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
/**
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws StandardException
* @throws NoSuitableRendererException
* @throws TaskException
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
*/
protected function execute()
{
$id = Registry::getRequest()->getRequestEscapedParameter('taskid');
$export = $this->configuration->getExportById($id);
[ $queryString, $parameters ] = $export->getQuery();
if ($this->d3GetConfig()->getConfigParam('d3datawizard_debug')) {
throw oxNew(
DebugException::class,
d3database::getInstance()->getPreparedStatementQuery($queryString, $parameters)
);
}
$export->run(Registry::getRequest()->getRequestEscapedParameter('format'));
}
/**
* @return Config
*/
public function d3GetConfig()
{
return Registry::getConfig();
}
public function getUserMessages()
{
return null;

View File

@ -19,6 +19,7 @@ use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use FormManager\Inputs\Checkbox;
use FormManager\Inputs\Input;
use FormManager\Inputs\Radio;
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
@ -29,6 +30,7 @@ abstract class ActionBase implements QueryBase
protected $formElements = [];
/**
* Ensure that the translations are equally available in the frontend and the backend
* @return string
*/
public function getDescription() : string
@ -75,7 +77,7 @@ abstract class ActionBase implements QueryBase
);
}
$affected = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->execute( $queryString, $parameters );
$affected = $this->d3GetDb()->execute( $queryString, $parameters );
throw oxNew(
Exceptions\TaskException::class,
@ -89,6 +91,15 @@ abstract class ActionBase implements QueryBase
);
}
/**
* @return DatabaseInterface|null
* @throws DatabaseConnectionException
*/
public function d3GetDb()
{
return DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC );
}
/**
* @return string
*/
@ -102,15 +113,12 @@ abstract class ActionBase implements QueryBase
*/
public function registerFormElement(Input $input)
{
switch (get_class($input)) {
case Radio::class:
case Checkbox::class:
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
$input->setAttribute('class', 'form-check-input');
break;
default:
$input->setTemplate('<p class="formElements">{{ label }} {{ input }}</p>');
$input->setAttribute('class', 'form-control');
if ($input instanceof Radio || $input instanceof Checkbox) {
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
$input->setAttribute('class', 'form-check-input');
} else {
$input->setTemplate('<p class="formElements">{{ label }} {{ input }}</p>');
$input->setAttribute('class', 'form-control');
}
$this->formElements[] = $input;
}

View File

@ -149,7 +149,13 @@ class Configuration
*/
public function getActionById($id) : ActionBase
{
return $this->getAllActions()[$id];
$allActions = $this->getAllActions();
if (false == $allActions[$id]) {
throw oxNew(DataWizardException::class, 'no action with id '.$id);
}
return $allActions[$id];
}
/**

View File

@ -36,6 +36,7 @@ abstract class ExportBase implements QueryBase
protected $formElements = [];
/**
* Ensure that the translations are equally available in the frontend and the backend
* @return string
*/
public function getDescription() : string
@ -68,23 +69,7 @@ abstract class ExportBase implements QueryBase
}
}
[ $rows, $fieldNames ] = $this->getExportData( $this->getQuery() );
$content = $this->renderContent($rows, $fieldNames, $format);
/** @var $oFS d3filesystem */
$oFS = oxNew( d3filesystem::class );
if (is_null($path)) {
$oFS->startDirectDownload( $oFS->filterFilename( $this->getExportFileName( $format ) ), $content );
} else {
$filePath = $oFS->trailingslashit($path).$oFS->filterFilename( $this->getExportFileName( $format ) );
if (false === $oFS->createFile($filePath, $content,true)) {
throw oxNew(ExportFileException::class, $filePath);
}
return $filePath;
}
return '';
return $this->executeExport($format, $path);
}
/**
@ -103,7 +88,15 @@ abstract class ExportBase implements QueryBase
*/
public function getRenderer($format): ExportRenderer\RendererInterface
{
return oxNew(RendererBridge::class)->getRenderer($format);
return $this->getRendererBridge()->getRenderer($format);
}
/**
* @return RendererBridge
*/
public function getRendererBridge()
{
return oxNew(RendererBridge::class);
}
/**
@ -171,13 +164,13 @@ abstract class ExportBase implements QueryBase
);
}
$rows = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC )->getAll( $queryString, $parameters );
$rows = $this->d3GetDb()->getAll( $queryString, $parameters );
if ( count( $rows ) <= 0 ) {
throw oxNew(
Exceptions\TaskException::class,
$this,
Registry::getLang()->translateString( 'D3_DATAWIZARD_ERR_NOEXPORTCONTENT' )
Registry::getLang()->translateString( 'D3_DATAWIZARD_ERR_NOEXPORTCONTENT', null, true )
);
}
@ -191,15 +184,12 @@ abstract class ExportBase implements QueryBase
*/
public function registerFormElement(Input $input)
{
switch (get_class($input)) {
case Radio::class:
case Checkbox::class:
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
$input->setAttribute('class', 'form-check-input');
break;
default:
$input->setTemplate('<p class="formElements">{{ label }} {{ input }}</p>');
$input->setAttribute('class', 'form-control');
if ($input instanceof Radio || $input instanceof Checkbox) {
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
$input->setAttribute('class', 'form-check-input');
} else {
$input->setTemplate('<p class="formElements">{{ label }} {{ input }}</p>');
$input->setAttribute('class', 'form-control');
}
$this->formElements[] = $input;
}
@ -219,4 +209,69 @@ abstract class ExportBase implements QueryBase
{
return $this->formElements;
}
/**
* @param string $format
* @param $path
* @return string
* @throws DBALException
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exceptions\NoSuitableRendererException
* @throws StandardException
* @throws d3ShopCompatibilityAdapterException
* @throws d3_cfg_mod_exception
*/
protected function executeExport(string $format, $path): string
{
$content = $this->getContent( $format );
/** @var $oFS d3filesystem */
$oFS = $this->getFileSystem();
if (is_null($path)) {
$oFS->startDirectDownload($oFS->filterFilename($this->getExportFileName($format)), $content);
} else {
$filePath = $oFS->trailingslashit($path) . $oFS->filterFilename($this->getExportFileName($format));
if (false === $oFS->createFile($filePath, $content, true)) {
throw oxNew(ExportFileException::class, $filePath);
}
return $filePath;
}
return '';
}
/**
* @return \OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface|null
* @throws DatabaseConnectionException
*/
protected function d3GetDb(): ?\OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface
{
return DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC);
}
/**
* @return d3filesystem|mixed
*/
protected function getFileSystem()
{
return oxNew(d3filesystem::class);
}
/**
* @param string $format
*
* @return string
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exceptions\NoSuitableRendererException
*/
public function getContent( string $format ): string
{
[ $rows, $fieldNames ] = $this->getExportData( $this->getQuery() );
$content = $this->renderContent( $rows, $fieldNames, $format );
return $content;
}
}

View File

@ -19,6 +19,7 @@ use D3\DataWizard\Application\Model\Exceptions\RenderException;
use League\Csv\EncloseField;
use League\Csv\Exception;
use League\Csv\Writer;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
class Csv implements RendererInterface
@ -36,13 +37,12 @@ class Csv implements RendererInterface
$csv = $this->getCsv();
$csv->insertOne( $fieldNames );
$csv->insertAll( $rows );
return method_exists($csv, 'getContent') ? $csv->getContent() : (string) $csv;
} catch (Exception $e) {
/** @var RenderException $newException */
$newException = oxNew(RenderException::class, $e->getMessage(), $e->getCode(), $e );
throw $newException;
}
return $csv->getContent();
}
public function getFileExtension(): string
@ -60,13 +60,13 @@ class Csv implements RendererInterface
EncloseField::addTo($csv, "\t\x1f");
$sEncloser = Registry::getConfig()->getConfigParam('sGiCsvFieldEncloser');
$sEncloser = $this->d3GetConfig()->getConfigParam('sGiCsvFieldEncloser');
if (false == $sEncloser) {
$sEncloser = '"';
}
$csv->setEnclosure($sEncloser);
$sDelimiter = Registry::getConfig()->getConfigParam('sCSVSign');
$sDelimiter = $this->d3GetConfig()->getConfigParam('sCSVSign');
if (false == $sDelimiter) {
$sDelimiter = ';';
}
@ -74,4 +74,20 @@ class Csv implements RendererInterface
return $csv;
}
/**
* @return string
*/
public function getTitleTranslationId(): string
{
return 'D3_DATAWIZARD_EXPORT_FORMAT_CSV';
}
/**
* @return Config
*/
public function d3GetConfig()
{
return Registry::getConfig();
}
}

View File

@ -0,0 +1,51 @@
<?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\Application\Model\ExportRenderer;
use D3\DataWizard\Application\Model\Exceptions\RenderException;
class Json implements RendererInterface
{
/**
* @param $rows
* @param $fieldNames
*
* @return string
* @throws RenderException
*/
public function getContent($rows, $fieldNames): string
{
$flags = JSON_PRETTY_PRINT;
$json = json_encode( $rows, $flags );
if ( $json === false ) {
throw oxNew( RenderException::class, json_last_error_msg(), json_last_error());
}
return $json;
}
public function getFileExtension(): string
{
return 'json';
}
/**
* @return string
*/
public function getTitleTranslationId(): string
{
return "D3_DATAWIZARD_EXPORT_FORMAT_JSON";
}
}

View File

@ -27,10 +27,19 @@ class Pretty implements RendererInterface
*/
public function getContent($rows, $fieldNames) : string
{
$renderer = oxNew(ArrayToTextTable::class, $rows);
$renderer = $this->getArrayToTextTableInstance($rows);
return $renderer->getTable();
}
/**
* @param $rows
* @return ArrayToTextTable
*/
public function getArrayToTextTableInstance($rows)
{
return oxNew(ArrayToTextTable::class, $rows);
}
/**
* @return string
*/
@ -38,4 +47,12 @@ class Pretty implements RendererInterface
{
return 'txt';
}
/**
* @return string
*/
public function getTitleTranslationId(): string
{
return "D3_DATAWIZARD_EXPORT_FORMAT_PRETTY";
}
}

View File

@ -21,6 +21,7 @@ class RendererBridge
{
const FORMAT_CSV = 'CSV';
const FORMAT_PRETTY = 'Pretty';
const FORMAT_JSON = 'JSON';
/**
* @return array
@ -29,10 +30,26 @@ class RendererBridge
{
return [
self::FORMAT_CSV => oxNew(Csv::class),
self::FORMAT_PRETTY => oxNew(Pretty::class)
self::FORMAT_PRETTY => oxNew(Pretty::class),
self::FORMAT_JSON => oxNew(Json::class)
];
}
public function getTranslatedRendererIdList()
{
$rendererList = $this->getRendererList();
array_walk($rendererList, [$this, 'translateRendererId']);
return $rendererList;
}
/**
* @param RendererInterface $instance
*/
protected function translateRendererId(RendererInterface &$instance)
{
$instance = $instance->getTitleTranslationId();
}
/**
* @param string $format
*

View File

@ -29,4 +29,9 @@ interface RendererInterface
* @return string
*/
public function getFileExtension() : string;
/**
* @return string
*/
public function getTitleTranslationId() : string;
}

View File

@ -22,11 +22,13 @@ interface QueryBase
public function run();
/**
* Ensure that the translations are equally available in the frontend and the backend
* @return string
*/
public function getTitle() : string;
/**
* Ensure that the translations are equally available in the frontend and the backend
* @return string
*/
public function getDescription() : string;

View File

@ -39,6 +39,7 @@ $aLang = array(
'D3_DATAWIZARD_EXPORT_SUBMIT' => 'Export starten',
'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV-Format',
'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty-Format',
'D3_DATAWIZARD_EXPORT_FORMAT_JSON' => 'JSON-Format',
'D3_DATAWIZARD_ACTION_SUBMIT' => 'Aktion starten',

View File

@ -39,6 +39,7 @@ $aLang = array(
'D3_DATAWIZARD_EXPORT_SUBMIT' => 'generate export',
'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV format',
'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty format',
'D3_DATAWIZARD_EXPORT_FORMAT_JSON' => 'JSON format',
'D3_DATAWIZARD_ACTION_SUBMIT' => 'run action',

View File

@ -101,7 +101,9 @@
[{/foreach}]
[{/if}]
[{include file=$submit}]
[{block name="exportSubmit"}]
[{include file=$submit}]
[{/block}]
</div>
</div>
</div>

View File

@ -1,6 +1,8 @@
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="startTask('[{$id}]')">
<i class="fas fa-magic"></i>
[{oxmultilang ident=$item->getButtonText()}]
</button>
</div>
[{block name="submitElements"}]
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="startTask('[{$id}]')">
<i class="fas fa-fw fa-magic"></i>
[{oxmultilang ident=$item->getButtonText()}]
</button>
</div>
[{/block}]

View File

@ -1,22 +1,24 @@
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="startTask('[{$id}]', 'CSV')">
<i class="fas fa-magic"></i>
[{oxmultilang ident=$item->getButtonText()}]
</button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">
<i class="fas fa-magic"></i>
[{block name="submitElements"}]
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="startTask('[{$id}]', 'CSV')">
<i class="fas fa-fw fa-magic"></i>
[{oxmultilang ident=$item->getButtonText()}]
</span>
</button>
<div class="dropdown-menu">
[{block name="dataWizardFormat"}]
<button class="dropdown-item" onclick="startTask('[{$id}]', 'CSV')">
[{oxmultilang ident="D3_DATAWIZARD_EXPORT_FORMAT_CSV"}]
</button>
<button class="dropdown-item" onclick="startTask('[{$id}]', 'Pretty')">
[{oxmultilang ident="D3_DATAWIZARD_EXPORT_FORMAT_PRETTY"}]
</button>
[{/block}]
</button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">
<i class="fas fa-fw fa-magic"></i>
[{oxmultilang ident=$item->getButtonText()}]
</span>
</button>
<div class="dropdown-menu">
[{block name="dataWizardFormat"}]
[{assign var="rendererBridge" value=$item->getRendererBridge()}]
[{foreach from=$rendererBridge->getTranslatedRendererIdList() key="key" item="translationId"}]
<button class="dropdown-item" onclick="startTask('[{$id}]', '[{$key}]')">
[{oxmultilang ident=$translationId}]
</button>
[{/foreach}]
[{/block}]
</div>
</div>
</div>
[{/block}]

View File

@ -1,5 +1,36 @@
# Changelog
## 1.5.0.0 (2021-12-21)
#### Added
- add tests
#### Changed
- adjust to CLI extension which can export to STDOUT
- restrict dependencies to OXID 6.2, use 2.x for newer installations
---
## 1.4.0.0 (2021-11-11)
#### Added
- add JSON export renderer
- add tpl block for easier extension
- enable controller based exception handling
#### Changed
- change deprecated CSV export code
- show registered export renderers in admin submit without required tpl block extension
- force get translations from admin
---
## 1.3.1.0 (2021-09-10)
- installable in OXID 6.3.1
---
## 1.3.0.0 (2021-07-29)
- can handle long task description text

View File

@ -7,7 +7,7 @@ The module `DataWizard` offers a framework for the simple integration of exports
The exports are defined via database queries or ready-made data lists. Various export formats are available. The generation is possible at any time and always recurring (within the system limits). These are offered as downloads in the browser.
All exports are grouped together for better clarity.
All exports and tasks are grouped together for better clarity.
Sample exports are included in the package d3/datawizardtasks. These are intended to serve as an implementation reference for individual exports.
@ -18,7 +18,7 @@ Sample exports are included in the package d3/datawizardtasks. These are intende
In the console in the shop root (above source and vendor), execute the following command:
```bash
php composer require d3/datawizard
php composer require d3/datawizard:^1.0
```
Activate the module in the admin area of the shop in "Extensions -> Modules".
@ -27,38 +27,17 @@ Activate the module in the admin area of the shop in "Extensions -> Modules".
The module represents the technical basic framework of the exports and does not claim to be complete. In order to adapt the scope to individual requirements, the following extensions are prepared:
- Add exports
- Add exports or tasks
- Use existing and new groups
- Add export formats
Independently of this, all extension options are available that the OXID Shop provides for modules.
### Add exports
## Extension packages
#### Define export
Each export is defined in a separate class. This export class must extend the class `D3\DataWizard\Application\Model\ExportBase`. All necessary functions are predefined in it. The following methods are available:
##### mandatory method calls:
- getTitle() - defines the title in the admin area and the base of the later export file name.
- getQuery() - contains the query as a prepared statement that defines the data to be exported
##### optional method calls:
- getDescription() - contains a short additional description of the export, this will be shown in the admin area
- getButtonText() - defines the text of the submit button in the admin area
- getExportFilenameBase() - defines the base of the later export filename
- executeQuery() - returns the compiled export data
- further methods, the adaptation of which, however, can lead to changed module behaviour and should therefore only be changed with caution
#### Register exports
In order to be able to use the created export in the module, it must be registered. For this purpose there is the class `D3\DataWizard\Application\Model\Configuration`. This is to be overloaded with the possibilities of the OXID shop and the method `configure()` contained therein is to be supplemented. The following call is available for registering an export:
```
$this->registerExport(self::GROUP_CATEGORY, oxNew(myCustomExport::class));
```
The first parameter contains the language identifier for the export group. Typical identifiers are already prepared in constants of the configuration class. The instance of the export class is expected as the 2nd parameter.
- `d3/datawizardtasks` - provides sample exports and their implementation reference
- `d3/datawizardcli` - provides the execution of exports or tasks via the command prompt (e.g. as cronjobs)
- `d3/datawizardlink` - provides URLs to generate exports from third party systems
## Changelog

View File

@ -3,11 +3,11 @@
# DÂł Data Wizard fĂĽr OXID eShop
Das Modul `DataWizard` bietet ein Framework zur einfachen Integration von Exporten ĂĽber den Adminbereich des OXID Shops.
Das Modul `DataWizard` bietet ein Framework zur einfachen Integration von Exporten und ausfĂĽhrbaren Tasks ĂĽber den Adminbereich des OXID Shops.
Die Exporte werden über Datenbankabfragen oder fertige Datenlisten definiert. Es stehen verschiedene Exportformate zur Verfügung. Die Generierung ist jederzeit und immer wiederkehrend möglich (im Rahmen der Systemgrenzen). Diese werden im Browser als Download angeboten.
Alle Exporte sind fĂĽr eine bessere Ăśbersichtlichkeit in Gruppen zusammengefasst.
Alle Exporte bzw. Tasks sind fĂĽr eine bessere Ăśbersichtlichkeit in Gruppen zusammengefasst.
Im Paket d3/datawizardtasks sind Beispielexporte enthalten. Diese sollen als Implementierungsreferenz fĂĽr individuelle Exporte dienen.
@ -18,7 +18,7 @@ Im Paket d3/datawizardtasks sind Beispielexporte enthalten. Diese sollen als Imp
Auf der Konsole im Shoproot (oberhalb von source und vendor) folgenden Befehl ausfĂĽhren:
```bash
php composer require d3/datawizard
php composer require d3/datawizard:^1.0
```
Aktivieren Sie das Modul im Shopadmin unter "Erweiterungen -> Module".
@ -27,38 +27,17 @@ Aktivieren Sie das Modul im Shopadmin unter "Erweiterungen -> Module".
Das Modul stellt das technische Grundgerüst der Exporte dar und erhebt keinen Anspruch auf Vollständigkeit. Um den Umfang an die individuellen Anforderungen anzupassen, sind folgende Erweiterungen vorbereitet:
- Exporte hinzufĂĽgen
- Exporte oder Tasks hinzufĂĽgen
- Verwendung bestehender und neuer Gruppen
- Exportformate ergänzen
Unabhängig dessen stehen alle Erweiterungsmöglichkeiten zur Verfügung, die der OXID Shop für Module bereitstellt.
### Exporte hinzufĂĽgen
## Erweiterungspakete
#### Export definieren
Jeder Export wird in einer separaten Klasse definiert. Diese Exportklasse muss die Klasse `D3\DataWizard\Application\Model\ExportBase` erweitern. Darin sind alle nötigen Funktionen vordefiniert. Folgende Methoden stehen hierbei zur Verfügung:
##### verpflichtende Methodenaufrufe:
- getTitle() - definiert den Titel im Adminbereich und die Basis des späteren Exportdateinamens
- getQuery() - enthält den Abfragequery als prepared statement, der die zu exportierenden Daten definiert
##### optionale Methodenaufrufe:
- getDescription() - enthält eine kurze zusätzliche Beschreibung des Exports, dieser wird im Adminbereich gezeigt
- getButtonText() - definiert den Text des Absenden-Buttons im Adminbereich
- getExportFilenameBase() - definiert die Basis des späteren Exportdateinamens
- executeQuery() - liefert die zusammengestellten Exportdaten
- weitere Methoden, deren Anpassung jedoch zum geänderten Modulverhalten führen können und daher nur mit Vorsicht zu verändern sind
#### Exporte registrieren
Um den erstellten Export auch im Modul verwenden zu können, muss dieser noch registriert werden. Hierzu gibt des die Klasse `D3\DataWizard\Application\Model\Configuration`. Diese ist mit den Möglichkeiten des OXID Shops zu überladen und die darin enthaltene Methode `configure()` zu ergänzen. Für die Registrierung eines Exportes steht Ihnen dazu folgender Aufruf zur Verfügung:
```
$this->registerExport(self::GROUP_CATEGORY, oxNew(myCustomExport::class));
```
Der erste Parameter enthält den Sprachident für die Exportgruppe. In Konstanten der configuration-Klasse sind schon typische Idents vorbereitet. Als 2. Parameter wird die Instanz der Exportklasse erwartet.
- `d3/datawizardtasks` - liefert Beispielexporte und deren Implementierungsreferenz
- `d3/datawizardcli` - ermöglicht die Ausführung der Exporte oder Tasks über die Eingabeaufforderung (z.B. als Cronjobs)
- `d3/datawizardlink` - stellt URLs zur VerfĂĽgung, um Exporte von Drittsystemen generieren zu lassen
## Changelog

View File

@ -26,11 +26,14 @@
],
"require": {
"php": ">=7.1",
"oxid-esales/oxideshop-ce": "6.3 - 6.9",
"oxid-esales/oxideshop-ce": "6.3 - 6.7",
"league/csv": "^9.0",
"mathieuviossat/arraytotexttable": "^1.0",
"form-manager/form-manager": "^5.1 || ^6.1"
},
"require-dev": {
"oxid-esales/oxideshop-ce": "6.8 - 6.9"
},
"extra": {
"oxideshop": {
"blacklist-filter": [
@ -42,7 +45,8 @@
},
"suggest": {
"d3/datawizardtasks": "useful example tasks for Data Wizard",
"d3/datawizardcli": "command line implementation for fully automated Data Wizard tasks"
"d3/datawizardcli": "command line implementation for fully automated Data Wizard tasks",
"d3/datawizardlink": "remote http link for Data Wizard module exports"
},
"autoload": {
"psr-4": {

View File

@ -32,7 +32,7 @@ $aModule = [
'en' => '',
],
'thumbnail' => '',
'version' => '1.3.0.0',
'version' => '1.5.0.0',
'author' => 'D&sup3; Data Development (Inh.: Thomas Dartsch)',
'email' => 'support@shopmodule.com',
'url' => 'https://www.oxidmodule.com/',

2
tests/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
reports
.phpunit.result.cache

52
tests/README.md Normal file
View File

@ -0,0 +1,52 @@
# D3 Datawizard Tests
## Requirements
Both unit and acceptance tests require OXID Testing Library installed.
See https://github.com/OXID-eSales/testing_library.
### Configuration
Please install the packages listed in the composer.json in "require-dev". Unfortunately Composer does not provide an automatic installation.
Here is an example of Testing Library configuration file `oxideshop/test_config.yml`
```
# This file is auto-generated during the composer install
mandatory_parameters:
shop_path: /var/www/oxideshop/source
shop_tests_path: /var/www/oxideshop/tests
partial_module_paths: d3/datawizard
optional_parameters:
shop_url: null
shop_serial: ''
enable_varnish: false
is_subshop: false
install_shop: false
remote_server_dir: null
shop_setup_path: null
restore_shop_after_tests_suite: false
test_database_name: null
restore_after_acceptance_tests: false
restore_after_unit_tests: false
tmp_path: /tmp/oxid_test_library/
database_restoration_class: DatabaseRestorer
activate_all_modules: false
run_tests_for_shop: false
run_tests_for_modules: true
screen_shots_path: null
screen_shots_url: null
browser_name: firefox
selenium_server_ip: 127.0.0.1
selenium_server_port: '4444'
additional_test_paths: null
```
## Unit Tests
To execute unit tests run the following:
```
cd /var/www/oxideshop/
vendor/bin/runtests
```

39
tests/additional.inc.php Executable file
View File

@ -0,0 +1,39 @@
<?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
*/
// Include datawizard test config
namespace D3\DataWizard\tests;
use D3\ModCfg\Tests\additional_abstract;
use Exception;
use OxidEsales\Eshop\Core\Exception\StandardException;
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'd3datawizard_config.php');
class additional extends additional_abstract
{
/**
* additional constructor.
* @throws StandardException
*/
public function __construct()
{
if (D3DATAWIZARD_REQUIRE_MODCFG) {
$this->reactivateModCfg();
}
}
}
try {
d3GetModCfgDIC()->get(additional::class);
} catch (Exception $e) {}

15
tests/d3datawizard_config.php Executable file
View File

@ -0,0 +1,15 @@
<?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
*/
define('D3DATAWIZARD_REQUIRE_MODCFG', true);

27
tests/phpunit.xml Normal file
View File

@ -0,0 +1,27 @@
<phpunit backupGlobals="true"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
cacheTokens="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../Application</directory>
<directory suffix=".php">../Modules</directory>
<directory suffix=".php">../public</directory>
<directory suffix=".php">../Setup</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="reports/logfile.xml"/>
</logging>
</phpunit>

View File

@ -0,0 +1,18 @@
<?php
namespace D3\DataWizard\tests\tools;
use D3\DataWizard\Application\Model\ActionBase;
class d3TestAction extends ActionBase
{
public function getTitle(): string
{
return 'TestTitle';
}
public function getQuery(): array
{
return ["UPDATE 1"];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace D3\DataWizard\tests\tools;
use D3\DataWizard\Application\Model\ExportBase;
class d3TestExport extends ExportBase
{
public function getTitle(): string
{
return 'TestTitle';
}
public function getQuery(): array
{
return "SELECT 1";
}
}

View File

@ -0,0 +1,193 @@
<?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\Controller\Admin;
use D3\DataWizard\Application\Controller\Admin\d3ActionWizard;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\tests\tools\d3TestAction;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class d3ActionWizardTest extends d3AdminControllerTest
{
/** @var d3ActionWizard */
protected $_oController;
protected $testClassName = d3ActionWizard::class;
public function setUp() : void
{
parent::setUp();
$this->_oController = oxNew($this->testClassName);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getGroups()
* @test
* @throws ReflectionException
*/
public function canGetGroups()
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getActionGroups'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getActionGroups')->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroups'
)
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getGroupTasks()
* @test
* @throws ReflectionException
* @dataProvider canGetGroupTasksDataProvider
*/
public function canGetGroupTasks($argument)
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getActionsByGroup'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getActionsByGroup')->with($argument)->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroupTasks',
[$argument]
)
);
}
/**
* @return array
*/
public function canGetGroupTasksDataProvider(): array
{
return [
['test1'],
['test2']
];
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute()
* @test
* @throws ReflectionException
* @dataProvider executePassDataProvider
*/
public function executePass($blDebug)
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(get_class(Registry::getRequest()))
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']);
Registry::set(Request::class, $requestMock);
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback(
function ($argName) use ($blDebug) {
switch ($argName) {
case 'd3datawizard_debug':
return $blDebug;
default:
return Registry::getConfig()->getConfigParam($argName);
}
}
);
/** @var d3ActionWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder(d3ActionWizard::class)
->onlyMethods(['d3GetConfig'])
->getMock();
$controllerMock->method('d3GetConfig')->willReturn($configMock);
$this->_oController = $controllerMock;
/** @var d3TestAction|MockObject $actionMock */
$actionMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'getQuery',
'run'
])
->getMock();
$actionMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$actionMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn(true);
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getActionById'])
->disableOriginalConstructor()
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getActionById')->with('testTaskId')->willReturn($actionMock);
$this->setValue($this->_oController, 'configuration', $configurationMock);
if ($blDebug) {
$this->expectException(DebugException::class);
}
$this->callMethod(
$this->_oController,
'execute'
);
}
public function executePassRequestCallback($varName)
{
switch ($varName) {
case 'taskid':
return 'testTaskId';
case 'format':
return RendererBridge::FORMAT_CSV;
default:
return oxNew(Request::class)->getRequestEscapedParameter($varName);
}
}
/**
* @return array
*/
public function executePassDataProvider(): array
{
return [
'no debug' => [false],
'debug' => [true],
];
}
}

View File

@ -0,0 +1,262 @@
<?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\Controller\Admin;
use D3\DataWizard\Application\Controller\Admin\d3ActionWizard;
use D3\DataWizard\Application\Controller\Admin\d3ExportWizard;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use Doctrine\DBAL\DBALException;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\UtilsView;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use ReflectionException;
abstract class d3AdminControllerTest extends d3ModCfgUnitTestCase
{
/** @var d3ActionWizard|d3ExportWizard */
protected $_oController;
protected $testClassName;
public function tearDown() : void
{
parent::tearDown();
unset($this->_oController);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::__construct
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::__construct
* @test
* @throws ReflectionException
*/
public function testConstructor()
{
$this->setValue($this->_oController, 'configuration', null);
$this->callMethod(
$this->_oController,
'__construct'
);
$this->assertInstanceOf(
Configuration::class,
$this->getValue(
$this->_oController,
'configuration'
)
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::runTask()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::runTask()
* @test
* @throws ReflectionException
*/
public function runTaskPass()
{
/** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder($this->testClassName)
->onlyMethods(['execute'])
->getMock();
$controllerMock->expects($this->once())->method('execute')->willReturn(true);
$this->_oController = $controllerMock;
$this->callMethod(
$this->_oController,
'runTask'
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::runTask()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::runTask()
* @test
* @param $exceptionClass
* @throws ReflectionException
* @dataProvider runTaskFailedDataProvider
*/
public function runTaskFailed($exceptionClass)
{
/** @var DataWizardException|DBALException|DatabaseErrorException|MockObject $exceptionMock */
$exceptionMock = $this->getMockBuilder($exceptionClass)
->disableOriginalConstructor()
->getMock();
$this->setValue($exceptionMock, 'message', 'exc_msg');
/** @var d3ActionWizard|d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder($this->testClassName)
->onlyMethods(['execute'])
->getMock();
$controllerMock->expects($this->once())->method('execute')->willThrowException($exceptionMock);
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockBuilder(get_class(Registry::getLogger()))
->onlyMethods(['error'])
->disableOriginalConstructor()
->getMock();
$loggerMock->expects($this->atLeastOnce())->method('error')->with('exc_msg')->willReturn(true);
Registry::set('logger', $loggerMock);
/** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(get_class(Registry::getUtilsView()))
->onlyMethods(['addErrorToDisplay'])
->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay')->with($exceptionMock)->willReturn(true);
Registry::set(UtilsView::class, $utilsViewMock);
$this->_oController = $controllerMock;
$this->callMethod(
$this->_oController,
'runTask'
);
}
/**
* @return \string[][]
*/
public function runTaskFailedDataProvider(): array
{
return [
[DataWizardException::class],
[DBALException::class],
[DatabaseErrorException::class],
];
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::execute()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute()
* @test
* @throws ReflectionException
* @dataProvider executePassDataProvider
*/
public function executePass($blDebug)
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(get_class(Registry::getRequest()))
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->expects($this->atLeastOnce())->method('getRequestEscapedParameter')->with('taskid')->willReturn('testTaskId');
Registry::set(Request::class, $requestMock);
/** @var d3TestAction|MockObject $actionMock */
$actionMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'getQuery',
'run'
])
->getMock();
$actionMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$actionMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn(true);
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getActionById'])
->disableOriginalConstructor()
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getActionById')->with('testTaskId')->willReturn($actionMock);
$this->setValue($this->_oController, 'configuration', $configurationMock);
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturn($blDebug);
Registry::set(Config::class, $configMock);
if ($blDebug) {
$this->expectException(DebugException::class);
}
$this->callMethod(
$this->_oController,
'execute'
);
}
/**
* @return array
*/
public function executePassDataProvider(): array
{
return [
'no debug' => [false],
'debug' => [true],
];
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getUserMessages()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getUserMessages()
* @test
* @throws ReflectionException
*/
public function canGetUserMessages()
{
$this->assertNull(
$this->callMethod(
$this->_oController,
'getUserMessages'
)
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::getHelpURL()
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getHelpURL()
* @test
* @throws ReflectionException
*/
public function canGetHelpUrl()
{
$this->assertNull(
$this->callMethod(
$this->_oController,
'getHelpURL'
)
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::d3GetConfig
* @covers \D3\DataWizard\Application\Controller\Admin\d3ActionWizard::d3GetConfig
* @test
* @throws ReflectionException
*/
public function canGetConfig()
{
$this->assertInstanceOf(
Config::class,
$this->callMethod(
$this->_oController,
'd3GetConfig'
)
);
}
}

View File

@ -0,0 +1,195 @@
<?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\Controller\Admin;
use D3\DataWizard\Application\Controller\Admin\d3ExportWizard;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\tests\tools\d3TestAction;
use D3\DataWizard\tests\tools\d3TestExport;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class d3ExportWizardTest extends d3AdminControllerTest
{
/** @var d3ExportWizard */
protected $_oController;
protected $testClassName = d3ExportWizard::class;
public function setUp() : void
{
parent::setUp();
$this->_oController = oxNew($this->testClassName);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroups()
* @test
* @throws ReflectionException
*/
public function canGetGroups()
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportGroups'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportGroups')->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroups'
)
);
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::getGroupTasks()
* @test
* @throws ReflectionException
* @dataProvider canGetGroupTasksDataProvider
*/
public function canGetGroupTasks($argument)
{
$expected = ['expected' => 'array'];
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportsByGroup'])
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportsByGroup')->with($argument)->willReturn($expected);
$this->setValue($this->_oController, 'configuration', $configurationMock);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oController,
'getGroupTasks',
[$argument]
)
);
}
/**
* @return array
*/
public function canGetGroupTasksDataProvider(): array
{
return [
['test1'],
['test2']
];
}
/**
* @covers \D3\DataWizard\Application\Controller\Admin\d3ExportWizard::execute()
* @test
* @throws ReflectionException
* @dataProvider executePassDataProvider
*/
public function executePass($blDebug)
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(get_class(Registry::getRequest()))
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->expects($this->any())->method('getRequestEscapedParameter')->willReturnCallback([$this, 'executePassRequestCallback']);
//OnConsecutiveCalls('testTaskId', 'CSV');
Registry::set(Request::class, $requestMock);
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback(
function ($argName) use ($blDebug) {
switch ($argName) {
case 'd3datawizard_debug':
return $blDebug;
default:
return Registry::getConfig()->getConfigParam($argName);
}
}
);
/** @var d3ExportWizard|MockObject $controllerMock */
$controllerMock = $this->getMockBuilder(d3ExportWizard::class)
->onlyMethods(['d3GetConfig'])
->getMock();
$controllerMock->method('d3GetConfig')->willReturn($configMock);
$this->_oController = $controllerMock;
/** @var d3TestAction|MockObject $exportMock */
$exportMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getQuery',
'run'
])
->getMock();
$exportMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['1']]);
$exportMock->expects($this->exactly((int) !$blDebug))->method('run')->willReturn('');
/** @var Configuration|MockObject $configurationMock */
$configurationMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getExportById'])
->disableOriginalConstructor()
->getMock();
$configurationMock->expects($this->atLeastOnce())->method('getExportById')->with('testTaskId')->willReturn($exportMock);
$this->setValue($this->_oController, 'configuration', $configurationMock);
if ($blDebug) {
$this->expectException(DebugException::class);
}
$this->callMethod(
$this->_oController,
'execute'
);
}
public function executePassRequestCallback($varName)
{
switch ($varName) {
case 'taskid':
return 'testTaskId';
case 'format':
return RendererBridge::FORMAT_CSV;
default:
return oxNew(Request::class)->getRequestEscapedParameter($varName);
}
}
/**
* @return array
*/
public function executePassDataProvider(): array
{
return [
'no debug' => [false],
'debug' => [true],
];
}
}

View File

@ -0,0 +1,316 @@
<?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;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\DataWizard\tests\tools\d3TestAction;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use FormManager\Inputs\Hidden;
use FormManager\Inputs\Number;
use FormManager\Inputs\Radio;
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
use OxidEsales\Eshop\Core\Exception\StandardException;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class ActionBaseTest extends d3ModCfgUnitTestCase
{
/** @var d3TestAction */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(d3TestAction::class);
}
public function tearDown() : void
{
parent::tearDown();
unset($this->_oModel);
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::getDescription
* @test
* @throws ReflectionException
*/
public function canGetDescription()
{
$this->assertIsString(
$this->callMethod(
$this->_oModel,
'getDescription'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::getButtonText
* @test
* @throws ReflectionException
*/
public function canGetButtonText()
{
$this->assertIsString(
$this->callMethod(
$this->_oModel,
'getButtonText'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::hasFormElements
* @test
* @throws ReflectionException
* @dataProvider canGetHasFormElementsDataProvider
*/
public function canGetHasFormElements($formElements, $expected)
{
$this->setValue($this->_oModel, 'formElements', $formElements);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'hasFormElements'
)
);
}
public function canGetHasFormElementsDataProvider()
{
return [
'hasFormElements' => [['abc', 'def'], true],
'hasNoFormElements' => [[], false],
];
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::getFormElements
* @test
* @throws ReflectionException
* @dataProvider canGetHasFormElementsDataProvider
*/
public function canGetFormElements($formElements)
{
$this->setValue($this->_oModel, 'formElements', $formElements);
$this->assertSame(
$formElements,
$this->callMethod(
$this->_oModel,
'getFormElements'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::registerFormElement
* @test
* @throws ReflectionException
* @dataProvider canRegisterFormElementDataProvider
*/
public function canRegisterFormElement($inputClass)
{
$oldCount = count($this->getValue($this->_oModel, 'formElements'));
/** @var Radio|MockObject $inputMock */
$inputMock = $this->getMockBuilder($inputClass)
->onlyMethods([
'setTemplate',
'setAttribute'
])
->getMock();
$inputMock->expects($this->atLeastOnce())->method('setTemplate');
$inputMock->expects($this->atLeastOnce())->method('setAttribute');
$this->callMethod(
$this->_oModel,
'registerFormElement',
[$inputMock]
);
$newCount = count($this->getValue($this->_oModel, 'formElements'));
$this->assertGreaterThan($oldCount, $newCount);
}
/**
* @return \string[][]
*/
public function canRegisterFormElementDataProvider(): array
{
return [
'Radio' => [Radio::class],
'Checkbox' => [Radio::class],
'Hidden' => [Hidden::class]
];
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::run
* @test
* @throws ReflectionException
*/
public function canRunWithoutFormElements()
{
$modelMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'hasFormElements',
'executeAction',
'getQuery'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(false);
$modelMock->expects($this->atLeastOnce())->method('executeAction')->willReturn(1);
$modelMock->expects($this->atLeastOnce())->method('getQuery')->willReturn([]);
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'run'
);
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::run
* @test
* @throws ReflectionException
* @dataProvider canRunWithFormElementsDataProvider
*/
public function canRunWithFormElements($elements, $blThrowException)
{
$expectedException = oxNew(StandardException::class);
$modelMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods([
'hasFormElements',
'executeAction',
'getQuery',
'getFormElements'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(true);
$modelMock->expects($this->exactly((int) !$blThrowException))->method('executeAction')->willReturn(1);
$modelMock->expects($this->exactly((int) !$blThrowException))->method('getQuery')->willReturn([]);
$modelMock->expects($this->atLeastOnce())->method('getFormElements')->willReturn($elements);
$this->_oModel = $modelMock;
if ($blThrowException) {
$this->expectException(get_class($expectedException));
}
$this->callMethod(
$this->_oModel,
'run'
);
}
/**
* @return array[]
*/
public function canRunWithFormElementsDataProvider(): array
{
/** @var Radio|MockObject $validMock */
$validMock = $this->getMockBuilder(Radio::class)
->onlyMethods(['isValid'])
->getMock();
$validMock->expects($this->atLeastOnce())->method('isValid')->willReturn(true);
$invalidField = new Number(null, [
'required' => true,
'min' => 1,
'max' => 10,
'step' => 5,
]);
$invalidField
->setValue(20)
->setErrorMessages(['errorMsgs']);
return [
'validElements' => [[$validMock, $validMock], false],
'invalidElements' => [[$validMock, $invalidField], true]
];
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::executeAction
* @test
* @throws ReflectionException
* @dataProvider canExecuteActionDataProvider
*/
public function canExecuteAction($query, $throwsException)
{
/** @var Database|MockObject $dbMock */
$dbMock = $this->getMockBuilder(Database::class)
->onlyMethods(['execute'])
->getMock();
$dbMock->expects($this->exactly((int) !$throwsException))->method('execute')->willReturn(true);
/** @var d3TestAction|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestAction::class)
->onlyMethods(['d3GetDb'])
->getMock();
$modelMock->expects($this->exactly((int) !$throwsException))->method('d3GetDb')->willReturn($dbMock);
$this->_oModel = $modelMock;
try {
$this->callMethod(
$this->_oModel,
'executeAction',
[[$query, ['parameters']]]
);
} catch (TaskException $e) {
if ($throwsException) {
$this->assertStringContainsString('ACTIONSELECT', $e->getMessage());
} else {
$this->assertStringContainsString('ACTIONRESULT', $e->getMessage());
}
}
}
/**
* @return array[]
*/
public function canExecuteActionDataProvider(): array
{
return [
'Select throws exception' => ['SELECT 1', true],
'Update dont throws exception' => ['UPDATE 1', false],
];
}
/**
* @covers \D3\DataWizard\Application\Model\ActionBase::d3GetDb
* @test
* @throws ReflectionException
*/
public function canGetDb()
{
$this->assertInstanceOf(
Database::class,
$this->callMethod(
$this->_oModel,
'd3GetDb'
)
);
}
}

View File

@ -0,0 +1,454 @@
<?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;
use D3\DataWizard\Application\Model\Configuration;
use D3\DataWizard\Application\Model\Exceptions\DataWizardException;
use D3\DataWizard\tests\tools\d3TestAction;
use D3\DataWizard\tests\tools\d3TestExport;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class ConfigurationTest extends d3ModCfgUnitTestCase
{
/** @var Configuration */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(Configuration::class);
}
public function tearDown() : void
{
parent::tearDown();
unset($this->_oModel);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
/** @var Configuration|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Configuration::class)
->disableOriginalConstructor()
->onlyMethods(['configure'])
->getMock();
$modelMock->expects($this->once())->method('configure');
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct'
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::configure()
* @test
* @throws \ReflectionException
*/
public function canConfigure()
{
$this->assertEmpty(
$this->callMethod(
$this->_oModel,
'configure'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::registerAction
* @test
* @throws \ReflectionException
*/
public function canRegisterAction()
{
$action = oxNew(d3TestAction::class);
$oldCount = count($this->getValue(
$this->_oModel,
'actions'
));
$this->callMethod(
$this->_oModel,
'registerAction',
['testGroup', $action]
);
$actions = $this->getValue(
$this->_oModel,
'actions'
);
$newCount = count($actions);
$flattedActions = $this->array_flatten($actions);
$this->assertGreaterThan($oldCount, $newCount);
$this->assertContains($action, $flattedActions);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::registerExport
* @test
* @throws \ReflectionException
*/
public function canRegisterExport()
{
$export = oxNew(d3TestExport::class);
$oldCount = count($this->getValue(
$this->_oModel,
'exports'
));
$this->callMethod(
$this->_oModel,
'registerExport',
['testGroup', $export]
);
$exports = $this->getValue(
$this->_oModel,
'exports'
);
$newCount = count($exports);
$flattedExports = $this->array_flatten($exports);
$this->assertGreaterThan($oldCount, $newCount);
$this->assertContains($export, $flattedExports);
}
/**
* @param $array
* @return array|false
*/
public function array_flatten($array) {
if (!is_array($array)) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $this->array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getGroupedActions()
* @test
* @throws \ReflectionException
*/
public function canGetGroupedActions()
{
$actionList = ['abc', 'def'];
$this->setValue(
$this->_oModel,
'actions',
$actionList
);
$this->assertSame(
$actionList,
$this->callMethod(
$this->_oModel,
'getGroupedActions'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getGroupedExports()
* @test
* @throws \ReflectionException
*/
public function canGetGroupedExports()
{
$exportList = ['abc', 'def'];
$this->setValue(
$this->_oModel,
'exports',
$exportList
);
$this->assertSame(
$exportList,
$this->callMethod(
$this->_oModel,
'getGroupedExports'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getActionGroups()
* @test
* @throws \ReflectionException
*/
public function canGetActionGroups()
{
$actionList = ['abc' => '123', 'def' => '456'];
$this->setValue(
$this->_oModel,
'actions',
$actionList
);
$this->assertSame(
['abc', 'def'],
$this->callMethod(
$this->_oModel,
'getActionGroups'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getExportGroups()
* @test
* @throws \ReflectionException
*/
public function canGetExportGroups()
{
$exportList = ['abc' => '123', 'def' => '456'];
$this->setValue(
$this->_oModel,
'exports',
$exportList
);
$this->assertSame(
['abc', 'def'],
$this->callMethod(
$this->_oModel,
'getExportGroups'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getActionsByGroup()
* @test
* @throws \ReflectionException
*/
public function canGetActionsByGroup()
{
$actionList = ['abc' => '123', 'def' => '456'];
$this->setValue(
$this->_oModel,
'actions',
$actionList
);
$this->assertSame(
'456',
$this->callMethod(
$this->_oModel,
'getActionsByGroup',
['def']
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getExportsByGroup()
* @test
* @throws \ReflectionException
*/
public function canGetExportsByGroup()
{
$exportList = ['abc' => '123', 'def' => '456'];
$this->setValue(
$this->_oModel,
'exports',
$exportList
);
$this->assertSame(
'456',
$this->callMethod(
$this->_oModel,
'getExportsByGroup',
['def']
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getAllActions()
* @test
* @throws \ReflectionException
*/
public function canGetAllActions()
{
/** @var Configuration|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Configuration::class)
->onlyMethods([
'getActionGroups',
'getActionsByGroup'
])
->getMock();
$modelMock->expects($this->once())->method('getActionGroups')->willReturn(['123', '456']);
$modelMock->expects($this->exactly(2))->method('getActionsByGroup')->willReturnOnConsecutiveCalls(
['123', '456'],
['789', '012']
);
$this->_oModel = $modelMock;
$this->assertSame(
['123','456', '789', '012'],
$this->callMethod(
$this->_oModel,
'getAllActions'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getAllExports()
* @test
* @throws \ReflectionException
*/
public function canGetAllExports()
{
/** @var Configuration|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Configuration::class)
->onlyMethods([
'getExportGroups',
'getExportsByGroup'
])
->getMock();
$modelMock->expects($this->once())->method('getExportGroups')->willReturn(['123', '456']);
$modelMock->expects($this->exactly(2))->method('getExportsByGroup')->willReturnOnConsecutiveCalls(
['123', '456'],
['789', '012']
);
$this->_oModel = $modelMock;
$this->assertSame(
['123', '456', '789', '012'],
$this->callMethod(
$this->_oModel,
'getAllExports'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getActionById()
* @test
* @throws \ReflectionException
* @dataProvider canGetActionByIdDataProvider
*/
public function canGetActionById($id, $throwException)
{
$expected = oxNew(d3TestAction::class);
/** @var Configuration|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getAllActions'])
->getMock();
$modelMock->expects($this->once())->method('getAllActions')->willReturn(['123' => oxNew(d3TestAction::class), '456' => $expected]);
$this->_oModel = $modelMock;
if ($throwException) {
$this->expectException(DataWizardException::class);
}
$return = $this->callMethod(
$this->_oModel,
'getActionById',
[$id]
);
if (!$throwException) {
$this->assertSame(
$expected,
$return
);
}
}
/**
* @covers \D3\DataWizard\Application\Model\Configuration::getExportById()
* @test
* @throws \ReflectionException
* @dataProvider canGetActionByIdDataProvider
*/
public function canGetExportById($id, $throwException)
{
$expected = oxNew(d3TestExport::class);
/** @var Configuration|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Configuration::class)
->onlyMethods(['getAllExports'])
->getMock();
$modelMock->expects($this->once())->method('getAllExports')->willReturn(['123' => oxNew(d3TestExport::class), '456' => $expected]);
$this->_oModel = $modelMock;
if ($throwException) {
$this->expectException(DataWizardException::class);
}
$return = $this->callMethod(
$this->_oModel,
'getExportById',
[$id]
);
if (!$throwException) {
$this->assertSame(
$expected,
$return
);
}
}
/**
* @return array[]
*/
public function canGetActionByIdDataProvider(): array
{
return [
'unset id' => ['987', true],
'set id' => ['456', false],
];
}
}

View File

@ -0,0 +1,76 @@
<?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\Exceptions;
use D3\DataWizard\Application\Model\Exceptions\DebugException;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class DebugExceptionTest extends d3ModCfgUnitTestCase
{
/** @var DebugException */
protected $_oModel;
/**
* @covers \D3\DataWizard\Application\Model\Exceptions\DebugException::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
$code = '500';
$exception = oxNew(\Exception::class);
/** @var DebugException|MockObject $modelMock */
$modelMock = $this->getMockBuilder(DebugException::class)
->disableOriginalConstructor()
->getMock();
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct',
['testMessage', $code, $exception]
);
$this->assertStringContainsString(
'DEBUG',
$this->callMethod(
$this->_oModel,
'getMessage'
)
);
$this->assertEquals(
$code,
$this->callMethod(
$this->_oModel,
'getCode'
)
);
$this->assertSame(
$exception,
$this->callMethod(
$this->_oModel,
'getPrevious'
)
);
}
}

View File

@ -0,0 +1,76 @@
<?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\Exceptions;
use D3\DataWizard\Application\Model\Exceptions\ExportFileException;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class ExportFileExceptionTest extends d3ModCfgUnitTestCase
{
/** @var ExportFileException */
protected $_oModel;
/**
* @covers \D3\DataWizard\Application\Model\Exceptions\ExportFileException::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
$code = '500';
$exception = oxNew(\Exception::class);
/** @var ExportFileException|MockObject $modelMock */
$modelMock = $this->getMockBuilder(ExportFileException::class)
->disableOriginalConstructor()
->getMock();
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct',
['testMessage', $code, $exception]
);
$this->assertStringContainsString(
'EXPORTFILEERROR',
$this->callMethod(
$this->_oModel,
'getMessage'
)
);
$this->assertEquals(
$code,
$this->callMethod(
$this->_oModel,
'getCode'
)
);
$this->assertSame(
$exception,
$this->callMethod(
$this->_oModel,
'getPrevious'
)
);
}
}

View File

@ -0,0 +1,96 @@
<?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\Exceptions;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
use D3\DataWizard\Application\Model\ExportBase;
use D3\DataWizard\tests\tools\d3TestExport;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use FormManager\Inputs\Number;
use PHPUnit\Framework\MockObject\MockObject;
class InputUnvalidExceptionTest extends d3ModCfgUnitTestCase
{
/** @var InputUnvalidException */
protected $_oModel;
/**
* @covers \D3\DataWizard\Application\Model\Exceptions\InputUnvalidException::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
$code = '500';
$exception = oxNew(\Exception::class);
/** @var Number $invalidField */
$invalidField = new Number(null, [
'required' => true,
'min' => 1,
'max' => 10,
'step' => 5,
]);
$invalidField
->setValue(20)
->setErrorMessages(['errorMsgs']);
/** @var ExportBase|MockObject $taskMock */
$taskMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods(['getTitle'])
->getMock();
$taskMock->expects($this->atLeastOnce())->method('getTitle')->willReturn('testTitle');
/** @var InputUnvalidException|MockObject $modelMock */
$modelMock = $this->getMockBuilder(InputUnvalidException::class)
->disableOriginalConstructor()
->getMock();
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct',
[$taskMock, $invalidField, $code, $exception]
);
$this->assertRegExp(
'/^testTitle\s-\s*->\s.*\sless\s/m',
$this->callMethod(
$this->_oModel,
'getMessage'
)
);
$this->assertEquals(
$code,
$this->callMethod(
$this->_oModel,
'getCode'
)
);
$this->assertSame(
$exception,
$this->callMethod(
$this->_oModel,
'getPrevious'
)
);
}
}

View File

@ -0,0 +1,76 @@
<?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\Exceptions;
use D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class NoSuitableRendererExceptionTest extends d3ModCfgUnitTestCase
{
/** @var NoSuitableRendererException */
protected $_oModel;
/**
* @covers \D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
$code = '500';
$exception = oxNew(\Exception::class);
/** @var NoSuitableRendererException|MockObject $modelMock */
$modelMock = $this->getMockBuilder(NoSuitableRendererException::class)
->disableOriginalConstructor()
->getMock();
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct',
['testMessage', $code, $exception]
);
$this->assertStringContainsString(
'NOSUITABLERENDERER',
$this->callMethod(
$this->_oModel,
'getMessage'
)
);
$this->assertEquals(
$code,
$this->callMethod(
$this->_oModel,
'getCode'
)
);
$this->assertSame(
$exception,
$this->callMethod(
$this->_oModel,
'getPrevious'
)
);
}
}

View File

@ -0,0 +1,84 @@
<?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\Exceptions;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\DataWizard\Application\Model\ExportBase;
use D3\DataWizard\tests\tools\d3TestExport;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class TaskExceptionTest extends d3ModCfgUnitTestCase
{
/** @var TaskException */
protected $_oModel;
/**
* @covers \D3\DataWizard\Application\Model\Exceptions\TaskException::__construct
* @test
* @throws \ReflectionException
*/
public function canConstruct()
{
$code = '500';
$exception = oxNew(\Exception::class);
/** @var ExportBase|MockObject $taskMock */
$taskMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods(['getTitle'])
->getMock();
$taskMock->expects($this->atLeastOnce())->method('getTitle')->willReturn('testTitle');
/** @var TaskException|MockObject $modelMock */
$modelMock = $this->getMockBuilder(TaskException::class)
->disableOriginalConstructor()
->getMock();
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'__construct',
[$taskMock, 'testMessage', $code, $exception]
);
$this->assertSame(
'testTitle - testMessage',
$this->callMethod(
$this->_oModel,
'getMessage'
)
);
$this->assertEquals(
$code,
$this->callMethod(
$this->_oModel,
'getCode'
)
);
$this->assertSame(
$exception,
$this->callMethod(
$this->_oModel,
'getPrevious'
)
);
}
}

View File

@ -0,0 +1,634 @@
<?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;
use D3\DataWizard\Application\Model\Exceptions\ExportFileException;
use D3\DataWizard\Application\Model\Exceptions\TaskException;
use D3\DataWizard\Application\Model\ExportRenderer\Csv;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\Application\Model\ExportRenderer\RendererInterface;
use D3\DataWizard\tests\tools\d3TestExport;
use D3\ModCfg\Application\Model\d3filesystem;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use FormManager\Inputs\Hidden;
use FormManager\Inputs\Number;
use FormManager\Inputs\Radio;
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
use OxidEsales\Eshop\Core\Exception\StandardException;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class ExportBaseTest extends d3ModCfgUnitTestCase
{
/** @var d3TestExport */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(d3TestExport::class);
}
public function tearDown() : void
{
parent::tearDown();
unset($this->_oModel);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getDescription
* @test
* @throws ReflectionException
*/
public function canGetDescription()
{
$this->assertIsString(
$this->callMethod(
$this->_oModel,
'getDescription'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getButtonText
* @test
* @throws ReflectionException
*/
public function canGetButtonText()
{
$this->assertIsString(
$this->callMethod(
$this->_oModel,
'getButtonText'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::hasFormElements
* @test
* @throws ReflectionException
* @dataProvider canGetHasFormElementsDataProvider
*/
public function canGetHasFormElements($formElements, $expected)
{
$this->setValue($this->_oModel, 'formElements', $formElements);
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'hasFormElements'
)
);
}
public function canGetHasFormElementsDataProvider()
{
return [
'hasFormElements' => [['abc', 'def'], true],
'hasNoFormElements' => [[], false],
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getFormElements
* @test
* @throws ReflectionException
* @dataProvider canGetHasFormElementsDataProvider
*/
public function canGetFormElements($formElements)
{
$this->setValue($this->_oModel, 'formElements', $formElements);
$this->assertSame(
$formElements,
$this->callMethod(
$this->_oModel,
'getFormElements'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::registerFormElement
* @test
* @throws ReflectionException
* @dataProvider canRegisterFormElementDataProvider
*/
public function canRegisterFormElement($inputClass)
{
$oldCount = count($this->getValue($this->_oModel, 'formElements'));
/** @var Radio|MockObject $inputMock */
$inputMock = $this->getMockBuilder($inputClass)
->onlyMethods([
'setTemplate',
'setAttribute'
])
->getMock();
$inputMock->expects($this->atLeastOnce())->method('setTemplate');
$inputMock->expects($this->atLeastOnce())->method('setAttribute');
$this->callMethod(
$this->_oModel,
'registerFormElement',
[$inputMock]
);
$newCount = count($this->getValue($this->_oModel, 'formElements'));
$this->assertGreaterThan($oldCount, $newCount);
}
/**
* @return \string[][]
*/
public function canRegisterFormElementDataProvider(): array
{
return [
'Radio' => [Radio::class],
'Checkbox' => [Radio::class],
'Hidden' => [Hidden::class]
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::run
* @test
* @throws ReflectionException
*/
public function canRunWithoutFormElements()
{
$format = 'myFormat';
$path = 'myPath';
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'hasFormElements',
'executeExport'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(false);
$modelMock->expects($this->atLeastOnce())->method('executeExport')->with($format, $path)->willReturn('some content');
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'run',
[$format, $path]
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::run
* @test
* @throws ReflectionException
* @dataProvider canRunWithFormElementsDataProvider
*/
public function canRunWithFormElements($elements, $blThrowException)
{
$format = 'myFormat';
$path = 'myPath';
$expectedException = oxNew(StandardException::class);
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'hasFormElements',
'executeExport',
'getFormElements'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('hasFormElements')->willReturn(true);
$modelMock->expects($this->exactly((int) !$blThrowException))->method('executeExport')->with($format, $path)->willReturn('some content');
$modelMock->expects($this->atLeastOnce())->method('getFormElements')->willReturn($elements);
$this->_oModel = $modelMock;
if ($blThrowException) {
$this->expectException(get_class($expectedException));
}
$this->callMethod(
$this->_oModel,
'run',
[$format, $path]
);
}
/**
* @return array[]
*/
public function canRunWithFormElementsDataProvider(): array
{
/** @var Radio|MockObject $validMock */
$validMock = $this->getMockBuilder(Radio::class)
->onlyMethods(['isValid'])
->getMock();
$validMock->expects($this->atLeastOnce())->method('isValid')->willReturn(true);
$invalidField = new Number(null, [
'required' => true,
'min' => 1,
'max' => 10,
'step' => 5,
]);
$invalidField
->setValue(20)
->setErrorMessages(['errorMsgs']);
return [
'validElements' => [[$validMock, $validMock], false],
'invalidElements' => [[$validMock, $invalidField], true]
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::executeExport
* @test
* @throws ReflectionException
* @dataProvider canExecuteExportDataProvider
*/
public function canExecuteExport($path, $throwsException)
{
/** @var d3filesystem|MockObject $fsMock */
$fsMock = $this->getMockBuilder(d3filesystem::class)
->onlyMethods([
'startDirectDownload',
'filterFilename',
'trailingslashit',
'createFile'
])
->getMock();
$fsMock->expects($this->exactly((int) !isset($path)))->method('startDirectDownload')->willReturn(true);
$fsMock->method('filterFilename')->willReturnArgument(0);
$fsMock->expects($this->exactly((int) isset($path)))->method('trailingslashit')->willReturnArgument(0);
$fsMock->expects($this->exactly((int) isset($path)))->method('createFile')->willReturn((bool) !$throwsException);
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getContent',
'getFileSystem',
'getExportFileName'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getContent')->willReturn('some content');
$modelMock->expects($this->atLeastOnce())->method('getFileSystem')->willReturn($fsMock);
$modelMock->expects($this->atLeastOnce())->method('getExportFileName')->willReturn('exportFileName');
$this->_oModel = $modelMock;
if ($path && $throwsException) {
$this->expectException(ExportFileException::class);
}
$this->callMethod(
$this->_oModel,
'executeExport',
['CSV', $path]
);
}
/**
* @return array[]
*/
public function canExecuteExportDataProvider(): array
{
return [
'unable to create path for saving file' => ['myPath', true],
'can create path for saving file' => ['myPath', false],
'no path for download' => [null, false],
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::d3GetDb
* @test
* @throws ReflectionException
*/
public function canGetDb()
{
$this->assertInstanceOf(
Database::class,
$this->callMethod(
$this->_oModel,
'd3GetDb'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getFileSystem
* @test
* @throws ReflectionException
*/
public function canGetFileSystem()
{
$this->assertInstanceOf(
d3filesystem::class,
$this->callMethod(
$this->_oModel,
'getFileSystem'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getRenderer
* @test
* @throws ReflectionException
*/
public function canGetRenderer()
{
/** @var RendererInterface|MockObject $rendererMock */
$rendererMock = $this->getMockBuilder(Csv::class)
->getMock();
/** @var RendererBridge|MockObject $rendererBridgeMock */
$rendererBridgeMock = $this->getMockBuilder(RendererBridge::class)
->onlyMethods(['getRenderer'])
->getMock();
$rendererBridgeMock->expects($this->atLeastOnce())->method('getRenderer')->willReturn($rendererMock);
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getRendererBridge'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getRendererBridge')->willReturn($rendererBridgeMock);
$this->_oModel = $modelMock;
$this->assertSame(
$rendererMock,
$this->callMethod(
$this->_oModel,
'getRenderer',
[RendererBridge::FORMAT_CSV]
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getRendererBridge
* @test
* @throws ReflectionException
*/
public function canGetRendererBridge()
{
$this->assertInstanceOf(
RendererBridge::class,
$this->callMethod(
$this->_oModel,
'getRendererBridge'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getFileExtension
* @test
* @throws ReflectionException
*/
public function canGetFileExtension()
{
$format = RendererBridge::FORMAT_CSV;
$expected = 'myFileExtension';
/** @var RendererInterface|MockObject $rendererMock */
$rendererMock = $this->getMockBuilder(Csv::class)
->onlyMethods(['getFileExtension'])
->getMock();
$rendererMock->expects($this->atLeastOnce())->method('getFileExtension')->willReturn($expected);
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getRenderer'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getRenderer')->with($format)->willReturn($rendererMock);
$this->_oModel = $modelMock;
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'getFileExtension',
[$format]
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::renderContent
* @test
* @throws ReflectionException
*/
public function canRenderContent()
{
$rows = ['row1', 'row2'];
$fieldnames = ['fieldname1', 'fieldname2'];
$format = RendererBridge::FORMAT_CSV;
$expected = 'myContent';
/** @var RendererInterface|MockObject $rendererMock */
$rendererMock = $this->getMockBuilder(Csv::class)
->onlyMethods(['getContent'])
->getMock();
$rendererMock->expects($this->atLeastOnce())->method('getContent')->with($rows, $fieldnames)->willReturn($expected);
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getRenderer'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getRenderer')->with($format)->willReturn($rendererMock);
$this->_oModel = $modelMock;
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'renderContent',
[$rows, $fieldnames, $format]
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getExportFilenameBase
* @test
* @throws ReflectionException
*/
public function canGetExportFilenameBase()
{
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getTitle'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getTitle')->willReturn('someTitle');
$this->_oModel = $modelMock;
$this->callMethod(
$this->_oModel,
'getExportFilenameBase'
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getExportFileName
* @test
* @throws ReflectionException
*/
public function canGetExportFileName()
{
$format = RendererBridge::FORMAT_CSV;
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getExportFilenameBase',
'getFileExtension'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getExportFilenameBase')->willReturn('base');
$modelMock->expects($this->atLeastOnce())->method('getFileExtension')->with($format)->willReturn('extension');
$this->_oModel = $modelMock;
$this->assertRegExp(
'/^base_(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})\.extension$/m',
$this->callMethod(
$this->_oModel,
'getExportFileName',
[$format]
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getExportData
* @test
* @throws ReflectionException
* @dataProvider canGetExportDataDataProvider
*/
public function canGetExportData($query, $throwsException, $dbResult)
{
/** @var Database|MockObject $dbMock */
$dbMock = $this->getMockBuilder(Database::class)
->onlyMethods(['getAll'])
->getMock();
$dbMock->expects($this->exactly((int) !$throwsException))->method('getAll')->willReturn($dbResult);
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'd3GetDb'
])
->getMock();
$modelMock->expects($this->exactly((int) !$throwsException))->method('d3GetDb')->willReturn($dbMock);
$this->_oModel = $modelMock;
try {
$result = $this->callMethod(
$this->_oModel,
'getExportData',
[[$query], ['param1', 'param2']]
);
$this->assertSame(
[
[
[
'field1' => 'content1',
'field2' => 'content2'
]
],
[
'field1',
'field2'
]
],
$result
);
} catch (TaskException $e) {
if ($throwsException) {
$this->assertStringContainsString('NOEXPORTSELECT', $e->getMessage());
} elseif (!count($dbResult)) {
$this->assertStringContainsString('kein Inhalt', $e->getMessage());
}
}
}
/**
* @return array[]
*/
public function canGetExportDataDataProvider(): array
{
return [
'not SELECT throws exception' => [' UPDATE 1', true, []],
'empty SELECT' => [' SELECT 1', false, []],
'fulfilled SELECT' => [' SELECT 1', false, [['field1' => 'content1', 'field2' => 'content2']]],
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportBase::getContent
* @test
* @throws ReflectionException
*/
public function canGetContent()
{
/** @var d3TestExport|MockObject $modelMock */
$modelMock = $this->getMockBuilder(d3TestExport::class)
->onlyMethods([
'getQuery',
'getExportData',
'renderContent'
])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getQuery')->willReturn(['SELECT 1', ['arg1', 'arg2']]);
$modelMock->expects($this->atLeastOnce())->method('getExportData')->willReturn([[1, 2], ['field1', 'field2']]);
$modelMock->expects($this->atLeastOnce())->method('renderContent')->willReturn('some content');
$this->_oModel = $modelMock;
$this->assertSame(
'some content',
$this->callMethod(
$this->_oModel,
'getContent',
['CSV']
)
);
}
}

View File

@ -0,0 +1,180 @@
<?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;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use PHPUnit\Framework\MockObject\MockObject;
class CsvTest extends ExportRendererTest
{
/** @var Csv */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(Csv::class);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getContent
* @test
* @throws \ReflectionException
* @dataProvider canGetContentDataProvider
*/
public function canGetContent($blThrowException)
{
$expected = 'expectedReturn';
$fieldList = ['field1', 'field2'];
$valueList = ['value1', 'value2'];
/** @var Writer|MockObject $csvMock */
$csvMockBuilder = $this->getMockBuilder(Writer::class);
$csvMockBuilder->disableOriginalConstructor();
$onlyMethods = ['insertOne', 'insertAll'];
if (method_exists($csvMockBuilder->getMock(), 'getContent')) {
$onlyMethods[] = 'getContent';
} else {
$csvMockBuilder->addMethods(['getContent']);
}
$csvMockBuilder->onlyMethods($onlyMethods);
$csvMock = $csvMockBuilder->getMock();
if ($blThrowException) {
$csvMock->expects($this->atLeastOnce())->method('getContent')->willThrowException(oxNew(Exception::class));
$this->expectException(RenderException::class);
} else {
$csvMock->expects($this->atLeastOnce())->method('getContent')->willReturn($expected);
}
$csvMock->expects($this->atLeastOnce())->method('insertOne')->with($fieldList)->willReturn(1);
$csvMock->expects($this->atLeastOnce())->method('insertAll')->with($valueList)->willReturn(1);
/** @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],
'no exception' => [false]
];
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv
* @test
* @throws \ReflectionException
*/
public function canGetCsv()
{
$this->assertInstanceOf(
Writer::class,
$this->callMethod(
$this->_oModel,
'getCsv'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getCsv
* @test
* @throws \ReflectionException
*/
public function canGetCsvNoSettings()
{
/** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam'])
->getMock();
$configMock->expects($this->atLeastOnce())->method('getConfigParam')->willReturnCallback(
function ($argName) {
switch ($argName) {
case 'sGiCsvFieldEncloser':
case 'sCSVSign':
return false;
default:
return Registry::getConfig()->getConfigParam($argName);
}
}
);
$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
* @throws \ReflectionException
*/
public function canGetConfig()
{
$this->assertInstanceOf(
Config::class,
$this->callMethod(
$this->_oModel,
'd3GetConfig'
)
);
}
}

View File

@ -0,0 +1,65 @@
<?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
*/
namespace D3\DataWizard\tests\unit\Application\Model\ExportRenderer;
use D3\DataWizard\Application\Model\ExportRenderer\RendererInterface;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
abstract class ExportRendererTest extends d3ModCfgUnitTestCase
{
/** @var RendererInterface */
protected $_oModel;
public function tearDown() : void
{
parent::tearDown();
unset($this->_oModel);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getFileExtension
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Json::getFileExtension
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Pretty::getFileExtension
* @test
* @throws \ReflectionException
*/
public function canGetFileExtension()
{
$this->assertRegExp(
"/^[a-z0-9._-]*$/i",
$this->callMethod(
$this->_oModel,
'getFileExtension'
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Csv::getTitleTranslationId
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Json::getTitleTranslationId
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Pretty::getTitleTranslationId
* @test
* @throws \ReflectionException
*/
public function canGetTitleTranslationId()
{
$this->assertIsString(
$this->callMethod(
$this->_oModel,
'getTitleTranslationId'
)
);
}
}

View File

@ -0,0 +1,66 @@
<?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\Json;
class JsonTest extends ExportRendererTest
{
/** @var Json */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(Json::class);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Json::getContent
* @test
* @throws \ReflectionException
* @dataProvider canGetContentDataProvider
*/
public function canGetContent($valueList, $expectException)
{
$fieldList = ['field1', 'field2'];
if ($expectException) {
$this->expectException(RenderException::class);
}
$this->assertJson(
$this->callMethod(
$this->_oModel,
'getContent',
[$valueList, $fieldList]
)
);
}
/**
* @return \string[][]
*/
public function canGetContentDataProvider(): array
{
return [
'valid' => [['value1', 'value2'], false],
'invalid' => [["text" => "\xB1\x31"], true] // malformed UTF8 chars
];
}
}

View File

@ -0,0 +1,85 @@
<?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\ExportRenderer\Pretty;
use MathieuViossat\Util\ArrayToTextTable;
use PHPUnit\Framework\MockObject\MockObject;
class PrettyTest extends ExportRendererTest
{
/** @var Pretty */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(Pretty::class);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Pretty::getContent
* @test
* @throws \ReflectionException
*/
public function canGetContent()
{
$expected = 'expectedReturn';
$fieldList = ['field1', 'field2'];
$valueList = ['value1', 'value2'];
/** @var ArrayToTextTable|MockObject $csvMock */
$arrayToTextTableMock = $this->getMockBuilder(ArrayToTextTable::class)
->disableOriginalConstructor()
->onlyMethods(['getTable'])
->getMock();
$arrayToTextTableMock->expects($this->atLeastOnce())->method('getTable')->willReturn($expected);
/** @var Pretty|MockObject $modelMock */
$modelMock = $this->getMockBuilder(Pretty::class)
->onlyMethods(['getArrayToTextTableInstance'])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getArrayToTextTableInstance')->willReturn($arrayToTextTableMock);
$this->_oModel = $modelMock;
$this->assertSame(
$expected,
$this->callMethod(
$this->_oModel,
'getContent',
[$valueList, $fieldList]
)
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\Pretty::getArrayToTextTableInstance
* @test
* @throws \ReflectionException
*/
public function canGetArrayToTextTableInstance()
{
$this->assertInstanceOf(
ArrayToTextTable::class,
$this->callMethod(
$this->_oModel,
'getArrayToTextTableInstance',
[['field1', 'field2']]
)
);
}
}

View File

@ -0,0 +1,151 @@
<?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\NoSuitableRendererException;
use D3\DataWizard\Application\Model\ExportRenderer\Csv;
use D3\DataWizard\Application\Model\ExportRenderer\Json;
use D3\DataWizard\Application\Model\ExportRenderer\Pretty;
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
use D3\DataWizard\Application\Model\ExportRenderer\RendererInterface;
use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class RendererBridgeTest extends d3ModCfgUnitTestCase
{
/** @var RendererBridge */
protected $_oModel;
public function setUp() : void
{
parent::setUp();
$this->_oModel = oxNew(RendererBridge::class);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oModel);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\RendererBridge::getRendererList
* @test
* @throws \ReflectionException
*/
public function canGetRendererList()
{
$list = $this->callMethod(
$this->_oModel,
'getRendererList'
);
$this->assertIsArray($list);
$this->assertTrue((bool) count($list));
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\RendererBridge::getTranslatedRendererIdList
* @test
* @throws \ReflectionException
*/
public function canGetTranslatedRendererIdList()
{
$utlist = $this->callMethod(
$this->_oModel,
'getRendererList'
);
$list = $this->callMethod(
$this->_oModel,
'getTranslatedRendererIdList'
);
$this->assertIsArray($list);
$this->assertTrue((bool) count($list));
$this->assertSame(count($utlist), count($list));
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\RendererBridge::translateRendererId
* @test
* @throws \ReflectionException
*/
public function canTranslateRendererId()
{
$expected = "expectedTranslation";
/** @var RendererInterface|MockObject $renderMock */
$renderMock = $this->getMockBuilder(Pretty::class)
->onlyMethods(['getTitleTranslationId'])
->getMock();
$renderMock->expects($this->atLeastOnce())->method('getTitleTranslationId')->willReturn($expected);
$this->callMethod(
$this->_oModel,
'translateRendererId',
[$renderMock]
);
}
/**
* @covers \D3\DataWizard\Application\Model\ExportRenderer\RendererBridge::getRenderer
* @test
* @param $format
* @param $blThrowException
* @throws \ReflectionException
* @dataProvider canGetRendererDataProvider
*/
public function canGetRenderer($format, $blThrowException)
{
/** @var RendererBridge|MockObject $modelMock */
$modelMock = $this->getMockBuilder(RendererBridge::class)
->onlyMethods(['getRendererList'])
->getMock();
$modelMock->expects($this->atLeastOnce())->method('getRendererList')->willReturn(
[
'CSV' => $this->getMockBuilder(Csv::class)->getMock(),
'Pretty' => $this->getMockBuilder(Pretty::class)->getMock(),
'JSON' => $this->getMockBuilder(Json::class)->getMock()
]
);
$this->_oModel = $modelMock;
if ($blThrowException) {
$this->expectException(NoSuitableRendererException::class);
}
$this->callMethod(
$this->_oModel,
'getRenderer',
[$format]
);
}
/**
* @return array
*/
public function canGetRendererDataProvider(): array
{
return [
'existing renderer'=> [RendererBridge::FORMAT_JSON, false],
'unknown renderer'=> ['unknownRenderer', true]
];
}
}