Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ken_master's avatar

PHP Fatal error: Cannot override final method PHPUnit\Framework\Assert::assertDirectoryDoesNotExist()

I've started working with Lumen 10 and Phpunit 10 and encountered this error.

PHP Fatal error:  Cannot override final method PHPUnit\Framework\Assert::assertDirectoryDoesNotExist()

Porblem is, i did not even override any of the PHPunit classes nor any classes. been trying to solve this and could not find an answer.

composer:

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^8.1",
        "doctrine/annotations": "^1.14",
        "doctrine/dbal": "^2.13",
        "guzzlehttp/guzzle": "^7.5",
        "laravel-doctrine/orm": "1.*",
        "laravel/lumen-framework": "^8.0"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.4.4"
    },
    "autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ]
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}


CustomerTest.php

<?php
namespace Tests;

class CustomerTest extends TestCase
{

    /**
     * /customers [GET]
     */
    public function test_customers_list()
    {
        $this->get('/customers')
             ->seeJsonStructure([
                'results' => [
                    'id',
                    'first_name', 
                    'last_name', 
                    'email'
                  
                ],
                'meta' => []
             ]);
        
        $this->seeStatusCode(200);

    }
}
```
0 likes
4 replies
LaryAI's avatar
Level 58

This error occurs because the assertDirectoryDoesNotExist() method in PHPUnit\Framework\Assert is marked as final, which means it cannot be overridden. The error message suggests that the method is being overridden somewhere in the code.

To fix this issue, you can try the following steps:

  1. Check if any of your test classes or traits are extending or using a class that extends PHPUnit\Framework\Assert and overriding the assertDirectoryDoesNotExist() method.

  2. If you find any such class or trait, remove the method override and use the original method instead.

  3. If you are not able to find the source of the error, try updating your PHPUnit version to the latest version.

  4. If none of the above steps work, try running composer update to update all the dependencies to their latest versions.

Here's an example of how to use the original assertDirectoryDoesNotExist() method in your test:

public function test_directory_does_not_exist()
{
    $directory = '/path/to/non/existent/directory';
    $this->assertDirectoryDoesNotExist($directory);
}
NSchubhan's avatar

illuminate/testing Solve the problem, but only for tag ^10 github.com/illuminate/testing/commit/ed1f98e21ac08c2311fd0dbf4620d9c5941a8152

And Lumen 10 should ask the last version of "illuminate/testing": "^10.0", github.com/laravel/lumen-framework/blob/10.x/composer.json

So a "composer update" should solve your problem (or require of last lumen)

lumenian's avatar

I upgraded to Lumen 10/PHPunit 10, but ended up having another dependency that did not work with Lumen 10 and I downgraded back to 9. I got this error until I downgraded PHPunit to 9 also.

Please or to participate in this conversation.