getFiles(), __DIR__); $files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('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__.'/'.sha1('v2foo').'.php')->andReturn(true); $files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100); $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(0); $this->assertTrue($compiler->isExpired('foo')); } public function testCompilePathIsProperlyCreated() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); $this->assertEquals(__DIR__.'/'.sha1('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__.'/'.sha1('v2foo').'.php', 'Hello World'); $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__.'/'.sha1('v2foo').'.php', 'Hello World'); $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__.'/'.sha1('v2foo').'.php', 'Hello World'); $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__.'/'.sha1('v2foo').'.php', 'Hello World'); // 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('', $compiler->compileString('{{{ $name }}}')); $this->assertSame('', $compiler->compileString('{{ $name }}')); $this->assertSame('', $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__.'/'.sha1('v2foo').'.php', $compiled); $compiler->compile('foo'); } /** * @return array */ public function appendViewPathDataProvider() { return [ 'No PHP blocks' => [ 'Hello World', 'Hello World', ], 'Single PHP block without closing ?>' => [ '', ], 'Ending PHP block.' => [ 'Hello world', 'Hello world', ], 'Ending PHP block without closing ?>' => [ 'Hello world', ], 'PHP block between content.' => [ 'Hello worldHi There', 'Hello worldHi There', ], 'Multiple PHP blocks.' => [ 'Hello worldHi ThereHello Again', 'Hello worldHi ThereHello Again', ], 'Multiple PHP blocks without closing ?>' => [ 'Hello worldHi ThereHi There', ], 'Short open echo tag' => [ 'Hello world', ], 'Echo XML declaration' => [ '\';', '\'; ?>', ], ]; } 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__.'/'.sha1('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__.'/'.sha1('v2').'.php', 'Hello World'); $compiler->setPath(null); $compiler->compile(); } public function testShouldStartFromStrictTypesDeclaration() { $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $strictTypeDecl = "assertSame(substr($compiler->compileString("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()); } protected function getFiles() { return m::mock(Filesystem::class); } }