Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:52:13+00:00 2026-05-29T07:52:13+00:00

I’m trying to add a custom assert to phpunit, following this tutorial , to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:52:14+00:00Added an answer on May 29, 2026 at 7:52 am

    In the end, I chose not to extend existing asserts, but to modify my complex assertion logic to return a simple boolean, that could then be tested using assertTrue(), and with an error message that could be retrieved with a simple getMessage() for display in the phpunit results. To be honest, it feels a whole lot easier to use this way

    include_once __DIR__.'/Complex.php';
    
    class complexAssert {
    
        private $_errorMessage = '';
    
        public function assertComplexEquals($expected, $actual, $delta = 0)
        {
            $expectedComplex = new Complex($expected);
            $actualComplex = new Complex($actual);
    
            if ($actualComplex->getReal() < ($expectedComplex->getReal() - $delta) ||
                $actualComplex->getReal() > ($expectedComplex->getReal() + $delta)) {
                $this->_errorMessage = 'Mismatched Real part: ' .
                                       $actualComplex->getReal() . 
                                       ' !== ' . 
                                       $expectedComplex->getReal();
                return FALSE;
            }
    
            if ($actualComplex->getImaginary() < ($expectedComplex->getImaginary() - $delta) ||
                $actualComplex->getImaginary() > ($expectedComplex->getImaginary() + $delta)) {
                $this->_errorMessage = 'Mismatched Imaginary part: ' .
                                       $actualComplex->getImaginary() . 
                                       ' !== ' . 
                                       $expectedComplex->getImaginary();
                return FALSE;
            }
    
            return TRUE;
        }
    
        public function getErrorMessage() {
            return $this->_errorMessage;
        }
    }
    

    My unit test script:

    //  Custom assertion class for handling precision of Complex numbers
    require_once __DIR__.'/../../custom/complexAssert.php';
    //  Data Provider handler
    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);
            $complexAssert = new complexAssert();
            $this->assertTrue($complexAssert->assertComplexEquals($expectedResult, $result, 1E-8),
                              $complexAssert->getErrorMessage());
        }
    
        public function providerIMSUM()
        {
            return new testDataFileIterator('rawTestData/Calculation/Engineering/IMSUM.data');
        }
    }
    

    and the logged phpunit result is clear enough:

    3) EngineeringTest::testIMSUB with data set #7 ('-12.34-5.67i', '-123.45-67.89', '#NUM!')
    Mismatched String: 111.11 !== #NUM!
    Failed asserting that false is true.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.