它表明在使用@Route
类进行功能测试时,WebTestCase
注释不起作用。
我的控制器如下所示:
<?php
namespace Foo\ShopBundle\Controller;
use Foo\ShopBundle\Entity\Cart;
use Foo\ShopBundle\Entity\ProductInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* @Route("cart")
*/
class CartController extends Controller
{
const CART_ID = 'cart_id';
/**
* @Route("/")
* @Template()
*/
public function indexAction()
{
return array('cart' => $this->getCart($this->getCurrentUserName(), $this->getCurrentSessionId()));
}
...
?>
如果我从浏览器中调用控制器,这将很好地工作。但是在
WebTestCase
中调用相同的URL会产生以下例外:Doctrine\Common\Annotations\AnnotationException : [Semantical Error] The annotation "@Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" in class Foo\ShopBundle\Controller\CartController does not exist, or could not be auto-loaded.
#0 /Users/ernst/Source/php/cinergy/shop/vendor/doctrine/common/lib/Doctrine/Common/Annotations/DocParser.php(597): Doctrine\Common\Annotations\AnnotationException::semanticalError('The annotation ...')
#1 /Users/ernst/Source/php/cinergy/shop/vendor/doctrine/common/lib/Doctrine/Common/Annotations/DocParser.php(533): Doctrine\Common\Annotations\DocParser->Annotation()
#2 /Users/ernst/Source/php/cinergy/shop/vendor/doctrine/common/lib/Doctrine/Common/Annotations/DocParser.php(297): Doctrine\Common\Annotations\DocParser->Annotations()
#3 /Users/ernst/Source/php/cinergy/shop/vendor/doctrine/common/lib/Doctrine/Common/Annotations/AnnotationReader.php(151): Doctrine\Common\Annotations\DocParser->parse('/**? * @Route("...', 'class Foo\S...')
#4 /Users/ernst/Source/php/cinergy/shop/vendor/doctrine/common/lib/Doctrine/Common/Annotations/FileCacheReader.php(90): Doctrine\Common\Annotations\AnnotationReader->getClassAnnotations(Object(ReflectionClass))
#5 /Users/ernst/Source/php/cinergy/shop/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/Metadata/Driver/AnnotationDriver.php(65): Doctrine\Common\Annotations\FileCacheReader->getClassAnnotations(Object(ReflectionClass))
#6 /Users/ernst/Source/php/cinergy/shop/vendor/jms/metadata/src/Metadata/Driver/LazyLoadingDriver.php(20): JMS\DiExtraBundle\Metadata\Driver\AnnotationDriver->loadMetadataForClass(Object(ReflectionClass))
#7 /Users/ernst/Source/php/cinergy/shop/vendor/jms/metadata/src/Metadata/MetadataFactory.php(77): Metadata\Driver\LazyLoadingDriver->loadMetadataForClass(Object(ReflectionClass))
#8 /Users/ernst/Source/php/cinergy/shop/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerResolver.php(84): Metadata\MetadataFactory->getMetadataForClass('Foo\ShopBun...')
#9 /Users/ernst/Source/php/cinergy/shop/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/HttpKernel/ControllerInjectorsWarmer.php(31): JMS\DiExtraBundle\HttpKernel\ControllerResolver->createInjector('Foo\ShopBun...')
#10 /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php(47): JMS\DiExtraBundle\HttpKernel\ControllerInjectorsWarmer->warmUp('/Users/ernst/So...')
#11 /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(595): Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp('/Users/ernst/So...')
#12 /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(133): Symfony\Component\HttpKernel\Kernel->initializeContainer()
#13 /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php(43): Symfony\Component\HttpKernel\Kernel->boot()
#14 /Users/ernst/Source/php/cinergy/shop/src/Foo/ShopBundle/Tests/Controller/CartControllerFunctionalTest.php(19): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()
#15 [internal function]: Foo\ShopBundle\Tests\Controller\CartControllerFunctionalTest->testIndexAction()
#16 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestCase.php(967): ReflectionMethod->invokeArgs(Object(Foo\ShopBundle\Tests\Controller\CartControllerFunctionalTest), Array)
#17 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestCase.php(825): PHPUnit_Framework_TestCase->runTest()
#18 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestResult.php(649): PHPUnit_Framework_TestCase->runBare()
#19 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestCase.php(770): PHPUnit_Framework_TestResult->run(Object(Foo\ShopBundle\Tests\Controller\CartControllerFunctionalTest))
#20 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestSuite.php(776): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#21 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestSuite.php(746): PHPUnit_Framework_TestSuite->runTest(Object(Foo\ShopBundle\Tests\Controller\CartControllerFunctionalTest), Object(PHPUnit_Framework_TestResult))
#22 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/Framework/TestSuite.php(706): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#23 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/TextUI/TestRunner.php(325): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#24 /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/TextUI/Command.php(177): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#25 /private/var/folders/kc/ylyxk45170z_g5h6c2_vr0_00000gp/T/ide-phpunit.php(102): PHPUnit_TextUI_Command->run(Array, true)
#26 /private/var/folders/kc/ylyxk45170z_g5h6c2_vr0_00000gp/T/ide-phpunit.php(442): IDE_PHPUnit_TextUI_Command::main()
#27 {main}
任何想法如何解决这一问题?
最佳答案
尝试将其添加到您的setUp()方法中(不要忘记定义$ vendorDir)
use Doctrine\Common\Annotations\AnnotationRegistry;
....
public function setUp()
{
/** To make annotations work here */
AnnotationRegistry::registerAutoloadNamespaces(array('Sensio\\Bundle\\FrameworkExtraBundle' => $vendorDir . '/sensio/framework-extra-bundle/'));
....
关于testing - WebTestCase不会自动加载@Route批注,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13582055/