Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\CacheWarmer;
use Symfony\Bundle\AsseticBundle\CacheWarmer\AssetManagerCacheWarmer;
class AssetManagerCacheWarmerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
}
public function testWarmUp()
{
$am = $this
->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
->disableOriginalConstructor()
->getMock()
;
$am->expects($this->once())->method('load');
$container = $this
->getMockBuilder('Symfony\\Component\\DependencyInjection\\Container')
->setConstructorArgs(array())
->getMock()
;
$container
->expects($this->once())
->method('get')
->with('assetic.asset_manager')
->will($this->returnValue($am))
;
$warmer = new AssetManagerCacheWarmer($container);
$warmer->warmUp('/path/to/cache');
}
}

View File

@@ -0,0 +1,202 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Command;
use Symfony\Bundle\AsseticBundle\Command\DumpCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
class DumpCommandTest extends \PHPUnit_Framework_TestCase
{
private $writeTo;
private $application;
private $definition;
private $kernel;
private $container;
private $am;
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
$this->writeTo = sys_get_temp_dir().'/assetic_dump';
$this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
->disableOriginalConstructor()
->getMock();
$this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
->disableOriginalConstructor()
->getMock();
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
$this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
$this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
->disableOriginalConstructor()
->getMock();
$this->application->expects($this->any())
->method('getDefinition')
->will($this->returnValue($this->definition));
$this->definition->expects($this->any())
->method('getArguments')
->will($this->returnValue(array()));
$this->definition->expects($this->any())
->method('getOptions')
->will($this->returnValue(array(
new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages.'),
new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'),
new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
)));
$this->application->expects($this->any())
->method('getKernel')
->will($this->returnValue($this->kernel));
$this->application->expects($this->once())
->method('getHelperSet')
->will($this->returnValue($this->helperSet));
$this->kernel->expects($this->any())
->method('getContainer')
->will($this->returnValue($this->container));
$writeTo = $this->writeTo;
$this->container->expects($this->any())
->method('getParameter')
->will($this->returnCallback(function($p) use($writeTo) {
if ('assetic.write_to' === $p) {
return $writeTo;
} else if ('assetic.variables' === $p) {
return array();
}
throw new \RuntimeException(sprintf('Unknown parameter "%s".', $p));
}));
$this->container->expects($this->once())
->method('get')
->with('assetic.asset_manager')
->will($this->returnValue($this->am));
$this->command = new DumpCommand();
$this->command->setApplication($this->application);
}
protected function tearDown()
{
if (is_dir($this->writeTo)) {
array_map('unlink', glob($this->writeTo.'/*'));
rmdir($this->writeTo);
}
}
public function testEmptyAssetManager()
{
$this->am->expects($this->once())
->method('getNames')
->will($this->returnValue(array()));
$this->command->run(new ArrayInput(array()), new NullOutput());
}
public function testDumpOne()
{
$asset = $this->getMock('Assetic\\Asset\\AssetInterface');
$this->am->expects($this->once())
->method('getNames')
->will($this->returnValue(array('test_asset')));
$this->am->expects($this->once())
->method('get')
->with('test_asset')
->will($this->returnValue($asset));
$this->am->expects($this->once())
->method('getFormula')
->with('test_asset')
->will($this->returnValue(array()));
$this->am->expects($this->once())
->method('isDebug')
->will($this->returnValue(false));
$asset->expects($this->once())
->method('getTargetPath')
->will($this->returnValue('test_asset.css'));
$asset->expects($this->once())
->method('dump')
->will($this->returnValue('/* test_asset */'));
$asset->expects($this->any())
->method('getVars')
->will($this->returnValue(array()));
$asset->expects($this->any())
->method('getValues')
->will($this->returnValue(array()));
$this->command->run(new ArrayInput(array()), new NullOutput());
$this->assertFileExists($this->writeTo.'/test_asset.css');
$this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
}
public function testDumpDebug()
{
$asset = $this->getMock('Assetic\\Asset\\AssetCollection');
$leaf = $this->getMock('Assetic\\Asset\\AssetInterface');
$this->am->expects($this->once())
->method('getNames')
->will($this->returnValue(array('test_asset')));
$this->am->expects($this->once())
->method('get')
->with('test_asset')
->will($this->returnValue($asset));
$this->am->expects($this->once())
->method('getFormula')
->with('test_asset')
->will($this->returnValue(array()));
$this->am->expects($this->once())
->method('isDebug')
->will($this->returnValue(true));
$asset->expects($this->once())
->method('getTargetPath')
->will($this->returnValue('test_asset.css'));
$asset->expects($this->once())
->method('dump')
->will($this->returnValue('/* test_asset */'));
$asset->expects($this->once())
->method('getIterator')
->will($this->returnValue(new \ArrayIterator(array($leaf))));
$asset->expects($this->any())
->method('getVars')
->will($this->returnValue(array()));
$asset->expects($this->any())
->method('getValues')
->will($this->returnValue(array()));
$leaf->expects($this->once())
->method('getTargetPath')
->will($this->returnValue('test_leaf.css'));
$leaf->expects($this->once())
->method('dump')
->will($this->returnValue('/* test_leaf */'));
$leaf->expects($this->any())
->method('getVars')
->will($this->returnValue(array()));
$leaf->expects($this->any())
->method('getValues')
->will($this->returnValue(array()));
$this->command->run(new ArrayInput(array()), new NullOutput());
$this->assertFileExists($this->writeTo.'/test_asset.css');
$this->assertFileExists($this->writeTo.'/test_leaf.css');
$this->assertEquals('/* test_asset */', file_get_contents($this->writeTo.'/test_asset.css'));
$this->assertEquals('/* test_leaf */', file_get_contents($this->writeTo.'/test_leaf.css'));
}
}

