cakephp2-php8/lib/Cake/Test/Case/Console/Helper/ProgressShellHelperTest.php

223 lines
5.8 KiB
PHP
Raw Normal View History

2015-12-01 12:02:51 +00:00
<?php
2015-12-01 14:26:22 +00:00
/**
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2015-12-01 14:26:22 +00:00
*
* 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)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
2015-12-01 14:26:22 +00:00
* @since 2.8
* @license https://opensource.org/licenses/mit-license.php MIT License
2015-12-01 14:26:22 +00:00
*/
App::uses("ProgressShellHelper", "Console/Helper");
2015-12-01 12:02:51 +00:00
App::uses("ConsoleOutputStub", "TestSuite/Stub");
/**
* ProgressHelper test.
* @property ConsoleOutputStub $consoleOutput
* @property ProgressShellHelper $helper
2015-12-01 12:02:51 +00:00
*/
2015-12-01 14:26:22 +00:00
class ProgressShellHelperTest extends CakeTestCase {
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
$this->consoleOutput = new ConsoleOutputStub();
$this->helper = new ProgressShellHelper($this->consoleOutput);
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test that a callback is required.*
*
* @expectedException \RuntimeException
* @return void
*/
public function testOutputFailure() {
2015-12-01 15:16:54 +00:00
$this->helper->output(array('not a callback'));
2015-12-01 14:26:22 +00:00
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test that the callback is invoked until 100 is reached.
*
* @return void
*/
public function testOutputSuccess() {
2015-12-01 15:16:54 +00:00
$this->helper->output(array(function ($progress) {
2015-12-01 14:26:22 +00:00
$progress->increment(20);
2015-12-01 15:16:54 +00:00
}));
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'==============> 20%',
'',
'=============================> 40%',
'',
'============================================> 60%',
'',
'===========================================================> 80%',
'',
'==========================================================================> 100%',
'',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test output with options
*
* @return void
*/
public function testOutputSuccessOptions() {
2015-12-01 15:16:54 +00:00
$this->helper->output(array(
2015-12-01 14:26:22 +00:00
'total' => 10,
'width' => 20,
'callback' => function ($progress) {
$progress->increment(2);
}
2015-12-01 15:16:54 +00:00
));
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'==> 20%',
'',
'=====> 40%',
'',
'========> 60%',
'',
'===========> 80%',
'',
'==============> 100%',
'',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test using the helper manually.
*
* @return void
*/
public function testIncrementAndRender() {
$this->helper->init();
$this->helper->increment(20);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
2015-12-01 15:16:54 +00:00
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'==============> 20%',
'',
'============================================> 60%',
'',
'==========================================================================> 100%',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test negative numbers
*
* @return void
*/
public function testIncrementWithNegatives() {
$this->helper->init();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(-60);
$this->helper->draw();
$this->helper->increment(80);
$this->helper->draw();
2015-12-01 15:16:54 +00:00
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'=============================> 40%',
'',
' 0%',
'',
'===========================================================> 80%',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test increment and draw with options
*
* @return void
*/
public function testIncrementWithOptions() {
2015-12-01 15:16:54 +00:00
$this->helper->init(array(
2015-12-01 14:26:22 +00:00
'total' => 10,
'width' => 20,
2015-12-01 15:16:54 +00:00
));
2015-12-01 14:26:22 +00:00
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
2015-12-01 12:02:51 +00:00
2015-12-01 15:16:54 +00:00
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'=====> 40%',
'',
'===========> 80%',
'',
'==============> 100%',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
2015-12-01 14:26:22 +00:00
/**
* Test increment and draw with value that makes the pad
* be a float
*
* @return void
*/
public function testIncrementFloatPad() {
2015-12-01 15:16:54 +00:00
$this->helper->init(array(
2015-12-01 14:26:22 +00:00
'total' => 50
2015-12-01 15:16:54 +00:00
));
2015-12-01 14:26:22 +00:00
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(3);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(8);
$this->helper->draw();
2015-12-01 15:16:54 +00:00
$expected = array(
2015-12-01 14:26:22 +00:00
'',
'=========> 14%',
'',
'====================> 28%',
'',
'==============================> 42%',
'',
'=========================================> 56%',
'',
'===================================================> 70%',
'',
'========================================================> 76%',
'',
'==============================================================> 84%',
'',
'==========================================================================> 100%',
2015-12-01 15:16:54 +00:00
);
2015-12-01 14:26:22 +00:00
$this->assertEquals($expected, $this->consoleOutput->messages());
}
2015-12-01 12:02:51 +00:00
}