Skip to content

Commit

Permalink
Add tests to assert how the Laravel Events affect template rendering (#…
Browse files Browse the repository at this point in the history
…395)

* Add tests to assert how the Laravel Events affect template rendering

* TwigBridgeTestTrait->getApplication : remove extra $extensions parameter

* Fix phpCs style

* Added missing parent setup() call
  • Loading branch information
filcius authored Aug 12, 2020
1 parent 3555932 commit cfa9a11
Show file tree
Hide file tree
Showing 12 changed files with 716 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ vendor

## Build
build/

#storage folder for twig compilation tests
tests/storage
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_script:

script:
- ./vendor/bin/phpunit --configuration phpunit.xml
- ./vendor/bin/phpcs --standard=PSR2 -p --report=full --report-checkstyle=build/logs/checkstyle.xml src/ tests/
- ./vendor/bin/phpcs --standard=PSR2 -p --ignore=./tests/storage/* --report=full --report-checkstyle=build/logs/checkstyle.xml src/ tests/

after_script:
- ./vendor/bin/coveralls -v
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"illuminate/view": "^5.5|^6|^7"
},
"require-dev": {
"ext-json": "*",
"laravel/framework": "5.5.*",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0",
Expand Down
8 changes: 6 additions & 2 deletions src/Engine/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\View\Engines\CompilerEngine;
use Twig\Error\LoaderError;
use Twig\Error\Error;
use Twig\Loader\LoaderInterface;
use TwigBridge\Twig\Loader;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ class Twig extends CompilerEngine
* @param \TwigBridge\Twig\Loader $loader
* @param array $globalData
*/
public function __construct(Compiler $compiler, Loader $loader, array $globalData = [])
public function __construct(Compiler $compiler, LoaderInterface $loader, array $globalData = [])
{
parent::__construct($compiler);

Expand Down Expand Up @@ -119,7 +120,10 @@ protected function handleTwigError(Error $ex)
} elseif ($templateFile) {
// Attempt to locate full path to file
try {
$file = $this->loader->findTemplate($templateFile);
if ($this->loader instanceof Loader) {
//Outside of unit test, we should be able to load the file
$file = $this->loader->findTemplate($templateFile);
}
} catch (LoaderError $exception) {
// Unable to load template
}
Expand Down
76 changes: 1 addition & 75 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,8 @@
namespace TwigBridge\Tests;

use PHPUnit\Framework\TestCase;
use Mockery as m;
use Illuminate\Foundation\Application;
use Illuminate\View\Factory;
use Illuminate\Config\Repository;
use Illuminate\View\Engines\EngineResolver;

abstract class Base extends TestCase
{
protected $twigBridgeRoot;

public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);

$this->twigBridgeRoot = realpath(__DIR__ . '/../src');
}

public function tearDown()
{
m::close();
}

protected function getApplication(array $customConfig = [])
{
$app = new Application;
$app->instance('path', __DIR__);

$app['env'] = 'production';
$app['path.config'] = __DIR__ . '/config';
$app['path.storage'] = __DIR__ . '/storage';

// Filesystem
$files = m::mock('Illuminate\Filesystem\Filesystem');
$app['files'] = $files;

// View
$finder = m::mock('Illuminate\View\ViewFinderInterface');
$finder->shouldReceive('addExtension');

$app['view'] = new Factory(
new EngineResolver,
$finder,
m::mock('Illuminate\Events\Dispatcher')
);

$config = include $this->twigBridgeRoot . '/../config/twigbridge.php';

$configData = [
'twigbridge' => [
'extensions' => $config['extensions'],
'twig' => [
'extension' => 'twig',
'environment' => [
'debug' => false,
'charset' => 'utf-8',
'base_template_class' => 'TwigBridge\Twig\Template',
'cache' => null,
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true,
'optimizations' => -1,
],
'globals' => [],
],
],
];

$configData['twigbridge'] = array_replace_recursive($configData['twigbridge'], $customConfig);

// Config
$app['config'] = new Repository($configData);

$app->bind('Illuminate\Config\Repository', function () use ($app) {
return $app['config'];
});

return $app;
}
use TwigBridgeTestTrait;
}
3 changes: 2 additions & 1 deletion tests/ServiceProvider/Bindings/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace TwigBridge\Tests\ServiceProvider\Bindings;

use TwigBridge\Tests\Base;
use TwigBridge\ServiceProvider;
use TwigBridge\Tests\Base;

