ci-phpunit-testのCodeIgniter 3.0.1対応

ci-phpunit-testをCodeIgniter 3.0.1対応させるための作業メモです。

現状の変更点の把握

$ cd ci-app-for-ci-phpunit-test/
$ cd vendor/kenjis/ci-phpunit-test/
$ bin/check-diff.sh > ci-phpunit-test-ci300.diff

tests/Bootstrap.phpおよびtests/_ci_phpunit_test/replacing/core/Loader.phpでの変更点が確認できる。

新バージョン(3.0.1)での変更点の把握

ci-phpunit-testフォルダにCodeIgniter-3.0.0.zipおよびCodeIgniter-3.0.1.zipを配置しておく。

$ bin/check-ci-diff.sh CodeIgniter-3.0.0.zip CodeIgniter-3.0.1.zip

CodeIgniter-3.0.0-CodeIgniter-3.0.1.diffおよびCodeIgniter-3.0.0-CodeIgniter-3.0.1.ci-phpunit-test-only.diffが作成される。

変更ファイル一覧

ci-phpunit-testで変更しているCodeIgniterのファイルは以下の通り。

index.php                     → tests/Bootstrap.php
system/core/Loader.php        → tests/_ci_phpunit_test/replacing/core/Loader.php
system/core/Common.php        → tests/_ci_phpunit_test/replacing/core/Common.php
system/helpers/url_helper.php → tests/_ci_phpunit_test/replacing/helper/url_helper.php

必要なファイルのアップデート

作成した

  • ci-phpunit-test-ci300.diff
  • CodeIgniter-3.0.0-CodeIgniter-3.0.1.ci-phpunit-test-only.diff
  • CodeIgniter-3.0.0-CodeIgniter-3.0.1.diff

を確認し、必要なファイルをアップデートする。

変更範囲については、https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md#can-and-cant も参照のこと。

アップデート内容の確認

$ bin/check-diff.sh

tests/Bootstrap.phpおよびtests/_ci_phpunit_test/replacing/core/Loader.phpでの変更点が確認できる。

関連

Tags: codeigniter, phpunit

Now you can write tests for CodeIgniter 3.0 apps that contains exit() with ci-phpunit-test

If you have apps that contains exit() or die(), normally you can't test them with PHPUnit.

Because an exit() in your app will abort the whole test suite.

But if you use CI PHPUnit Test v0.5.0, you can test your CodeIgniter apps like that without any modifications.

(2016/02/28 Added) If you are not familiar with Testing, I recommend our Ebook. CodeIgniter Testing Guide. It is Beginners' Guide to Automated Testing in PHP.

Requirements

  • CodeIgniter 3.0
  • PHP 5.4 or later
  • PHPUnit (4.7 is recommended)
  • ci-phpunit-test v0.5.0 (or later)

Setup

Installing CI PHPUnit Test is very easy.

Step 0

Install CodeIgniter 3.0.0. Download CodeIgniter 3.0.0 from http://www.codeigniter.com/download and unzip it.

Step 1

Install CI PHPUnit Test. Download latest version from https://github.com/kenjis/ci-phpunit-test/releases and unzip it. Copy application/tests folder into application folder.

CodeIgniter-3.0.0/
└── application/
    └── tests/

Step 2

Install PHPUnit 4.7. See https://phpunit.de/manual/4.7/en/installation.html.

For example, download https://phar.phpunit.de/phpunit.phar, and copy it to CodeIgniter project application/tests folder.

CodeIgniter-3.0.0/
└── application/
    └── tests/
        └── phpunit.phar

Now you're ready to run phpuint tests.

$ cd CodeIgniter-3.0.0/application/tests/
$ php phpunit.phar 
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

...

Time: 896 ms, Memory: 9.25Mb

OK (3 tests, 3 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

Okay.

Application Code containing exit()

If you have a controller like this:

application/controllers/Test.php

<?php

class Test extends CI_Controller
{
    public function index()
    {
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode(['foo' => 'bar']))
            ->_display();
        exit();
    }
}

You can write a test case with CI PHPUnit Test like below:

application/tests/controllers/Test_test.php

<?php

class Test_test extends TestCase
{
    public function test_index()
    {
        $output = $this->request('GET', 'test/index');
        $this->assertContains('{"foo":"bar"}', $output);
    }
}

But when phpunit runs Test controller, the whole test suite will be aborted by the exit() in the controller like below:

$ php phpunit.phar 
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

{"foo":"bar"}$ 

There is no test results. So you can't test the controller normally.

Converting exit() to Exception

But now CI PHPUnit Test has a new functionality to convert exit() to exception.

To enable it, you just change TestCase::$enable_patcher to true.

(2015/08/12 Added) The way to enable this functionality has changed on CI PHPUnit Test v0.6.0. See https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md#monkey-patching.

--- a/application/tests/TestCase.php
+++ b/application/tests/TestCase.php
@@ -2,5 +2,5 @@

 class TestCase extends CIPHPUnitTestCase
 {
-   public static $enable_patcher = false;
+   public static $enable_patcher = true;
 }

Let's run phpunit again.

$ php phpunit.phar 
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

E...

Time: 1.23 seconds, Memory: 10.50Mb

There was 1 error:

1) Test_test::test_index
CIPHPUnitTestExitException: exit() called in Test::index()

/Users/kenji/CodeIgniter-3.0.0/application/controllers/Test.php:12
/Users/kenji/CodeIgniter-3.0.0/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:281
/Users/kenji/CodeIgniter-3.0.0/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:243
/Users/kenji/CodeIgniter-3.0.0/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:87
/Users/kenji/CodeIgniter-3.0.0/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:60
/Users/kenji/CodeIgniter-3.0.0/application/tests/controllers/Test_test.php:7
/Users/kenji/CodeIgniter-3.0.0/application/tests/phpunit.phar:537

FAILURES!
Tests: 4, Assertions: 3, Errors: 1.

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

Now you can see an error message below:

1) Test_test::test_index
CIPHPUnitTestExitException: exit() called in Test::index()

Yes, exit() was converted to CIPHPUnitTestExitException.

Cool, isn't it?

So you can test it to catch the exception.

application/tests/controllers/Test_test.php

<?php

class Test_test extends TestCase
{
    public function test_index()
    {
        try {
            $output = $this->request('GET', 'test/index');
        } catch (CIPHPUnitTestExitException $e) {
            $output = ob_get_clean();
        }
        $this->assertContains('{"foo":"bar"}', $output);
    }
}

Let's run phpunit again.

$ php phpunit.phar 
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

....

Time: 1.34 seconds, Memory: 10.50Mb

OK (4 tests, 4 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

Okay. All green.

Summary

  • Installing CI PHPUnit Test is very easy. You don't have to modify CodeIgniter files at all.
  • You can write tests for application code that contains exit() or die() with CI PHPUnit Test.

If you are interested in CI PHPUnit Test, see the official site:

Tags: english

ci-phpunit-test v0.4.0をリリースしました

CodeIgniter 3.0でPHPUnitをより簡単に使う「CI PHPUnit Test」のv0.4.0をリリースしました。

以下よりダウンロードできます。

「CI PHPUnit Test」については、「CodeIgniter 3.0でPHPUnitを使う」をご覧ください。

インストールはフォルダを1つドラッグするだけという簡単さです。

公式サイトもあります(英語)。

今までの改良点

以前の記事「CodeIgniter 3.0でPHPUnitを使う」は2015/05/19でしたので、それからいろいろ改良されてます。

と言ってもコントローラのテスト方法の改良がほとんどです。

例えば、以下のようにテストが少しすっきり書けるようになっています。

それから、ドキュメント(英語)がかなり改良されました。

まとめ

  • CI PHPUnit Test v0.4.0がリリースされました
  • CodeIgniter 3.0で簡単にPHPUnitを使うことができます
  • コントローラのテストも簡単に書くことができます

CodeIgniter 3.0でテストを書きたいという人は試してみてください。

関連

Tags: codeigniter, phpunit, release