1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
<?php
namespace Illuminate\Tests\View;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class ViewBladeCompilerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(false);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testCannotConstructWithBadCachePath()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Please provide a valid cache path.');
new BladeCompiler($this->getFiles(), null);
}
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true);
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(0);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testIsExpiredReturnsFalseWhenUseCacheIsTrueAndNoFileModification()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true);
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(0);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(100);
$this->assertFalse($compiler->isExpired('foo'));
}
public function testIsExpiredReturnsTrueWhenUseCacheIsFalse()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__, $basePath = '', $useCache = false);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testCompilePathIsProperlyCreated()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiler->getCompiledPath('foo'));
}
public function testCompileCompilesFileAndReturnsContents()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
}
public function testCompileCompilesFileAndReturnsContentsCreatingDirectory()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(false);
$files->shouldReceive('makeDirectory')->once()->with(__DIR__, 0777, true, true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
}
public function testCompileCompilesAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
$this->assertSame('foo', $compiler->getPath());
}
public function testCompileSetAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->setPath('foo');
$this->assertSame('foo', $compiler->getPath());
}
public function testCompileWithPathSetBefore()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
// set path before compilation
$compiler->setPath('foo');
// trigger compilation with $path
$compiler->compile();
$this->assertSame('foo', $compiler->getPath());
}
public function testRawTagsCanBeSetToLegacyValues()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->setEchoFormat('%s');
$this->assertSame('<?php echo e($name); ?>', $compiler->compileString('{{{ $name }}}'));
$this->assertSame('<?php echo $name; ?>', $compiler->compileString('{{ $name }}'));
$this->assertSame('<?php echo $name; ?>', $compiler->compileString('{{
$name
}}'));
}
/**
* @dataProvider appendViewPathDataProvider
*
* @param string $content
* @param string $compiled
*/
public function testIncludePathToTemplate($content, $compiled)
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn($content);
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiled);
$compiler->compile('foo');
}
/**
* @return array
*/
public static function appendViewPathDataProvider()
{
return [
'No PHP blocks' => [
'Hello World',
'Hello World<?php /**PATH foo ENDPATH**/ ?>',
],
'Single PHP block without closing ?>' => [
'<?php echo $path',
'<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Ending PHP block.' => [
'Hello world<?php echo $path ?>',
'Hello world<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Ending PHP block without closing ?>' => [
'Hello world<?php echo $path',
'Hello world<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'PHP block between content.' => [
'Hello world<?php echo $path ?>Hi There',
'Hello world<?php echo $path ?>Hi There<?php /**PATH foo ENDPATH**/ ?>',
],
'Multiple PHP blocks.' => [
'Hello world<?php echo $path ?>Hi There<?php echo $path ?>Hello Again',
'Hello world<?php echo $path ?>Hi There<?php echo $path ?>Hello Again<?php /**PATH foo ENDPATH**/ ?>',
],
'Multiple PHP blocks without closing ?>' => [
'Hello world<?php echo $path ?>Hi There<?php echo $path',
'Hello world<?php echo $path ?>Hi There<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Short open echo tag' => [
'Hello world<?= echo $path',
'Hello world<?= echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Echo XML declaration' => [
'<?php echo \'<?xml version="1.0" encoding="UTF-8"?>\';',
'<?php echo \'<?xml version="1.0" encoding="UTF-8"?>\'; ?><?php /**PATH foo ENDPATH**/ ?>',
],
];
}
public function testDontIncludeEmptyPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World');
$compiler->setPath('');
$compiler->compile();
}
public function testDontIncludeNullPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with(null)->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World');
$compiler->setPath(null);
$compiler->compile();
}
public function testShouldStartFromStrictTypesDeclaration()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$strictTypeDecl = "<?php\ndeclare(strict_types = 1);";
$this->assertSame(substr($compiler->compileString("<?php\ndeclare(strict_types = 1);\nHello World"),
0, strlen($strictTypeDecl)), $strictTypeDecl);
}
public function testComponentAliasesCanBeConventionallyDetermined()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->component('App\Foo\Bar');
$this->assertEquals(['bar' => 'App\Foo\Bar'], $compiler->getClassComponentAliases());
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->component('App\Foo\Bar', null, 'prefix');
$this->assertEquals(['prefix-bar' => 'App\Foo\Bar'], $compiler->getClassComponentAliases());
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->component('App\View\Components\Forms\Input');
$this->assertEquals(['forms:input' => 'App\View\Components\Forms\Input'], $compiler->getClassComponentAliases());
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->component('App\View\Components\Forms\Input', null, 'prefix');
$this->assertEquals(['prefix-forms:input' => 'App\View\Components\Forms\Input'], $compiler->getClassComponentAliases());
}
public function testAnonymousComponentNamespacesCanBeStored()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->anonymousComponentNamespace(' public/frontend ', 'frontend');
$this->assertEquals(['frontend' => 'public.frontend'], $compiler->getAnonymousComponentNamespaces());
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->anonymousComponentNamespace('public/frontend/', 'frontend');
$this->assertEquals(['frontend' => 'public.frontend'], $compiler->getAnonymousComponentNamespaces());
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->anonymousComponentNamespace('/admin/components', 'admin');
$this->assertEquals(['admin' => 'admin.components'], $compiler->getAnonymousComponentNamespaces());
// Test directory is automatically inferred from the prefix if not given.
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->anonymousComponentNamespace('frontend');
$this->assertEquals(['frontend' => 'frontend'], $compiler->getAnonymousComponentNamespaces());
// Test that the prefix can also contain dots.
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->anonymousComponentNamespace('frontend/auth', 'frontend.auth');
$this->assertEquals(['frontend.auth' => 'frontend.auth'], $compiler->getAnonymousComponentNamespaces());
}
protected function getFiles()
{
return m::mock(Filesystem::class);
}
}
|