3分でCodeIgniter 3.xからPHPUnit 6.0を使う
PHPUnit 6.0がリリースされたわけですが、CodeIgniter 3.xでももちろん使えます。
なお、PHPUnit 6.0を使うにはPHP 7.0以上が必要です。
CodeIgniterのインストール
ComposerからCodeIgniter 3.xの最新版をインストールします。
$ composer create-project kenjis/codeigniter-composer-installer ci31
以下のように、現在だとCodeIgniter 3.1.3がインストールされます。
Installing kenjis/codeigniter-composer-installer (v0.4.6)
- Installing kenjis/codeigniter-composer-installer (v0.4.6)
Loading from cache
Created project in ci31
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing codeigniter/framework (3.1.3)
Loading from cache
kenjis/codeigniter-composer-installer suggests installing kenjis/codeigniter-cli (A command-line tool for CodeIgniter 3.0)
kenjis/codeigniter-composer-installer suggests installing kenjis/ci-phpunit-test (An easier way to use PHPUnit with CodeIgniter 3.0)
kenjis/codeigniter-composer-installer suggests installing kenjis/codeigniter-ss-twig (A Simple and Secure Twig integration for CodeIgniter 3.0)
kenjis/codeigniter-composer-installer suggests installing kenjis/codeigniter-doctrine (A simple Doctrine integration for CodeIgniter 3.0)
kenjis/codeigniter-composer-installer suggests installing kenjis/codeigniter-deployer (A Deployment Tool for CodeIgniter 3.0)
codeigniter/framework suggests installing paragonie/random_compat (Provides better randomness in PHP 5.x)
Writing lock file
Generating autoload files
> Kenjis\CodeIgniter\Installer::postInstall
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing mikey179/vfsstream (v1.1.0)
Loading from cache
Writing lock file
Generating autoload files
==================================================
`public/.htaccess` was installed. If you don't need it, please remove it.
If you want to install translations for system messages or some third party libraries,
$ cd <codeigniter_project_folder>
$ php bin/install.php
Above command will show help message.
See <https://github.com/kenjis/codeigniter-composer-installer> for details
==================================================
PHPUnit 6.0のインストール
ComposerからPHPUnit 6.0をインストールします。
$ cd ci31/
$ composer require phpunit/phpunit:6.0.*
以下のように、現在だとPHPUnit 6.0.6がインストールされます。
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing sebastian/version (2.0.1)
Loading from cache
- Installing sebastian/resource-operations (1.0.0)
Loading from cache
- Installing sebastian/recursion-context (2.0.0)
Loading from cache
- Installing sebastian/object-enumerator (2.0.0)
Loading from cache
- Installing sebastian/global-state (1.1.1)
Loading from cache
- Installing sebastian/exporter (2.0.0)
Loading from cache
- Installing sebastian/environment (2.0.0)
Loading from cache
- Installing sebastian/diff (1.4.1)
Loading from cache
- Installing sebastian/comparator (1.2.4)
Loading from cache
- Installing doctrine/instantiator (1.0.5)
Loading from cache
- Installing phpunit/php-text-template (1.2.1)
Loading from cache
- Installing phpunit/phpunit-mock-objects (4.0.0)
Loading from cache
- Installing phpunit/php-timer (1.0.8)
Loading from cache
- Installing phpunit/php-file-iterator (1.4.2)
Loading from cache
- Installing sebastian/code-unit-reverse-lookup (1.0.0)
Loading from cache
- Installing phpunit/php-token-stream (1.4.9)
Loading from cache
- Installing phpunit/php-code-coverage (5.0.0)
Loading from cache
- Installing webmozart/assert (1.2.0)
Loading from cache
- Installing phpdocumentor/reflection-common (1.0)
Loading from cache
- Installing phpdocumentor/type-resolver (0.2.1)
Loading from cache
- Installing phpdocumentor/reflection-docblock (3.1.1)
Loading from cache
- Installing phpspec/prophecy (v1.6.2)
Loading from cache
- Installing myclabs/deep-copy (1.6.0)
Loading from cache
- Installing phpunit/phpunit (6.0.6)
Loading from cache
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit suggests installing phpunit/php-invoker (^1.1)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating autoload files
ci-phpunit-testのインストール
CodeIgniterからPHPUnitを簡単に使うために、ci-phpunit-testをインストールします。なお、PHPUnit 6.0を使うためには、ci-phpunit-test 0.14.0以上が必要です。
$ composer require kenjis/ci-phpunit-test --dev
以下のように、現在だとci-phpunit-test 0.14.0がインストールされます。
Using version ^0.14.0 for kenjis/ci-phpunit-test
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing nikic/php-parser (v3.0.4)
Downloading: 100%
- Installing kenjis/ci-phpunit-test (v0.14.0)
Downloading: 100%
Writing lock file
Generating autoload files
実はこれだけではまだインストールが完了しません。以下のコマンドを実行します。
$ php vendor/kenjis/ci-phpunit-test/install.php
これで、ci-phpunit-testがインストールされました。
PHPUnitの実行
$ vendor/bin/phpunit -c application/tests/ --debug
以下のように、ci-phpunit-testに付属しているサンプルのテストが実行されます。
PHPUnit 6.0.6 by Sebastian Bergmann and contributors.
Error: No code coverage driver is available
Starting test 'Welcome_test::test_index'.
.
Starting test 'Welcome_test::test_method_404'.
.
Starting test 'Welcome_test::test_APPPATH'.
. 3 / 3 (100%)
Time: 80 ms, Memory: 4.00MB
OK (3 tests, 3 assertions)
関連
Date: 2017/02/15