Skip to content

Commit

Permalink
Auto-Inject into Test\Unit (Codeception#4070)
Browse files Browse the repository at this point in the history
* allow to inject actor classes and other objects from DI into the Test\Unit via inject
deprecates using  $this->tester in favor of common _inject()

* fixed creating actor object via di
  • Loading branch information
DavertMik authored and bulzgkri committed Jun 16, 2017
1 parent f048356 commit 09df081
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Codeception/Test/Unit.php
Expand Up @@ -3,6 +3,7 @@

use Codeception\Configuration;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Di;
use Codeception\Scenario;
use Codeception\TestInterface;

Expand Down Expand Up @@ -39,14 +40,18 @@ protected function setUp()
}
return;
}
$scenario = new Scenario($this);
$actorClass = $this->getMetadata()->getCurrent('actor');
if ($actorClass) {
$I = new $actorClass($scenario);
$property = lcfirst(Configuration::config()['actor']);
$this->$property = $I;

/** @var $di Di **/
$di = $this->getMetadata()->getService('di');
$di->set(new Scenario($this));

// auto-inject $tester property
if (($this->getMetadata()->getCurrent('actor')) && ($property = lcfirst(Configuration::config()['actor']))) {
$this->$property = $di->instantiate($this->getMetadata()->getCurrent('actor'));
}
$this->getMetadata()->getService('di')->injectDependencies($this); // injecting dependencies

// Auto inject into the _inject method
$di->injectDependencies($this); // injecting dependencies
$this->_before();
}

Expand Down

0 comments on commit 09df081

Please sign in to comment.