View File

@@ -0,0 +1,167 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Controller;
use Symfony\Bundle\AsseticBundle\Controller\AsseticController;
class AsseticControllerTest extends \PHPUnit_Framework_TestCase
{
protected $request;
protected $headers;
protected $am;
protected $cache;
protected $controller;
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
$this->request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request');
$this->headers = $this->getMock('Symfony\\Component\\HttpFoundation\\ParameterBag');
$this->request->headers = $this->headers;
$this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
->disableOriginalConstructor()
->getMock();
$this->cache = $this->getMock('Assetic\\Cache\\CacheInterface');
$this->controller = new AsseticController($this->request, $this->am, $this->cache);
}
public function testRenderNotFound()
{
$this->setExpectedException('Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException');
$name = 'foo';
$this->am->expects($this->once())
->method('has')
->with($name)
->will($this->returnValue(false));
$this->controller->render($name);
}
public function testRenderLastModifiedFresh()
{
$asset = $this->getMock('Assetic\\Asset\\AssetInterface');
$name = 'foo';
$lastModified = strtotime('2010-10-10 10:10:10');
$ifModifiedSince = gmdate('D, d M Y H:i:s', $lastModified).' GMT';
$asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
$this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
$this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
$asset->expects($this->once())->method('getLastModified')->will($this->returnValue($lastModified));
$this->headers->expects($this->once())->method('get')->with('If-Modified-Since')->will($this->returnValue($ifModifiedSince));
$asset->expects($this->never())
->method('dump');
$response = $this->controller->render($name);
$this->assertEquals(304, $response->getStatusCode(), '->render() sends a Not Modified response when If-Modified-Since is fresh');
}
public function testRenderLastModifiedStale()
{
$asset = $this->getMock('Assetic\\Asset\\AssetInterface');
$name = 'foo';
$content = '==ASSET_CONTENT==';
$lastModified = strtotime('2010-10-10 10:10:10');
$ifModifiedSince = gmdate('D, d M Y H:i:s', $lastModified - 300).' GMT';
$asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
$this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
$this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
$asset->expects($this->exactly(2))->method('getLastModified')->will($this->returnValue($lastModified));
$this->headers->expects($this->once())->method('get')->with('If-Modified-Since')->will($this->returnValue($ifModifiedSince));
$this->cache->expects($this->once())
->method('has')
->with($this->isType('string'))
->will($this->returnValue(false));
$asset->expects($this->once())
->method('dump')
->will($this->returnValue($content));
$response = $this->controller->render($name);
$this->assertEquals(200, $response->getStatusCode(), '->render() sends an OK response when If-Modified-Since is stale');
$this->assertEquals($content, $response->getContent(), '->render() sends the dumped asset as the response content');
}
public function testRenderETagFresh()
{
$asset = $this->getMock('Assetic\\Asset\\AssetInterface');
$name = 'foo';
$formula = array(array('js/core.js'), array(), array(''));
$etag = md5(serialize($formula + array('last_modified' => null)));
$asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
$this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
$this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
$this->am->expects($this->once())
->method('hasFormula')
->with($name)
->will($this->returnValue(true));
$this->am->expects($this->once())
->method('getFormula')
->with($name)
->will($this->returnValue($formula));
$this->request->expects($this->once())
->method('getETags')
->will($this->returnValue(array('"'.$etag.'"')));
$asset->expects($this->never())
->method('dump');
$response = $this->controller->render($name);
$this->assertEquals(304, $response->getStatusCode(), '->render() sends a Not Modified response when If-None-Match is fresh');
}
public function testRenderETagStale()
{
$asset = $this->getMock('Assetic\\Asset\\AssetInterface');
$name = 'foo';
$content = '==ASSET_CONTENT==';
$formula = array(array('js/core.js'), array(), array(''));
$etag = md5(serialize($formula + array('last_modified' => null)));
$asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
$this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
$this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
$this->am->expects($this->once())
->method('hasFormula')
->with($name)
->will($this->returnValue(true));
$this->am->expects($this->once())
->method('getFormula')
->with($name)
->will($this->returnValue($formula));
$this->request->expects($this->once())
->method('getETags')
->will($this->returnValue(array('"123"')));
$asset->expects($this->once())
->method('dump')
->will($this->returnValue($content));
$response = $this->controller->render($name);
$this->assertEquals(200, $response->getStatusCode(), '->render() sends an OK response when If-None-Match is stale');
$this->assertEquals($content, $response->getContent(), '->render() sends the dumped asset as the response content');
}
}

