slimでphpunit使用して結合テスト
今回は、結合テストを行う方法を記す。
goutteはブラウザエミュレートしてくれるphpライブラリです。
今回はgoutteを使ってブラウザのリクエストをエミュレートして正常に表示が行われるかのテストを行います。
goutteのインストール
$ vi composer.json "require": { "slim/slim": "2.*", "slim/extras": "2.0.*", "twig/twig": "1.*", "pimple/pimple": "v1.0.2", "phpunit/phpunit": "3.7.21", "fabpot/goutte": "v1.0.1" }, $ php composer.phar update
結合テストの設定ファイルを作成
tests/integrateディレクトリのテストファイルを結合テストとして読込するよう設定します。
$ vi config/integrate.xml <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap = "bootstrap.php" > <testsuites> <testsuite name="integrate test"> <directory>../tests/integrate</directory> </testsuite> </testsuites> </phpunit>
結合テストを作成
hoge.comにリクエストを送信して表示内容に「hello hoge」が含まれるかテストします。
$ mkdir -p tests/integrate/ vi tests/integrate/TopTest.php <?php namespace Taka512; use Goutte\Client; class TopTest extends \PHPUnit_Framework_TestCase { public function testGetName() { $client = new Client(); $crawler = $client->request('GET', 'http://hoge.com/'); $content = $client->getResponse()->getContent(); $this->assertRegExp('/hello hoge/', $content); } }
テスト実行
$ vendor/bin/phpunit -c config/integrate.xml PHPUnit 3.7.21 by Sebastian Bergmann. Configuration read from /home/coh2/config/integrate.xml . Time: 0 seconds, Memory: 6.25Mb OK (1 test, 1 assertion)