Skip to content

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Jun 10, 2025

I have a use-case where I am writing tests that leverage a data provider. The data provider gives invalid data, an exception class, and the message we are expecting. In different conditions, different exceptions are reported. I want to confirm that they are indeed reported

public static function missingAttributesDataProvider(): array
{
    return [
        'no id' => [
            ['name' => 'Totwell', 'product' => 'Taylor Otwell branded tater tots'],
            UnexpectedValueException::class,
            'No id present'
        ]
    ];
}

#[Test]
#[DataProvider('missingAttributesDataProvider')]
public function missingAttribute_throwsException(array $attributes, string $expectedExceptionClass, string $message): void
{
    Exceptions::fake();

    // Do something with those $attributes

    // Then
    Exceptions::assertReported(fn ($e) => $e instanceof $expectedExceptionClass && $e->getMessage() === $message);
}

But this blows up, because there's no typehint provided to the Closure.

With this change, I can change my test to:

$reported = Exceptions::reported();
$this->assertCount(1, $reported);
$this->assertInstanceOf($expectedExceptionClass, $reported[0]);
$this->assertSame($message, $reported[0]->getMessage());

@taylorotwell taylorotwell merged commit 70ec9fc into laravel:12.x Jun 10, 2025
39 of 60 checks passed
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

Successfully merging this pull request may close these issues.

2 participants