View File

@@ -0,0 +1,215 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\DependencyInjection;
use Symfony\Bundle\AsseticBundle\DependencyInjection\AsseticExtension;
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckYuiFilterPass;
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckClosureFilterPass;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\HttpFoundation\Request;
class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
{
private $kernel;
private $container;
static public function assertSaneContainer(Container $container, $message = '')
{
$errors = array();
foreach ($container->getServiceIds() as $id) {
try {
$container->get($id);
} catch (\Exception $e) {
$errors[$id] = $e->getMessage();
}
}
self::assertEquals(array(), $errors, $message);
}
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
$this->container = new ContainerBuilder();
$this->container->addScope(new Scope('request'));
$this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request');
$this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper'));
$this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper'))
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Routing\\RouterInterface')));
$this->container->register('twig', 'Twig_Environment');
$this->container->setParameter('kernel.bundles', array());
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
$this->container->setParameter('kernel.root_dir', __DIR__);
$this->container->setParameter('kernel.charset', 'UTF-8');
$this->container->set('kernel', $this->kernel);
}
/**
* @dataProvider getDebugModes
*/
public function testDefaultConfig($debug)
{
$this->container->setParameter('kernel.debug', $debug);
$extension = new AsseticExtension();
$extension->load(array(array()), $this->container);
$this->assertFalse($this->container->has('assetic.filter.yui_css'), '->load() does not load the yui_css filter when a yui value is not provided');
$this->assertFalse($this->container->has('assetic.filter.yui_js'), '->load() does not load the yui_js filter when a yui value is not provided');
$this->assertSaneContainer($this->getDumpedContainer());
}
public function getDebugModes()
{
return array(
array(true),
array(false),
);
}
/**
* @dataProvider getFilterNames
*/
public function testFilterConfigs($name, $config = array())
{
$extension = new AsseticExtension();
$extension->load(array(array('filters' => array($name => $config))), $this->container);
$this->assertSaneContainer($this->getDumpedContainer());
}
public function getFilterNames()
{
return array(
array('closure', array('jar' => '/path/to/closure.jar')),
array('coffee'),
array('compass'),
array('cssembed', array('jar' => '/path/to/cssembed.jar')),
array('cssimport'),
array('cssrewrite'),
array('jpegoptim'),
array('jpegtran'),
array('less'),
array('lessphp'),
array('optipng'),
array('packager'),
array('pngout'),
array('sass'),
array('scss'),
array('sprockets', array('include_dirs' => array('foo'))),
array('stylus'),
array('yui_css', array('jar' => '/path/to/yuicompressor.jar')),
array('yui_js', array('jar' => '/path/to/yuicompressor.jar')),
);
}
/**
* @dataProvider getUseControllerKeys
*/
public function testUseController($bool, $includes, $omits)
{
$extension = new AsseticExtension();
$extension->load(array(array('use_controller' => $bool)), $this->container);
foreach ($includes as $id) {
$this->assertTrue($this->container->has($id), '"'.$id.'" is registered when use_controller is '.$bool);
}
foreach ($omits as $id) {
$this->assertFalse($this->container->has($id), '"'.$id.'" is not registered when use_controller is '.$bool);
}
$this->assertSaneContainer($this->getDumpedContainer());
}
public function getUseControllerKeys()
{
return array(
array(true, array('assetic.routing_loader', 'assetic.controller'), array()),
array(false, array(), array('assetic.routing_loader', 'assetic.controller')),
);
}
/**
* @dataProvider getClosureJarAndExpected
*/
public function testClosureCompilerPass($jar, $expected)
{
$this->container->addCompilerPass(new CheckClosureFilterPass());
$extension = new AsseticExtension();
$extension->load(array(array(
'filters' => array(
'closure' => array('jar' => $jar),
),
)), $this->container);
$container = $this->getDumpedContainer();
$this->assertSaneContainer($container);
$this->assertTrue($this->container->getDefinition($expected)->hasTag('assetic.filter'));
$this->assertNotEmpty($container->getParameter('assetic.filter.closure.java'));
}
public function getClosureJarAndExpected()
{
return array(
array(null, 'assetic.filter.closure.api'),
array('/path/to/closure.jar', 'assetic.filter.closure.jar'),
);
}
public function testInvalidYuiConfig()
{
$this->setExpectedException('RuntimeException', 'assetic.filters.yui_js');
$this->container->addCompilerPass(new CheckYuiFilterPass());
$extension = new AsseticExtension();
$extension->load(array(array(
'filters' => array(
'yui_js' => array(),
),
)), $this->container);
$this->getDumpedContainer();
}
private function getDumpedContainer()
{
static $i = 0;
$class = 'AsseticExtensionTestContainer'.$i++;
$this->container->compile();
$dumper = new PhpDumper($this->container);
eval('?>'.$dumper->dump(array('class' => $class)));
$container = new $class();
$container->enterScope('request');
$container->set('request', Request::create('/'));
$container->set('kernel', $this->kernel);
return $container;
}
}

