Skip to content

assertStringStartsWith after assertNotEmpty fails #231

Closed

Description

@gisostallenberg

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');
    }
}

Activity

  1. ondrejmirtes commented on May 23, 2025

    @ondrejmirtes
    Member

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

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

  2. ondrejmirtes commented on May 23, 2025

    @ondrejmirtes
    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),
    ),
    ),

  3. staabm commented on Oct 29, 2025

    @staabm
    Contributor

    I was not able to reproduce the issue, but I think phpstan/phpstan-src#4473 will have fixed it in case its still relevant

  4. staabm commented on Nov 15, 2025

    @staabm
    Contributor

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

  5. github-actions commented on Mar 13, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  6. locked as resolved and limited conversation to collaborators on Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions