Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit custom asserts help needed

I'm trying to add a custom assert to phpunit, following this tutorial, to validate complex numbers returned as a string (e.g.

"-123+456i"

by the method that I'm testing) to a defined precision for both the real and imaginary components. I've put a Complex.php class to parse the string into the real and imaginary parts, and put together the following assertion 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
{
    /**
     * @dataProvider providerIMSUM
     */
    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 error (but failed) when I was simply doing an assertEquals... but now I've added the include and changed to my new assert, it's crashing claiming that it can't call the undefined method assertComplexEquals().

Has anybody had any success extending phpunit with custom asserts, and can see what I'm doing wrong?

like image 932
Mark Baker Avatar asked Jan 23 '12 00:01

Mark Baker


People also ask

What is assert in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

How do I run a PHPUnit test case?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.


1 Answers

Obviously the only way to get $this->someCustomAssertion worked is to extend PHPUnit_Framework_TestCase and create wrapper-methods there, or call your custom assertions statically.

Zend Framework, for example, just extends PHPUnit_Framework_TestCase with additional methods (assertions)

like image 114
zerkms Avatar answered Oct 01 '22 02:10

zerkms