AsseticControllerTest.php 6.8KB

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