Featured image of post Symfony2でrouting.ymlにドメイン指定したらWebTestCaseがこけたよってお話

Symfony2でrouting.ymlにドメイン指定したらWebTestCaseがこけたよってお話

Twitter ツイート Hatena Bookmark ブックマーク

おはようございます。ポリドッグです。
さて、今日はrouting.ymlでドメイン指定したら、WebTestCaseがこけてしまったのでどう回避したかをここに残しておこうと思います。

例えば、特定のコントローラ配下はサブドメインにしたい場合はrouting.ymlにこんな感じに書けばいいんですよ。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// vim app/config/routing.yml

main:
    resource: "@AppBundle/Controller/"
    type: annotation
    host: "{domain}"
    defaults:
        domain: "%domain%"
    requirements:
        domain: "%domain%"

user:
    resource: "@AppBundle/Controller/DomainController.php"
    type: annotation
    host: "user.{domain}"
    defaults:
        domain: "%domain%"
    requirements:
        domain: "%domain%"

で、この場合普通にWebTestCaseで適当にこんな感じにテスト書いてあるとここけるぽいです。

1
2
3
4
5
6
7
8
9
<?php
class UserControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/hello/polidog');
    }
}

これを実行すると残念ながら404になりました。。。

解決策

ルーティングの設定をテストの時だけ変更してあげればいいので、routing_test.ymlを作成します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// vim app/config/routing_test.yml

_main:
    resource: routing.yml


main:
    resource: "@AppBundle/Controller/"
    type: annotation

user:
    resource: "@AppBundle/Controller/DomainController.php"
    type: annotation

あとは、config_test.ymlにrouting_test.ymlを読ませるようにする設定を追記すれば終わりです。

1
2
3
4
framework:
    router:
        resource: "%kernel.root_dir%/config/routing_test.yml"
        strict_requirements: ~

こんな感じで書いておけば、ドメイン指定をした場合でもWebTestCaseがこけることなく実行できると思います。

参考

comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。