[SOLVED]Error: Call to a member function get() on null

Any reason why one would get this error?? I’m using @odan 's slim 4 skeleton. I’m trying to run tests and this error keeps coming up.

There was 1 error:
1) App\Test\TestCase\Domain\User\Repository\UserCreatorRepositoryTest::testInsertUser
Error: Call to a member function get() on null


Tests: 1, Assertions: 0, Errors: 1.
Script phpunit --configuration phpunit.xml handling the test event returned with error code 2

The error seems to be coming from this line:

use AppTestTrait;

 * Get database connection
 * @return PDO The PDO instance
protected function getConnection(): PDO
    return $this->container->get(PDO::class);

I have built my project around this skeleton and the file with the same code can be found here: DatabaseTestTrait.php.
$this->container points to a list of container references as written below:

     * Bootstrap app
     * @before
     * @throws UnexpectedValueException
     * @return void
    protected function setUp(): void
        $this->app = require __DIR__ . '/../../config/bootstrap.php';

        $container = $this->app->getContainer();
        if ($container === null) {
            throw new UnexpectedValueException('Container must be initialized');

        $this->container = $container;

The file with this code can be checked here: AppTestTrait.php.

In my case, return $this->container->get(PDO::class); points to the container reference as written below:

// Other container references listed here
PDO::class => function (ContainerInterface $container) {
        $settings = $container->get('settings')['db'];

        $host = $settings['host'];
        $port = $settings['port'];
        $dbname = $settings['database'];
        $username = $settings['username'];
        $password = $settings['password'];
        $charset = $settings['charset'];
        $flags = $settings['flags'];
        $dsn = "mysql:host=$host:$port;dbname=$dbname;charset=$charset";

        return new PDO($dsn, $username, $password, $flags);

When I consume an API endpoint that uses the database connection, it works. Only tests that need the PDO fail.

The setUp() method is a Phpunit specific method that runs already before each test. You should not add the @before annotation to the setUp() method. Try to remove the @before annotation within the DocBlock of the setUp method.


Still experiencing the same problem.

It seems adding the @before annotation on setupDatabase() in DatabaseTestTrait.php made it execute before the setUp() method. I have changed it to setUpContainer() then added the @before annotation to it again and…now it works. :man_facepalming:t2:
Thank you once again @odan.