Skip to content

Commit

Permalink
Merge pull request #4 from daison12006013/master
Browse files Browse the repository at this point in the history
Resolving Compiler and Enhancing Test Suite
  • Loading branch information
jenssegers committed Mar 21, 2016
2 parents f4302d3 + 71b6480 commit 64e40c9
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 13 deletions.
21 changes: 21 additions & 0 deletions src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Blade {
*/
protected $container;

/**
* Engine Resolver
*
* @var
*/
protected $engineResolver;

/**
* Constructor.
*
Expand All @@ -31,6 +38,8 @@ public function __construct($viewPaths, $cachePath, ContainerInterface $containe
$this->setupContainer();

(new ViewServiceProvider($this->container))->register();

$this->engineResolver = $this->container->make('view.engine.resolver');
}

/**
Expand Down Expand Up @@ -67,6 +76,18 @@ public function render($view, $data = [], $mergeData = [])
return $this->container['view']->make($view, $data, $mergeData)->render();
}

/**
* Get the compiler
*
* @return mixed
*/
public function compiler()
{
$bladeEngine = $this->engineResolver->resolve('blade');

return $bladeEngine->getCompiler();
}

/**
* Pass any method to the view factory instance.
*
Expand Down
76 changes: 63 additions & 13 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,88 @@

use Jenssegers\Blade\Blade;

class BladeTest extends PHPUnit_Framework_TestCase {
class BladeTest extends PHPUnit_Framework_TestCase
{
private $blade;

public function testBasic()
public function setUp()
{
$blade = new Blade('tests/views', 'tests/cache');
$this->blade = new Blade('tests/views', 'tests/cache');

$this->blade->compiler()->directive('datetime', function ($expression) {
return "<?php echo with({$expression})->format('F d, Y g:i a'); ?>";
});
}

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

public function testVariables()
{
$blade = new Blade('tests/views', 'tests/cache');

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

public function testNonBlade()
{
$blade = new Blade('tests/views', 'tests/cache');

$output = $blade->make('plain')->render();
$output = $this->blade->make('plain')->render();
$this->assertEquals('this is plain php', trim($output));
}

public function testRenderAlias()
{
$blade = new Blade('tests/views', 'tests/cache');

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

public function testExtenderBlade()
{
$users = require __DIR__.'/data/users.php';

$blade_name = 'extender';

$output = $this->blade
->make($blade_name, $users)
->render();

# uncomment if you want to update the sample output
// $this->write($output, $blade_name);

$this->assertEquals(
$output,
$this->read($blade_name)
);
}

/**
* Html Writer on sample_output folder
*
* @param string $output The HTML output
* @param string $blade_name The blade file name/path
*
* @return void
*/
private function write($output, $blade_name)
{
$file_path = __DIR__.'/sample_output/'.$blade_name.'.html';

file_put_contents($file_path, $output);
}

/**
* HTML Reader on sample_output folder
*
* @param string $blade_name The blade file name/path
*
* @return string
*/
private function read($blade_name)
{
$file_path = __DIR__.'/sample_output/'.$blade_name.'.html';

return file_get_contents($file_path);
}
}
22 changes: 22 additions & 0 deletions tests/data/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

return [

'users' => [
[
'id' => 1,
'name' => 'John Doe',
'email' => 'john.doe@doe.com',
],
[
'id' => 2,
'name' => 'Jen Doe',
'email' => 'jen.doe@example.com',
],
[
'id' => 3,
'name' => 'Jerry Doe',
'email' => 'jerry.doe@doe.com',
],
],
];
56 changes: 56 additions & 0 deletions tests/sample_output/extender.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html>
<head>
<title>Test Title
</title>
</head>
<body>

Default<br>
Still Default<br>

&lt;script type=&quot;text/javascript&quot;&gt;alert(&quot;Hacked!&quot;);&lt;/script&gt;<br>

Hello, <strong>John Doe</strong>.<br>

hello &lt;strong&gt;John Doe&lt;/strong&gt;
<br>



01/01/2017 23:59:59 <br>

January 01, 2017 11:59 pm <br>

You are not signed in.

<div class="container">
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>1</td>
<td>John Doe</td>
<td>john.doe@doe.com</td>
</tr>
<tr>
<td>2</td>
<td>Jen Doe</td>
<td>jen.doe@example.com</td>
</tr>
<tr>
<td>3</td>
<td>Jerry Doe</td>
<td>jerry.doe@doe.com</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
66 changes: 66 additions & 0 deletions tests/views/extender.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@extends('main.layout')

@section('title')
Test Title
@endsection

@section('content')

{{-- you must get 'Default' --}}
{{ isset($name) ? $name : 'Default' }}<br>
{{ $name or 'Still Default' }}<br>

{{-- it must strip out this XSS sample --}}
{{ '<script type="text/javascript">alert("Hacked!");</script>' }}<br>

{{-- this must be bold --}}
<?php $name = '<strong>John Doe</strong>' ?>
Hello, {!! $name !!}.<br>

{{-- using include directive --}}
@include('variables', ['name' => $name]) <br>

{{-- @each('variables', $users, 'name') <br> --}}

<?php
$dateObj = new DateTime('2017-01-01 23:59:59');
?>

{{-- using object with() --}}
<?php echo with($dateObj)->format('m/d/Y H:i:s'); ?> <br>

{{-- using custom directive --}}
@datetime($dateObj) <br>

{{-- using unless, reversing 'true' --}}
@unless ($auth_check = false)
You are not signed in.
@endunless

<div class="container">
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered">
@if (count($users) > 0)
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user['id'] }}</td>
<td>{{ $user['name'] }}</td>
<td>{{ $user['email'] }}</td>
</tr>
@endforeach
@else
<tr>
<td colspan="3">No users found!</td>
</tr>
@endif
</table>
</div>
</div>
</div>
@endsection
9 changes: 9 additions & 0 deletions tests/views/main/layout.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>

0 comments on commit 64e40c9

Please sign in to comment.