LoginSignup
53
57

More than 5 years have passed since last update.

FuelPHP【チュートリアル】-- コントローラーのテストの方法 --

Posted at

FuelPHPのコントローラーのテストの書き方がなかったので試して見た

環境

  • centos6.3
  • fuelPHP1.5.3

事前準備に必要なもの

参考にしたサイト

testコントローラーの配置場所

デフォルトでfuel/app/tests/controllerのディレクトリが作成済みですのでそこにコントローラーのテストクラスを作成していきます。

fuel/app/tests
      ├── controller
      │   └── auth.php
      ├── model
      └── view

コントローラー側

コントローラー側でテストをする際はView側にアサインした値の検証が出来れば大体のことはテストできると思います。そこでこんなサンプルを作ります。

fuel/app/classes/controller/hoge.php
<?php

class Controller_Hoge extends Controller_Hybrid
{
    public function before()
    {
        parent::before();
    }

    public function action_fuga()
    {
        $this->template->title = 'htmlでのtitileタグ';
        $this->template->content = View::forge('hoge/fuga');
        //CSSを追加する
        $this->template->add_css = array('hoge.css');
    }   
}

コントローラーテストクラスの作成

コントローラーの雛形です。

fuel/app/tests/controller/hoge.php
/**
 * Tests for hoge
 *
 * @group App
 * @group Controller
 * @group Hoge
 */
class Test_Controller_Hoge extends TestCase
{
    public function setup()
    {

    }
}

値を検証する方法

先ほど作ったController_Hogeのadd_cssの値を検証する場合

fuel/app/tests/controller/hoge.php
    /**
     * @test
     * @group hogecontroller
     **/
    public function test_add_cssの値が入っているか確認する()
    {
        $expected = array('hoge.css');

        $response = Request::forge('hoge/fuga')
                             ->execute()
                             ->response();

        $assertValue = $response->body->add_css;

        $this->assertSame($expected, $assertValue);
    }

上記でoilコマンドを実行すればOKが返ってきます

php oil t --group=hogecontroller

Tests Running...This may take a few moments.
PHPUnit 3.7.18 by Sebastian Bergmann.

Configuration read from /home/web/lo.fuel.base.org/fuel/app/phpunit.xml

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertion)

controller内でのRedirect()を実行の場合

FuelPHPではRedirect()を実行するとexitが入っていてテスト自体も止まってしまいます

fuel/app/classes/controller/hoge.php
<?php

class Controller_Hoge extends Controller_Hybrid
{
    public function action_fugaredirect()
    {
        $this->template->is_error = true;
        //Response::redirectの中身でexitを実行しているので止まってしまう
        return Response::redirect('error/404');
    }
}

fuel/core/response.php

fuel/core/classes/response.php
    public static function redirect($url = '', $method = 'location', $code = 302)
    {
        $response = new static;

        $response->set_status($code);

        if (strpos($url, '://') === false)
        {
            $url = $url !== '' ? \Uri::create($url) : \Uri::base();
        }

        strpos($url, '*') !== false and $url = \Uri::segment_replace($url);

        if ($method == 'location')
        {
            $response->set_header('Location', $url);
        }
        elseif ($method == 'refresh')
        {
            $response->set_header('Refresh', '0;url='.$url);
        }
        else
        {
            return;
        }

        $response->send(true);
        exit; ##<= テストを止めてしまう
    }

そこでresponse.phpを独自のpackageで作成してテストのときは止めないようにしてみましょう。

fuel/packages/my/classes/response.php
namespace My;

class Response extends \Fuel\Core\Response
{
    /**
     * Override Fuel\Core\Response redirect method
     *
     * @param   string  $url     The url
     * @param   string  $method  The redirect method
     * @param   int     $code    The redirect status code
     *
     * @return  void
     */
    public static function redirect($url = '', $method = 'location', $code = 302)
    {
        $response = new static;
        $response->set_status($code);

        if (strpos($url, '://') === false)
        {
            $url = $url !== '' ? \Uri::create($url) : \Uri::base();
        }

        strpos($url, '*') !== false and $url = \Uri::segment_replace($url);

        if ($method == 'location')
        {
            $response->set_header('Location', $url);
        }
        elseif ($method == 'refresh')
        {
            $response->set_header('Refresh', '0;url='.$url);
        }
        else
        {
            return;
        }

        //テストではない場合はexitを実行
        if (\Fuel\Core\Fuel::$env != 'test')
        {
            $response->send(true);
            exit;
        }

        $response->send(true);
    }
}

そしてこのクラスをロードするようにfuel/app/config/config.phpに追加します

fuel/app/config/config.php
        'packages'  => array(
            'my', //追加する
        ),

redirectを実行したコントローラーのテストを実行してみる

先ほどのコントローラーでアサインしたis_errorでアサートできるかテストを書いてみましょう

コントローラー側

fuel/app/classes/controller/hoge.php
<?php

class Controller_Hoge extends Controller_Hybrid
{
    public function action_fugaredirect()
    {
        $this->template->is_error = true;
        return Response::redirect('error/404');
    }
}

テスト側

fuel/app/tests/controller/hoge.php
    /**
     * @test
     * @group hogeredirect
     **/
    public function test_is_errorがtrueか()
    {
        $expected = true;

        $_GET = array('username' => 'hoge', 'password' => 'huga');
        $response = Request::forge('auth/account')
                           ->execute()
                           ->response();

        $assertValue = $response->body->is_error;
        $this->assertSame($expected, $assertValue);
    }

テスト実行

php oil t --group=hogeredirect

Tests Running...This may take a few moments.
PHPUnit 3.7.18 by Sebastian Bergmann.

Configuration read from /home/web/lo.fuel.base.org/fuel/app/phpunit.xml

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertion)

テストが動きました。

53
57
0

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
53
57