2010-03-18 14:26:21 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-03-18 14:26:21 +00:00
*
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
2010-04-07 02:07:59 +01:00
*
2010-03-18 14:26:21 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Components\Templating\Helper ;
use Symfony\Components\Templating\Helper\AssetsHelper ;
class AssetsHelperTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
{
$helper = new AssetsHelper ( 'foo' , 'http://www.example.com' , 'abcd' );
$this -> assertEquals ( '/foo/' , $helper -> getBasePath (), '__construct() takes a base path as its first argument' );
$this -> assertEquals ( array ( 'http://www.example.com' ), $helper -> getBaseURLs (), '__construct() takes a base URL as its second argument' );
$this -> assertEquals ( 'abcd' , $helper -> getVersion (), '__construct() takes a version as its thrid argument' );
}
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
public function testGetSetBasePath ()
{
$helper = new AssetsHelper ();
$helper -> setBasePath ( 'foo/' );
$this -> assertEquals ( '/foo/' , $helper -> getBasePath (), '->setBasePath() prepends a / if needed' );
$helper -> setBasePath ( '/foo' );
$this -> assertEquals ( '/foo/' , $helper -> getBasePath (), '->setBasePath() appends a / is needed' );
$helper -> setBasePath ( '' );
$this -> assertEquals ( '/' , $helper -> getBasePath (), '->setBasePath() returns / if no base path is defined' );
$helper -> setBasePath ( '0' );
$this -> assertEquals ( '/0/' , $helper -> getBasePath (), '->setBasePath() returns /0/ if 0 is given' );
}
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
public function testGetSetVersion ()
{
$helper = new AssetsHelper ();
$helper -> setVersion ( 'foo' );
$this -> assertEquals ( 'foo' , $helper -> getVersion (), '->setVersion() sets the version' );
}
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
public function testSetGetBaseURLs ()
2010-03-18 14:26:21 +00:00
{
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ();
$helper -> setBaseURLs ( 'http://www.example.com/' );
$this -> assertEquals ( array ( 'http://www.example.com' ), $helper -> getBaseURLs (), '->setBaseURLs() removes the / at the of an absolute base path' );
$helper -> setBaseURLs ( array ( 'http://www1.example.com/' , 'http://www2.example.com/' ));
$URLs = array ();
2010-05-07 15:09:11 +01:00
for ( $i = 0 ; $i < 20 ; $i ++ ) {
2010-05-06 12:25:53 +01:00
$URLs [] = $helper -> getBaseURL ( $i );
}
$URLs = array_values ( array_unique ( $URLs ));
sort ( $URLs );
$this -> assertEquals ( array ( 'http://www1.example.com' , 'http://www2.example.com' ), $URLs , '->getBaseURL() returns a random base URL if several are given' );
$helper -> setBaseURLs ( '' );
$this -> assertEquals ( '' , $helper -> getBaseURL ( 1 ), '->getBaseURL() returns an empty string if no base URL exist' );
2010-03-18 14:26:21 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetUrl ()
{
$helper = new AssetsHelper ();
$this -> assertEquals ( 'http://example.com/foo.js' , $helper -> getUrl ( 'http://example.com/foo.js' ), '->getUrl() does nothing if an absolute URL is given' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ();
$this -> assertEquals ( '/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() appends a / on relative paths' );
$this -> assertEquals ( '/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() does nothing on absolute paths' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( '/foo' );
$this -> assertEquals ( '/foo/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() appends the basePath on relative paths' );
$this -> assertEquals ( '/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() does not append the basePath on absolute paths' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( null , 'http://assets.example.com/' );
$this -> assertEquals ( 'http://assets.example.com/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() prepends the base URL' );
$this -> assertEquals ( 'http://assets.example.com/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() prepends the base URL' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( null , 'http://www.example.com/foo' );
$this -> assertEquals ( 'http://www.example.com/foo/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() prepends the base URL with a path' );
$this -> assertEquals ( 'http://www.example.com/foo/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() prepends the base URL with a path' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( '/foo' , 'http://www.example.com/' );
$this -> assertEquals ( 'http://www.example.com/foo/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() prepends the base URL and the base path if defined' );
$this -> assertEquals ( 'http://www.example.com/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() prepends the base URL but not the base path on absolute paths' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( '/bar' , 'http://www.example.com/foo' );
$this -> assertEquals ( 'http://www.example.com/foo/bar/foo.js' , $helper -> getUrl ( 'foo.js' ), '->getUrl() prepends the base URL and the base path if defined' );
$this -> assertEquals ( 'http://www.example.com/foo/foo.js' , $helper -> getUrl ( '/foo.js' ), '->getUrl() prepends the base URL but not the base path on absolute paths' );
2010-03-18 14:26:21 +00:00
2010-05-06 12:25:53 +01:00
$helper = new AssetsHelper ( '/bar' , 'http://www.example.com/foo' , 'abcd' );
$this -> assertEquals ( 'http://www.example.com/foo/bar/foo.js?abcd' , $helper -> getUrl ( 'foo.js' ), '->getUrl() appends the version if defined' );
}
2010-03-18 14:26:21 +00:00
}