Skip to content

Commit

Permalink
noiselabsGH-95 Rework the SmartyEngine::renderTemplateFunction when c…
Browse files Browse the repository at this point in the history
…aching is disabled

* Rename the Functional test suite to Integration
  • Loading branch information
vitorbrandao committed Nov 12, 2021
1 parent dbb1b96 commit 1dfb8ac
Show file tree
Hide file tree
Showing 56 changed files with 793 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.phpunit.cache
.php-cs-fixer.cache
composer.lock
/cache
/docs/_build/
/tests/Sandbox/App/cache
/tests/Sandbox/App/logs
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude(['tests/Sandbox', 'tools', 'tmp'])
->exclude(['cache', 'tests/Sandbox', 'tools', 'tmp'])
->in(__DIR__)
;

Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,27 @@ build: ## Build Docker images (in parallel)
$(MAKE) -j4 build-php7-sf4 build-php7-sf5 build-php8-sf4 build-php8-sf5
.PHONY: build

test-php7-sf4: ## Run unit and functional tests in the php7-sf4 image
test-php7-sf4: ## Run phpunit tests in the php7-sf4 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf4 composer test
.PHONY: test-php7-sf4

test-php7-sf5: ## Run unit and functional tests in the php7-sf5 image
test-php7-sf5: ## Run phpunit tests in the php7-sf5 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf5 composer test
.PHONY: test-php7-sf5

test-php8-sf4: ## Run unit and functional tests in the php8-sf4 image
test-php8-sf4: ## Run phpunit tests in the php8-sf4 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf4 composer test
.PHONY: test-php8-sf4

test-php8-sf5: ## Run unit and functional tests in the php8-sf5 image
test-php8-sf5: ## Run phpunit tests in the php8-sf5 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf5 composer test
.PHONY: test-php8-sf5

test: ## Run unit and functional tests
test: ## Run phpunit tests
$(MAKE) test-php7-sf4 test-php7-sf5 test-php8-sf4 test-php8-sf5
.PHONY: test

test-parallel: ## Run unit and functional tests in parallel
test-parallel: ## Run phpunit tests in parallel
$(MAKE) -j4 test-php7-sf4 test-php7-sf5 test-php8-sf4 test-php8-sf5
.PHONY: test-parallel

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/phpunit-bridge": "^5",
"symfony/security": "^3.4.48|^4|^5",
"symfony/security-acl": "^3|^4|^5",
"symfony/test-pack": "^1.0",
"symfony/translation": "^4|^5",
"symfony/yaml": "^4|^5"
},
Expand Down
9 changes: 6 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<php>
<ini name="assert.exception" value="1"/>
<ini name="date.timezone" value="Europe/London"/>
<ini name="error_reporting" value="22519"/>
<ini name="error_reporting" value="-1"/>
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="log_errors_max_len" value="0"/>
Expand All @@ -33,8 +33,11 @@
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="functional">
<directory suffix="Test.php">tests/Functional</directory>
<testsuite name="integration">
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
<testsuite name="application">
<directory suffix="Test.php">tests/Application</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/Extension/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use NoiseLabs\Bundle\SmartyBundle\Extension\ExtensionInterface;

/**
* The Plugin base class represents a OO approach to the Smarty plugin
* The Plugin base class represents an OO approach to the Smarty plugin
* architecture.
*
* See {@link http://www.smarty.net/docs/en/plugins.tpl}.
Expand Down
67 changes: 30 additions & 37 deletions src/SmartyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ class SmartyEngine implements EngineInterface
*/
private $templateLoader;

/**
* @var array
*/
private $registeredPlugins;

