LoginSignup
14
13

More than 5 years have passed since last update.

EC-CUBE3のメモ - ユニットテスト -

Last updated at Posted at 2015-03-23

画面を作ってみる
の続き。

画面ができたら、次はユニットテストを作ります。

テスト範囲

テストポリシーに記載がありますが、以下3つはテスト書くことが望ましい。(Serviceは常に必須)

  • Routing
  • Service
  • FormType

でもそんなに難しくない。
テストはtestsディレクトリにあります。

Routingのテスト

コントローラ単位で作成。
tests/Eccube/Tests/Web/Admin/Basis/PointController.php

namespace Eccube\Tests\Web;

use Silex\WebTestCase;
use Eccube\Application;

class PointControllerTest extends WebTestCase
{
    public $app;

    public function createApplication()
    {
        $app = new Application();
        $this->app = $app;

        $app['debug'] = true;
        $app['session.test'] = true;
        $app['exception_handler']->disable();

        return $app;
    }

    public function testRoutingAdminBasisPoint()
    {
        $client = $this->createClient();
        $crawler = $client->request('GET', '/admin/basis/point');
        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

createApplicationの部分は、初期化です。そのままコピペでよいでしょう。

実際のテストはtestRoutingAdminBasisPointです。
URLにアクセスし、レスポンスを確認する、という内容。厳密にはもっと色々必要ですが、まずはこれくらい。

Serviceのテスト

今回つくったポイント管理には、Serviceクラスはないので、今回はなし。

FormTypeのテスト

namespace Eccube\Tests\Form\Type;

use Symfony\Component\Form\Test\TypeTestCase;

class PointTypeTest extends TypeTestCase
{

    /** @var \Eccube\Application */
    private $app;

    /** @var array デフォルト値(正常系)を設定 */
    private $formData = array(
        'point_rate' => 10,
        'welcome_point' => 10,
    );

    public function setUp()
    {
        parent::setUp();

        $this->app = new \Eccube\Application;
        $this->app['debug'] = true;
        $this->app['session.test'] = true;
        $this->app['exception_handler']->disable();

        // CSRF tokenを無効にしてFormを作成
        $this->form = $this->app['form.factory']
            ->createBuilder('point', null, array(
                'csrf_protection' => false,
            ))
            ->getForm();
    }

    public function testValidData()
    {
        $this->form->submit($this->formData);
        $this->assertTrue($this->form->isValid());
    }

    public function testInvalidPointRate_NotBlank()
    {
        $this->formData['point_rate'] = '';
        $this->form->submit($this->formData);
        $this->assertFalse($this->form->isValid());
    }

    public function testInvalidPointRate_Min()
    {
        $this->formData['point_rate'] = -1;
        $this->form->submit($this->formData);
        $this->assertFalse($this->form->isValid());
    }

    public function testInvalidPointRate_Max()
    {
        $this->formData['point_rate'] = 101;
        $this->form->submit($this->formData);
        $this->assertFalse($this->form->isValid());
    }

以下welcome_pointのテスト...

setupは、コントローラと同様、初期化処理。
それ以降は、フォームのバリデーションをテストしていきます。

  • testValidData
    • 期待する値が入ってきた際のテスト
  • testInvalidPointRate_NotBlank
    • 空白が投入された際のテスト
  • testInvalidPointRate_Min
    • 最小値チェック
  • testInvalidPointRate_Max
    • 最大値チェック

ユニットテストの実行

テストを作ったら、テストを実行します。

$ vendor/bin/phpunit (ec-cubeのルートディレクトリで実行)
PHPUnit 3.7.38 by Sebastian Bergmann.
The Xdebug extension is not loaded. No code coverage will be generated.
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from /vagrant/eccube3/phpunit.xml.dist

............ 

Time: 9.54 seconds, Memory: 14.25Mb
OK (7 tests, 7 assertions)

「OK」が表示され、グリーンになれば成功です。

上記の場合、すべてのテストが実行されるので、少し時間がかかります。

個別にテスト実行する場合は、以下のようにします。

$ vendor/bin/phpunit tests/Eccube/Tests/Form/Type/PointTypeTest.php

14
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13