加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在symfony 2功能测试中找不到路由

发布时间:2020-12-13 22:45:08 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试编写一个symfony 2功能测试.这是我的代码: ?phpnamespace WebSiteMainBundleTestsController;use SymfonyBundleFrameworkBundleTestWebTestCase;class ProductControllerTest extends WebTestCase{ public function testPageContents() {
我正在尝试编写一个symfony 2功能测试.这是我的代码:

<?php

namespace WebSiteMainBundleTestsController;

use SymfonyBundleFrameworkBundleTestWebTestCase;

class ProductControllerTest extends WebTestCase
{

    public function testPageContents()
    {
        $domCategoryLinksExpr = '.catalog-col-block > ul > li > a';

        $client = static::createClient();

        $crawler = $client->request('GET','/catalog/');
        $this->assertTrue($client->getResponse()->getStatusCode() == '200');

        $countCategories = $crawler->filter($domCategoryLinksExpr)->count();
        $this->assertTrue($crawler->filter($domCategoryLinksExpr)->count() > 0);

        $categoryLink = $crawler->filter($domCategoryLinksExpr)->eq(rand(1,$countCategories))->link();
        $crawler = $client->click($categoryLink);
    }
}

但是当我运行这个测试时:

phpunit -c app src/WebSite/MainBundle/Tests/Controller/

我懂了:

1) WebSiteMainBundleTestsControllerProductControllerTest::testPageContents
SymfonyComponentHttpKernelExceptionNotFoundHttpException: No route found for “GET /app_dev.php/catalog/psp”

/app_dev.php/catalog/psp是$categoryLink-> getUri()的动态值.和
此路由存在且在Web浏览器中正常工作.有任何想法吗?

UPD:

这是我的路由规则:

routing_dev.yml:
   ... 
   _main:
       resource: routing.yml
   ....

routing.yml:
    ....
    WebSiteCategoryBundle:
       resource: "@WebSiteCategoryBundle/Controller/"
       type:     annotation
       prefix:   /    
    ....

src/WebSite/CategoryBundle/CategoryController.php:
    /**
    * @Route("/catalog")
    */
    class CategoryController extends Controller
    {
        /**
        * @Route("/{alias}",name="show_category" )
        * @Template()
        */
        public function showAction( $alias )
        {
           // some action here
        }
    }

它在浏览器中运行良好,但似乎$crowler确实没有看到这个注释规则.

UPD2:问题出现在Symfony 2标准版中缺少的“routing_test.yml”中.
所以我创建它:

routing_test.yml:
    _main:
        resource: routing_dev.yml

和异常消失.谢谢大家.

解决方法

如果您发布了routing.yml,那就太好了

你也可以看看:
http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/routing.html

您可以在操作中使用路由注释.

为了解决您的问题,我希望看到您的路由配置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读