phpphpunitarbitrary-precisionbcmath

Phpunit and floating point numbers stored as strings


I'm currently using BC Math extension in a project.

In my unit tests there are some comparisons that would be similar to the below:

This will pass:

        $this->assertEquals('1.23456789123456789123434', 
                            '1.2345678912345678912343434654654654654'
        ); 

This will not pass:

         $this->assertEquals('1.23456789123456789123434', 
                             '1.23456789123456719123434'
         ); 

I've read a few bits that seem to suggest two string will be treated as numeric if is_numeric returns true. Is there already functionality in phpunit to compare two numeric string as strings. I know I can write a custom assertion but don't want to if the functionality exists already?

I have looked pretty hard and don't seem to be able to be able to see the functionality but feel it must exist...


Solution

  • Ok straight after posting I work out the answer...

    You can use $this->assertSame() like ...

    this will pass:

        $this->assertSame('1.2345678912345678912343434654654654654',
                          '1.2345678912345678912343434654654654654'
        );
    

    this will not pass:

        $this->assertSame('1.234567891234567891234343465465465465',
                          '1.2345678912345678912343434654654654654'
        );