Skip to content

Commit

Permalink
OXDEV-8388 Fix export tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaIvanovski authored and angel-dimitrov committed Jun 7, 2024
1 parent d582bf1 commit d2e4429
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Application/Export/Cli/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function getConfigurationParameters($argv)
$config = include $configFile;

if (!is_array($config)) {
exit('Config file has wrong format.'."\n");
throw new \Exception('Config file has wrong format.'."\n");
}

return $config;
Expand Down
2 changes: 1 addition & 1 deletion Command/ExportDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class ExportDataCommand extends Command
{

protected function configure()
{
$this
Expand All @@ -36,6 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$output->writeln('<fg=green>' . $message . '</>');

return Command::SUCCESS;
}
}
5 changes: 5 additions & 0 deletions Service/ModuleSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ public function getExportPath(): string
{
return $this->moduleSettingService->getString(self::EXPORT_PATH, Module::MODULE_ID);
}

public function saveExportPath(string $path): void
{
$this->moduleSettingService->saveString(self::EXPORT_PATH, $path, Module::MODULE_ID);
}
}
6 changes: 5 additions & 1 deletion tests/Integration/Application/ExportViaAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

use OxidEsales\Eshop\Core\Registry;
use OxidEsales\PersonalizationModule\Application\Controller\Admin\Tab\ExportTabController;
use OxidEsales\PersonalizationModule\Service\ModuleSettings;
use OxidEsales\PersonalizationModule\Traits\ServiceContainer;

class ExportViaAdminTest extends AbstractExportDataInCSV
{
use ServiceContainer;

/**
* @inheritdoc
*/
Expand All @@ -22,7 +26,7 @@ protected function setParametersForExport($exportParentProducts, $exportVars, $c
$_POST['blExportVars'] = $exportVars;
$_POST['iStart'] = 1;

Registry::getConfig()->saveShopConfVar('str', 'sOePersonalizationExportPath', $exportPath, 1, 'module:oepersonalization');
$this->getServiceFromContainer(ModuleSettings::class)->saveExportPath($exportPath);
}

protected function runExport()
Expand Down
91 changes: 54 additions & 37 deletions tests/Integration/Application/ExportViaCmdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,105 @@
namespace OxidEsales\PersonalizationModule\Tests\Integration\Application;

use OxidEsales\Eshop\Core\Registry;
use OxidEsales\PersonalizationModule\Command\ExportDataCommand;
use Symfony\Component\Console\Tester\CommandTester;

class ExportViaCmdTest extends AbstractExportDataInCSV
{
private $exportCommand = '/var/www/bin/oe-console oe:personalization:export';

protected $exportPath = 'export/oepersonalization';

public function tearDown(): void
{
parent::tearDown();
$exportDir = Registry::getConfig()->getConfigParam('sShopDir') . $this->exportPath;
if (file_exists($exportDir.'/products.csv')) {
unlink($exportDir.'/products.csv');
if (file_exists($exportDir . '/products.csv')) {
unlink($exportDir . '/products.csv');
}
if (file_exists($exportDir.'/categories.csv')) {
unlink($exportDir.'/categories.csv');
if (file_exists($exportDir . '/categories.csv')) {
unlink($exportDir . '/categories.csv');
}
if (file_exists($exportDir) && is_dir($exportDir)) {
rmdir($exportDir);
}
$configFile = __DIR__.'/../../fixtures/config/params.php';
$configFile = __DIR__ . '/../../fixtures/config/params.php';
file_put_contents($configFile, '<?php ');
}

protected function setParametersForExport($exportParentProducts, $exportVars, $categories, $minStock, $exportPath, $shopId)
{
$fileContent = '<?php return ["exportVariants" => '.
json_encode($exportVars).', "shopId" => '.
$shopId.', "exportVariantsParentProduct" => '.
json_encode($exportParentProducts).', "exportCategories" => '.
json_encode($categories).', "exportPath" => "'.
$exportPath.'", "exportMinStock" => '.
json_encode($minStock).'];';

$configFile = __DIR__.'/../../fixtures/config/params.php';
$fileContent = '<?php return ["exportVariants" => ' .
json_encode($exportVars) . ', "shopId" => ' .
$shopId . ', "exportVariantsParentProduct" => ' .
json_encode($exportParentProducts) . ', "exportCategories" => ' .
json_encode($categories) . ', "exportPath" => "' .
$exportPath . '", "exportMinStock" => ' .
json_encode($minStock) . '];';

$configFile = __DIR__ . '/../../fixtures/config/params.php';
file_put_contents($configFile, $fileContent);

$this->configFile = $configFile;
}

protected function runExport($configFile = null)
protected function runExport()
{
$command = new ExportDataCommand();
$commandTester = new CommandTester($command);
$commandTester->execute(['--config' => $configFile]);
$commandTester = $this->getComamndTester();
$commandTester->execute([
'--config' => $this->configFile
]);
$exportDir = Registry::getConfig()->getConfigParam('sShopDir') . $this->exportPath;
$success = false;
if (file_exists($exportDir.'/products.csv') && file_exists($exportDir.'/categories.csv')) {

if (
file_exists($exportDir . '/products.csv') &&
file_exists($exportDir . '/categories.csv')
) {
$success = true;
}

$this->assertTrue($success);
}

public function testIfWrongCommandWillBeUsed()
{
$configFile = __DIR__.'/../../fixtures/config/params.php';
exec($this->exportCommand . ' --configuration ' . escapeshellarg($configFile), $returnOutput);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "--configuration" option does not exist.');

$message = 'Unknown command: --configuration'.
'. If you want to override the configuration file for the export, please, use the "--config" command';
$this->assertEquals($message, $returnOutput[0]);
$commandTester = $this->getComamndTester();
$commandTester->execute([
'--configuration' => __DIR__ . '/../../fixtures/config/params.php'
]);
}

public function testIfNotExistingConfigFileWillBeUsed()
{
$configFile = __DIR__.'/../../fixtures/config/wrong_name.php';
$feedFile = VENDOR_PATH. '/bin/oe-personalization-data-feed';
exec($this->exportCommand . ' --config ' . escapeshellarg($configFile), $returnOutput);
$configFile = __DIR__ . '/../../fixtures/config/wrong_name.php';

$this->expectException(\Exception::class);
$this->expectExceptionMessage(sprintf("File does not exist: %s", $configFile));

$message = 'File does not exist: ' . $configFile;
$this->assertEquals($message, $returnOutput[0]);
$commandTester = $this->getComamndTester();
$commandTester->execute([
'--config' => $configFile
]);
}

public function testIfConfigFileIsEmpty()
{
$configFile = __DIR__.'/../../fixtures/config/params.php';
$feedFile = VENDOR_PATH. '/bin/oe-personalization-data-feed';
exec($this->exportCommand . ' --config ' . escapeshellarg($configFile), $returnOutput);
$configFile = __DIR__ . '/../../fixtures/config/params.php';

$message = 'Config file has wrong format.';
$this->assertEquals($message, $returnOutput[0]);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Config file has wrong format.');

$commandTester = $this->getComamndTester();
$commandTester->execute([
'--config' => $configFile
]);
}

private function getComamndTester(): CommandTester
{
return new CommandTester(
$this->get('console.command_loader')->get('oe:personalization:export')
);
}
}

0 comments on commit d2e4429

Please sign in to comment.