class CommandTest extends Base
{
protected $app;

public function setUp()
{
parent::setup();
$this->app = $this->getApplication();

$provider = new ServiceProvider($this->app);
Expand Down
100 changes: 100 additions & 0 deletions tests/TwigBridgeTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace TwigBridge\Tests;

use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Mockery as m;
use TwigBridge\ServiceProvider;

trait TwigBridgeTestTrait
{
protected $twigBridgeRoot;


public function setup()
{
$this->twigBridgeRoot = realpath(__DIR__ . '/../src');
}

public function tearDown()
{
m::close();
}

/**
* @param array $customConfig that will override the default config.
* A recursive merge is apply except for $customConfig['extensions'] which
* will replace the whole 'extensions' if present
* @return Application
*/
protected function getApplication(array $customConfig = [])
{
$app = new Application;
$app->instance('path', __DIR__);

$app['env'] = 'production';
$app['path.config'] = __DIR__ . '/config';
$app['path.storage'] = __DIR__ . '/storage';

// Filesystem
$files = m::mock('Illuminate\Filesystem\Filesystem');
$app['files'] = $files;

// View
$finder = m::mock('Illuminate\View\ViewFinderInterface');
$finder->shouldReceive('addExtension');

$app['view'] = new Factory(
new EngineResolver,
$finder,
m::mock('Illuminate\Events\Dispatcher')
);

if (!isset($customConfig['extensions'])) {
$config = include $this->twigBridgeRoot . '/../config/twigbridge.php';
$customConfig['extensions'] = $config['extensions'];
}

$configData = [
'twigbridge' => [
'twig' => [
'extension' => 'twig',
'environment' => [
'debug' => false,
'charset' => 'utf-8',
'base_template_class' => 'TwigBridge\Twig\Template',
'cache' => null,
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true,
'optimizations' => -1,
],
'globals' => [],
],
],
];

$configData['twigbridge'] = array_replace_recursive($configData['twigbridge'], $customConfig);

// Config
$app['config'] = new Repository($configData);

$app->bind('Illuminate\Config\Repository', function () use ($app) {
return $app['config'];
});

return $app;
}

protected function addBridgeServiceToApplication(Application $app)
{
$provider = new ServiceProvider($app);

// Register and boot provider
$provider->register();
$provider->boot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
--TEST--
Test how Laravel events affect embeded twig template
--TEMPLATE--
{% embed 'template.html.twig' %}
{% block content1 %}
{{ parent() }}

content from embedded:
{{ variableFromComposingEvent_index|default('none') }}
{{ variableFromCreatingEvent_index|default('none') }}
{{ variableFromComposingEvent_templatehtml|default('none') }}
{{ variableFromCreatingEvent_templatehtml|default('none') }}
{% endblock %}
{% endembed %}

{% include 'template.html.twig' with {} only %}
Content from index.html:
{{ variableFromComposingEvent_index|default('none') }}
{{ variableFromCreatingEvent_index|default('none') }}
{{ variableFromComposingEvent_templatehtml|default('none') }}
{{ variableFromCreatingEvent_templatehtml|default('none') }}

--TEMPLATE(template.html.twig)--

{% block content %}
content from template_content:
{{ variableFromComposingEvent_index|default('none') }}
{{ variableFromCreatingEvent_index|default('none') }}
{{ variableFromComposingEvent_templatehtml|default('none') }}
{{ variableFromCreatingEvent_templatehtml|default('none') }}
{% endblock %}
{% block content1 %}
content from template_content1:
{{ variableFromComposingEvent_index|default('none') }}
{{ variableFromCreatingEvent_index|default('none') }}
{{ variableFromComposingEvent_templatehtml|default('none') }}
{{ variableFromCreatingEvent_templatehtml|default('none') }}
{% endblock %}
{% block content2 %}
content from template_content2:
{{ variableFromComposingEvent_index|default('none') }}
{{ variableFromCreatingEvent_index|default('none') }}
from composing template.html event
from creating template.html event
{% endblock %}

--DATA--
return []
--EXPECT--
content from template_content:
from composing index event
from creating index event
from composing template.html event
from creating template.html event
content from template_content1:
from composing index event
from creating index event
from composing template.html event
from creating template.html event


content from embedded:
from composing index event
from creating index event
from composing template.html event
from creating template.html event
content from template_content2:
from composing index event
from creating index event
from composing template.html event
from creating template.html event



content from template_content:
none
none
from composing template.html event
from creating template.html event
content from template_content1:
none
none
from composing template.html event
from creating template.html event
content from template_content2:
none
none
from composing template.html event
from creating template.html event
Content from index.html:
from composing index event
from creating index event
none
none
--EXPECT_EVENT_COUNTS--
{
"index": 1,
"template.html": 2
}
Loading

0 comments on commit cfa9a11

Please sign in to comment.