From f891d8b78831f7515ebd7be16b6f8cd7245d795e Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Fri, 25 Jul 2014 10:19:52 +0200 Subject: [PATCH] =?UTF-8?q?Add=20ContainerAwareTestCase=20and=20ExportHand?= =?UTF-8?q?lerTest=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/T?= =?UTF-8?q?helia/ImportExport/Export/ExportHandler.php=20=09modifi=C3=A9:?= =?UTF-8?q?=20=20=20=20=20=20=20=20=20core/lib/Thelia/ImportExport/Export/?= =?UTF-8?q?Type/ProductPricesExport.php=20=09nouveau=20fichier:=20core/lib?= =?UTF-8?q?/Thelia/Tests/ContainerAwareTestCase.php=20=09modifi=C3=A9:=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20core/lib/Thelia/Tests/Controller/Contro?= =?UTF-8?q?llerTestBase.php=20=09nouveau=20fichier:=20core/lib/Thelia/Test?= =?UTF-8?q?s/ImportExport/Export/ExportHandlerTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportExport/Export/ExportHandler.php | 40 +++++- .../Export/Type/ProductPricesExport.php | 2 +- .../Thelia/Tests/ContainerAwareTestCase.php | 78 +++++++++++ .../Tests/Controller/ControllerTestBase.php | 63 +-------- .../ImportExport/Export/ExportHandlerTest.php | 124 ++++++++++++++++++ 5 files changed, 247 insertions(+), 60 deletions(-) create mode 100644 core/lib/Thelia/Tests/ContainerAwareTestCase.php create mode 100644 core/lib/Thelia/Tests/ImportExport/Export/ExportHandlerTest.php diff --git a/core/lib/Thelia/ImportExport/Export/ExportHandler.php b/core/lib/Thelia/ImportExport/Export/ExportHandler.php index 948e1cee3..8acf4ddc8 100644 --- a/core/lib/Thelia/ImportExport/Export/ExportHandler.php +++ b/core/lib/Thelia/ImportExport/Export/ExportHandler.php @@ -15,6 +15,7 @@ use Propel\Runtime\ActiveQuery\Criterion\Exception\InvalidValueException; use Propel\Runtime\ActiveQuery\ModelCriteria; use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\Template\Element\BaseLoop; +use Thelia\Core\Template\Element\Exception\LoopException; use Thelia\Core\Translation\Translator; use Thelia\Model\Lang; use Thelia\ImportExport\AbstractHandler; @@ -173,9 +174,46 @@ abstract class ExportHandler extends AbstractHandler return $return; } + public function renderLoop($type, array $args = array()) + { + $loopsDefinition = $this->container->getParameter("thelia.parser.loops"); + + if (!isset($loopsDefinition[$type])) { + throw new LoopException( + Translator::getInstance()->trans( + "The loop \"%loop\" doesn't exist", + [ + "%loop" => $type + ] + ) + ); + } + + $reflection = new \ReflectionClass($loopsDefinition[$type]); + + if (!$reflection->isSubclassOf("Thelia\\Core\\Template\\Element\\BaseLoop")) { + throw new LoopException( + Translator::getInstance()->trans( + "The class \"%class\" must be a subclass of %baseClass", + [ + "%class" => $loopsDefinition[$type], + "%baseClass" => "Thelia\\Core\\Template\\Element\\BaseLoop", + ] + ) + ); + } + + /** @var BaseLoop $loopInstance */ + $loopInstance = $reflection->newInstance($this->container); + + $loopInstance->initializeArgs($args); + + return $loopInstance; + } + /** * @param Lang $lang * @return ModelCriteria|array|BaseLoop */ - abstract protected function buildDataSet(Lang $lang); + abstract public function buildDataSet(Lang $lang); } \ No newline at end of file diff --git a/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php b/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php index 7fd82b505..059f5544f 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php @@ -60,7 +60,7 @@ class ProductPricesExport extends ExportHandler * @param Lang $lang * @return FormatterData */ - protected function buildDataSet(Lang $lang) + public function buildDataSet(Lang $lang) { $locale = $lang->getLocale(); diff --git a/core/lib/Thelia/Tests/ContainerAwareTestCase.php b/core/lib/Thelia/Tests/ContainerAwareTestCase.php new file mode 100644 index 000000000..0a5c08f7b --- /dev/null +++ b/core/lib/Thelia/Tests/ContainerAwareTestCase.php @@ -0,0 +1,78 @@ + + */ +abstract class ContainerAwareTestCase extends \PHPUnit_Framework_TestCase +{ + protected $import; + + /** @var ContainerInterface */ + protected $container; + + /** @var Session */ + protected $session; + + 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); + + $request = new Request(); + $request->setSession($this->session); + + $container->set("request", $request); + + $container->set("thelia.securitycontext", new SecurityContext($request)); + + $this->buildContainer($container); + + return $container; + } + + public function getSession() + { + return new Session(); + } + + public function setUp() + { + + Tlog::getNewInstance(); + + $this->session = $this->getSession(); + $this->container = $this->getContainer(); + + } + + /** + * Use this method to build the container with the services that you need. + */ + abstract protected function buildContainer(ContainerBuilder $container); +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Controller/ControllerTestBase.php b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php index b0052f1e0..c81c428ac 100644 --- a/core/lib/Thelia/Tests/Controller/ControllerTestBase.php +++ b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php @@ -11,72 +11,24 @@ /*************************************************************************************/ 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; -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; +use Thelia\Controller\BaseController; +use Thelia\Tests\ContainerAwareTestCase; /** * Class ControllerTestBase * @package Thelia\Tests\ImportExport\Import * @author Benjamin Perche */ -abstract class ControllerTestBase extends \PHPUnit_Framework_TestCase +abstract class ControllerTestBase extends ContainerAwareTestCase { - protected $import; - - /** @var ContainerInterface */ - protected $container; - - /** @var Session */ - protected $session; - - /** @var ImportController */ + /** @var BaseController */ 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); - - $request = new Request(); - $request->setSession($this->session); - - $container->set("request", $request); - - $this->buildContainer($container); - - return $container; - } - - public function getSession() - { - return new Session(); - } - public function setUp() { + parent::setUp(); - Tlog::getNewInstance(); - - $this->session = $this->getSession(); - $this->container = $this->getContainer(); $this->controller = $this->getController(); $this->controller->setContainer($this->container); @@ -87,9 +39,4 @@ abstract class ControllerTestBase extends \PHPUnit_Framework_TestCase */ abstract protected function getController(); - /** - * Use this method to build the container with the services that you need. - */ - abstract protected function buildContainer(ContainerBuilder $container); - } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/ImportExport/Export/ExportHandlerTest.php b/core/lib/Thelia/Tests/ImportExport/Export/ExportHandlerTest.php new file mode 100644 index 000000000..e08fe32ad --- /dev/null +++ b/core/lib/Thelia/Tests/ImportExport/Export/ExportHandlerTest.php @@ -0,0 +1,124 @@ + + */ +class ExportHandlerTest extends ContainerAwareTestCase +{ + /** @var \Thelia\ImportExport\Export\ExportHandler */ + protected $handler; + + /** + * Use this method to build the container with the services that you need. + */ + protected function buildContainer(ContainerBuilder $container) + { + $container->setParameter( + "Thelia.parser.loops", [ + "address" => "Thelia\\Core\\Template\\Loop\\Address", + ] + ); + } + + + public function setUp() + { + parent::setUp(); + + + $this->handler = $this->getMock( + "Thelia\\ImportExport\\Export\\ExportHandler", + [ + "getHandledTypes", + "buildDataSet" + ], + [ + $this->container + ] + ); + + $this->handler->expects($this->any()) + ->method("getHandledTypes") + ->willReturn([FormatType::TABLE, FormatType::UNBOUNDED]) + ; + } + + public function testRenderLoop() + { + $customerId = CustomerQuery::create() + ->findOne() + ->getId(); + + $this->handler + ->expects($this->any()) + ->method("buildDataSet") + ->willReturn($this->handler->renderLoop("address", ["customer"=>$customerId])) + ; + + $lang = Lang::getDefaultLanguage(); + + $loop = $this->handler->buildDataSet($lang); + + $this->assertInstanceOf( + "Thelia\\Core\\Template\\Loop\\Address", + $loop + ); + + $data = $this->handler->buildData($lang); + + $addresses = AddressQuery::create() + ->filterByCustomerId($customerId) + ->find() + ->toArray("Id") + ; + + foreach ($data->getData() as $row) { + $this->assertArrayHasKey("id", $row); + + $this->assertArrayHasKey($row["id"], $addresses); + + $this->assertEquals(count($addresses), $row["loop_total"]); + + $address = $addresses[$row["id"]]; + + $this->assertEquals($row["address1"], $address["Address1"]); + $this->assertEquals($row["address2"], $address["Address2"]); + $this->assertEquals($row["address3"], $address["Address3"]); + $this->assertEquals($row["cellphone"], $address["Cellphone"]); + $this->assertEquals($row["city"], $address["City"]); + $this->assertEquals($row["company"], $address["Company"]); + $this->assertEquals($row["country"], $address["CountryId"]); + $this->assertEquals($row["create_date"], $address["CreatedAt"]); + $this->assertEquals($row["update_date"], $address["UpdatedAt"]); + $this->assertEquals($row["firstname"], $address["Firstname"]); + $this->assertEquals($row["lastname"], $address["Lastname"]); + $this->assertEquals($row["id"], $address["Id"]); + $this->assertEquals($row["label"], $address["Label"]); + $this->assertEquals($row["phone"], $address["Phone"]); + $this->assertEquals($row["title"], $address["TitleId"]); + $this->assertEquals($row["zipcode"], $address["Zipcode"]); + } + + } +} \ No newline at end of file