testing - WebTestCase 不会自动加载@Route 注释

标签 testing symfony annotations routes

在使用 WebTestCase 类进行功能测试时,@Route 注释似乎不起作用。

我的 Controller 看起来像这样:

<?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()));
    }

    ...

?>

如果我从我的浏览器调用 Controller ,这工作正常。但是在 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() 方法(不要忘记定义 $vendor Dir)

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/

相关文章:

r - 如何在 ggplot 中注释图框之外的图?

android - AbstractProcessor 不生成类

android - 使用 Android-Espresso 运行多个测试时出现内存不足异常

unit-testing - 您将如何实现,以便每个测试类将输出记录到一个单独的文件中?

symfony - 模式和路径有什么区别?

symfony - FOSElasticaBundle上的未定义索引

php - 如何仅在 Doctrine 中按日期部分分组

JAVA - 如何从Annotation获取注解?

testing - 如何使用 Jest 来测试iFrame的内容

git - 在功能分支上测试时如何使用 Git pull 请求