my_add_return_int() という(自作の PHP Extension の中にある)関数
のテストを行っています。正常系は通りますが、引数が足りない場合の例外を
捕捉できません。
bash
1vagrant:~/php/ext/my_ext$ cat /usr/local/lib/php.ini 2extension=/home/vagrant/php/ext/my_ext/modules/my_ext.so 3
php
1vagrant:~/php/ext/my_ext$ cat tests/MyAddReturnIntTest.php 2<?php 3 4use PHPUnit\Framework\TestCase; 5 6class MyAddReturnIntTest extends TestCase 7{ 8 public function test3plus4returns7() 9 { 10 $ret = my_add_return_int(3, 4); 11 $this->assertEquals(7, $ret); 12 } 13 14 /** 15 * @expectedException InvalidArgumentException 16 */ 17 public function testLackOfParameter() 18 { 19 $ret = my_add_return_int(3); 20 } 21} 22
bash
1vagrant:~/php/ext/my_ext$ php -v 2PHP 7.1.5 (cli) (built: Jun 7 2017 09:13:29) ( NTS DEBUG ) 3Copyright (c) 1997-2017 The PHP Group 4Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies 5 6vagrant:~/php/ext/my_ext$ phpunit tests/MyAddReturnIntTest.php 7PHPUnit 6.2.1 by Sebastian Bergmann and contributors. 8 9.E 2 / 2 (100%) 10 11Time: 133 ms, Memory: 10.00MB 12 13There was 1 error: 14 151) MyAddReturnIntTest::testLackOfParameter 16Wrong parameter count for my_add_return_int() 17 18/usr/local/src/php-7.1.5/ext/my_ext/tests/MyAddReturnIntTest.php:18 19 20ERRORS! 21Tests: 2, Assertions: 1, Errors: 1. 22 23
@expectedException を
$this->expectException(InvalidArgumentException::class);
に書き換えても同じエラーでした。試しに error_reporting() を入れてみると
php
1 /** 2 * @expectedException InvalidArgumentException 3 */ 4 public function testLackOfParameter() 5 { 6 error_reporting(E_ERROR); 7 $this->expectException(InvalidArgumentException::class); 8 $ret = my_add_return_int(3); 9 } 10
bash
11) MyAddReturnIntTest::testLackOfParameter 2Failed asserting that exception of type "InvalidArgumentException" is thrown. 3
となります。
無理やり RuntimeException に変換すれば補足できるのですが、PHPUnit のドキュメントの
記述と齟齬がある気がして、今ひとつ納得がいきません。ご教示をお願いします。
php
1<?php 2 3use PHPUnit\Framework\TestCase; 4 5class MyAddReturnIntTest extends TestCase 6{ 7 public static function setUpBeforeClass() { 8 set_error_handler(function($errno, $errstr, $errfile, $errline) { 9 throw new RuntimeException($errstr . " on line " 10 . $errline . " in file " . $errfile); 11 }); 12 } 13 14 /** 15 * @expectedException RuntimeException 16 */ 17 public function testLackOfParameter() 18 { 19 error_reporting(E_ERROR); 20 $ret = my_add_return_int(3); 21 } 22} 23
bash
1vagrant:~/php/ext/my_ext$ phpunit tests/MyAddReturnIntTest.php 2PHPUnit 6.2.1 by Sebastian Bergmann and contributors. 3 4.. 2 / 2 (100%) 5 6Time: 122 ms, Memory: 10.00MB 7 8OK (2 tests, 2 assertions) 9
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/13 05:42
2017/06/13 05:50