Skip to content

StringEndsWith matches too loosely (numeric strings) #3461

Closed
@guilliamxavier

Description

@guilliamxavier
Q A
PHPUnit version 6.5.13
PHP version 7.2.10
Installation Method PHAR

PHPUnit incorrectly agrees that the string "zzz0E2" ends with "0E1"
(while it correctly disagrees that the string "0E2zzz" starts with "0E1").

Test:

<?php

class BugTest extends \PHPUnit\Framework\TestCase
{
    public function testStringStartsWith()
    {
        $this->assertStringStartsWith('0E1', '0E1zzz');
    }

    public function testStringStartsNotWith()
    {
        $this->assertStringStartsNotWith('0E1', '0E2zzz');
    }

    public function testStringEndsWith()
    {
        $this->assertStringEndsWith('0E1', 'zzz0E1');
    }

    public function testStringEndsNotWith()
    {
        $this->assertStringEndsNotWith('0E1', 'zzz0E2'); // ***
    }
}

Expected result:

PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 142 ms, Memory: 8.00MB

OK (4 tests, 4 assertions)

Actual result:

PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

...F                                                                4 / 4 (100%)

Time: 142 ms, Memory: 8.00MB

There was 1 failure:

1) BugTest::testStringEndsNotWith
Failed asserting that 'zzz0E2' ends not with "0E1".

/z/BugTest.php:22

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.

Cause:
StringEndsWith::matches() (

return \substr($other, 0 - \strlen($this->suffix)) == $this->suffix;
) uses loose == but should use strict === ('0E2' == '0E1' is true but not '0E2' === '0E1').

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions