Correct way of getting dependencies when using PHPUnit

Notice: This thread is very old.
dkorpar
Member | 132
+
+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?

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