From 78794472e1b72ccc6a55ac71450c0debbdd2eb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubomi=CC=81r=20S=CC=8Ctork?= Date: Wed, 30 Oct 2013 12:42:01 +0100 Subject: [PATCH 1/4] Migrated ProgressBarTask for CakePHP 2.x --- .../Command/Task/ProgressBarTask.php | 10 +++++++--- .../Case/Console/Command/Task/ProgressBarTaskTest.php | 0 2 files changed, 7 insertions(+), 3 deletions(-) rename vendors/shells/tasks/progress_bar.php => Console/Command/Task/ProgressBarTask.php (94%) rename tests/cases/vendors/shells/tasks/progress_bar.test.php => Test/Case/Console/Command/Task/ProgressBarTaskTest.php (100%) diff --git a/vendors/shells/tasks/progress_bar.php b/Console/Command/Task/ProgressBarTask.php similarity index 94% rename from vendors/shells/tasks/progress_bar.php rename to Console/Command/Task/ProgressBarTask.php index 60a5402..79ddc18 100644 --- a/vendors/shells/tasks/progress_bar.php +++ b/Console/Command/Task/ProgressBarTask.php @@ -1,4 +1,7 @@ Date: Thu, 31 Oct 2013 11:24:11 +0100 Subject: [PATCH 2/4] Fix variable name --- Console/Command/Task/ProgressBarTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Console/Command/Task/ProgressBarTask.php b/Console/Command/Task/ProgressBarTask.php index 79ddc18..6a02222 100644 --- a/Console/Command/Task/ProgressBarTask.php +++ b/Console/Command/Task/ProgressBarTask.php @@ -137,7 +137,7 @@ public function next($inc = 1) { * @access public */ public function out($message = null, $newlines = 0, $level = Shell::NORMAL) { - return parent::out($message, $newLines, $level); + return parent::out($message, $newlines, $level); } /** From 29aefbb121c8590fe3105099abd1bcf05fb9caab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubomi=CC=81r=20S=CC=8Ctork?= Date: Sun, 10 Nov 2013 20:33:12 +0100 Subject: [PATCH 3/4] Doc. comments cleanup --- Console/Command/Task/ProgressBarTask.php | 40 ++++++------------------ 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/Console/Command/Task/ProgressBarTask.php b/Console/Command/Task/ProgressBarTask.php index 6a02222..6ca8faf 100644 --- a/Console/Command/Task/ProgressBarTask.php +++ b/Console/Command/Task/ProgressBarTask.php @@ -1,8 +1,5 @@ * @license MIT - * */ +App::uses('AppShell', 'Console/Command'); + /** * ProgressBarTask class * - * @uses Shell - * @package progress_bar - * @subpackage progress_bar.vendors.shells.tasks + * @uses AppShell + * @package ProgressBar + * @subpackage ProgressBar.Console.Command.Task */ class ProgressBarTask extends AppShell { @@ -26,7 +24,6 @@ class ProgressBarTask extends AppShell { * Console Width * * @var int - * @access public */ public $terminalWidth = null; @@ -34,7 +31,6 @@ class ProgressBarTask extends AppShell { * message displayed during updates * * @var string '' - * @access public */ public $message = ''; @@ -42,7 +38,6 @@ class ProgressBarTask extends AppShell { * Maximum value on the bar * * @var int - * @access public */ public $total = 100; @@ -50,7 +45,6 @@ class ProgressBarTask extends AppShell { * Size * * @var int - * @access public */ public $size = 25; @@ -58,7 +52,6 @@ class ProgressBarTask extends AppShell { * Amount Completed * * @var int - * @access public */ public $done = 0; @@ -66,7 +59,6 @@ class ProgressBarTask extends AppShell { * Start Time * * @var mixed - * @access public */ public $startTime = null; @@ -74,7 +66,6 @@ class ProgressBarTask extends AppShell { * String length for the previous line. Used to overwrite hanging chars/ * * @var int - * @access public */ public $strLenPrevLine = null; @@ -82,7 +73,6 @@ class ProgressBarTask extends AppShell { * Execute the task - nothing to do by default * * @return void - * @access public */ public function execute() { } @@ -94,7 +84,6 @@ public function execute() { * if you don't know the exact number of steps it's going to take * * @return void - * @access public */ public function finish() { if ($this->done < $this->total) { @@ -107,7 +96,6 @@ public function finish() { * * @param string $message '' * @return void - * @access public */ public function message($message = '') { $this->message = $message; @@ -116,8 +104,8 @@ public function message($message = '') { /** * Increment the progress * + * @param integer $inc increment value * @return void - * @access public */ public function next($inc = 1) { $this->done += $inc; @@ -134,7 +122,6 @@ public function next($inc = 1) { * @param integer $newlines Number of newlines to append * @param integer $level The message's output level * @return integer Returns the number of bytes returned from writing to stdout. - * @access public */ public function out($message = null, $newlines = 0, $level = Shell::NORMAL) { return parent::out($message, $newlines, $level); @@ -143,16 +130,9 @@ public function out($message = null, $newlines = 0, $level = Shell::NORMAL) { /** * Set the values and output * - * @return void - * @access public - */ -/** - * set method - * * @param string $done Amount completed * @param string $doneSize bar size * @return void - * @access public */ public function set($done = null, $doneSize = null) { if ($done) { @@ -202,8 +182,8 @@ public function set($done = null, $doneSize = null) { * Start a progress bar * * @param string $total Total value of the progress bar + * @param boolean $clear append one line * @return void - * @access public */ public function start($total, $clear = true) { $this->total = $total; @@ -219,7 +199,6 @@ public function start($total, $clear = true) { * Calculate remaining time in a nice format * * @return void - * @access public */ protected function _niceRemaining() { $now = time(); @@ -247,7 +226,6 @@ protected function _niceRemaining() { * @TODO can you get windows to tell you the size of the terminal? * @param mixed $width null * @return void - * @access protected */ protected function _setTerminalWidth($width = null) { if ($width === null) { @@ -261,5 +239,5 @@ protected function _setTerminalWidth($width = null) { $this->size = min(max(4, $width / 10), $this->size); $this->terminalWidth = $width; } + } -?> From 78e6dd262cdd7511af6bd215d9fd20c25a88883c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubomi=CC=81r=20S=CC=8Ctork?= Date: Sun, 10 Nov 2013 20:33:26 +0100 Subject: [PATCH 4/4] Fixed test case for CakePHP 2.x --- .../Command/Task/ProgressBarTaskTest.php | 125 ++++++++---------- 1 file changed, 55 insertions(+), 70 deletions(-) diff --git a/Test/Case/Console/Command/Task/ProgressBarTaskTest.php b/Test/Case/Console/Command/Task/ProgressBarTaskTest.php index 7a7a7b9..b80a98f 100644 --- a/Test/Case/Console/Command/Task/ProgressBarTaskTest.php +++ b/Test/Case/Console/Command/Task/ProgressBarTaskTest.php @@ -12,28 +12,20 @@ * * @author Matt Curry * @license MIT - * */ -App::import('Shell', 'Shell', false); -App::import('Vendor', 'ProgressBar.ProgressBarTask', array('file' => 'shells' . DS . 'tasks' . DS . 'progress_bar.php')); - -if (!defined('DISABLE_AUTO_DISPATCH')) { - define('DISABLE_AUTO_DISPATCH', true); -} -if (!class_exists('ShellDispatcher')) { - ob_start(); - $argv = false; - require CAKE . 'console' . DS . 'cake.php'; - ob_end_clean(); -} +App::uses('ShellDispatcher', 'Console'); +App::uses('ConsoleOutput', 'Console'); +App::uses('ConsoleInput', 'Console'); +App::uses('Shell', 'Console'); +App::uses('ProgressBarTask', 'ProgressBar.Console/Command/Task'); /** * TestProgressBarTask class * * @uses ProgressBarTask - * @package progress_bar - * @subpackage progress_bar.tests.cases.vendors.shells.tasks + * @package ProgressBar + * @subpackage ProgressBar.Test.Case.Console.Command.Task */ class TestProgressBarTask extends ProgressBarTask { @@ -41,7 +33,6 @@ class TestProgressBarTask extends ProgressBarTask { * Output generated during test * * @var array - * @access public */ public $messages = array(); @@ -49,71 +40,74 @@ class TestProgressBarTask extends ProgressBarTask { * niceRemaining proxy method * * @return void - * @access public */ - function niceRemaining() { + public function niceRemaining() { return $this->_niceRemaining(); } - function messages($clear = true) { - $return = $this->messages; - if ($clear) { - $this->messages = array(); - } - return $return; +/** + * Overrides parent method, forcing info about terminal width to 80, so tests + * can pass in both console and browser. + * + * @param mixed $width null + * @return void + */ + protected function _setTerminalWidth($width = null) { + parent::_setTerminalWidth(80); } - function out($message) { +/** + * Overrides parent method, pushing messages to public property. + * + * @param mixed $message + * @param integer $newlines + * @param integer $level + * @return integer + */ + public function out($message = null, $newlines = 0, $level = Shell::NORMAL) { $this->messages[] = $message; } -} -Mock::generatePartial( - 'ShellDispatcher', 'TestProgressBarTaskMockShellDispatcher', - array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment') -); +} -Mock::generatePartial( - 'TestProgressBarTask', 'MockProgressBarTask', - array('in', '_stop', 'err') -); /** * ProgressBarTask Test class * - * @package progress_bar - * @subpackage progress_bar.tests.cases.vendors.shells.tasks + * @package ProgressBar + * @subpackage ProgressBar.Test.Case.Console.Command.Task */ class ProgressBarTaskTest extends CakeTestCase { /** - * startTest method + * setUp method * * @return void - * @access public */ - public function startTest() { - $this->Dispatcher = new TestProgressBarTaskMockShellDispatcher(); - $this->Dispatcher->shellPaths = App::path('shells'); - $this->Task = new MockProgressBarTask($this->Dispatcher); - $this->Task->Dispatch = $this->Dispatcher; - $this->Task->path = TMP . 'tests' . DS; + public function setUp() { + parent::setUp(); + $out = $this->getMock('ConsoleOutput', array(), array(), '', false); + $in = $this->getMock('ConsoleInput', array(), array(), '', false); + + $this->Task = $this->getMock('TestProgressBarTask', + array('in', 'err', '_stop'), + array($out, $out, $in) + ); } /** - * endTest method + * tearDown method * * @return void - * @access public */ - public function endTest() { - ClassRegistry::flush(); + public function tearDown() { + parent::tearDown(); + unset($this->Task); } /** * testStartup method * * @return void - * @access public */ public function testStartup() { $total = 100; @@ -128,20 +122,19 @@ public function testStartup() { * testSimpleFormatting method * * @return void - * @access public */ public function testSimpleFormatting() { $this->Task->start(100); $this->Task->next(1); - $result = end($this->Task->messages()); + $result = end($this->Task->messages); $this->assertPattern('@\[>\s+\] 1.0% 1/100.*remaining$@', $result); $this->Task->next(49); - $result = end($this->Task->messages()); + $result = end($this->Task->messages); $this->assertPattern('@\[-+>\s+\] 50.0% 50/100.*remaining$@', $result); $this->Task->next(50); - $result = end($this->Task->messages()); + $result = end($this->Task->messages); $this->assertPattern('@\[-+>\] 100.0% 100/100.*remaining$@', $result); } @@ -151,28 +144,26 @@ public function testSimpleFormatting() { * Test/demonstrate what happens when you bail early or overrun. * * @return void - * @access public */ public function testSimpleBoundaries() { $this->Task->start(100); $this->Task->next(50); $this->Task->finish(1); - $result = end($this->Task->messages()); - $this->assertPattern('@\[-{25}>] 50.0% 50/100.*remaining$@', $result); + $result = end($this->Task->messages); + $this->assertPattern('@\[-{8}>] 50.0% 50/100.*remaining$@', $result); $this->Task->start(100); $this->Task->next(150); - $result = end($this->Task->messages()); - $this->assertPattern('@\[-{25}>\] 150.0% 150/100.*remaining$@', $result); + $result = end($this->Task->messages); + $this->assertPattern('@\[-{8}>\] 150.0% 150/100.*remaining$@', $result); } /** * testMessageUsage method * * @return void - * @access public */ public function testMessageUsage() { $this->Task->message('Running your 100 step process'); @@ -180,20 +171,19 @@ public function testMessageUsage() { $this->Task->terminalWidth = 100; $this->Task->next(1); - $result = end($this->Task->messages()); + $result = end($this->Task->messages); $this->assertPattern('@Running your 100 step process\s+1.0% 1/100.*remaining \[>\s+\]$@', $result); - $this->Task->message('Changed and muuuuuuuuuuuuuuuuuch longer message'); + $this->Task->message('Changed and muuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuch longer message'); $this->Task->next(1); - $result = end($this->Task->messages()); - $this->assertPattern('@Changed and muuuuuuuuuuuuuuuuuch longer messa... 2.0% 2/100.*remaining \[>\s+\]$@', $result); + $result = end($this->Task->messages); + $this->assertPattern('@Changed and muuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuch longer messa... 2.0% 2/100.*remaining \[>\s+\]$@', $result); } /** * testExecuteNothing method * * @return void - * @access public */ public function testExecuteNothing() { $this->assertNull($this->Task->execute()); @@ -203,7 +193,6 @@ public function testExecuteNothing() { * testNext method * * @return void - * @access public */ public function testNext() { $this->Task->start(100); @@ -215,7 +204,6 @@ public function testNext() { * testNiceRemainingUnknown method * * @return void - * @access public */ public function testNiceRemainingUnknown() { $this->Task->start(100); @@ -232,7 +220,6 @@ public function testNiceRemainingUnknown() { * testNiceRemainingBasic method * * @return void - * @access public */ public function testNiceRemainingBasic() { // 2 seconds per iteration, should take 20 seconds total. @@ -269,7 +256,6 @@ public function testNiceRemainingBasic() { * testNiceRemainingMinutes method * * @return void - * @access public */ public function testNiceRemainingMinutes() { // 2 seconds per iteration, should take 120 seconds total. @@ -307,7 +293,6 @@ public function testNiceRemainingMinutes() { * testSet method * * @return void - * @access public */ public function testSet() { $this->Task->start(100); @@ -317,5 +302,5 @@ public function testSet() { $this->Task->set(200); $this->assertEqual($this->Task->done, 100); } + } -?> \ No newline at end of file