From 27a32e64e66ca1696e65a6c713ac0b536768119e Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 21 Jul 2014 11:13:03 +0200 Subject: [PATCH] =?UTF-8?q?Add=20ProductPriceImport=20test=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/ImportExport/?= =?UTF-8?q?Import/Type/ProductPriceImport.php=20=09modifi=C3=A9:=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20core/lib/Thelia/Tests/Controller/ControllerTe?= =?UTF-8?q?stBase.php=20=09nouveau=20fichier:=20core/lib/Thelia/Tests/Impo?= =?UTF-8?q?rtExport/Import/ProductPriceImportTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Import/Type/ProductPriceImport.php | 3 +- .../Tests/Controller/ControllerTestBase.php | 8 +- .../Import/ProductPriceImportTest.php | 114 ++++++++++++++++++ 3 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 core/lib/Thelia/Tests/ImportExport/Import/ProductPriceImportTest.php diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php index be1543965..f008f7aba 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductPriceImport.php @@ -15,6 +15,7 @@ use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\FormatType; use Thelia\Core\Translation\Translator; use Thelia\ImportExport\Import\ImportHandler; +use Thelia\Model\Currency; use Thelia\Model\CurrencyQuery; use Thelia\Model\ProductPrice; use Thelia\Model\ProductPriceQuery; @@ -82,7 +83,7 @@ class ProductPriceImport extends ImportHandler } if ($currency === null) { - $currency = $this->getRequest()->getSession()->getCurrency(); + $currency = Currency::getDefaultCurrency(); } $price = ProductPriceQuery::create() diff --git a/core/lib/Thelia/Tests/Controller/ControllerTestBase.php b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php index c601360c2..b0052f1e0 100644 --- a/core/lib/Thelia/Tests/Controller/ControllerTestBase.php +++ b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php @@ -13,6 +13,7 @@ namespace Thelia\Tests\Controller; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Controller\Admin\ImportController; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarArchiveBuilder; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarBz2ArchiveBuilder; @@ -36,7 +37,10 @@ abstract class ControllerTestBase extends \PHPUnit_Framework_TestCase { protected $import; + /** @var ContainerInterface */ protected $container; + + /** @var Session */ protected $session; /** @var ImportController */ @@ -51,13 +55,13 @@ abstract class ControllerTestBase extends \PHPUnit_Framework_TestCase $container->set("event_dispatcher", $dispatcher); - $this->buildContainer($container); - $request = new Request(); $request->setSession($this->session); $container->set("request", $request); + $this->buildContainer($container); + return $container; } diff --git a/core/lib/Thelia/Tests/ImportExport/Import/ProductPriceImportTest.php b/core/lib/Thelia/Tests/ImportExport/Import/ProductPriceImportTest.php new file mode 100644 index 000000000..83d8f6683 --- /dev/null +++ b/core/lib/Thelia/Tests/ImportExport/Import/ProductPriceImportTest.php @@ -0,0 +1,114 @@ + + */ +class ProductPriceImportTest extends ControllerTestBase +{ + /** + * Use this method to build the container with the services that you need. + */ + protected function buildContainer(ContainerBuilder $container) + { + + } + + + /** + * @return \Thelia\Controller\BaseController The controller you want to test + */ + protected function getController() + { + return new ImportController(); + } + + + public function setUp() + { + parent::setUp(); + + $this->import = new ProductPriceImport($this->container); + + } + + public function testImport() + { + $currency = Currency::getDefaultCurrency(); + + $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->getPricesByCurrency($currency)->getPrice() === $entry["price"] = rand(0, 1000)); + while ($pse->getPricesByCurrency($currency)->getPromoPrice() === $entry["promo_price"] = rand(0, 1000)); + + $data[$pse->getId()] = $entry; + + $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()], + [ + "price" => $entry->getPricesByCurrency($currency)->getPrice(), + "promo_price" => $entry->getPricesByCurrency($currency)->getPromoPrice(), + "ref" => $entry->getRef() + ] + ); + } + } + +} \ No newline at end of file