|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CHStudio\LaravelTransclude\Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Illuminate\Contracts\Foundation\Application; |
| 7 | +use Illuminate\Support\Facades\Facade; |
| 8 | +use Illuminate\View\Factory; |
| 9 | +use Illuminate\View\Engines\EngineResolver; |
| 10 | +use Illuminate\View\Engines\CompilerEngine; |
| 11 | +use Illuminate\View\Compilers\BladeCompiler; |
| 12 | +use CHStudio\LaravelTransclude\TranscludeCompiler; |
| 13 | +use CHStudio\LaravelTransclude\TranscludeServiceProvider as SUT; |
| 14 | + |
| 15 | +class TranscludeServiceProviderTest extends TestCase |
| 16 | +{ |
| 17 | + public function testBladeDirectivesAreRegistered() |
| 18 | + { |
| 19 | + $blade = $this->createMock(BladeCompiler::class); |
| 20 | + $blade //Ensure Blade directives are called |
| 21 | + ->expects($this->exactly(3)) |
| 22 | + ->method('directive') |
| 23 | + ->withConsecutive( |
| 24 | + [$this->equalTo('transclude'), $this->callback(function($item) { |
| 25 | + $this->assertTrue(is_callable($item)); |
| 26 | + $this->assertInstanceOf(TranscludeCompiler::class, $item[0]); |
| 27 | + $this->assertEquals('compileTransclude', $item[1]); |
| 28 | + |
| 29 | + return true; |
| 30 | + })], |
| 31 | + [$this->equalTo('endtransclude'), $this->callback(function($item) { |
| 32 | + $this->assertTrue(is_callable($item)); |
| 33 | + $this->assertInstanceOf(TranscludeCompiler::class, $item[0]); |
| 34 | + $this->assertEquals('compileEndTransclude', $item[1]); |
| 35 | + |
| 36 | + return true; |
| 37 | + })], |
| 38 | + [$this->equalTo('transcluded'), $this->callback(function($item) { |
| 39 | + $this->assertTrue(is_callable($item)); |
| 40 | + $this->assertInstanceOf(TranscludeCompiler::class, $item[0]); |
| 41 | + $this->assertEquals('compileTranscluded', $item[1]); |
| 42 | + |
| 43 | + return true; |
| 44 | + })] |
| 45 | + ); |
| 46 | + |
| 47 | + $engine = $this->createMock(CompilerEngine::class); |
| 48 | + $engine |
| 49 | + ->expects($this->exactly(3)) |
| 50 | + ->method('getCompiler') |
| 51 | + ->will($this->returnValue($blade)); |
| 52 | + |
| 53 | + $resolver = new EngineResolver; |
| 54 | + $resolver->register('blade', function() use ($engine) { |
| 55 | + return $engine; |
| 56 | + }); |
| 57 | + |
| 58 | + $factory = $this->createMock(Factory::class); |
| 59 | + $factory |
| 60 | + ->expects($this->exactly(3)) |
| 61 | + ->method('getEngineResolver') |
| 62 | + ->will($this->returnValue($resolver)); |
| 63 | + |
| 64 | + $factory //Check view environement is shared |
| 65 | + ->expects($this->once()) |
| 66 | + ->method('share') |
| 67 | + ->with( |
| 68 | + $this->callback(function($item) { |
| 69 | + $this->assertEquals((new TranscludeCompiler)->getName(), $item); |
| 70 | + return true; |
| 71 | + }), |
| 72 | + $this->callback(function($item) { |
| 73 | + $this->assertInstanceOf(TranscludeCompiler::class, $item); |
| 74 | + return true; |
| 75 | + }) |
| 76 | + ); |
| 77 | + |
| 78 | + $app = $this->createMock(\ArrayAccess::class); |
| 79 | + $app |
| 80 | + ->expects($this->any()) |
| 81 | + ->method('offsetGet') |
| 82 | + ->with('view') |
| 83 | + ->will($this->returnValue($factory)); |
| 84 | + |
| 85 | + Facade::setFacadeApplication($app); |
| 86 | + $sut = new SUT($app); |
| 87 | + |
| 88 | + $sut->boot(); |
| 89 | + } |
| 90 | +} |
0 commit comments