1

在使用类进行功能测试时,@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()));
    }

    ...

?>

如果我从浏览器调用控制器,这工作正常。但是在 a 中调用相同的 URL 会WebTestCase产生以下异常:

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}

任何想法如何解决这一问题?

4

4 回答 4

2

尝试将此添加到您的 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/'));
....
于 2012-11-27T12:04:27.597 回答
2

虽然这个问题已经很老了,但我想补充一点:

请确保您在测试期间始终指向您的 phpunit.xml(.dist) 文件。

这意味着:

# OK
phpunit -c app/ src/MyNamespace/Bundlee/MyBundle/Controller/MyControllerTest.php

将工作。

然而-c app,由于 Symfony 无法正常启动,因此添加该命令会破坏测试运行程序:

# ERROR
phpunit src/MyNamespace/Bundlee/MyBundle/Controller/MyControllerTest.php

在后一种情况下,自动加载无法正常初始化,因此 Doctrine Annotations 也无法正确注册。

不再需要 setUp 方法中提到的“调整”!

于 2013-11-28T14:30:53.660 回答
1

检查您的 phpunit.xml.dist 文件。您的引导参数应该指向该文件./autoload.php(假设该文件位于 Symfony 应用程序目录中)。如果您的引导值是../vendor/autoload.php您没有注册“loadClass”。

于 2014-08-25T11:58:27.650 回答
1

我遇到了同样的问题。原来我的 phpunit.xml 中有以下内容:

<phpunit backupGlobals="false"
         ...
         bootstrap="vendor/autoload.php">

更改的值为我bootstrap解决app/bootstrap.php.cache了问题。注释加载器现在已正确注册。

如果你看一下https://github.com/symfony/symfony-standard/blob/2.8/app/phpunit.xml.dist#L8,你会看到默认的 Symfony 应用程序使用这个文件,因为它是 PHPUnit 引导程序也是。

请注意,我的 phpunit.xml 在我的项目的根目录中,而不是在app目录中。如果 phpunit.xml 文件在app目录中,您可以简单地使用bootstrap.php.cache.

于 2015-08-14T10:34:45.197 回答