Skip to content

Commit

Permalink
// Unit-testing with UrlGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
xGouley authored and kelu95 committed Oct 12, 2015
1 parent f7c04bc commit 3380463
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 41 deletions.
32 changes: 24 additions & 8 deletions Adapter/Admin/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use PrestaShop\PrestaShop\Adapter\Adapter_LegacyContext;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Symfony\Component\Routing\RequestContext;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Symfony\Component\Routing\Route;

/**
* This UrlGeneratorInterface implementation (in a Sf service) will provides Legacy URLs.
Expand Down Expand Up @@ -76,8 +76,27 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
// Base path contains admin directory ('admin-dev' or random 'admin-xxxx')
$basePath = $this->legacyContext->getAdminBaseUrl();

// Try to get controller & parameters with mapping options
$route = $this->router->getRouteCollection()->get($name);
// resolve route & legacy mapping
list($legacyController, $legacyParameters) = $this->getLegacyOptions($name, $parameters);

return $basePath.$this->legacyContext->getAdminLink($legacyController, true, $legacyParameters);
}

/**
* Try to get controller & parameters with mapping options.
*
* If failed to find options, then return the input values.
*
* @param string $routeName
* @param string[] $parameters The route parameters to convert
* @return array[] An array with: the legacy controller name, then the parameters array
*/
final public function getLegacyOptions($routeName, $parameters = array())
{
$legacyController = $routeName;
$legacyParameters = $parameters;

$route = $this->router->getRouteCollection()->get($routeName);
if ($route) {
if ($route->hasOption('_legacy_controller')) {
$legacyController = $route->getOption('_legacy_controller');
Expand All @@ -89,11 +108,8 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
}
}
}

return $basePath.$this->legacyContext->getAdminLink($legacyController, true, $legacyParameters);
return array($legacyController, $legacyParameters);
}

// TODO !4: TU

/* (non-PHPdoc)
* @see \Symfony\Component\Routing\RequestContextAwareInterface::setContext()
Expand Down
27 changes: 0 additions & 27 deletions Adapter/LegacyContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace PrestaShop\PrestaShop\Adapter;

use PrestaShop\PrestaShop\Core\Foundation\Exception\DevelopmentErrorException;
use PrestaShop\PrestaShop\Core\Business\Context as NewContext;
use \Context as OldContext;

/**
Expand Down Expand Up @@ -54,32 +53,6 @@ public function getContext()
return $legacyContext;
}

/**
* Will merge the legacy Context values inside the new Context object.
*
* @param NewContext $newContext
*/
public function mergeContextWithLegacy(NewContext &$newContext)
{
$legacyContext = $this->getContext();

// inject from Legacy to new Context
foreach (get_object_vars($legacyContext) as $key => $value) { // gets public attributes.
if ($value !== null) {
$newContext->$key = $value;
}
}

// simulate a controller object in legacy Context
$legacyContext->controller = new stdClass();
$legacyContext->controller->controller_type = 'unknown';

// add a default currency
if (!$legacyContext->currency) {
$legacyContext->currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
}
}

/**
* Gets the Admin base url (actually random directory name).
* @return string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ public function testCatalog()
$client = static::createClient();

$crawler = $client->request('GET', '/product/catalog');
$this->assertEquals(200, $client->getResponse()->getStatusCode());

$crawler = $client->request('POST', '/product/catalog');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
// TODO !9 : add check of HTML listing table, OR empty case
}

public function testCatalogParams()
{
$client = static::createClient();

$crawler = $client->request('GET', '/product/catalog/1/2/id_product/asc');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
//$this->assertContains('Welcome to Symfony', $crawler->filter('#container h1')->text());

$crawler = $client->request('GET', '/product/catalog/a/b/toto/titi');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
}
19 changes: 18 additions & 1 deletion tests/TestCase/UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace PrestaShop\PrestaShop\Tests\TestCase;
namespace PrestaShop\PrestaShop\tests\TestCase;

use Cache;
use Configuration;
Expand All @@ -12,6 +12,7 @@
use PrestaShop\PrestaShop\Tests\Fake\FakeConfiguration;
use PrestaShop\PrestaShop\Tests\Helper\Mocks\FakeEntityMapper;
use Phake;
use Symfony\Component\HttpKernel\Kernel;

class UnitTestCase extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -40,6 +41,11 @@ class UnitTestCase extends PHPUnit_Framework_TestCase
*/
public $cache;

/**
* @var Kernel
*/
public $sfKernel;

public function setupDatabaseMock()
{
$this->database = Phake::mock('Db');
Expand Down Expand Up @@ -78,6 +84,17 @@ public function setConfiguration(array $keys)
return $fakeConfiguration;
}

public function setupSfKernel()
{
// Prepare Symfony kernel to resolve route.
$loader = require_once __DIR__.'/../../app/bootstrap.php.cache';
require_once __DIR__.'/../../app/AppKernel.php';
$this->sfKernel = new \AppKernel('test', true);
$this->sfKernel->loadClassCache();
$this->sfKernel->boot();
return $this->sfKernel;
}

public function teardown()
{
Cache::deleteTestingInstance();
Expand Down
62 changes: 62 additions & 0 deletions tests/Unit/Adapter/Admin/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\PrestaShop\tests\Unit\Adapter\Admin;

use Core_Foundation_IoC_Container;
use PrestaShop\PrestaShop\Adapter\Admin\UrlGenerator;
use PrestaShop\PrestaShop\Tests\TestCase\UnitTestCase;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Phake;

class UrlGeneratorTest extends UnitTestCase
{
private $legacyContext;

public function setUp()
{
parent::setUp();

$this->context->language = new \stdClass();
$this->context->language->id = 42;
$this->legacyContext = Phake::partialMock('PrestaShop\\PrestaShop\\Adapter\\LegacyContext');
Phake::when($this->legacyContext)->getAdminBaseUrl()->thenReturn('admin_fake_base');

$this->setupSfKernel();
}

public function test_generate_equivalent_route()
{
$router = $this->sfKernel->getContainer()->get('router');
$generator = new UrlGenerator($this->legacyContext, $router);

// the following route contains a "_legacy" equivalent
list($controller, $parameters) = $generator->getLegacyOptions('admin_product_catalog');
$this->assertEquals('AdminProducts', $controller);
$this->assertCount(0, $parameters);

// TODO !8 : faire un test sur admin_product_form
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Tests\Unit\Core\Foundation\IoC;

use Exception;

use PHPUnit_Framework_TestCase;

use Core_Foundation_Crypto_Hashing as Hashing;

define('_COOKIE_KEY_', '2349123849231-4123');
// FIXME: Defining this here will break all other Unit tests using UnitTestCase class!
//define('_COOKIE_KEY_', '2349123849231-4123');

class Core_Foundation_Crypto_Hashing_Test extends PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 3380463

Please sign in to comment.