vendor/bundles/Symfony/Bundle/AsseticBundle/Tests/Controller/AsseticControllerTest.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony framework.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * This source file is subject to the MIT license that is bundled
       
     9  * with this source code in the file LICENSE.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\AsseticBundle\Tests\Controller;
       
    13 
       
    14 use Symfony\Bundle\AsseticBundle\Controller\AsseticController;
       
    15 
       
    16 class AsseticControllerTest extends \PHPUnit_Framework_TestCase
       
    17 {
       
    18     protected $request;
       
    19     protected $headers;
       
    20     protected $am;
       
    21     protected $cache;
       
    22 
       
    23     protected $controller;
       
    24 
       
    25     protected function setUp()
       
    26     {
       
    27         if (!class_exists('Assetic\\AssetManager')) {
       
    28             $this->markTestSkipped('Assetic is not available.');
       
    29         }
       
    30 
       
    31         $this->request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request');
       
    32         $this->headers = $this->getMock('Symfony\\Component\\HttpFoundation\\ParameterBag');
       
    33         $this->request->headers = $this->headers;
       
    34         $this->am = $this->getMockBuilder('Assetic\\Factory\\LazyAssetManager')
       
    35             ->disableOriginalConstructor()
       
    36             ->getMock();
       
    37         $this->cache = $this->getMock('Assetic\\Cache\\CacheInterface');
       
    38 
       
    39         $this->controller = new AsseticController($this->request, $this->am, $this->cache);
       
    40     }
       
    41 
       
    42     public function testRenderNotFound()
       
    43     {
       
    44         $this->setExpectedException('Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException');
       
    45 
       
    46         $name = 'foo';
       
    47 
       
    48         $this->am->expects($this->once())
       
    49             ->method('has')
       
    50             ->with($name)
       
    51             ->will($this->returnValue(false));
       
    52 
       
    53         $this->controller->render($name);
       
    54     }
       
    55 
       
    56     public function testRenderLastModifiedFresh()
       
    57     {
       
    58         $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
       
    59 
       
    60         $name = 'foo';
       
    61         $lastModified = strtotime('2010-10-10 10:10:10');
       
    62         $ifModifiedSince = gmdate('D, d M Y H:i:s', $lastModified).' GMT';
       
    63 
       
    64         $asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
       
    65         $this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
       
    66         $this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
       
    67         $asset->expects($this->once())->method('getLastModified')->will($this->returnValue($lastModified));
       
    68         $this->headers->expects($this->once())->method('get')->with('If-Modified-Since')->will($this->returnValue($ifModifiedSince));
       
    69 
       
    70         $asset->expects($this->never())
       
    71             ->method('dump');
       
    72 
       
    73         $response = $this->controller->render($name);
       
    74         $this->assertEquals(304, $response->getStatusCode(), '->render() sends a Not Modified response when If-Modified-Since is fresh');
       
    75     }
       
    76 
       
    77     public function testRenderLastModifiedStale()
       
    78     {
       
    79         $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
       
    80 
       
    81         $name = 'foo';
       
    82         $content = '==ASSET_CONTENT==';
       
    83         $lastModified = strtotime('2010-10-10 10:10:10');
       
    84         $ifModifiedSince = gmdate('D, d M Y H:i:s', $lastModified - 300).' GMT';
       
    85 
       
    86         $asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
       
    87         $this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
       
    88         $this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
       
    89         $asset->expects($this->exactly(2))->method('getLastModified')->will($this->returnValue($lastModified));
       
    90         $this->headers->expects($this->once())->method('get')->with('If-Modified-Since')->will($this->returnValue($ifModifiedSince));
       
    91 
       
    92         $this->cache->expects($this->once())
       
    93             ->method('has')
       
    94             ->with($this->isType('string'))
       
    95             ->will($this->returnValue(false));
       
    96         $asset->expects($this->once())
       
    97             ->method('dump')
       
    98             ->will($this->returnValue($content));
       
    99 
       
   100         $response = $this->controller->render($name);
       
   101         $this->assertEquals(200, $response->getStatusCode(), '->render() sends an OK response when If-Modified-Since is stale');
       
   102         $this->assertEquals($content, $response->getContent(), '->render() sends the dumped asset as the response content');
       
   103     }
       
   104 
       
   105     public function testRenderETagFresh()
       
   106     {
       
   107         $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
       
   108 
       
   109         $name = 'foo';
       
   110         $formula = array(array('js/core.js'), array(), array(''));
       
   111         $etag = md5(serialize($formula + array('last_modified' => null)));
       
   112 
       
   113         $asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
       
   114         $this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
       
   115         $this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
       
   116 
       
   117         $this->am->expects($this->once())
       
   118             ->method('hasFormula')
       
   119             ->with($name)
       
   120             ->will($this->returnValue(true));
       
   121         $this->am->expects($this->once())
       
   122             ->method('getFormula')
       
   123             ->with($name)
       
   124             ->will($this->returnValue($formula));
       
   125         $this->request->expects($this->once())
       
   126             ->method('getETags')
       
   127             ->will($this->returnValue(array('"'.$etag.'"')));
       
   128         $asset->expects($this->never())
       
   129             ->method('dump');
       
   130 
       
   131         $response = $this->controller->render($name);
       
   132         $this->assertEquals(304, $response->getStatusCode(), '->render() sends a Not Modified response when If-None-Match is fresh');
       
   133     }
       
   134 
       
   135     public function testRenderETagStale()
       
   136     {
       
   137         $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
       
   138 
       
   139         $name = 'foo';
       
   140         $content = '==ASSET_CONTENT==';
       
   141         $formula = array(array('js/core.js'), array(), array(''));
       
   142         $etag = md5(serialize($formula + array('last_modified' => null)));
       
   143 
       
   144         $asset->expects($this->any())->method('getFilters')->will($this->returnValue(array()));
       
   145         $this->am->expects($this->once())->method('has')->with($name)->will($this->returnValue(true));
       
   146         $this->am->expects($this->once())->method('get')->with($name)->will($this->returnValue($asset));
       
   147 
       
   148         $this->am->expects($this->once())
       
   149             ->method('hasFormula')
       
   150             ->with($name)
       
   151             ->will($this->returnValue(true));
       
   152         $this->am->expects($this->once())
       
   153             ->method('getFormula')
       
   154             ->with($name)
       
   155             ->will($this->returnValue($formula));
       
   156         $this->request->expects($this->once())
       
   157             ->method('getETags')
       
   158             ->will($this->returnValue(array('"123"')));
       
   159         $asset->expects($this->once())
       
   160             ->method('dump')
       
   161             ->will($this->returnValue($content));
       
   162 
       
   163         $response = $this->controller->render($name);
       
   164         $this->assertEquals(200, $response->getStatusCode(), '->render() sends an OK response when If-None-Match is stale');
       
   165         $this->assertEquals($content, $response->getContent(), '->render() sends the dumped asset as the response content');
       
   166     }
       
   167 }