Browse Source

Build out Cell baking.

Extend SimpleBakeTask to generate the class, test and template. I still
need to finish off the TestTask changes.
mark_story 12 years ago
parent
commit
87edcf0d0a

+ 80 - 0
src/Console/Command/Task/CellTask.php

@@ -0,0 +1,80 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console\Command\Task;
+
+use Cake\Console\Command\Task\SimpleBakeTask;
+
+/**
+ * Task for creating cells.
+ */
+class CellTask extends SimpleBakeTask {
+
+/**
+ * Task name used in path generation.
+ *
+ * @var string
+ */
+	public $pathFragment = 'View/Cell/';
+
+/**
+ * {@inheritDoc}
+ */
+	public function name() {
+		return 'cell';
+	}
+
+/**
+ * {@inheritDoc}
+ */
+	public function fileName($name) {
+		return $name . 'Cell.php';
+	}
+
+/**
+ * {@inheritDoc}
+ */
+	public function template() {
+		return 'cell';
+	}
+
+/**
+ * Bake the Cell class and template file.
+ *
+ * @param string $name The name of the cell to make.
+ * @return void
+ */
+	public function bake($name) {
+		$this->bakeTemplate($name);
+		return parent::bake($name);
+	}
+
+/**
+ * Bake an empty file for a cell.
+ *
+ * @param string $name The name of the cell a template is needed for.
+ * @return void
+ */
+	public function bakeTemplate($name) {
+		$templatePath = implode(DS, ['Template', 'Cell', $name, 'display.ctp']);
+		$restore = $this->pathFragment;
+		$this->pathFragment = $templatePath;
+
+		$path = $this->getPath();
+		$this->pathFragment = $restore;
+
+		$this->createFile($path, '');
+	}
+
+}

+ 41 - 0
src/Console/Templates/default/classes/cell.ctp

@@ -0,0 +1,41 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+echo "<?php\n"; ?>
+namespace <?= $namespace ?>\View\Cell;
+
+use Cake\View\Cell;
+
+/**
+ * <?= $name ?> cell
+ */
+class <?= $name ?>Cell extends Cell {
+
+/**
+ * List of valid options that can be passed into this
+ * cell's constructor.
+ *
+ * @var array
+ */
+	protected $_validCellOptions = [];
+
+/**
+ * Default display method.
+ *
+ * @return void
+ */
+	public function display() {
+	}
+
+}

+ 106 - 0
tests/TestCase/Console/Command/Task/CellTaskTest.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Console\Command\Task;
+
+use Cake\Console\Command\Task\TemplateTask;
+use Cake\Core\Configure;
+use Cake\Core\Plugin;
+use Cake\TestSuite\TestCase;
+
+/**
+ * CellTaskTest class
+ */
+class CellTaskTest extends TestCase {
+
+/**
+ * setup method
+ *
+ * @return void
+ */
+	public function setUp() {
+		parent::setUp();
+		$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
+
+		$this->Task = $this->getMock(
+			'Cake\Console\Command\Task\CellTask',
+			['in', 'err', 'createFile', '_stop'],
+			[$io]
+		);
+		$this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask',
+			[],
+			[$io]
+		);
+		$this->Task->Template = new TemplateTask($io);
+		$this->Task->Template->initialize();
+		$this->Task->Template->interactive = false;
+	}
+
+/**
+ * Test the excute method.
+ *
+ * @return void
+ */
+	public function testExecute() {
+		$this->Task->Test->expects($this->once())
+			->method('bake')
+			->with('cell', 'Example');
+
+		$this->Task->expects($this->at(0))
+			->method('createFile')
+			->with(
+				$this->_normalizePath(APP . 'Template/Cell/Example/display.ctp'),
+				''
+			);
+		$this->Task->expects($this->at(1))
+			->method('createFile')
+			->with(
+				$this->_normalizePath(APP . 'View/Cell/ExampleCell.php'),
+				$this->stringContains('class ExampleCell extends Cell')
+			);
+
+		$this->Task->execute('Example');
+	}
+
+/**
+ * Test baking within a plugin.
+ *
+ * @return void
+ */
+	public function testExecutePlugin() {
+		Plugin::load('TestPlugin');
+
+		$path = Plugin::path('TestPlugin');
+
+		$this->Task->plugin = 'TestPlugin';
+		$this->Task->expects($this->at(0))
+			->method('createFile')
+			->with(
+				$this->_normalizePath($path . 'Template/Cell/Example/display.ctp'),
+				''
+			);
+		$this->Task->expects($this->at(1))
+			->method('createFile')
+			->with(
+				$this->_normalizePath($path . 'View/Cell/ExampleCell.php'),
+				$this->stringContains('class ExampleCell extends Cell')
+			);
+
+		$result = $this->Task->bake('Example');
+		$this->assertContains('namespace TestPlugin\View\Cell;', $result);
+		$this->assertContains('use Cake\View\Cell;', $result);
+		$this->assertContains('class ExampleCell extends Cell {', $result);
+	}
+
+}