Add ContainerAwareTestCase and ExportHandlerTest

modifié:         core/lib/Thelia/ImportExport/Export/ExportHandler.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php
	nouveau fichier: core/lib/Thelia/Tests/ContainerAwareTestCase.php
	modifié:         core/lib/Thelia/Tests/Controller/ControllerTestBase.php
	nouveau fichier: core/lib/Thelia/Tests/ImportExport/Export/ExportHandlerTest.php
This commit is contained in:
Benjamin Perche
2014-07-25 10:19:52 +02:00
parent f61006912d
commit f891d8b788
5 changed files with 247 additions and 60 deletions

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -0,0 +1,78 @@
<?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;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\Security\SecurityContext;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
/**
* Class ContainerAwareTestCase
* @package Thelia\Tests
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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);
}

View File

@@ -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 <bperche@openstudio.fr>
*/
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);
}

View File

@@ -0,0 +1,124 @@
<?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\Export;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Thelia\Core\FileFormat\FormatType;
use Thelia\Model\AddressQuery;
use Thelia\Model\CustomerQuery;
use Thelia\Model\Lang;
use Thelia\Model\Map\AddressTableMap;
use Thelia\Tests\ContainerAwareTestCase;
/**
* Class ExportHandlerTest
* @package Thelia\Tests\ImportExport\Export
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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"]);
}
}
}