FuelPHPをやってみる (24) - Goutteでコントローラーテスト -

2012-12-06T00:00:00+00:00 FuelPHP PHP

何やら https://github.com/fabpot/Goutte っていのが便利らしく使ってみた

fuel/app/views/home.php

<html>
  <body>
    <h2>Login Form</h2>
    <form id="login_form" action="/home/login" method="post">
      <input type="text" name="username" />
      <input type="password" name="password" />
      <input type="submit" value="login" />
    </form>
  </body>
</html>

fuel/app/classes/controller/home.php

<?php

class Controller_Home extends Controller {
    public function get_index() {
        return View::forge("home");
    }

    public function post_login() {
        $username = Input::post("username");
        $password = Input::post("password");

        if ($username === "demo" && $password === "demo") {
            return Response::forge("OK");
        }

        Response::redirect("/");
    }
}

んまぁ単純にビューを表示して、そこからポストされたデータを検証してレスポンス出したり、リダイレクトしたりするだけ。これをGoutteを使ってテストする

fuel/app/tests/controller/test_home.php

<?php

require_once APPPATH."goutte.phar";

class Test_Controller_Home extends TestCase {

    private $goutte;

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

        $this->goutte = new GoutteClient();
        $this->goutte->followRedirects(false);
    }

    public function test_get_index1() {
        $crawler = $this->goutte->request("GET", Uri::create("/"));

        // Goutteでリクエストした結果のレスポンスを取得
        $response = $this->goutte->getResponse();
        $this->assertThat($response, $this->logicalNot($this->isNull()));
        $this->assertThat($response->getStatus(), $this->equalTo(200));

        // fuel/app/views/home.phpの<h2>内のテキストを検証
        $this->assertThat($crawler->filter("h2")->text(), $this->equalTo("Login Form"));

    }

    public function test_get_index2() {
        $crawler = $this->goutte->request("GET", Uri::create("/"));

        // ボタンからformを取得
        $form = $crawler->selectButton("login")->form();

        // データを設定せずに普通にサブミット
        $crawler = $this->goutte->submit($form);

        // サブミットした結果のレスポンスを取得
        $response = $this->goutte->getResponse();
        $this->assertThat($response, $this->logicalNot($this->isNull()));
        $this->assertThat($response->getStatus(), $this->equalTo(302));
    }

    public function test_get_index3() {
        $crawler = $this->goutte->request("GET", Uri::create("/"));
        $form = $crawler->selectButton("login")->form();

        // データを設定してサブミット
        $crawler = $this->goutte->submit(
            $form,
            array(
                "username" => "demo",
                "password" => "demo"
            )
        );

        $response = $this->goutte->getResponse();

        $this->assertThat($response, $this->logicalNot($this->isNull()));
        $this->assertThat($response->getStatus(), $this->equalTo(200));
        $this->assertThat($response->getContent(), $this->equalTo("OK"));
    }
}

っていう感じな模様。ちょっとmechanizeチックに出来る模様

ちょっと難点かなって思ったのが、これSymfonyなパッケージを使ってる模様なので、テストで使う際のメソッドとか調べるのが若干微妙な感じもしなくはないけど、利用性はかなり高そうなので という感じですかね。

FuelPHPをやってみる (25) - ログ出力をテストする - joda-timeとhamcrest