/**
* Constructor.
*
Expand Down Expand Up @@ -192,7 +187,7 @@ public function __call($name, $args)
*
* @return Smarty The Smarty instance
*/
public function getSmarty()
public function getSmarty(): Smarty
{
$this->registerFilters();
$this->registerPlugins();
Expand All @@ -211,7 +206,7 @@ public function getSmarty()
*
* @return string The evaluated template as a string
*/
public function render($name, array $parameters = [])
public function render($name, array $parameters = []): string
{
$template = $this->load($name);

Expand Down Expand Up @@ -268,12 +263,10 @@ public function render($name, array $parameters = [])
* @param mixed $name A template name
* @param bool $load If we should load template content right away. Default: true
*
*@throws SmartyException
* @throws RuntimeException
* @throws SmartyException
*
* @return \Smarty_Internal_Template
*/
public function createTemplate($name, $load = true)
public function createTemplate($name, bool $load = true): Smarty_Internal_Template
{
$template = $this->load($name);
$template = $this->smarty->createTemplate($template, $this->smarty);
Expand All @@ -292,15 +285,12 @@ public function createTemplate($name, $load = true)
/**
* Compiles a template object.
*
* @param mixed $name A template name
* @param bool $forceCompile
* @param mixed $name A template name
*
*@throws SmartyException
* @throws RuntimeException
* @throws SmartyException
*
* @return \Smarty_Internal_Template
*/
public function compileTemplate($name, $forceCompile = false)
public function compileTemplate($name, bool $forceCompile = false): Smarty_Internal_Template
{
$template = $this->load($name);
$template = $this->smarty->createTemplate($template, $this->smarty);
Expand Down Expand Up @@ -332,7 +322,7 @@ public function compileTemplate($name, $forceCompile = false)
* @throws RuntimeException
* @throws SmartyException
*/
public function renderTemplateFunction($template, $name, array $attributes = [])
public function renderTemplateFunction($template, string $name, array $attributes = [])
{
if (!$template instanceof Smarty_Internal_Template) {
$template = $this->createTemplate($template);
Expand All @@ -345,12 +335,18 @@ public function renderTemplateFunction($template, $name, array $attributes = [])
throw new RuntimeException($e->getMessage());
}
} else {
$function = 'smarty_template_function_'.$name;
if (!isset($this->smarty->registered_plugins[PluginInterface::TYPE_FUNCTION][$name])
|| empty($this->smarty->registered_plugins[PluginInterface::TYPE_FUNCTION][$name])) {
throw new RuntimeException(sprintf("Unable to find template function '%s'", $name));
}

$function = $this->smarty->registered_plugins[PluginInterface::TYPE_FUNCTION][$name][0];

if (!is_callable($function)) {
throw new RuntimeException(sprintf('Template function "%s" is not defined in "%s".', $name, $template->source->filepath), -1, null, $template);
throw new RuntimeException(sprintf("Template function '%s' is not callable", $name));
}

$function($template, $attributes);
$function(...$attributes);
}
}

Expand All @@ -359,12 +355,12 @@ public function renderTemplateFunction($template, $name, array $attributes = [])
* @param string $name Function name
* @param array $attributes Attributes to pass to the template function
*
*@throws SmartyException
* @throws RuntimeException
* @throws SmartyException
*
* @return string the output returned by the template function
*/
public function fetchTemplateFunction($template, $name, array $attributes = [])
public function fetchTemplateFunction($template, string $name, array $attributes = []): string
{
ob_start();
$this->renderTemplateFunction($template, $name, $attributes);
Expand All @@ -381,7 +377,7 @@ public function fetchTemplateFunction($template, $name, array $attributes = [])
*
* @return bool true if the template exists, false otherwise
*/
public function exists($name)
public function exists($name): bool
{
try {
$this->load($name);
Expand All @@ -399,7 +395,7 @@ public function exists($name)
*
* @return bool True if this class supports the given resource, false otherwise
*/
public function supports($name)
public function supports($name): bool
{
if ($name instanceof Smarty_Internal_Template) {
return true;
Expand All @@ -419,7 +415,7 @@ public function supports($name)
*
* @return Response A Response instance
*/
public function renderResponse($view, array $parameters = [], Response $response = null)
public function renderResponse($view, array $parameters = [], Response $response = null): ?Response
{
if (null === $response) {
$response = new Response();
Expand All @@ -433,7 +429,7 @@ public function renderResponse($view, array $parameters = [], Response $response
/**
* Loads the given template.
*
* @param string $name A template name
* @param Smarty_Internal_Template|string $name A template name
*
* @throws RuntimeException
*
Expand All @@ -458,7 +454,7 @@ public function load($name)
*
* @return bool Whether the extension is registered or not
*/
public function hasExtension($name)
public function hasExtension(string $name): bool
{
return array_key_exists($name, $this->extensions);
}
Expand All @@ -470,7 +466,7 @@ public function hasExtension($name)
*
* @return ExtensionInterface An ExtensionInterface instance
*/
public function getExtension($name)
public function getExtension(string $name): ExtensionInterface
{
if (false === $this->hasExtension($name)) {
throw new InvalidArgumentException(sprintf('The "%s" extension is not enabled.', $name));
Expand All @@ -494,7 +490,7 @@ public function addExtension(ExtensionInterface $extension)
*
* @param string $name The extension name
*/
public function removeExtension($name)
public function removeExtension(string $name)
{
unset($this->extensions[$name]);
}
Expand All @@ -518,7 +514,7 @@ public function setExtensions(array $extensions)
*
* @return ExtensionInterface[] An array of extensions
*/
public function getExtensions()
public function getExtensions(): array
{
return $this->extensions;
}
Expand All @@ -542,7 +538,7 @@ public function addFilter(FilterInterface $filter)
*
* @return FilterInterface[] An array of Filters
*/
public function getFilters()
public function getFilters(): array
{
if (null === $this->filters) {
$this->filters = [];
Expand Down Expand Up @@ -588,11 +584,9 @@ public function addPlugin(PluginInterface $plugin)
* Gets the collection of plugins, optionally filtered by an extension
* name.
*
* @param string $extensionName
*
* @return Extension\Plugin\PluginInterface[] An array of plugins
*/
public function getPlugins($extensionName = '')
public function getPlugins(string $extensionName = ''): array
{
if (null === $this->plugins) {
$this->plugins = [];
Expand Down Expand Up @@ -636,7 +630,6 @@ protected function registerPlugin(PluginInterface $plugin)
// verify that plugin isn't registered yet. That would cause a SmartyException.
if (!isset($this->smarty->registered_plugins[$plugin->getType()][$plugin->getName()])) {
$this->smarty->registerPlugin($plugin->getType(), $plugin->getName(), $plugin->getCallback());
$this->registeredPlugins[] = $plugin->getType().'_'.$plugin->getName();
}
}

Expand All @@ -646,7 +639,7 @@ protected function registerPlugin(PluginInterface $plugin)
* @param string $name The global name
* @param mixed $value The global value
*/
public function addGlobal($name, $value)
public function addGlobal(string $name, $value)
{
$this->globals[$name] = $value;
}
Expand Down
86 changes: 86 additions & 0 deletions tests/Application/AbstractWebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the NoiseLabs-SmartyBundle package.
*
* Copyright (c) 2011-2021 Vítor Brandão <[email protected]>
*
* NoiseLabs-SmartyBundle is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* NoiseLabs-SmartyBundle is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with NoiseLabs-SmartyBundle; if not, see
* <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);

namespace NoiseLabs\Bundle\SmartyBundle\Tests\Application;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractWebTestCase extends SymfonyWebTestCase
{
public static function assertRedirect($response, $location)
{
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
}

public static function setUpBeforeClass(): void
{
static::deleteTmpDir();
}

public static function tearDownAfterClass(): void
{
static::deleteTmpDir();
}

protected static function deleteTmpDir()
{
if (!file_exists($dir = sys_get_temp_dir().'/'.static::getVarDir())) {
return;
}

$fs = new Filesystem();
$fs->remove($dir);
}

protected static function getKernelClass(): string
{
require_once __DIR__.'/AppKernel.php';

return AppKernel::class;
}

protected static function createKernel(array $options = []): KernelInterface
{
$class = self::getKernelClass();

if (!isset($options['test_case'])) {
throw new \InvalidArgumentException('The option "test_case" must be set.');
}

return new $class(
__DIR__,
static::getVarDir(),
$options['test_case'],
$options['root_config'] ?? 'config.yml',
$options['environment'] ?? strtolower(static::getVarDir().$options['test_case']),
$options['debug'] ?? false
);
}

protected static function getVarDir(): string
{
return 'SB'.substr(strrchr(static::class, '\\'), 1);
}
}
Loading

0 comments on commit 1dfb8ac

Please sign in to comment.