Correct way of getting dependencies when using PHPUnit

Notice: This thread is very old.

3 years ago

dkorpar
Member | 77
+
+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?

3 years ago

Tomáš Votruba
Moderator | 1154
+
+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);
}