Skip to content

Instantly share code, notes, and snippets.

@kkanazaw
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkanazaw/8764838 to your computer and use it in GitHub Desktop.
Save kkanazaw/8764838 to your computer and use it in GitHub Desktop.
絶対パスでrequire_onceしているクラスをなんとかテストする
<?php
class HogeTest extends PHPUnit_Framework_TestCase {
// テスト実行前にtmpディレクトリ以下に*.phpファイルをコピー
// でtmp以下のファイルを置換する
public static function setUpBeforeClass() {
`mkdir -p tmp; cp *.php tmp/`;
`find tmp/ -name '*.php' | xargs perl -i -pe 's/(require_once)/\/\/$1/g;'`;
//改めてここでrequire_onceしてあげる(本物の代わりに、スタブを用意して呼ぶこともできる)
require_once("tmp/lib/Hoge.php");
}
function testNew() {
$hoge = new Hoge();
$this->assertTrue(is_object($hoge));
}
//テストが終わった後は消すことも可能(残しておけばカバレッジ取ることも可能)
public static function tearDownAfterClass() {
`rm -rf ./tmp`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment