テストでフィクスチャが読み込めない

40 views
Skip to first unread message

田中宏典

unread,
Oct 19, 2016, 7:53:42 PM10/19/16
to 日本Symfonyユーザー会
Crawlerを用いたファンクショナルテストで、フィクスチャの読み込みクラスBlogArticleLoaderがうまく読み込めません。
フィクスチャで20件のダミーデータを生成し、画面表示されるかどうかを確認するためにダミーデータの件数をカウントしています。
しかし、結果が0件となってしまいます。

http://127.0.0.1:8000/blog/にアクセスすると画面表示自体はうまくいっているため、フィクスチャの読み込みクラスBlogArticleLoaderが動作することは確認しています。
また、$crawlerを出力してみてヘッダーとフッターは表示されたことから、テストコード内のGETリクエストも動作することは確認しています。

なぜテストがうまくいかないのでしょうか?
もしくは、その原因を探るためにデバッグするにはどうすればよいでしょうか?


■環境
Symfony 3.1.4
PHPUnit 5.6.1
nelmio/alice 2.2.2
hautelook/alice-bundle v1.3.1
liip/functional-test-bundle 1.6.3


■ターミナル
$ php phpunit.phar -c ./ --debug src/AppBundle/Tests/Controller/BlogControllerTest.php
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.
Starting test 'AppBundle\Tests\Controller\BlogControllerTest::ブログ記事一覧が表示されること'.
F                                                                   1 / 1 (100%)
Time: 564 ms, Memory: 35.00MB
There was 1 failure:
1) AppBundle\Tests\Controller\BlogControllerTest::ブログ記事一覧が表示されること
Failed asserting that 0 matches expected 20.
/Users/tanaka/classic-symfony/src/AppBundle/Tests/Controller/BlogControllerTest.php:24
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


■テストコード
<?php
namespace AppBundle\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;
use AppBundle\DataFixtures\ORM\BlogArticleLoader;

class BlogControllerTest extends WebTestCase
{
    /**
     * @test
     */
    public function ブログ記事一覧が表示されること()
    {
        $this->loadFixtures([
            BlogArticleLoader::class
        ]);

        $client = static::createClient();
        $crawler = $client->request('GET', '/blog/');

        $this->assertThat(
            $crawler->filter('li.blog-article')->count(),
            $this->equalTo(20)
        );
    }
}


■テスト対象コード
<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class BlogController extends Controller
{
    /**
     * @Route("/blog/")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $blogArticleRepository = $em->getRepository('AppBundle:BlogArticle');
        $blogList = $blogArticleRepository->findBy([], ['targetDate' => 'DESC']);

        return $this->render('Blog/index.html.twig',
            ['blogList' => $blogList]);
    }
}


■フィクスチャの読み込み
<?php
namespace AppBundle\DataFixtures\ORM;

use Hautelook\AliceBundle\Doctrine\DataFixtures\AbstractLoader;
use Nelmio\Alice\Fixtures;

class BlogArticleLoader extends AbstractLoader
{
    public function getFixtures()
    {
        return array(
            __DIR__ . '/../../Resources/fixtures/BlogArticle.yml',
        );
    }
}


■フィクスチャデータ生成
src/AppBundle/Resources/fixtures/BlogArticle.yml
AppBundle\Entity\BlogArticle:
    blog{1..20}:
        title: <lastName()> <firstName()>の記事
        targetDate: <dateTime()>
        content: 今日は<numberBetween(1, 100)>人の来場者がありました。....

Reply all
Reply to author
Forward
0 new messages