Correct way of getting dependencies when using PHPUnit

about a year ago

dkorpar
Member | 48
+
+1
-

My current code includes:

public function setUp()
{
    parent::setUp();
    $this->container = $GLOBALS['container'];
    $this->db = $this->container->getByType('Dibi\Connection');
}

I have seperate bootstrap.php/config for tests, as well as seperate temp directory.
While this works I'm thinking is there some better way?

about a year ago

Tomáš Votruba
Moderator | 1130
+
+2
-

Hi, your setup has good logic.

Instead of using $GLOBALS I recommend using encapsulation in ContainerFactory.

Use like this

protected function setUp()
{
    $container = (new ContainerFactory)->create();
    $this->database = $container->getByType(Dibi\Connection::class);
}