Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Testbench\TCompiledContainer

Martin Zlámal edited this page Jul 24, 2016 · 4 revisions

Testbench prepares DIC for you so you can use it easily in your tests:

use \Testbench\TCompiledContainer;

public function testWhatever()
{
	/** @var \Nette\DI\Container $serviceLocator */
	$serviceLocator = $this->getContainer();
	//use it...
}

It's of course possible to get service using $this->getService('Nette\Application\Application'); or regenerate DIC container to get new instance using $this->refreshContainer();. It's also possible to refresh container with new parameters. Just use second parameter as array:

$refreshedContainer = $this->refreshContainer([
	'extensions' => ['test' => 'Testbench\FakeExtension'],
	'services' => ['test' => 'Testbench\FakeExtension'],
	'test' => ['xxx' => ['yyy']],
]);
Assert::same(['xxx' => ['yyy']], $refreshedContainer->parameters['test']);
Assert::type('Testbench\FakeExtension', $extension = $refreshedContainer->getService('test'));

If you have slow tests it can be useful to change RUNLEVEL. Runlevel is environment variable and default value is zero. Higher value means slower tests:

use \Testbench\TCompiledContainer;

public function testSlow()
{
	$this->changeRunLevel(\Testbench::QUICK); // (int)0
	$this->changeRunLevel(\Testbench::FINE); // (int)5
	$this->changeRunLevel(\Testbench::SLOW); // (int)10
}

Test is skipped if you run tests with lower runlevel. Higher runlevel can be set using command line this way (Linux):

$ RUNLEVEL=10 vendor/bin/run-tests ...

There are also useful shortcuts for changing runlevel:

use \Testbench\TCompiledContainer;

public function testSlow()
{
	$this->markTestAsSlow(); // same as \Testbench::FINE
	$this->markTestAsVerySlow(); // same as \Testbench::SLOW
}
Clone this wiki locally