# How to init Database connection in phpunit Functional test

**URL:** <https://discourse.laminas.dev/t/how-to-init-database-connection-in-phpunit-functional-test/3503>\
**Category:** Mezzio\
**Tags:** phpunit, laminas-db\
**Created:** [October 18, 2023, 3:24pm UTC](https://discourse.laminas.dev/t/how-to-init-database-connection-in-phpunit-functional-test/3503 "2023-10-18T15:24:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rolek](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@rolek](https://discourse.laminas.dev/u/rolek)\
**Post date:** [October 18, 2023, 3:24pm UTC](https://discourse.laminas.dev/t/how-to-init-database-connection-in-phpunit-functional-test/3503/1 "2023-10-18T15:24:55Z")

</div>

Hi everyone!

I’m trying to write some functional tests for my application. I found this post [Mezzio Example: Functional and Unit Testing - Marc Guyer](https://marc.guyer.me/posts/mezzio-example/functional-and-unit-testing/) which got me started somewhat, but the problem I’m having is that when I run the test, the application doesn’t have a connection to the database.

Cut down to the basics, the test has this setUp function, that initializes the app.

```auto
    protected function setUp(): void
    {   
        parent::setUp(); 
        $this->container = require __DIR__. '/../config/container.php';
        $this->app = $this->container->get(Application::class);
        $factory = $this->container->get(MiddlewareFactory::class);
        (require __DIR__. '/../config/pipeline.php')($this->app, $factory, $this->container);
        (require __DIR__. '/../config/routes.php')($this->app, $factory, $this->container);
    }   

```

How can I ensure that the application, when I instantiate classes, has a database connection?

If I need to provide more information, please let me know!

Thanks in advance, Roel

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 18, 2023, 6:26pm UTC](https://discourse.laminas.dev/t/how-to-init-database-connection-in-phpunit-functional-test/3503/2 "2023-10-18T18:26:50Z")

</div>

For Mezzio you can use mezzio-test:

> **[GitHub - trinet-at/mezzio-test: Testing helpers for mezzio projects](https://github.com/trinet-at/mezzio-test)**
>
> Testing helpers for mezzio projects. Contribute to trinet-at/mezzio-test development by creating an account on GitHub.

> This will build your application container, bootstrap the mezzio Application (pipeline, routes) and registers a custom `\Laminas\Stratigility\Middleware\ErrorHandler` listener, which will just re-throw any exception. Thus, the native exception assertions can be used (eg. `$this->expectException()` in PHPUnit).  
> …  
> The container and router can also be retrieved with `MezzioTestEnvironment->container()` and `->router()` , respectively.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 18, 2023, 7:13pm UTC](https://discourse.laminas.dev/t/how-to-init-database-connection-in-phpunit-functional-test/3503/3 "2023-10-18T19:13:44Z")

</div>

> [@rolek](#):
>
> Cut down to the basics, the test has this setUp function, that initializes the app.

However, initializing the app is not necessary, the container is sufficient:

```php
/** @var Psr\Container\ContainerInterface $container */
$container = require __DIR__. '/../config/container.php';

```

If you use laminas-db then:

```php
$dbAdapter = $container->get(Laminas\Db\Adapter\AdapterInterface::class);

```
