Skip to content

Commit

Permalink
Merge pull request #94 from vitorbrandao/GH-78-improve-test-coverage
Browse files Browse the repository at this point in the history
 GH-78 Set the correct bundle path following best practices refactor; Improve test coverage
  • Loading branch information
vitorbrandao authored Nov 7, 2021
2 parents ade83ac + b85bb7e commit dbb1b96
Show file tree
Hide file tree
Showing 47 changed files with 1,366 additions and 204 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ test-parallel: ## Run unit and functional tests in parallel
.PHONY: test-parallel

sh-php7-sf4: ## Get a shell in the php7-sf4 container
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf4 sh
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php7-sf4 sh
.PHONY: sh-php7-sf4

sh-php7-sf5: ## Get a shell in the php7-sf5 container
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf5 sh
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php7-sf5 sh
.PHONY: sh-php7-sf5

sh-php8-sf4: ## Get a shell in the php8-sf4 container
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf4 sh
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php8-sf4 sh
.PHONY: sh-php8-sf4

sh-php8-sf5: ## Get a shell in the php8-sf5 container
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf5 sh
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php8-sf5 sh
.PHONY: sh-php8-sf5

web-php7-sf4: ## Launches the built-in PHP web server in the php7-sf4 container
Expand Down Expand Up @@ -193,5 +193,5 @@ composer-sf5: ## Install composer dependencies using sf5

### PHPUnit tests ###
phpunit: ## Run PHPUnit tests
XDEBUG_MODE=coverage phpunit --coverage-text
XDEBUG_MODE=coverage phpunit --coverage-text --coverage-html=.phpunit.cache/html
.PHONY: phpunit
11 changes: 11 additions & 0 deletions Resources/views/hello.html.smarty
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmartyBundle</title>
</head>
<body>
<h1>Hello, SmartyBundle!</h1>
</body>
</html>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"symfony/asset": "^4|^5",
"symfony/console": "^4|^5",
"symfony/expression-language": "^4|^5",
"symfony/phpunit-bridge": "^4|^5",
"symfony/phpunit-bridge": "^5",
"symfony/security": "^3.4.48|^4|^5",
"symfony/security-acl": "^3|^4|^5",
"symfony/translation": "^4|^5",
Expand Down
8 changes: 0 additions & 8 deletions config/smarty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,9 @@

<service id="smarty.templating.finder" class="NoiseLabs\Bundle\SmartyBundle\Loader\TemplateFinder" public="true">
<argument type="service" id="kernel" />
<argument type="service" id="smarty.templating.loader"/>
<argument type="service" id="templating.filename_parser" />
<argument>%kernel.root_dir%/Resources</argument>
<argument>%smarty.options%</argument>
</service>

<!--service id="smarty.cache_warmer" class="NoiseLabs\Bundle\SmartyBundle\CacheWarmer\SmartyCacheWarmer" public="false">
<tag name="kernel.cache_warmer" priority="255" />
<argument type="service" id="service_container" />
<argument type="service" id="smarty.templating.finder" />
</service-->

</services>
</container>
96 changes: 0 additions & 96 deletions src/CacheWarmer/SmartyCacheWarmer.php

This file was deleted.

15 changes: 5 additions & 10 deletions src/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class CompileCommand extends Command
*/
private $finder;

protected static $defaultName = 'smarty:compile';

public function __construct(SmartyEngine $engine, TemplateFinder $finder)
{
parent::__construct();
Expand All @@ -59,13 +61,10 @@ public function __construct(SmartyEngine $engine, TemplateFinder $finder)
protected function configure()
{
$this
->setName('smarty:compile')
->setDefinition([
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the templates'),
])
->setDescription('Compiles all known Smarty templates')
->addArgument(
'bundle',
InputArgument::OPTIONAL,
'A bundle name'
)
->setHelp(
<<<'EOF'
The following command finds all known Smarty templates and compiles them:
Expand Down Expand Up @@ -114,10 +113,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
} catch (\Exception $e) {
$e = SmartyBundleRuntimeException::createFromPrevious($e, $template);
// problem during compilation, log it and give up
if ($verbose) {
$output->writeln('');
}
$output->writeln(sprintf("<error>ERROR: Failed to compile Smarty template \"%s\"</error>\n-> %s\n", (string) $template, $e->getMessage()));
++$count['failed'];
}
Expand Down
6 changes: 5 additions & 1 deletion src/Command/TemplatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class TemplatesCommand extends Command
*/
private $finder;

protected static $defaultName = 'smarty:templates';

public function __construct(TemplateFinder $finder)
{
parent::__construct();
Expand All @@ -51,8 +53,10 @@ public function __construct(TemplateFinder $finder)
protected function configure()
{
$this
->setDefinition([
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name where to load the templates'),
])
->setDescription('Lists all Smarty templates')
->addArgument('bundle', InputArgument::OPTIONAL, 'A bundle name')
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command lists all Smarty templates found in the current project
Expand Down
9 changes: 7 additions & 2 deletions src/Extension/ActionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\BlockPlugin;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\ModifierPlugin;
use SmartyException;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;

/**
Expand Down Expand Up @@ -82,6 +83,10 @@ public function renderBlockAction(array $parameters = [], $controller = null, $t
'options' => [],
], $parameters);

if (!$controller) {
throw new SmartyException('The controller parameter must be specified');
}

return $this->render($controller, $parameters['attributes'], $parameters['options']);
}
}
Expand All @@ -91,8 +96,8 @@ public function renderBlockAction(array $parameters = [], $controller = null, $t
*
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
*
* @see Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper::render()
* @see Symfony\Bundle\TwigBundle\Extension\ActionsExtension::renderAction()
* @see \Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper::render()
* @see \Symfony\Bundle\TwigBundle\Extension\ActionsExtension::renderAction()
*/
public function renderModifierAction($controller, array $attributes = [], array $options = [])
{
Expand Down
Empty file removed src/Extension/FormExtension.php
Empty file.
Empty file.
4 changes: 1 addition & 3 deletions src/Loader/FileLocatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ class FileLocatorFactory
* @param KernelInterface $kernel A KernelInterface instance
* @param null|string $path The path the global resource directory
* @param array $paths An array of paths where to look for resources
*
* @return FileLocator
*/
public static function createFileLocator(KernelInterface $kernel, $path, array $paths, array $extraTemplatePaths)
public static function createFileLocator(KernelInterface $kernel, ?string $path, array $paths, array $extraTemplatePaths): FileLocator
{
return new FileLocator($kernel, $path, array_merge($paths, $extraTemplatePaths));
}
Expand Down
44 changes: 19 additions & 25 deletions src/Loader/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
namespace NoiseLabs\Bundle\SmartyBundle\Loader;

use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateFilenameParser;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;

/**
Expand All @@ -44,7 +44,7 @@ class TemplateFinder implements TemplateFinderInterface
private $kernel;

/**
* @var TemplateNameParserInterface
* @var TemplateFilenameParser
*/
private $parser;

Expand All @@ -53,11 +53,6 @@ class TemplateFinder implements TemplateFinderInterface
*/
private $rootDir;

/**
* @var TemplateLoader
*/
private $templateLoader;

/**
* @var array
*/
Expand All @@ -66,29 +61,31 @@ class TemplateFinder implements TemplateFinderInterface
/**
* Constructor.
*
* @param KernelInterface $kernel A KernelInterface instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
* @param string $rootDir The directory where global templates can be stored
* @param KernelInterface $kernel A KernelInterface instance
* @param TemplateFilenameParser $parser A TemplateNameParserInterface instance
* @param string $rootDir The directory where global templates can be stored
*/
public function __construct(
KernelInterface $kernel,
TemplateLoader $templateLoader,
TemplateNameParserInterface $parser,
$rootDir,
TemplateFilenameParser $parser,
string $rootDir,
array $smartyOptions
) {
$this->kernel = $kernel;
$this->templateLoader = $templateLoader;
$this->parser = $parser;
$this->rootDir = $rootDir;

$this->templateDirs = [];
$this->templateDirs = [
$rootDir.'/Resources/views',
$rootDir.'/templates',
];

if (isset($smartyOptions['template_dir'])) {
$this->templateDirs[] = $smartyOptions['template_dir'];
}
if (isset($smartyOptions['templates_dir']) && is_array($smartyOptions['templates_dir'])) {
$this->templateDirs = array_merge($this->templateDirs, $smartyOptions['templates_dir']);
}
$this->templateDirs = array_unique($this->templateDirs);
}

/**
Expand All @@ -111,12 +108,7 @@ public function findAllTemplates()
return $templates;
}

/**
* @param string $name
*
* @return BundleInterface
*/
public function getBundle($name)
public function getBundle(string $name): BundleInterface
{
return $this->kernel->getBundle($name);
}
Expand All @@ -128,9 +120,11 @@ public function getBundle($name)
*
* @return array An array of templates of type TemplateReferenceInterface
*/
public function findTemplatesInBundle(BundleInterface $bundle)
public function findTemplatesInBundle(BundleInterface $bundle): array
{
$templates = $this->findTemplatesInFolder($bundle->getPath().'/Resources/views');
$bundleTemplatesDir = is_dir($bundle->getPath().'/Resources/views') ?
$bundle->getPath().'/Resources/views' : $bundle->getPath().'/templates';
$templates = $this->findTemplatesInFolder($bundleTemplatesDir);
$name = $bundle->getName();

foreach (array_keys($templates) as $k) {
Expand All @@ -147,7 +141,7 @@ public function findTemplatesInBundle(BundleInterface $bundle)
*
* @return array|TemplateReferenceInterface[] An array of templates of type TemplateReferenceInterface
*/
private function findTemplatesInFolder($dir)
private function findTemplatesInFolder(string $dir): array
{
if (!$dir || !is_dir($dir)) {
return [];
Expand Down
7 changes: 1 addition & 6 deletions src/Loader/TemplateNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@

class TemplateNotFoundException extends RuntimeException
{
/**
* @param string $templateName
*
* @return TemplateNotFoundException
*/
public static function couldNotLocate($templateName)
public static function couldNotLocate(string $templateName): TemplateNotFoundException
{
return new static(sprintf('Could not locate template "%s"', $templateName));
}
Expand Down
Loading

0 comments on commit dbb1b96

Please sign in to comment.