使用@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}
任何想法如何解决这个问题?
答案 0 :(得分:2)
尝试将此添加到您的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/'));
....
答案 1 :(得分: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方法中提到的“tweaks”!
答案 2 :(得分:1)
检查你的phpunit.xml.dist文件。您的引导参数应指向文件./autoload.php
(假设该文件位于Symfony app目录中)。
如果您的引导值为../vendor/autoload.php
,则表示您未注册&#39; loadClass&#39;。
答案 3 :(得分: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
。