Файл: symfony-2.7/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php
Строк: 473
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyBundleFrameworkBundleTestsController;
use SymfonyBundleFrameworkBundleTestsTestCase;
use SymfonyBundleFrameworkBundleControllerControllerNameParser;
use SymfonyComponentClassLoaderClassLoader;
class ControllerNameParserTest extends TestCase
{
protected $loader;
protected function setUp()
{
$this->loader = new ClassLoader();
$this->loader->addPrefixes(array(
'TestBundle' => __DIR__.'/../Fixtures',
'TestApplication' => __DIR__.'/../Fixtures',
));
$this->loader->register();
}
public function tearDown()
{
spl_autoload_unregister(array($this->loader, 'loadClass'));
$this->loader = null;
}
public function testParse()
{
$parser = $this->createParser();
$this->assertEquals('TestBundleFooBundleControllerDefaultController::indexAction', $parser->parse('FooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
$this->assertEquals('TestBundleFooBundleControllerSubDefaultController::indexAction', $parser->parse('FooBundle:SubDefault:index'), '->parse() converts a short a:b:c notation string to a class::method string');
$this->assertEquals('TestBundleFabpotFooBundleControllerDefaultController::indexAction', $parser->parse('SensioFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
$this->assertEquals('TestBundleSensioCmsFooBundleControllerDefaultController::indexAction', $parser->parse('SensioCmsFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
$this->assertEquals('TestBundleFooBundleControllerTestDefaultController::indexAction', $parser->parse('FooBundle:Test\Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
$this->assertEquals('TestBundleFooBundleControllerTestDefaultController::indexAction', $parser->parse('FooBundle:Test/Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
try {
$parser->parse('foo:');
$this->fail('->parse() throws an InvalidArgumentException if the controller is not an a:b:c string');
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws an InvalidArgumentException if the controller is not an a:b:c string');
}
}
public function testBuild()
{
$parser = $this->createParser();
$this->assertEquals('FooBundle:Default:index', $parser->build('TestBundleFooBundleControllerDefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
$this->assertEquals('FooBundle:SubDefault:index', $parser->build('TestBundleFooBundleControllerSubDefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
try {
$parser->build('TestBundleFooBundleControllerDefaultController::index');
$this->fail('->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
}
try {
$parser->build('TestBundleFooBundleControllerDefault::indexAction');
$this->fail('->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
}
try {
$parser->build('FooControllerDefaultController::indexAction');
$this->fail('->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws an InvalidArgumentException if the controller is not an aController::cAction string');
}
}
/**
* @dataProvider getMissingControllersTest
*/
public function testMissingControllers($name)
{
$parser = $this->createParser();
try {
$parser->parse($name);
$this->fail('->parse() throws a InvalidArgumentException if the string is in the valid format, but not matching class can be found');
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws a InvalidArgumentException if the class is found but does not exist');
}
}
public function getMissingControllersTest()
{
return array(
array('FooBundle:Fake:index'), // a normal bundle
array('SensioFooBundle:Fake:index'), // a bundle with children
);
}
/**
* @expectedException
* @dataProvider getInvalidBundleNameTests
*/
public function testInvalidBundleName($bundleName, $suggestedBundleName)
{
$parser = $this->createParser();
try {
$parser->parse($bundleName);
} catch (Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '->parse() throws a InvalidArgumentException if the bundle does not exist');
if (false === $suggestedBundleName) {
// make sure we don't have a suggestion
$this->assertNotContains('Did you mean', $e->getMessage());
} else {
$this->assertContains(sprintf('Did you mean "%s"', $suggestedBundleName), $e->getMessage());
}
}
}
public function getInvalidBundleNameTests()
{
return array(
array('FoodBundle:Default:index', 'FooBundle:Default:index'),
array('CrazyBundle:Default:index', false),
);
}
private function createParser()
{
$bundles = array(
'SensioFooBundle' => array($this->getBundle('TestBundleFabpotFooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundleSensioFooBundle', 'SensioFooBundle')),
'SensioCmsFooBundle' => array($this->getBundle('TestBundleSensioCmsFooBundle', 'SensioCmsFooBundle')),
'FooBundle' => array($this->getBundle('TestBundleFooBundle', 'FooBundle')),
'FabpotFooBundle' => array($this->getBundle('TestBundleFabpotFooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundleSensioFooBundle', 'SensioFooBundle')),
);
$kernel = $this->getMock('SymfonyComponentHttpKernelKernelInterface');
$kernel
->expects($this->any())
->method('getBundle')
->will($this->returnCallback(function ($bundle) use ($bundles) {
if (!isset($bundles[$bundle])) {
throw new InvalidArgumentException(sprintf('Invalid bundle name "%s"', $bundle));
}
return $bundles[$bundle];
}))
;
$bundles = array(
'SensioFooBundle' => $this->getBundle('TestBundleFabpotFooBundle', 'FabpotFooBundle'),
'SensioCmsFooBundle' => $this->getBundle('TestBundleSensioCmsFooBundle', 'SensioCmsFooBundle'),
'FooBundle' => $this->getBundle('TestBundleFooBundle', 'FooBundle'),
'FabpotFooBundle' => $this->getBundle('TestBundleFabpotFooBundle', 'FabpotFooBundle'),
);
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue($bundles))
;
return new ControllerNameParser($kernel);
}
private function getBundle($namespace, $name)
{
$bundle = $this->getMock('SymfonyComponentHttpKernelBundleBundleInterface');
$bundle->expects($this->any())->method('getName')->will($this->returnValue($name));
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue($namespace));
return $bundle;
}
}