こんなのは一々メモするほどでもないけど、公式マニュアルに沿って PHPUnit をインストールし、
使ってみたので備忘録をば。
インストール
公式マニュアルに載っている通り、取得・権限付与・bin に移動・バージョン確認の順に実施してい
く。
$ wget https://phar.phpunit.de/phpunit-6.5.phar
$ chmod +x phpunit-6.5.phar
$ sudo mv phpunit-6.5.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit 6.5.6 by Sebastian Bergmann and contributors.
※版数以外は公式ままです。執筆時の安定版は 6.5 だったので、6.5 をインストールしました。
次に、テスト対象のプログラムを用意します。
restaurant-check.php
<?php
function restaurant_check($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100);
$tip_amount = $meal * ($tip / 100);
$total_amount = $meal + $tax_amount + $tip_amount;
return $total_amount;
}
?>
そして、テスターとなるプログラムを用意します。
RestaurantCheckTest.php
<?php
include 'restaurant-check.php';
class RestaurantCheckTest extends \PHPUnit\Framework\TestCase {
public function testWithTaxAndTip() {
$meal = 100;
$tax = 10;
$tip = 20;
$result = restaurant_check($meal, $tax, $tip);
$this->assertEquals(130, $result);
}
}
?>
後は、phpunit
を実行するだけです。
$ phpunit RestaurantCheckTest.php
PHPUnit 6.5.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 179 ms, Memory: 8.00MB
OK (1 test, 1 assertion)
テストは成功しています。
以上。