ControllerNameParserTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\ClassLoader\ClassLoader;
  14. class ControllerNameParserTest extends TestCase
  15. {
  16. protected $loader;
  17. protected function setUp()
  18. {
  19. $this->loader = new ClassLoader();
  20. $this->loader->addPrefixes(array(
  21. 'TestBundle' => __DIR__.'/../Fixtures',
  22. 'TestApplication' => __DIR__.'/../Fixtures',
  23. ));
  24. $this->loader->register();
  25. }
  26. protected function tearDown()
  27. {
  28. spl_autoload_unregister(array($this->loader, 'loadClass'));
  29. $this->loader = null;
  30. }
  31. public function testParse()
  32. {
  33. $parser = $this->createParser();
  34. $this->assertEquals('TestBundle\FooBundle\Controller\DefaultController::indexAction', $parser->parse('FooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  35. $this->assertEquals('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction', $parser->parse('FooBundle:Sub\Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  36. $this->assertEquals('TestBundle\Fabpot\FooBundle\Controller\DefaultController::indexAction', $parser->parse('SensioFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  37. $this->assertEquals('TestBundle\Sensio\Cms\FooBundle\Controller\DefaultController::indexAction', $parser->parse('SensioCmsFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  38. $this->assertEquals('TestBundle\FooBundle\Controller\Test\DefaultController::indexAction', $parser->parse('FooBundle:Test\\Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  39. $this->assertEquals('TestBundle\FooBundle\Controller\Test\DefaultController::indexAction', $parser->parse('FooBundle:Test/Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
  40. try {
  41. $parser->parse('foo:');
  42. $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an a:b:c string');
  43. } catch (\Exception $e) {
  44. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an a:b:c string');
  45. }
  46. }
  47. public function testBuild()
  48. {
  49. $parser = $this->createParser();
  50. $this->assertEquals('FoooooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
  51. $this->assertEquals('FoooooBundle:Sub\Default:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
  52. try {
  53. $parser->build('TestBundle\FooBundle\Controller\DefaultController::index');
  54. $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  55. } catch (\Exception $e) {
  56. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  57. }
  58. try {
  59. $parser->build('TestBundle\FooBundle\Controller\Default::indexAction');
  60. $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  61. } catch (\Exception $e) {
  62. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  63. }
  64. try {
  65. $parser->build('Foo\Controller\DefaultController::indexAction');
  66. $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  67. } catch (\Exception $e) {
  68. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string');
  69. }
  70. }
  71. /**
  72. * @dataProvider getMissingControllersTest
  73. */
  74. public function testMissingControllers($name)
  75. {
  76. $parser = $this->createParser();
  77. try {
  78. $parser->parse($name);
  79. $this->fail('->parse() throws a \InvalidArgumentException if the class is found but does not exist');
  80. } catch (\Exception $e) {
  81. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws a \InvalidArgumentException if the class is found but does not exist');
  82. }
  83. }
  84. public function getMissingControllersTest()
  85. {
  86. return array(
  87. array('FooBundle:Fake:index'), // a normal bundle
  88. array('SensioFooBundle:Fake:index'), // a bundle with children
  89. );
  90. }
  91. /**
  92. * @dataProvider getInvalidBundleNameTests
  93. */
  94. public function testInvalidBundleName($bundleName, $suggestedBundleName)
  95. {
  96. $parser = $this->createParser();
  97. try {
  98. $parser->parse($bundleName);
  99. $this->fail('->parse() throws a \InvalidArgumentException if the bundle does not exist');
  100. } catch (\Exception $e) {
  101. $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws a \InvalidArgumentException if the bundle does not exist');
  102. if (false === $suggestedBundleName) {
  103. // make sure we don't have a suggestion
  104. $this->assertNotContains('Did you mean', $e->getMessage());
  105. } else {
  106. $this->assertContains(sprintf('Did you mean "%s"', $suggestedBundleName), $e->getMessage());
  107. }
  108. }
  109. }
  110. public function getInvalidBundleNameTests()
  111. {
  112. return array(
  113. 'Alternative will be found using levenshtein' => array('FoodBundle:Default:index', 'FooBundle:Default:index'),
  114. 'Alternative will be found using partial match' => array('FabpotFooBund:Default:index', 'FabpotFooBundle:Default:index'),
  115. 'Bundle does not exist at all' => array('CrazyBundle:Default:index', false),
  116. );
  117. }
  118. private function createParser()
  119. {
  120. $bundles = array(
  121. 'SensioFooBundle' => array($this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundle\Sensio\FooBundle', 'SensioFooBundle')),
  122. 'SensioCmsFooBundle' => array($this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle')),
  123. 'FooBundle' => array($this->getBundle('TestBundle\FooBundle', 'FooBundle')),
  124. 'FabpotFooBundle' => array($this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'), $this->getBundle('TestBundle\Sensio\FooBundle', 'SensioFooBundle')),
  125. );
  126. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  127. $kernel
  128. ->expects($this->any())
  129. ->method('getBundle')
  130. ->will($this->returnCallback(function ($bundle) use ($bundles) {
  131. if (!isset($bundles[$bundle])) {
  132. throw new \InvalidArgumentException(sprintf('Invalid bundle name "%s"', $bundle));
  133. }
  134. return $bundles[$bundle];
  135. }))
  136. ;
  137. $bundles = array(
  138. 'SensioFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
  139. 'SensioCmsFooBundle' => $this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle'),
  140. 'FoooooBundle' => $this->getBundle('TestBundle\FooBundle', 'FoooooBundle'),
  141. 'FooBundle' => $this->getBundle('TestBundle\FooBundle', 'FooBundle'),
  142. 'FabpotFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
  143. );
  144. $kernel
  145. ->expects($this->any())
  146. ->method('getBundles')
  147. ->will($this->returnValue($bundles))
  148. ;
  149. return new ControllerNameParser($kernel);
  150. }
  151. private function getBundle($namespace, $name)
  152. {
  153. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
  154. $bundle->expects($this->any())->method('getName')->will($this->returnValue($name));
  155. $bundle->expects($this->any())->method('getNamespace')->will($this->returnValue($namespace));
  156. return $bundle;
  157. }
  158. }