phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

assertStringStartsWith after assertNotEmpty fails

Open gisostallenberg opened this issue 5 months ago • 2 comments
trafficstars

assertStringStartsWith after assertNotEmpty fails with error "Parameter #1 $prefix of method PHPUnit\Framework\Assert::assertStringStartsWith() expects non-empty-string', while the string cannot be empty due to the assertNotEmpty call.

<?php

use PHPUnit\Framework\TestCase;

class FooTest extends TestCase {
    public function testFoo(): void
    {
        $string = ([
            '',
            'bar',
        ])[rand(0, 1)];
        $this->assertNotEmpty($string);
        $this->assertStringStartsWith($string, 'baz');
    }
}

gisostallenberg avatar May 23 '25 07:05 gisostallenberg

Can't reproduce the problem: https://phpstan.org/r/91888ed1-d67f-4749-9d9c-a617da10722b

Please show the output of vendor/bin/phpstan diagnose.

ondrejmirtes avatar May 23 '25 07:05 ondrejmirtes

I get the problem now, phpstan-phpunit has a special type-specifying extension that doesn't work for strings: https://github.com/phpstan/phpstan-phpunit/blob/d35895e0388e9aa822af00e1dfd4f8086fd8938d/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php#L168-L177

ondrejmirtes avatar May 23 '25 07:05 ondrejmirtes

I was not able to reproduce the issue, but I think https://github.com/phpstan/phpstan-src/pull/4473 will have fixed it in case its still relevant

staabm avatar Oct 29 '25 13:10 staabm

@gisostallenberg please double check whether this issue still reproduces on the latest release

staabm avatar Nov 15 '25 10:11 staabm