Bundle - getBundleViewPath() method - verify name of bundle

This commit is contained in:
Meritoo
2017-12-21 22:43:45 +01:00
parent 0e4c33241e
commit 7d23ff59d1
3 changed files with 108 additions and 12 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Exception\Bundle;
use Exception;
/**
* An exception used while name of bundle is incorrect
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class IncorrectBundleNameException extends Exception
{
/**
* Class constructor
*
* @param string $bundleName Incorrect name of bundle
*/
public function __construct($bundleName)
{
$template = 'Name of bundle \'%s\' is incorrect. It should start with big letter and end with "Bundle". Is'
. ' there everything ok?';
$message = sprintf($template, $bundleName);
parent::__construct($message);
}
}

View File

@@ -8,6 +8,8 @@
namespace Meritoo\Common\Utilities; namespace Meritoo\Common\Utilities;
use Meritoo\Common\Exception\Bundle\IncorrectBundleNameException;
/** /**
* Useful methods for bundle * Useful methods for bundle
* *
@@ -17,12 +19,14 @@ namespace Meritoo\Common\Utilities;
class Bundle class Bundle
{ {
/** /**
* Returns path to view / template of given bundle * Returns path of given bundle to view / template with given extension
* *
* @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template" * @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template"
* @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle" * @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle"
* @param string $extension (optional) Extension of the view / template * @param string $extension (optional) Extension of the view / template
* @return string|null * @return string|null
*
* @throws IncorrectBundleNameException
*/ */
public static function getBundleViewPath($viewPath, $bundleName, $extension = 'html.twig') public static function getBundleViewPath($viewPath, $bundleName, $extension = 'html.twig')
{ {
@@ -34,6 +38,13 @@ class Bundle
return null; return null;
} }
/*
* Given name of bundle is invalid?
*/
if (!Regex::isValidBundleName($bundleName)) {
throw new IncorrectBundleNameException($bundleName);
}
/* /*
* Path of the view / template doesn't end with given extension? * Path of the view / template doesn't end with given extension?
*/ */

View File

@@ -9,6 +9,7 @@
namespace Meritoo\Common\Test\Utilities; namespace Meritoo\Common\Test\Utilities;
use Generator; use Generator;
use Meritoo\Common\Exception\Bundle\IncorrectBundleNameException;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Utilities\Bundle; use Meritoo\Common\Utilities\Bundle;
@@ -29,6 +30,7 @@ class BundleTest extends BaseTestCase
* @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template" * @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template"
* @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle" * @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle"
* *
* @throws IncorrectBundleNameException
* @dataProvider provideEmptyViewPathAndBundle * @dataProvider provideEmptyViewPathAndBundle
*/ */
public function testGetBundleViewPathUsingEmptyPathAndBundle($viewPath, $bundleName) public function testGetBundleViewPathUsingEmptyPathAndBundle($viewPath, $bundleName)
@@ -36,11 +38,30 @@ class BundleTest extends BaseTestCase
self::assertNull(Bundle::getBundleViewPath($viewPath, $bundleName)); self::assertNull(Bundle::getBundleViewPath($viewPath, $bundleName));
} }
/**
* @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template"
* @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle"
*
* @throws IncorrectBundleNameException
* @dataProvider provideViewPathAndIncorrectBundleName
*/
public function testGetBundleViewPathUsingIncorrectBundleName($viewPath, $bundleName)
{
$template = 'Name of bundle \'%s\' is incorrect. It should start with big letter and end with "Bundle". Is'
. ' there everything ok?';
$message = sprintf($template, $bundleName);
$this->setExpectedException(IncorrectBundleNameException::class, $message);
Bundle::getBundleViewPath($viewPath, $bundleName);
}
/** /**
* @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template" * @param string $viewPath Path of the view / template, e.g. "MyDirectory/my-template"
* @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle" * @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle"
* @param string $expected Expected path to view / template * @param string $expected Expected path to view / template
* *
* @throws IncorrectBundleNameException
* @dataProvider provideViewPathAndBundle * @dataProvider provideViewPathAndBundle
*/ */
public function testGetBundleViewPathUsingDefaultExtension($viewPath, $bundleName, $expected) public function testGetBundleViewPathUsingDefaultExtension($viewPath, $bundleName, $expected)
@@ -54,6 +75,7 @@ class BundleTest extends BaseTestCase
* @param string $extension (optional) Extension of the view / template * @param string $extension (optional) Extension of the view / template
* @param string $expected Expected path to view / template * @param string $expected Expected path to view / template
* *
* @throws IncorrectBundleNameException
* @dataProvider provideViewPathAndBundleAndExtension * @dataProvider provideViewPathAndBundleAndExtension
*/ */
public function testGetBundleViewPathUsingCustomExtension($viewPath, $bundleName, $extension, $expected) public function testGetBundleViewPathUsingCustomExtension($viewPath, $bundleName, $extension, $expected)
@@ -84,6 +106,29 @@ class BundleTest extends BaseTestCase
]; ];
} }
/**
* Provides path of the view / template and incorrect name of bundle
*
* @return Generator
*/
public function provideViewPathAndIncorrectBundleName()
{
yield[
'User:Active',
'myExtra',
];
yield[
'User:Active',
'MyExtra',
];
yield[
'User:Active',
'MySuperExtraGorgeous',
];
}
/** /**
* Provides path of the view / template and name of bundle * Provides path of the view / template and name of bundle
* *
@@ -92,15 +137,21 @@ class BundleTest extends BaseTestCase
public function provideViewPathAndBundle() public function provideViewPathAndBundle()
{ {
yield[ yield[
'Ipsum', ':User',
'Lorem', 'MyExtraBundle',
'Lorem:Ipsum.html.twig', 'MyExtraBundle::User.html.twig',
]; ];
yield[ yield[
'FusceElementum', 'User:Active',
'LobortisTincidunt', 'MyExtraBundle',
'LobortisTincidunt:FusceElementum.html.twig', 'MyExtraBundle:User:Active.html.twig',
];
yield[
'User:Active',
'MySuperExtraGorgeousBundle',
'MySuperExtraGorgeousBundle:User:Active.html.twig',
]; ];
} }
@@ -112,17 +163,17 @@ class BundleTest extends BaseTestCase
public function provideViewPathAndBundleAndExtension() public function provideViewPathAndBundleAndExtension()
{ {
yield[ yield[
'Ipsum', 'User:Active',
'Lorem', 'MyExtraBundle',
'', '',
null, null,
]; ];
yield[ yield[
'Ipsum', 'User:Active',
'Lorem', 'MyExtraBundle',
'js.twig', 'js.twig',
'Lorem:Ipsum.js.twig', 'MyExtraBundle:User:Active.js.twig',
]; ];
} }
} }