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
/path/to/tests/TestCase/DatabaseTestTrait.php:46
/path/to/tests/TestCase/DatabaseTestTrait.php:29
ERRORS!
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.