Skip to content

Commit

Permalink
Merge pull request #44 from TullariS/master
Browse files Browse the repository at this point in the history
Support for BladeCompiler::if() + Unit Tests
  • Loading branch information
jenssegers authored Mar 17, 2020
2 parents 3d8a9be + e5084f4 commit d221b71
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Container\Container as ContainerInterface;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Contracts\View\View;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Facade;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory;
use Illuminate\View\ViewServiceProvider;

class Blade implements FactoryContract
{
/**
* @var Container
* @var Application
*/
protected $container;

Expand Down Expand Up @@ -59,6 +61,11 @@ public function directive(string $name, callable $handler)
{
$this->compiler->directive($name, $handler);
}

public function if($name, callable $callback)
{
$this->compiler->if($name, $callback);
}

public function exists($view): bool
{
Expand Down Expand Up @@ -120,5 +127,7 @@ protected function setupContainer(array $viewPaths, string $cachePath)
'view.compiled' => $cachePath,
];
}, true);

Facade::setFacadeApplication($this->container);
}
}
111 changes: 93 additions & 18 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Illuminate\View\Factory;
use Illuminate\View\View;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\ViewFinderInterface;
use Jenssegers\Blade\Blade;
use PHPUnit\Framework\TestCase;

Expand All @@ -17,6 +21,15 @@ public function setUp()
$this->blade->directive('datetime', function ($expression) {
return "<?php echo with({$expression})->format('F d, Y g:i a'); ?>";
});

$this->blade->if('ifdate', function ($date) {
return $date instanceof DateTime;
});
}

public function testCompilerGetter()
{
$this->assertInstanceOf(BladeCompiler::class, $this->blade->compiler());
}

public function testBasic()
Expand All @@ -25,6 +38,11 @@ public function testBasic()
$this->assertEquals('hello world', trim($output));
}

public function testExists()
{
$this->assertFalse($this->blade->exists('nonexistentview'));
}

public function testVariables()
{
$output = $this->blade->make('variables', ['name' => 'John Doe']);
Expand All @@ -34,12 +52,49 @@ public function testVariables()
public function testNonBlade()
{
$output = $this->blade->make('plain');
$this->assertEquals('this is plain php', trim($output));
$this->assertEquals('{{ this is plain php }}', trim($output));
}

public function testFile()
{
$output = $this->blade->file('tests/views/basic.blade.php');
$this->assertEquals('hello world', trim($output));
}

public function testShare()
{
$this->blade->share('name', 'John Doe');

$output = $this->blade->make('variables');
$this->assertEquals('hello John Doe', trim($output));
}

public function testComposer()
{
$this->blade->composer('variables', function (View $view) {
$view->with('name', 'John Doe and ' . $view->offsetGet('name'));
});

$output = $this->blade->make('variables', ['name' => 'Jane Doe']);
$this->assertEquals('hello John Doe and Jane Doe', trim($output));
}

public function testCreator()
{
$this->blade->creator('variables', function (View $view) {
$view->with('name', 'John Doe');
});
$this->blade->composer('variables', function (View $view) {
$view->with('name', 'Jane Doe and ' . $view->offsetGet('name'));
});

$output = $this->blade->make('variables');
$this->assertEquals('hello Jane Doe and John Doe', trim($output));
}

public function testRenderAlias()
{
$output = $this->blade->make('basic');
$output = $this->blade->render('basic');
$this->assertEquals('hello world', trim($output));
}

Expand All @@ -49,6 +104,37 @@ public function testDirective()
$this->assertEquals('Your birthday is August 19, 1989 12:00 am', trim($output));
}

public function testIf()
{
$output = $this->blade->make('if', ['birthday' => new DateTime('1989/08/19')]);
$this->assertEquals('Birthday August 19, 1989 12:00 am detected', trim($output));
}

public function testAddNamespace()
{
$this->blade->addNamespace('other', 'tests/views/other');

$output = $this->blade->make('other::basic');
$this->assertEquals('hello other world', trim($output));
}

public function testReplaceNamespace()
{
$this->blade->addNamespace('other', 'tests/views/other');
$this->blade->replaceNamespace('other', 'tests/views/another');

$output = $this->blade->make('other::basic');
$this->assertEquals('hello another world', trim($output));
}

public function testViewGetter()
{
/** @var Factory $view */
$view = $this->blade;

$this->assertInstanceOf(ViewFinderInterface::class, $view->getFinder());
}

public function testOther()
{
$users = [
Expand All @@ -75,31 +161,20 @@ public function testOther()
'authenticated' => false,
]);

$this->write($output, 'other');

$this->assertEquals($output, $this->expected('other'));
}

public function testExtends()
{
$output = $this->blade->make('extends');

$this->write($output, 'extends');

$this->assertEquals($output, $this->expected('extends'));
}

private function write(string $output, string $file)
private function expected(string $file): string
{
$file_path = __DIR__ . '/expected/' . $file . '.html';

file_put_contents($file_path, $output);
return file_get_contents($file_path);
}

private function expected(string $file): string
public function testExtends()
{
$file_path = __DIR__ . '/expected/' . $file . '.html';
$output = $this->blade->make('extends');

return file_get_contents($file_path);
$this->assertEquals($output, $this->expected('extends'));
}
}
1 change: 1 addition & 0 deletions tests/views/another/basic.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello another world
3 changes: 3 additions & 0 deletions tests/views/if.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ifdate($birthday)
Birthday {{ $birthday->format('F d, Y g:i a') }} detected
@endifdate
1 change: 1 addition & 0 deletions tests/views/other/basic.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello other world
2 changes: 1 addition & 1 deletion tests/views/plain.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
this is plain php
{{ this is plain <?php echo 'php'; ?> }}

0 comments on commit d221b71

Please sign in to comment.