PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/LoaderTest.php

https://github.com/clauddiu/Laravel3-Blade
PHP | 95 lines | 74 code | 18 blank | 3 comment | 0 complexity | 0efa7c3ba0be209988b953f2d887f5d4 MD5 | raw file
  1. <?php
  2. use Mockery as m;
  3. class LoaderTest extends PHPUnit_Framework_TestCase {
  4. public function tearDown()
  5. {
  6. m::close();
  7. }
  8. /**
  9. * @expectedException Illuminate\Blade\InvalidViewException
  10. */
  11. public function testExceptionsIsThrownForViewsThatDontExist()
  12. {
  13. $loader = $this->getLoader();
  14. $loader->getFilesystem()->shouldReceive('exists')->once()->with(__DIR__.'/foo.blade.php')->andReturn(false);
  15. $loader->getPath('foo');
  16. }
  17. public function testWhenViewIsExpiredItIsCompiled()
  18. {
  19. list($compiler, $files) = $this->getMocks();
  20. $loader = $this->getMock('Illuminate\Blade\Loader', array('isExpired'), array($compiler, $files, __DIR__, __DIR__));
  21. $loader->expects($this->once())->method('isExpired')->will($this->returnValue(true));
  22. $loader->getFilesystem()->shouldReceive('exists')->once()->andReturn(true);
  23. $loader->getFilesystem()->shouldReceive('get')->once()->with(__DIR__.'/foo.blade.php')->andReturn('bar');
  24. $loader->getFilesystem()->shouldReceive('put')->once()->with(__DIR__.'/'.md5(__DIR__.'/foo.blade.php'), 'bar');
  25. $loader->getCompiler()->shouldReceive('compile')->once()->with('bar')->andReturn('bar');
  26. $return = $loader->getPath('foo');
  27. $this->assertEquals(__DIR__.'/'.md5(__DIR__.'/foo.blade.php'), $return);
  28. }
  29. public function testCompilerIsNotCalledWhenTemplateIsntExpired()
  30. {
  31. list($compiler, $files) = $this->getMocks();
  32. $loader = $this->getMock('Illuminate\Blade\Loader', array('isExpired'), array($compiler, $files, __DIR__, __DIR__));
  33. $loader->expects($this->once())->method('isExpired')->will($this->returnValue(false));
  34. $loader->getFilesystem()->shouldReceive('exists')->once()->andReturn(true);
  35. $loader->getCompiler()->shouldReceive('compile')->never();
  36. $this->assertEquals(__DIR__.'/'.md5(__DIR__.'/foo.blade.php'), $loader->getPath('foo'));
  37. }
  38. public function testNamedViewsMayBeCompiled()
  39. {
  40. list($compiler, $files) = $this->getMocks();
  41. $loader = $this->getMock('Illuminate\Blade\Loader', array('isExpired'), array($compiler, $files, __DIR__, __DIR__));
  42. $loader->expects($this->once())->method('isExpired')->will($this->returnValue(true));
  43. $loader->getFilesystem()->shouldReceive('exists')->once()->andReturn(true);
  44. $loader->getFilesystem()->shouldReceive('get')->once()->with(__DIR__.'/name/foo.blade.php')->andReturn('bar');
  45. $loader->getFilesystem()->shouldReceive('put')->once()->with(__DIR__.'/'.md5(__DIR__.'/name/foo.blade.php'), 'bar');
  46. $loader->getCompiler()->shouldReceive('compile')->once()->with('bar')->andReturn('bar');
  47. $loader->addTemplatePath('name', __DIR__.'/name');
  48. $return = $loader->getPath('name::foo');
  49. $this->assertEquals(__DIR__.'/'.md5(__DIR__.'/name/foo.blade.php'), $return);
  50. }
  51. public function testNestedViewsMayBeCompiled()
  52. {
  53. list($compiler, $files) = $this->getMocks();
  54. $loader = $this->getMock('Illuminate\Blade\Loader', array('isExpired'), array($compiler, $files, __DIR__, __DIR__));
  55. $loader->expects($this->once())->method('isExpired')->will($this->returnValue(true));
  56. $loader->getFilesystem()->shouldReceive('exists')->once()->andReturn(true);
  57. $loader->getFilesystem()->shouldReceive('get')->once()->with(__DIR__.'/foo/bar.blade.php')->andReturn('bar');
  58. $loader->getFilesystem()->shouldReceive('put')->once()->with(__DIR__.'/'.md5(__DIR__.'/foo/bar.blade.php'), 'bar');
  59. $loader->getCompiler()->shouldReceive('compile')->once()->with('bar')->andReturn('bar');
  60. $return = $loader->getPath('foo.bar');
  61. $this->assertEquals(__DIR__.'/'.md5(__DIR__.'/foo/bar.blade.php'), $return);
  62. }
  63. public function testRawPathViewPathsAreCorrectlyHandled()
  64. {
  65. $loader = $this->getLoader();
  66. $this->assertEquals('bar', $loader->getFullPath('path: bar'));
  67. }
  68. protected function getLoader()
  69. {
  70. list($compiler, $files) = $this->getMocks();
  71. return new Illuminate\Blade\Loader($compiler, $files, __DIR__, __DIR__);
  72. }
  73. protected function getMocks()
  74. {
  75. return array(m::mock('Illuminate\Blade\Compiler'), m::mock('Illuminate\Filesystem'));
  76. }
  77. }