Added Tools\URL test case, and a test case superclass for initializing

Tools\URL
This commit is contained in:
franck
2013-09-06 01:55:16 +02:00
parent 07f0f34e5c
commit c8a5734a9e
6 changed files with 279 additions and 36 deletions

View File

@@ -113,7 +113,7 @@
<default key="_controller">Thelia\Controller\Admin\CurrencyController::deleteAction</default>
</route>
<route id="admin.configuration.currencies.delete" path="/admin/configuration/currencies/update-position">
<route id="admin.configuration.currencies.update-position" path="/admin/configuration/currencies/update-position">
<default key="_controller">Thelia\Controller\Admin\CurrencyController::updatePositionAction</default>
</route>

View File

@@ -37,7 +37,7 @@ use Thelia\Tools\URL;
*
* @package Thelia\Tests\Action\ImageTest
*/
class ImageTest extends \PHPUnit_Framework_TestCase
class ImageTest extends \Thelia\Tests\TestCaseWithURLToolSetup
{
protected $request;
@@ -49,27 +49,13 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
$url = new URL($container, 'dev');
$container->set("event_dispatcher", $dispatcher);
$container->set("thelia.url.manager", $dispatcher);
$request = new Request();
$request->setSession($this->session);
$container->set("request", $request);
$router = $this->getMock("Symfony\Component\Routing\Router");
$container->set("router.admin", $router);
$context = $this->getMock("Symfony\Component\Routing\RequestContext");
$context->setHost('localhost');
$context->setPort(80);
$context->setScheme('http');
$context->getBaseUrl('/tagada/tsointsoin/');
$router->setContext($context);
return $container;
}

View File

@@ -34,7 +34,7 @@ use Thelia\Core\HttpFoundation\Session\Session;
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
abstract class BaseLoopTestor extends \PHPUnit_Framework_TestCase
abstract class BaseLoopTestor extends \Thelia\Tests\TestCaseWithURLToolSetup
{
protected $request;
protected $dispatcher;

View File

@@ -0,0 +1,64 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Tests;
/**
* This class provides URL Tool class initialisation
*
* @package Thelia\Tests\TestCaseWithURLSetup
*/
class TestCaseWithURLToolSetup extends \PHPUnit_Framework_TestCase {
public function __construct() {
$this->setupURLTool();
}
protected function setupURLTool() {
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
$context = new \Symfony\Component\Routing\RequestContext(
'/thelia/index.php',
'GET',
'localhost',
'http',
80,
443,
'/path/to/action'
);
$router = $this->getMockBuilder("Symfony\Component\Routing\Router")
->disableOriginalConstructor()
->getMock();
$router->expects($this->any())
->method('getContext')
->will($this->returnValue($context));
$container->set("router.admin", $router);
new \Thelia\Tools\URL($container);
}
}

View File

@@ -28,10 +28,208 @@ use Thelia\Tools\URL;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
* @author Franck Allimant <eroudeix@openstudio.fr>
*
*/
class URLTest extends \PHPUnit_Framework_TestCase
{
protected $context;
public function setUp()
{
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
$router = $this->getMockBuilder("Symfony\Component\Routing\Router")
->disableOriginalConstructor()
->getMock();
$this->context = new \Symfony\Component\Routing\RequestContext(
'/thelia/index.php',
'GET',
'localhost',
'http',
80,
443,
'/path/to/action'
);
$router->expects($this->any())
->method('getContext')
->will($this->returnValue($this->context));
$container->set("router.admin", $router);
new \Thelia\Tools\URL($container);
}
public function testGetIndexPage() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia/index.php', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia/', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/', $url);
}
public function testGetBaseUrl() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/thelia/index.php', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/thelia/', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/', $url);
}
public function testAbsoluteUrl() {
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/path/to/action', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/index.php/path/to/action', $url);
}
public function testAbsoluteUrlOnAbsolutePath() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
}
public function testAbsoluteUrlOnAbsolutePathWithParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlOnAbsolutePathWithParametersAddParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlWithParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/index.php/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/path/to/action?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlPathOnly() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array(), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
}
public function testAbsoluteUrlPathOnlyWithParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/path/to/action?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlFromIndexWithParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/index.php/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlPathOnlyFromIndexWithParameters() {
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/?p1=v1&p2=v2', $url);
}
public function testRetrieve()
{

View File

@@ -27,33 +27,30 @@ use Thelia\Model\ConfigQuery;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Rewriting\RewritingRetriever;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Thelia\Core\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RequestContext;
class URL
{
protected $resolver = null;
protected $retriever = null;
protected $container;
protected $environment;
protected $requestContext;
const PATH_TO_FILE = true;
const WITH_INDEX_PAGE = false;
private static $instance = null;
protected static $instance = null;
public function __construct(ContainerInterface $container, $environment)
public function __construct(ContainerInterface $container)
{
// Allow singleton style calls once intanciated.
// For this to work, the URL service has to be instanciated very early. This is done manually
// in TheliaHttpKernel, by calling $this->container->get('thelia.url.manager');
self::$instance = $this;
$this->container = $container;
$this->environment = $environment;
$this->requestContext = $container->get('router.admin')->getContext();
$this->retriever = new RewritingRetriever();
$this->resolver = new RewritingResolver();
@@ -79,28 +76,26 @@ class URL
*/
public function getBaseUrl()
{
$requestContext = $this->container->get('router.admin')->getContext();
if ($host = $this->requestContext->getHost()) {
if ($host = $requestContext->getHost()) {
$scheme = $requestContext->getScheme();
$scheme = $this->requestContext->getScheme();
$port = '';
if ('http' === $scheme && 80 != $requestContext->getHttpPort()) {
$port = ':'.$requestContext->getHttpPort();
} elseif ('https' === $scheme && 443 != $requestContext->getHttpsPort()) {
$port = ':'.$requestContext->getHttpsPort();
if ('http' === $scheme && 80 != $this->requestContext->getHttpPort()) {
$port = ':'.$this->requestContext->getHttpPort();
} elseif ('https' === $scheme && 443 != $this->requestContext->getHttpsPort()) {
$port = ':'.$this->requestContext->getHttpsPort();
}
$schemeAuthority = "$scheme://$host"."$port";
}
return $schemeAuthority.$requestContext->getBaseUrl().'/';
return $schemeAuthority.$this->requestContext->getBaseUrl();
}
/**
* @return string the index page, which is basically the base_url in prod environment.
* @return string the index page, which is in fact the base URL.
*/
public function getIndexPage()
{
@@ -133,7 +128,7 @@ class URL
}
// Normalize the given path
$base = $base_url . ltrim($path, '/');
$base = rtrim($base_url, '/') . '/' . ltrim($path, '/');
} else
$base = $path;