Skip to content

assertStringStartsWith after assertNotEmpty fails #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gisostallenberg opened this issue May 23, 2025 · 2 comments
Open

assertStringStartsWith after assertNotEmpty fails #231

gisostallenberg opened this issue May 23, 2025 · 2 comments

Comments

@gisostallenberg
Copy link

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');
    }
}
@ondrejmirtes
Copy link
Member

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

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

@ondrejmirtes
Copy link
Member

I get the problem now, phpstan-phpunit has a special type-specifying extension that doesn't work for strings:

'Empty' => static fn (Scope $scope, Arg $actual): Expr\BinaryOp\BooleanOr => new Expr\BinaryOp\BooleanOr(
new Instanceof_($actual->value, new Name(EmptyIterator::class)),
new Expr\BinaryOp\BooleanOr(
new Expr\BinaryOp\BooleanAnd(
new Instanceof_($actual->value, new Name(Countable::class)),
new Identical(new FuncCall(new Name('count'), [new Arg($actual->value)]), new LNumber(0)),
),
new Expr\Empty_($actual->value),
),
),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants