AsseticServiceProviderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace SilexAssetic\Tests;
  3. use Silex\Application;
  4. use SilexAssetic\AsseticServiceProvider;
  5. use Symfony\Component\HttpFoundation\Request;
  6. class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function setUp()
  9. {
  10. if (!class_exists('Assetic\\AssetManager')) {
  11. $this->markTestSkipped('Assetic was not installed.');
  12. }
  13. }
  14. public function testEverythingLoaded()
  15. {
  16. $app = new Application();
  17. $app->register(new AsseticServiceProvider());
  18. $app['assetic.path_to_web'] = sys_get_temp_dir();
  19. $app->get('/', function () use ($app) {
  20. return 'AsseticExtensionTest';
  21. });
  22. $request = Request::create('/');
  23. $response = $app->handle($request);
  24. $this->assertInstanceOf('Assetic\Factory\AssetFactory', $app['assetic']);
  25. $this->assertInstanceOf('Assetic\AssetManager', $app['assetic.asset_manager']);
  26. $this->assertInstanceOf('Assetic\FilterManager', $app['assetic.filter_manager']);
  27. $this->assertInstanceOf('Assetic\AssetWriter', $app['assetic.asset_writer']);
  28. $this->assertInstanceOf('Assetic\Factory\LazyAssetManager', $app['assetic.lazy_asset_manager']);
  29. }
  30. public function testFilterFormRegistration()
  31. {
  32. $app = new Application();
  33. $app->register(new AsseticServiceProvider());
  34. $app['assetic.path_to_web'] = sys_get_temp_dir();
  35. $app['assetic.filter_manager'] = $app->share(
  36. $app->extend('assetic.filter_manager', function($fm, $app) {
  37. $fm->set('test_filter', new \Assetic\Filter\CssMinFilter());
  38. return $fm;
  39. })
  40. );
  41. $app->get('/', function () use ($app) {
  42. return 'AsseticExtensionTest';
  43. });
  44. $request = Request::create('/');
  45. $response = $app->handle($request);
  46. $this->assertTrue($app['assetic.filter_manager']->has('test_filter'));
  47. $this->assertInstanceOf('Assetic\Filter\CssMinFilter', $app['assetic.filter_manager']->get('test_filter'));
  48. }
  49. public function testAssetFormRegistration()
  50. {
  51. $app = new Application();
  52. $app->register(new AsseticServiceProvider());
  53. $app['assetic.path_to_web'] = sys_get_temp_dir();
  54. $app['assetic.asset_manager'] = $app->share(
  55. $app->extend('assetic.asset_manager', function($am, $app) {
  56. $asset = new \Assetic\Asset\FileAsset(__FILE__);
  57. $asset->setTargetPath(md5(__FILE__));
  58. $am->set('test_asset', $asset);
  59. return $am;
  60. })
  61. );
  62. $app->get('/', function () {
  63. return 'AsseticExtensionTest';
  64. });
  65. $request = Request::create('/');
  66. $response = $app->handle($request);
  67. $this->assertTrue($app['assetic.asset_manager']->has('test_asset'));
  68. $this->assertInstanceOf('Assetic\Asset\FileAsset', $app['assetic.asset_manager']->get('test_asset'));
  69. $this->assertTrue(file_exists(sys_get_temp_dir() . '/' . md5(__FILE__)));
  70. }
  71. public function testTwigAddExtension()
  72. {
  73. if (!class_exists('Twig_Environment')) {
  74. $this->markTestSkipped('Twig was not installed.');
  75. }
  76. $app = new Application();
  77. $app['twig'] = $app->share(function () {
  78. return new \Twig_Environment(new \Twig_Loader_String());
  79. });
  80. $app->register(new AsseticServiceProvider());
  81. $app['assetic.path_to_web'] = sys_get_temp_dir();
  82. $this->assertInstanceOf('Assetic\\Extension\\Twig\\AsseticExtension', $app['twig']->getExtension('assetic'));
  83. }
  84. public function tearDown()
  85. {
  86. if (file_exists(sys_get_temp_dir() . '/' . md5(__FILE__))) {
  87. unlink(sys_get_temp_dir() . '/' . md5(__FILE__));
  88. }
  89. }
  90. }