From a731873ff375013f3fa395fc6066dc0203bc499b Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 21 Jul 2014 09:44:28 +0200 Subject: [PATCH] Refactor tests to add abstract class for controller tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit modifié: core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php renommé: core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php -> core/lib/Thelia/Tests/Controller/ControllerTestBase.php nouveau fichier: core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php modifié: core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php --- .../Import/Type/ProductStockImport.php | 2 +- .../ControllerTestBase.php} | 34 ++++++----- .../Controller/ImportExportControllerTest.php | 56 +++++++++++++++++++ .../Import/ProductStockImportTest.php | 7 +-- 4 files changed, 76 insertions(+), 23 deletions(-) rename core/lib/Thelia/Tests/{ImportExport/Import/ImportTestBase.php => Controller/ControllerTestBase.php} (78%) create mode 100644 core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php index fdc0e20a4..de5e38999 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php @@ -19,7 +19,7 @@ use Thelia\ImportExport\Import\ImportHandler; use Thelia\Model\ProductSaleElementsQuery; /** - * Class ImportTestBase + * Class ControllerTestBase * @package Thelia\ImportExport\Import * @author Benjamin Perche */ diff --git a/core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php similarity index 78% rename from core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php rename to core/lib/Thelia/Tests/Controller/ControllerTestBase.php index 1f317f528..ab596a1fb 100644 --- a/core/lib/Thelia/Tests/ImportExport/Import/ImportTestBase.php +++ b/core/lib/Thelia/Tests/Controller/ControllerTestBase.php @@ -10,8 +10,9 @@ /* file that was distributed with this source code. */ /*************************************************************************************/ -namespace Thelia\Tests\ImportExport\Import; +namespace Thelia\Tests\Controller; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Thelia\Controller\Admin\ImportController; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarArchiveBuilder; use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarBz2ArchiveBuilder; @@ -27,11 +28,11 @@ use Thelia\Core\Translation\Translator; use Thelia\Log\Tlog; /** - * Class ImportTestBase + * Class ControllerTestBase * @package Thelia\Tests\ImportExport\Import * @author Benjamin Perche */ -class ImportTestBase extends \PHPUnit_Framework_TestCase +abstract class ControllerTestBase extends \PHPUnit_Framework_TestCase { protected $import; @@ -50,20 +51,7 @@ class ImportTestBase extends \PHPUnit_Framework_TestCase $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); + $this->buildContainer($container); $request = new Request(); $request->setSession($this->session); @@ -85,9 +73,19 @@ class ImportTestBase extends \PHPUnit_Framework_TestCase $this->session = $this->getSession(); $this->container = $this->getContainer(); - $this->controller = new ImportController(); + $this->controller = $this->getController(); $this->controller->setContainer($this->container); } + /** + * @return \Thelia\Controller\BaseController The controller you want to test + */ + abstract function getController(); + + /** + * Use this method to build the container with the services that you need. + */ + abstract function buildContainer(ContainerBuilder $container); + } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php b/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php new file mode 100644 index 000000000..d1af6900e --- /dev/null +++ b/core/lib/Thelia/Tests/Controller/ImportExportControllerTest.php @@ -0,0 +1,56 @@ + + */ +abstract class ControllerExportControllerTest extends ControllerTestBase +{ + + /** + * @return mixed + */ + function buildContainer(ContainerBuilder $container) + { + $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()) + ->add(new CSVFormatter()) + ; + + $container->set("thelia.manager.formatter_manager", $archiveBuilderManager); + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php b/core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php index eaa6e19aa..6ce75c9d6 100644 --- a/core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php +++ b/core/lib/Thelia/Tests/ImportExport/Import/ProductStockImportTest.php @@ -14,15 +14,14 @@ namespace Thelia\Tests\ImportExport\Import; use Thelia\Core\FileFormat\Formatting\Formatter\JsonFormatter; use Thelia\ImportExport\Import\Type\ProductStockImport; use Thelia\Model\ProductSaleElementsQuery; +use Thelia\Tests\Controller\ControllerTestBase; /** - * Class ProductStockImportTest + * Class ProductStockControllerTest * @package Thelia\Tests\ImportExport\Import * @author Benjamin Perche - * - * This class tests the import controller too */ -class ProductStockImportTest extends ImportTestBase +class ProductStockControllerTest extends ControllerTestBase { public function setUp() {