PHPUnitを使ってprivateメソッドをテストする

2012-12-21T00:00:00+00:00 PHP

Closure::bindを使って出来るらしい。実際に自分でやらないと信用しないタイプな人間なもんで

<?php

class Sample {
    private function say() {
        return "hoge";
    }
}

class SampleTestCase extends PHPUnit_Framework_TestCase {
    public function test() {
        $c = Closure::bind(
            function() {
                $sample = new Sample();

                // わざとコケるようにしている
                $this->assertThat($sample->say(), $this->equalTo("fuga"));
            },
            $this,
            "Sample"
        );
        $c();
    }
}

いちお、ClosureクラスはPHPUnitの機能では無い

jsonicを使ってみる gradle使ってみる (5) - warプラグイン -