James Bond
James Bond

Reputation: 3006

PHPUnit: how to use assertions outside of the "test..." methods?

I have the following code:

private function registerShutdownFunction(): void
{
    register_shutdown_function(function () {
        $this->dropDatabasesAndUsersIfExist();
    });
}

And this code:

private function dropDatabasesAndUsersIfExist(): void
{
    // some code for deletion of the databases...

    foreach ($connections as $connection) {
        $this->assertNotContains($connection, $databases);
    }
}

But dropDatabasesAndUsersIfExist is not a "test..." method. And phpunit ignores assertions outside of the test methods.

And seems there are problems may occur, because this shutdown function running directly before the die of the script...

Upvotes: 0

Views: 692

Answers (1)

Philip Weinke
Philip Weinke

Reputation: 1844

You can use PHPUnit's Assert class outside of test cases if that is really what you want to do:

PHPUnit\Framework\Assert::assertNotContains($connection, $databases);

Edit: After reading your question one more time I'm not really sure if my answer helps you. If I got you right, you are already using the assertion but it did not behave as you'd expect it. My guess is that you want the whole test run to fail if any of the assertions in dropDatabasesAndUsersIfExist was not met.

One solution could be to move the checks you are doing in dropDatabasesAndUsersIfExist to a separate test class that should be executed last. You can achieve this by appending another test suite with the new class right after your test suite(s).

Upvotes: 2

Related Questions