Add tests for ImportController::processImport and ProductStockImport
modifié: core/lib/Thelia/Controller/Admin/ImportController.php modifié: core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php modifié: core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/XMLFormatterTest.php nouveau fichier: core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php nouveau fichier: core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php
This commit is contained in:
@@ -197,7 +197,7 @@ class ImportController extends BaseAdminController
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function retrieveFormatTools(
|
public function retrieveFormatTools(
|
||||||
$fileName,
|
$fileName,
|
||||||
ImportHandler $handler,
|
ImportHandler $handler,
|
||||||
FormatterManager $formatterManager,
|
FormatterManager $formatterManager,
|
||||||
@@ -250,7 +250,7 @@ class ImportController extends BaseAdminController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkFileExtension($fileName, $uploadFormat)
|
public function checkFileExtension($fileName, $uploadFormat)
|
||||||
{
|
{
|
||||||
$splitName = explode(".", $fileName);
|
$splitName = explode(".", $fileName);
|
||||||
$ext = "";
|
$ext = "";
|
||||||
@@ -271,7 +271,7 @@ class ImportController extends BaseAdminController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processImport(
|
public function processImport(
|
||||||
$content,
|
$content,
|
||||||
ImportHandler $handler,
|
ImportHandler $handler,
|
||||||
AbstractFormatter $formatter = null,
|
AbstractFormatter $formatter = null,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ use Thelia\ImportExport\Import\ImportHandler;
|
|||||||
use Thelia\Model\ProductSaleElementsQuery;
|
use Thelia\Model\ProductSaleElementsQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ProductStockImport
|
* Class ImportTestBase
|
||||||
* @package Thelia\ImportExport\Import
|
* @package Thelia\ImportExport\Import
|
||||||
* @author Benjamin Perche <bperche@openstudio.fr>
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -152,8 +152,6 @@ class XMLFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$data = $this->formatter->decode($raw);
|
$data = $this->formatter->decode($raw);
|
||||||
|
|
||||||
var_dump($data->getData());
|
|
||||||
|
|
||||||
$this->assertEquals(["foo" => "bar"], $data->getData());
|
$this->assertEquals(["foo" => "bar"], $data->getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
93
core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php
Normal file
93
core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* This file is part of the Thelia package. */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : dev@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||||
|
/* file that was distributed with this source code. */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Tests\ImportExport\Import;
|
||||||
|
use Symfony\Component\DependencyInjection\Container;
|
||||||
|
use Thelia\Controller\Admin\ImportController;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarBz2ArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarGzArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilderManager;
|
||||||
|
use Thelia\Core\FileFormat\Formatting\Formatter\JsonFormatter;
|
||||||
|
use Thelia\Core\FileFormat\Formatting\Formatter\XMLFormatter;
|
||||||
|
use Thelia\Core\FileFormat\Formatting\FormatterManager;
|
||||||
|
use Thelia\Core\HttpFoundation\Request;
|
||||||
|
use Thelia\Core\HttpFoundation\Session\Session;
|
||||||
|
use Thelia\Core\Translation\Translator;
|
||||||
|
use Thelia\Log\Tlog;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ImportTestBase
|
||||||
|
* @package Thelia\Tests\ImportExport\Import
|
||||||
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
|
*/
|
||||||
|
class ImportTestBase extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $import;
|
||||||
|
|
||||||
|
protected $container;
|
||||||
|
protected $session;
|
||||||
|
|
||||||
|
/** @var ImportController */
|
||||||
|
protected $controller;
|
||||||
|
|
||||||
|
public function getContainer()
|
||||||
|
{
|
||||||
|
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
|
||||||
|
$container->set("thelia.translator", new Translator(new Container()));
|
||||||
|
|
||||||
|
$dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
|
||||||
|
|
||||||
|
$container->set("event_dispatcher", $dispatcher);
|
||||||
|
|
||||||
|
$archiveBuilderManager = (new ArchiveBuilderManager("dev"))
|
||||||
|
->add(new ZipArchiveBuilder())
|
||||||
|
->add(new TarArchiveBuilder())
|
||||||
|
->add(new TarBz2ArchiveBuilder())
|
||||||
|
->add(new TarGzArchiveBuilder())
|
||||||
|
;
|
||||||
|
$container->set("thelia.manager.archive_builder_manager", $archiveBuilderManager);
|
||||||
|
|
||||||
|
$formatterManager = (new FormatterManager())
|
||||||
|
->add(new XMLFormatter())
|
||||||
|
->add(new JsonFormatter())
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->set("thelia.manager.formatter_manager", $archiveBuilderManager);
|
||||||
|
|
||||||
|
$request = new Request();
|
||||||
|
$request->setSession($this->session);
|
||||||
|
|
||||||
|
$container->set("request", $request);
|
||||||
|
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSession()
|
||||||
|
{
|
||||||
|
return new Session();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
|
||||||
|
Tlog::getNewInstance();
|
||||||
|
|
||||||
|
$this->session = $this->getSession();
|
||||||
|
$this->container = $this->getContainer();
|
||||||
|
$this->controller = new ImportController();
|
||||||
|
$this->controller->setContainer($this->container);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* This file is part of the Thelia package. */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : dev@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||||
|
/* file that was distributed with this source code. */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Tests\ImportExport\Import;
|
||||||
|
use Thelia\Core\FileFormat\Formatting\Formatter\JsonFormatter;
|
||||||
|
use Thelia\ImportExport\Import\Type\ProductStockImport;
|
||||||
|
use Thelia\Model\ProductSaleElementsQuery;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ProductStockImportTest
|
||||||
|
* @package Thelia\Tests\ImportExport\Import
|
||||||
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
|
*
|
||||||
|
* This class tests the import controller too
|
||||||
|
*/
|
||||||
|
class ProductStockImportTest extends ImportTestBase
|
||||||
|
{
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->import = new ProductStockImport($this->container);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateStock()
|
||||||
|
{
|
||||||
|
$query = ProductSaleElementsQuery::create()
|
||||||
|
->addAscendingOrderByColumn('RAND()')
|
||||||
|
->limit(3)
|
||||||
|
->find()
|
||||||
|
;
|
||||||
|
|
||||||
|
$jsonData = [];
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
/** @var \Thelia\Model\ProductSaleElements $pse */
|
||||||
|
foreach ($query as $pse) {
|
||||||
|
|
||||||
|
$entry = [];
|
||||||
|
|
||||||
|
$entry["ref"] = $pse->getRef();
|
||||||
|
/**
|
||||||
|
* Be sure to get a different value.
|
||||||
|
*/
|
||||||
|
while ($pse->getQuantity() === $entry["stock"] = rand(0, 1000));
|
||||||
|
|
||||||
|
$data[$pse->getId()] = $entry["stock"];
|
||||||
|
|
||||||
|
$jsonData[] = $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonString = json_encode($jsonData);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"Import successfully done",
|
||||||
|
$this->controller->processImport(
|
||||||
|
$jsonString,
|
||||||
|
$this->import,
|
||||||
|
new JsonFormatter(),
|
||||||
|
null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = ProductSaleElementsQuery::create()->findPks(array_keys($data));
|
||||||
|
|
||||||
|
/** @var \Thelia\Model\ProductSaleElements $entry */
|
||||||
|
foreach ($query as $entry) {
|
||||||
|
$this->assertEquals(
|
||||||
|
$data[$entry->getId()],
|
||||||
|
$entry->getQuantity()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user