View File

@@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Factory;
use Symfony\Bundle\AsseticBundle\Factory\AssetFactory;
class AssetFactoryTest extends \PHPUnit_Framework_TestCase
{
protected $kernel;
protected $factory;
protected $container;
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
$this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->parameterBag = $this->getMock('Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBagInterface');
$this->factory = new AssetFactory($this->kernel, $this->container, $this->parameterBag, '/path/to/web');
}
public function testBundleNotation()
{
$input = '@MyBundle/Resources/css/main.css';
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
$this->parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnCallback(function($v) { return $v; }));
$this->kernel->expects($this->once())
->method('getBundle')
->with('MyBundle')
->will($this->returnValue($bundle));
$this->kernel->expects($this->once())
->method('locateResource')
->with($input)
->will($this->returnValue('/path/to/MyBundle/Resources/css/main.css'));
$bundle->expects($this->once())
->method('getPath')
->will($this->returnValue('/path/to/MyBundle'));
$coll = $this->factory->createAsset($input)->all();
$asset = $coll[0];
$this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
$this->assertEquals('Resources/css/main.css', $asset->getSourcePath(), '->createAsset() sets the asset path');
}
/**
* @dataProvider getGlobs
*/
public function testBundleGlobNotation($input)
{
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
$this->parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnCallback(function($v) { return $v; }));
$this->kernel->expects($this->once())
->method('getBundle')
->with('MyBundle')
->will($this->returnValue($bundle));
$this->kernel->expects($this->once())
->method('locateResource')
->with('@MyBundle/Resources/css/')
->will($this->returnValue('/path/to/MyBundle/Resources/css/'));
$bundle->expects($this->once())
->method('getPath')
->will($this->returnValue('/path/to/MyBundle'));
$coll = $this->factory->createAsset($input)->all();
$asset = $coll[0];
$this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
$this->assertNull($asset->getSourcePath(), '->createAsset() sets the asset path to null');
}
public function getGlobs()
{
return array(
array('@MyBundle/Resources/css/*'),
array('@MyBundle/Resources/css/*/*.css'),
);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Factory\Resource;
use Symfony\Bundle\AsseticBundle\Factory\Resource\CoalescingDirectoryResource;
class CoalescingDirectoryResourceTest extends \PHPUnit_Framework_TestCase
{
public function testFiltering()
{
$dir1 = $this->getMock('Assetic\\Factory\\Resource\\IteratorResourceInterface');
$file1a = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$file1b = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$dir2 = $this->getMock('Assetic\\Factory\\Resource\\IteratorResourceInterface');
$file2a = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$file2c = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$dir1->expects($this->any())
->method('getIterator')
->will($this->returnValue(new \ArrayIterator(array($file1a, $file1b))));
$file1a->expects($this->any())
->method('__toString')
->will($this->returnValue('FooBundle:Foo:file1.foo.bar'));
$file1b->expects($this->any())
->method('__toString')
->will($this->returnValue('FooBundle:Foo:file2.foo.bar'));
$dir2->expects($this->any())
->method('getIterator')
->will($this->returnValue(new \ArrayIterator(array($file2a, $file2c))));
$file2a->expects($this->any())
->method('__toString')
->will($this->returnValue('BarBundle:Foo:file1.foo.bar'));
$file2c->expects($this->any())
->method('__toString')
->will($this->returnValue('BarBundle:Foo:file3.foo.bar'));
$resource = new CoalescingDirectoryResource(array($dir1, $dir2));
$actual = array();
foreach ($resource as $file) {
$actual[] = (string) $file;
}
$expected = array(
'FooBundle:Foo:file1.foo.bar',
'FooBundle:Foo:file2.foo.bar',
'BarBundle:Foo:file3.foo.bar',
);
$this->assertEquals($expected, $actual);
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Factory\Resource;
use Symfony\Bundle\AsseticBundle\Factory\Resource\FileResource;
class FileResourceTest extends \PHPUnit_Framework_TestCase
{
private $loader;
protected function setUp()
{
$this->loader = $this->getMock('Symfony\\Component\\Templating\\Loader\\LoaderInterface');
}
public function testCastAsString()
{
$baseDir = '/path/to/MyBundle/Resources/views/';
$resource = new FileResource($this->loader, 'MyBundle', $baseDir, $baseDir.'Section/template.html.twig');
$this->assertEquals('MyBundle:Section:template.html.twig', (string) $resource);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests;
use Symfony\Bundle\AsseticBundle\FilterManager;
class FilterManagerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
}
public function testGet()
{
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$filter = $this->getMock('Assetic\\Filter\\FilterInterface');
$container->expects($this->exactly(2))
->method('get')
->with('assetic.filter.bar')
->will($this->returnValue($filter));
$fm = new FilterManager($container, array('foo' => 'assetic.filter.bar'));
$this->assertSame($filter, $fm->get('foo'), '->get() loads the filter from the container');
$this->assertSame($filter, $fm->get('foo'), '->get() loads the filter from the container');
}
public function testHas()
{
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$fm = new FilterManager($container, array('foo' => 'assetic.filter.bar'));
$this->assertTrue($fm->has('foo'), '->has() returns true for lazily mapped filters');
}
public function testGetNames()
{
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$filter = $this->getMock('Assetic\\Filter\\FilterInterface');
$fm = new FilterManager($container, array('foo' => 'assetic.filter.bar'));
$fm->set('bar', $filter);
$this->assertEquals(array('foo', 'bar'), $fm->getNames(), '->getNames() returns all lazy and normal filter names');
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Filesystem\Filesystem;
/**
* @group functional
*/
class FunctionalTest extends \PHPUnit_Framework_TestCase
{
protected $cacheDir;
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
$this->cacheDir = __DIR__.'/Resources/cache';
if (file_exists($this->cacheDir)) {
$filesystem = new Filesystem();
$filesystem->remove($this->cacheDir);
}
mkdir($this->cacheDir, 0777, true);
}
protected function tearDown()
{
$filesystem = new Filesystem();
$filesystem->remove($this->cacheDir);
}
public function testTwigRenderDebug()
{
$kernel = new TestKernel('test', true);
$kernel->boot();
$container = $kernel->getContainer();
$container->enterScope('request');
$container->set('request', new Request());
$content = $container->get('templating')->render('::layout.html.twig');
$crawler = new Crawler($content);
$this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
$this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
}
public function testPhpRenderDebug()
{
$kernel = new TestKernel('test', true);
$kernel->boot();
$container = $kernel->getContainer();
$container->enterScope('request');
$container->set('request', new Request());
$content = $container->get('templating')->render('::layout.html.php');
$crawler = new Crawler($content);
$this->assertEquals(3, count($crawler->filter('link[href$=".css"]')));
$this->assertEquals(2, count($crawler->filter('script[src$=".js"]')));
}
}

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title><?php $view['slots']->output('title') ?></title>
<?php $view['slots']->output('stylesheets') ?>
</head>
<body>
<?php $view['slots']->output('_content') ?>
<?php $view['slots']->output('javascripts') ?>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>{% block title '' %}</title>
{% block stylesheets '' %}
</head>
<body>
{% block content '' %}
{% block javascripts '' %}
</body>
</html>

View File

@@ -0,0 +1,17 @@
<?php $view->extend('::base.html.php') ?>
<?php $view['slots']->start('stylesheets') ?>
<?php foreach($view['assetic']->stylesheets('stylesheet1.css, stylesheet2.css, @TestBundle/Resources/css/bundle.css') as $url): ?>
<link href="<?php echo $view->escape($url) ?>" type="text/css" rel="stylesheet" />
<?php endforeach; ?>
<?php $view['slots']->stop() ?>
<?php $view['slots']->start('javascripts') ?>
<?php foreach($view['assetic']->javascripts('javascript1.js, javascript2.js') as $url): ?>
<script src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>
<?php $view['slots']->stop() ?>
<?php foreach ($view['assetic']->image('logo.png') as $url): ?>
<img src="<?php echo $view->escape($url) ?>">
<?php endforeach; ?>

View File

@@ -0,0 +1,19 @@
{% extends '::base.html.twig' %}
{% block stylesheets %}
{% stylesheets 'stylesheet1.css' 'stylesheet2.css' '@TestBundle/Resources/css/bundle.css' %}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}
{% endblock %}
{% block javascripts %}
{% javascripts 'javascript1.js' 'javascript2.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
{% block content %}
{% image 'logo.png' %}
<img src="{{ asset_url }}">
{% endimage %}
{% endblock %}

View File

@@ -0,0 +1,40 @@
framework:
charset: UTF-8
secret: xxxxxxxxxx
csrf_protection:
enabled: true
router: { resource: "%kernel.root_dir%/config/routing.yml" }
validation: { enabled: true, enable_annotations: true }
templating: { engines: ['twig', 'php'] }
session:
lifetime: 3600
auto_start: false
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
assetic:
use_controller: true
read_from: "%kernel.root_dir%/web"
bundles: [ TestBundle ]
assets:
jquery: js/jquery.js
app_css:
inputs:
- css/main.css
- css/more.css
- @widget_css
filters: [ ?yui_css ]
output: css/packed/app.css
widget_css:
inputs: css/widget.sass
filters: sass
filters:
sass:
apply_to: "\.sass$"
yui_css:
jar: %kernel.root_dir/java/yui-compressor-2.4.6.jar
twig:
functions:
yui_css: { output: css/*.css }

View File

@@ -0,0 +1,3 @@
_assetic:
resource: .
type: assetic

View File

@@ -0,0 +1 @@
// javascript1.js

View File

@@ -0,0 +1 @@
// javascript2.js

View File

@@ -0,0 +1 @@
/* stylesheet1.css */

View File

@@ -0,0 +1 @@
/* stylesheet2.css */

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\Templating;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\AssetInterface;
use Assetic\Asset\StringAsset;
use Assetic\Factory\AssetFactory;
use Symfony\Bundle\AsseticBundle\Templating\AsseticHelper;
class AsseticHelperTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Assetic\\AssetManager')) {
$this->markTestSkipped('Assetic is not available.');
}
}
/**
* @dataProvider getDebugAndCount
*/
public function testUrls($debug, $count, $message)
{
$helper = new AsseticHelperForTest(new AssetFactory('/foo', $debug), $debug);
$urls = $helper->javascripts(array('js/jquery.js', 'js/jquery.plugin.js'));
$this->assertInstanceOf('Traversable', $urls, '->javascripts() returns an array');
$this->assertEquals($count, count($urls), $message);
}
public function getDebugAndCount()
{
return array(
array(false, 1, '->javascripts() returns one url when not in debug mode'),
array(true, 2, '->javascripts() returns many urls when in debug mode'),
);
}
}
class AsseticHelperForTest extends AsseticHelper
{
protected function getAssetUrl(AssetInterface $asset, $options = array())
{
return $asset->getTargetPath();
}
}

View File

@@ -0,0 +1 @@
/* bundle.css */

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests\TestBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class TestBundle extends Bundle
{
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Tests;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
class TestKernel extends Kernel
{
public function getRootDir()
{
return __DIR__.'/Resources';
}
public function registerBundles()
{
return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Symfony\Bundle\TwigBundle\TwigBundle(),
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
new \Symfony\Bundle\AsseticBundle\Tests\TestBundle\TestBundle(),
);
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/Resources/config/config.yml');
}
}

View File

@@ -0,0 +1,17 @@
<?php
require_once $_SERVER['SYMFONY'].'/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespace('Symfony', $_SERVER['SYMFONY']);
$loader->registerNamespace('Assetic', $_SERVER['ASSETIC']);
$loader->registerPrefix('Twig_', $_SERVER['TWIG']);
$loader->register();
spl_autoload_register(function($class)
{
if (0 === strpos($class, 'Symfony\\Bundle\\AsseticBundle\\') &&
file_exists($file = __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 3)).'.php')) {
require_once $file;
}
});