Browse Source

Start Command class.

Mark Story 8 years ago
parent
commit
c9ef4b6de3
2 changed files with 105 additions and 0 deletions
  1. 54 0
      src/Console/Command.php
  2. 51 0
      tests/TestCase/Console/CommandTest.php

+ 54 - 0
src/Console/Command.php

@@ -0,0 +1,54 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console;
+
+use Cake\Datasource\ModelAwareTrait;
+use Cake\Log\LogTrait;
+use Cake\ORM\Locator\LocatorAwareTrait;
+
+/**
+ * Base class for console commands.
+ */
+class Command
+{
+    use LocatorAwareTrait;
+    use LogTrait;
+    use ModelAwareTrait;
+
+    /**
+     * Constructor
+     *
+     * By default CakePHP will construct command objects when
+     * building the CommandCollection for your application.
+     */
+    public function __construct()
+    {
+        $locator = $this->getTableLocator() ? : 'Cake\ORM\TableRegistry';
+        $this->modelFactory('Table', [$locator, 'get']);
+    }
+
+    /**
+     * Hook method invoked by CakePHP when a command is about to be executed.
+     *
+     * Override this method and implement expensive/important setup steps that
+     * should not run on every command run. This method will be called *before*
+     * the options and arguments are validated and processed.
+     *
+     * @return void
+     */
+    public function initialize()
+    {
+    }
+}

+ 51 - 0
tests/TestCase/Console/CommandTest.php

@@ -0,0 +1,51 @@
+<?php
+/**
+ * CakePHP :  Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Console;
+
+use Cake\Console\Command;
+use Cake\ORM\Locator\TableLocator;
+use Cake\ORM\Table;
+use Cake\TestSuite\TestCase;
+
+
+/**
+ * Test case for Console\Command
+ */
+class CommandTest extends TestCase
+{
+    /**
+     * test orm locator is setup
+     *
+     * @return void
+     */
+    public function testConstructorSetsLocator()
+    {
+        $command = new Command();
+        $result = $command->getTableLocator();
+        $this->assertInstanceOf(TableLocator::class, $result);
+    }
+
+    /**
+     * test loadModel is configured properly
+     *
+     * @return void
+     */
+    public function testConstructorLoadModel()
+    {
+        $command = new Command();
+        $command->loadModel('Comments');
+        $this->assertInstanceOf(Table::class, $command->Comments);
+    }
+}