Skip to content

Commit

Permalink
Allow for table locator injection into Shell.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 21, 2017
1 parent 3b684c8 commit 186041b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Console/Shell.php
Expand Up @@ -21,7 +21,7 @@
use Cake\Filesystem\File;
use Cake\Log\LogTrait;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Locator\TableLocator;
use Cake\ORM\Locator\LocatorInterface;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\Text;
Expand Down Expand Up @@ -175,17 +175,19 @@ class Shell
* Constructs this Shell instance.
*
* @param \Cake\Console\ConsoleIo|null $io An io instance.
* @param \Cake\ORM\Locator\LocatorInterface|null $locator Table locator instance.
* @link https://book.cakephp.org/3.0/en/console-and-shells.html#Shell
*/
public function __construct(ConsoleIo $io = null)
public function __construct(ConsoleIo $io = null, LocatorInterface $locator = null)
{
if (!$this->name) {
list(, $class) = namespaceSplit(get_class($this));
$this->name = str_replace(['Shell', 'Task'], '', $class);
}
$this->_io = $io ?: new ConsoleIo();
$this->_tableLocator = $locator;

$this->modelFactory('Table', [$this->getTableLocator() ?: new TableLocator(), 'get']);
$this->modelFactory('Table', [$this->getTableLocator(), 'get']);
$this->Tasks = new TaskRegistry($this);

$this->_mergeVars(
Expand Down
18 changes: 17 additions & 1 deletion tests/TestCase/Console/ShellTest.php
Expand Up @@ -141,7 +141,7 @@ public function setUp()
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
->disableOriginalConstructor()
->getMock();
$this->Shell = new ShellTestShell($this->io);
$this->Shell = new ShellTestShell($this->io, $this->getTableLocator());

if (is_dir(TMP . 'shell_test')) {
$Folder = new Folder(TMP . 'shell_test');
Expand All @@ -158,6 +158,22 @@ public function testConstruct()
{
$this->assertEquals('ShellTestShell', $this->Shell->name);
$this->assertInstanceOf('Cake\Console\ConsoleIo', $this->Shell->io());
$this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', $this->Shell->getTableLocator());
$this->assertSame($this->io, $this->Shell->io());
$this->assertSame($this->getTableLocator(), $this->Shell->getTableLocator());
}

/**
* testConstruct method without args
*
* @return void
*/
public function testConstructWithoutArgs()
{
$shell = new Shell();

$this->assertInstanceOf('Cake\Console\ConsoleIo', $shell->io());
$this->assertInstanceOf('Cake\ORM\Locator\LocatorInterface', $shell->getTableLocator());
}

/**
Expand Down

0 comments on commit 186041b

Please sign in to comment.