-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to assert how the Laravel Events affect template rendering (#…
…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
Showing
12 changed files
with
716 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ vendor | |
|
||
## Build | ||
build/ | ||
|
||
#storage folder for twig compilation tests | ||
tests/storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
tests/TwigIntegrationTests/Fixtures/LaravelEventsNode/embed_template.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.