2013-01-10 15:28:59 -06:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-17 14:30:19 +00:00
|
|
|
namespace Illuminate\Tests\View;
|
|
|
|
|
|
2022-10-26 14:53:22 +01:00
|
|
|
use ErrorException;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2020-06-01 13:05:00 +01:00
|
|
|
use Illuminate\Filesystem\Filesystem;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Illuminate\View\Compilers\CompilerInterface;
|
|
|
|
|
use Illuminate\View\Engines\CompilerEngine;
|
2022-10-26 14:53:22 +01:00
|
|
|
use Illuminate\View\ViewException;
|
2013-01-10 15:28:59 -06:00
|
|
|
use Mockery as m;
|
2016-12-30 21:31:11 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2023-07-11 14:10:55 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2016-12-30 21:31:11 +01:00
|
|
|
class ViewCompilerEngineTest extends TestCase
|
2015-06-01 16:26:53 +01:00
|
|
|
{
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testViewsMayBeRecompiledAndRendered()
|
|
|
|
|
{
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/basic.php');
|
|
|
|
|
$engine->getCompiler()->shouldReceive('isExpired')->once()->with(__DIR__.'/fixtures/foo.php')->andReturn(true);
|
|
|
|
|
$engine->getCompiler()->shouldReceive('compile')->once()->with(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
$results = $engine->get(__DIR__.'/fixtures/foo.php');
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('Hello World
|
2015-06-01 16:35:44 +01:00
|
|
|
', $results);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testViewsAreNotRecompiledIfTheyAreNotExpired()
|
|
|
|
|
{
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/basic.php');
|
|
|
|
|
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);
|
|
|
|
|
$engine->getCompiler()->shouldReceive('compile')->never();
|
|
|
|
|
$results = $engine->get(__DIR__.'/fixtures/foo.php');
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('Hello World
|
2015-06-01 16:35:44 +01:00
|
|
|
', $results);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2023-07-11 14:10:55 +01:00
|
|
|
public function testRegularExceptionsAreReThrownAsViewExceptions()
|
|
|
|
|
{
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/regular-exception.php');
|
|
|
|
|
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->expectException(ViewException::class);
|
|
|
|
|
$this->expectExceptionMessage('regular exception message');
|
|
|
|
|
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHttpExceptionsAreNotReThrownAsViewExceptions()
|
|
|
|
|
{
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/http-exception.php');
|
|
|
|
|
$engine->getCompiler()->shouldReceive('isExpired')->once()->andReturn(false);
|
|
|
|
|
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('http exception message');
|
|
|
|
|
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 15:44:28 +01:00
|
|
|
public function testThatViewsAreNotAskTwiceIfTheyAreExpired()
|
|
|
|
|
{
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
$engine->getCompiler()->shouldReceive('getCompiledPath')->with(__DIR__.'/fixtures/foo.php')->andReturn(__DIR__.'/fixtures/basic.php');
|
|
|
|
|
$engine->getCompiler()->shouldReceive('isExpired')->twice()->andReturn(false);
|
|
|
|
|
$engine->getCompiler()->shouldReceive('compile')->never();
|
|
|
|
|
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
|
|
|
|
|
$engine->forgetCompiledOrNotExpired();
|
|
|
|
|
|
|
|
|
|
$engine->get(__DIR__.'/fixtures/foo.php');
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 14:53:22 +01:00
|
|
|
public function testViewsAreRecompiledWhenCompiledViewIsMissingViaFileNotFoundException()
|
2015-06-01 15:56:31 +01:00
|
|
|
{
|
2022-10-26 14:53:22 +01:00
|
|
|
$compiled = __DIR__.'/fixtures/basic.php';
|
|
|
|
|
$path = __DIR__.'/fixtures/foo.php';
|
|
|
|
|
|
|
|
|
|
$files = m::mock(Filesystem::class);
|
|
|
|
|
$engine = $this->getEngine($files);
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andReturn('compiled-content');
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new FileNotFoundException(
|
|
|
|
|
"File does not exist at path {$path}."
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andReturn('compiled-content');
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('getCompiledPath')
|
|
|
|
|
->times(3)
|
|
|
|
|
->with($path)
|
|
|
|
|
->andReturn($compiled);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('isExpired')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(true);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('compile')
|
|
|
|
|
->twice()
|
|
|
|
|
->with($path);
|
|
|
|
|
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewsAreRecompiledWhenCompiledViewIsMissingViaRequireException()
|
|
|
|
|
{
|
|
|
|
|
$compiled = __DIR__.'/fixtures/basic.php';
|
|
|
|
|
$path = __DIR__.'/fixtures/foo.php';
|
|
|
|
|
|
|
|
|
|
$files = m::mock(Filesystem::class);
|
|
|
|
|
$engine = $this->getEngine($files);
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andReturn('compiled-content');
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new ErrorException(
|
|
|
|
|
"require({$path}): Failed to open stream: No such file or directory",
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andReturn('compiled-content');
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('getCompiledPath')
|
|
|
|
|
->times(3)
|
|
|
|
|
->with($path)
|
|
|
|
|
->andReturn($compiled);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('isExpired')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(true);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('compile')
|
|
|
|
|
->twice()
|
|
|
|
|
->with($path);
|
|
|
|
|
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewsAreRecompiledJustOnceWhenCompiledViewIsMissing()
|
|
|
|
|
{
|
|
|
|
|
$compiled = __DIR__.'/fixtures/basic.php';
|
|
|
|
|
$path = __DIR__.'/fixtures/foo.php';
|
|
|
|
|
|
|
|
|
|
$files = m::mock(Filesystem::class);
|
|
|
|
|
$engine = $this->getEngine($files);
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andReturn('compiled-content');
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new FileNotFoundException(
|
|
|
|
|
"File does not exist at path {$path}."
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new FileNotFoundException(
|
|
|
|
|
"File does not exist at path {$path}."
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('getCompiledPath')
|
|
|
|
|
->times(3)
|
|
|
|
|
->with($path)
|
|
|
|
|
->andReturn($compiled);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('isExpired')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(true);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('compile')
|
|
|
|
|
->twice()
|
|
|
|
|
->with($path);
|
|
|
|
|
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
|
|
|
|
|
$this->expectException(ViewException::class);
|
|
|
|
|
$this->expectExceptionMessage("File does not exist at path {$path}.");
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewsAreNotRecompiledOnRegularViewException()
|
|
|
|
|
{
|
|
|
|
|
$compiled = __DIR__.'/fixtures/basic.php';
|
|
|
|
|
$path = __DIR__.'/fixtures/foo.php';
|
|
|
|
|
|
|
|
|
|
$files = m::mock(Filesystem::class);
|
|
|
|
|
$engine = $this->getEngine($files);
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new Exception(
|
|
|
|
|
'Just an regular error...'
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('isExpired')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(false);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('compile')
|
|
|
|
|
->never();
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('getCompiledPath')
|
|
|
|
|
->once()
|
|
|
|
|
->with($path)
|
|
|
|
|
->andReturn($compiled);
|
|
|
|
|
|
|
|
|
|
$this->expectException(ViewException::class);
|
|
|
|
|
$this->expectExceptionMessage('Just an regular error...');
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewsAreNotRecompiledIfTheyWereJustCompiled()
|
|
|
|
|
{
|
|
|
|
|
$compiled = __DIR__.'/fixtures/basic.php';
|
|
|
|
|
$path = __DIR__.'/fixtures/foo.php';
|
|
|
|
|
|
|
|
|
|
$files = m::mock(Filesystem::class);
|
|
|
|
|
$engine = $this->getEngine($files);
|
|
|
|
|
|
|
|
|
|
$files->shouldReceive('getRequire')
|
|
|
|
|
->once()
|
|
|
|
|
->with($compiled, [])
|
|
|
|
|
->andThrow(new FileNotFoundException(
|
|
|
|
|
"File does not exist at path {$path}."
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('isExpired')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn(true);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('compile')
|
|
|
|
|
->once()
|
|
|
|
|
->with($path);
|
|
|
|
|
|
|
|
|
|
$engine->getCompiler()
|
|
|
|
|
->shouldReceive('getCompiledPath')
|
|
|
|
|
->once()
|
|
|
|
|
->with($path)
|
|
|
|
|
->andReturn($compiled);
|
|
|
|
|
|
|
|
|
|
$this->expectException(ViewException::class);
|
|
|
|
|
$this->expectExceptionMessage("File does not exist at path {$path}.");
|
|
|
|
|
$engine->get($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getEngine($filesystem = null)
|
|
|
|
|
{
|
|
|
|
|
return new CompilerEngine(m::mock(CompilerInterface::class), $filesystem ?: new Filesystem);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2014-02-27 19:30:59 +00:00
|
|
|
}
|