FuelPHPのコントローラーをWebサーバーにデプロイする事無くテストする

2013-03-07T00:00:00+00:00 FuelPHP PHP

あくまで「HTTPリクエストを行うのが前提」っていう所です

でコントローラーが

<?php

class Controller_Home extends Controller_Rest {

    protected $_supported_formats = array(
        "html" => "text/html",
        "json" => "application/json"
    );

    public function get_index() {
        if ($this->format == "html") {
            throw new HttpNotFoundException();
        }

        return $this->response(array("message" => "hoge fuga foobar"));
    }
}

な感じでController_Restなアクションコントローラーを用意。あくまでテストする名目なので適当

でこれをテストする方法において、Webサーバーにデプロイしている前提ではなくPHPに組み込まれているStandalone WebServerを使ってテストする。但し、事前的にサーバーを起動しておくような事をせずにテストするにはっていう所だと思うのですが

<?php

class SampleTestCase extends PHPUnit_Framework_TestCase {

    private $pid;

    public function setUp() {
        $this->pid = pcntl_fork();
    }

    public function tearDown() {
        posix_kill($this->pid, SIGINT);
    }

    public function test1() {
        if ($this->pid == 0) {
            pcntl_exec(
                "/home/kinjouj/.phpbrew/php/php-5.4.12/bin/php",
                ["-S", "localhost:8080", "-t", DOCROOT."/public"]
            );
        } elseif($this->pid) {
            sleep(1);

            $driver = Request::forge("http://localhost:8080/", "Curl");
            $driver->set_mime_type("json");

            $res = $driver->execute()->response();

            $this->assertThat($res->status, $this->equalTo(200));
            $this->assertThat(
                $res->body,
                $this->equalTo(array("message" => "hoge fuga foobar"))
            );

            var_dump($res);
        }
    }
}

(DOCROOTはphpunitでpublicな所に設定されてないのでStandalone WebServerで参照するルートがおかしくなるのでDOCROOT/publicを割り当てる)

oil testすると

Tests Running...This may take a few moments.
[Thu Mar  7 14:01:17 2013] 127.0.0.1:42225 [200]: /
PHPUnit 3.7.15 by Sebastian Bergmann.

Configuration read from /home/kinjouj/Desktop/sample/fuel/core/phpunit.xml

.class Fuel\CoreResponse#24 (3) {
    public $status => int(200)
    public $headers =>array(0) {
    }
    public $body =>array(1) {
        "message" => string(16) "hoge fuga foobar"
    }
}


Time: 0 seconds, Memory: 3.00Mb

OK (1 test, 2 assertions)
PHP 5.4.12 Development Server started at Thu Mar  7 14:01:16 2013
Listening on http://localhost:8080
Document root is /home/kinjouj/Desktop/sample/public
Press Ctrl-C to quit.

っていう感じになる。一応返ってきてはいるみたい

んまぁネタですねww でもこういう仕組みっていうのもちゃんとPHPにもほしいような気もする所

chrome.appなPattern&Practice superagentでリダイレクトをフォローしない