phpunitのテストコードひな形作成


テストを書くのは楽しいのだけれどもなるべくテストコード書く量へらしたいよねってことでphpunitのひな形作成ライブラリである[PHPUnit_SkeletonGenerator](http://phpunit.de/manual/3.7/ja/skeleton-generator.html)を使ってみたメモ。

実行ファイルからのテストコードのひな形作成。テストコードからの実行ファイルの作成ができるみたい。

# インストール

    sudo pear channel-discover pear.phpunit.de
    sudo pear channel-discover components.ez.no
    sudo pear channel-discover pear.symfony-project.com
    sudo pear install phpunit/PHPUnit_SkeletonGenerator

# コマンドヘルプ

    phpunit-skelgen

```
PHPUnit Skeleton Generator 1.2.1 by Sebastian Bergmann.

Usage: phpunit-skelgen --class ClassTest
       phpunit-skelgen --class -- ClassTest [ClassTest.php] [Class] [Class.php]
       phpunit-skelgen --test Class [Class.php] [ClassTest] [ClassTest.php]
       phpunit-skelgen --test -- Class [Class.php] [ClassTest] [ClassTest.php]

  --class             Generate Class [in Class.php] based on ClassTest [in ClassTest.php]
  --test              Generate ClassTest [in ClassTest.php] based on Class [in Class.php]

  --bootstrap <file>  A "bootstrap" PHP file that is run at startup

  --help              Print this usage information
  --version           Print the version
```

# 実行方法

    phpunit-skelgen --test <class名>
    phpunit-skelgen --test -- <class名> <classファイル>
    phpunit-skelgen --bootstrap <autoloaderの指定>

ためしに`lib/model`下全部のtestファイルを作成。

    ls lib/model/|grep php | sed 's/.php//' | for f in $(xargs) ; do phpunit-skelgen --bootstrap test/bootstrap/phpunit.bootstrap.php --test $f ; done

作成されたテストコードは指定した実行ファイルと同じディレクトリに出力される。

    ll lib/model/*Test.php

すでに自分の方で作成済みのコードがあるので作成されたものに上書き

    mv test/phpunit/lib/model/* lib/model/
    mv lib/model/*Test.php test/phpunit/lib/model/

ちなみに作成されたものはこんな感じ

```
<?php
/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-08-20 at 18:32:51.
 */
class MemberTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var Member
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = new Member;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * @covers Member::getProfile
     * @todo   Implement testGetProfile().
     */
    public function testGetProfile()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
          'This test has not been implemented yet.'
        );
    }
```


# テスト実行

    phpunit

```
PHPUnit 3.7.23 by Sebastian Bergmann.

Configuration read from /Users/cube/huge/foge/muu/wann/nyann/phpunit.xml.dist

......................I.........................I...IIIFIIIII   61 / 1674 (  3%)
IIFIIIIIIFIIIIIIIIIIIFIIF.FIIIIIIFIIFIIFIIIIIIIFIIIFIFIIFIIII  122 / 1674 (  7%)
IIFIIIIIIIIIIIIIIIIIIFIIIIIIIIIIIFFIFIIIIIIIIIIIIIIIFIIFIIFII  183 / 1674 ( 10%)
IIIIIIIIIIIFFIIIIIIIIIIIIIFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII  244 / 1674 ( 14%)
I............IIIIIIIII.IFIIFFFFFIIIFIIIIIIIIIIIIIIIIIIIIIIIII  305 / 1674 ( 18%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIIIIIIIIIIIIIIIFIIIIIIII  366 / 1674 ( 21%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII....IIIIIIIIIIIIIFFIIFIIIIF  427 / 1674 ( 25%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFIIIIIIII  488 / 1674 ( 29%)
IIIIIIIIIIIII.....IIIIIIIIIIIIIIIIII....IIIIIIIIIIIIIIIIIIIII  549 / 1674 ( 32%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII  610 / 1674 ( 36%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII  671 / 1674 ( 40%)
IIIIIIIIIIIIIIIIIIIIIIIII..IIIIIIIIIIIIIIIIIIIIIIFIIIIIIIIIII  732 / 1674 ( 43%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII....II..IIIIII...IIIIIIIII  793 / 1674 ( 47%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFF  854 / 1674 ( 51%)
FFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII  915 / 1674 ( 54%)
IIIIIIIIIII..IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII.......IIII  976 / 1674 ( 58%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 1037 / 1674 ( 61%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 1098 / 1674 ( 65%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 1159 / 1674 ( 69%)
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 1220 / 1674 ( 72%)
```

がんばるぞー