
Symfony2でrouting.ymlにドメイン指定したらWebTestCaseがこけたよってお話
July 22, 2015,
tags:
symfony
php
おはようございます。ポリドッグです。
さて、今日はrouting.ymlでドメイン指定したら、WebTestCaseがこけてしまったのでどう回避したかをここに残しておこうと思います。
例えば、特定のコントローラ配下はサブドメインにしたい場合はrouting.ymlにこんな感じに書けばいいんですよ。
// 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で適当にこんな感じにテスト書いてあるとここけるぽいです。
<?php
class UserControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/hello/polidog');
}
}
これを実行すると残念ながら404になりました。。。
解決策
ルーティングの設定をテストの時だけ変更してあげればいいので、routing_test.yml
を作成します。
// 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を読ませるようにする設定を追記すれば終わりです。
framework:
router:
resource: "%kernel.root_dir%/config/routing_test.yml"
strict_requirements: ~
こんな感じで書いておけば、ドメイン指定をした場合でもWebTestCaseがこけることなく実行できると思います。