I am trying to add a custom assert to phpunit following this tutorial to check for complex numbers returned as a string (e.g.
"- 123 + 456i"
the method I'm testing) to a certain accuracy for both the real and imaginary components. I placed the Complex.php class to parse the string into the real and imaginary parts and compiled the following statement class as complexAssert.php:
require_once 'PHPUnit/Framework/Assert.php'; include_once getcwd().'/custom/Complex.php'; class complexAssert extends PHPUnit_Framework_Assert { public function assertComplexEquals($expected, $actual, $message = '', $delta = 0) { $expectedComplex = new Complex($expected); $actualComplex = new Complex($actual); if (!($actualComplex->getReal() >= ($expectedComplex - $delta) && $actualComplex->getReal() <= ($expectedComplex + $delta))) { return $this->fail($message); } if (!($actualComplex->getImaginary() >= ($expectedComplex - $delta) && $actualComplex->getImaginary() <= ($expectedComplex + $delta))) { return $this->fail($message); } } }
My unit test script:
require_once getcwd().'/custom/complexAssert.php'; require_once 'testDataFileIterator.php'; class EngineeringTest extends PHPUnit_Framework_TestCase { public function testIMSUM() { $args = func_get_args(); $expectedResult = array_pop($args); $result = call_user_func_array(array('PHPExcel_Calculation_Engineering','IMSUM'),$args); $this->assertComplexEquals($expectedResult, $result); } public function providerIMSUM() { return new testDataFileIterator('rawTestData/Calculation/Engineering/IMSUM.data'); } }
The unit tests worked without errors (but failed) when I just did assertEquals ... but now I added include and changed it to a new assert, it crashes, claiming that it cannot call the undefined assertComplexEquals () method.
Has anyone been successful in extending phpunit with custom statements and can see what I'm doing wrong?
source share