123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- /**
- * Lithium: the most rad php framework
- *
- * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
- */
- namespace lithium\tests\cases\console;
- use lithium\console\Request;
- use lithium\console\Response;
- use lithium\tests\mocks\console\MockCommand;
- class CommandTest extends \lithium\test\Unit {
- public $request;
- public function setUp() {
- $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
- $this->classes = array('response' => 'lithium\tests\mocks\console\MockResponse');
- }
- public function testConstruct() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = $this->request->env();
- $result = $command->request->env();
- $this->assertEqual($expected, $result);
- $this->request->params = array(
- 'case' => 'lithium.tests.cases.console.CommandTest'
- );
- $command = new MockCommand(array('request' => $this->request));
- $this->assertTrue(!empty($command->case));
- }
- public function testInvoke() {
- $command = new MockCommand(array('request' => $this->request));
- $response = $command('testRun');
- $result = $response;
- $this->assertTrue($result instanceof Response);
- $expected = 'testRun';
- $result = $response->testAction;
- $this->assertEqual($expected, $result);
- }
- public function testInvokeSettingResponseStatus() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = 0;
- $result = $command('testReturnNull')->status;
- $this->assertEqual($expected, $result);
- $expected = 0;
- $result = $command('testReturnTrue')->status;
- $this->assertEqual($expected, $result);
- $expected = 1;
- $result = $command('testReturnFalse')->status;
- $this->assertEqual($expected, $result);
- $expected = -1;
- $result = $command('testReturnNegative1')->status;
- $this->assertEqual($expected, $result);
- $expected = 1;
- $result = $command('testReturn1')->status;
- $this->assertEqual($expected, $result);
- $expected = 3;
- $result = $command('testReturn3')->status;
- $this->assertEqual($expected, $result);
- $expected = 'this is a string';
- $result = $command('testReturnString')->status;
- $this->assertEqual($expected, $result);
- $expected = 1;
- $result = $command('testReturnEmptyArray')->status;
- $this->assertEqual($expected, $result);
- $expected = 0;
- $result = $command('testReturnArray')->status;
- $this->assertEqual($expected, $result);
- }
- public function testOut() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "ok\n";
- $result = $command->out('ok');
- $this->assertEqual($expected, $result);
- }
- public function testOutArray() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "line 1\nline 2\n";
- $command->out(array('line 1', 'line 2'));
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testError() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "ok\n";
- $result = $command->error('ok');
- $this->assertEqual($expected, $result);
- }
- public function testErrorArray() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "line 1\nline 2\n";
- $command->error(array('line 1', 'line 2'));
- $result = $command->response->error;
- $this->assertEqual($expected, $result);
- }
- public function testNl() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "\n\n\n";
- $result = $command->nl(3);
- $this->assertEqual($expected, $result);
- }
- public function testHr() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "----\n";
- $command->hr(4);
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testHeader() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "----\nheader\n----\n";
- $command->header('header', 4);
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testColumns() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "data1\t\ndata2\t\n";
- $command->columns(array('col1' => 'data1', 'col2' => 'data2'));
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testHelp() {
- $command = new MockCommand(array('request' => $this->request));
- $return = $command->__invoke('_help');
- $this->assertTrue($return instanceOf \lithium\tests\mocks\console\MockResponse);
- $expected = "DESCRIPTION.*This is the Mock Command";
- $result = $command->response->output;
- $this->assertPattern("/{$expected}/s", $result);
- $command = new MockCommand(array('request' => $this->request));
- $return = $command->__invoke('_help');
- $expected = "testRun";
- $result = $command->response->output;
- $this->assertPattern("/{$expected}/m", $result);
- }
- public function testIn() {
- $command = new MockCommand(array('request' => $this->request));
- fwrite($command->request->input, 'nada mucho');
- rewind($command->request->input);
- $expected = "nada mucho";
- $result = $command->in('What up dog?');
- $this->assertEqual($expected, $result);
- $expected = "What up dog? \n > ";
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testQuit() {
- $command = new MockCommand(array('request' => $this->request));
- fwrite($command->request->input, "q\n");
- rewind($command->request->input);
- $result = $command->in('This should return bool false');
- $this->assertFalse($result);
- }
- public function testInWithDefaultOption() {
- $command = new MockCommand(array('request' => $this->request));
- fwrite($command->request->input, ' ');
- rewind($command->request->input);
- $expected = "y";
- $result = $command->in('What up dog?', array('default' => 'y'));
- $this->assertEqual($expected, $result);
- $expected = "What up dog? \n [y] > ";
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- fwrite($command->request->input, "\n");
- fwrite($command->request->input, 'n');
- rewind($command->request->input);
- $expected = "y";
- $result = $command->in('R U Sure?', array('choices' => array('y', 'n'), 'default' => 'y'));
- $this->assertEqual($expected, $result);
- }
- public function testInWithOptions() {
- $command = new MockCommand(array('request' => $this->request));
- fwrite($command->request->input, 'y');
- rewind($command->request->input);
- $expected = "y";
- $result = $command->in('Everything Cool?', array('choices' => array('y', 'n')));
- $this->assertEqual($expected, $result);
- $expected = "Everything Cool? (y/n) \n > ";
- $result = $command->response->output;
- $this->assertEqual($expected, $result);
- }
- public function testInWithBadInput(){
- $command = new MockCommand(array('request' => $this->request));
- fwrite($command->request->input, "f\n");
- fwrite($command->request->input, 'y');
- rewind($command->request->input);
- $expected = "y";
- $result = $command->in('Everything Cool?', array('choices' => array('y', 'n')));
- $this->assertEqual($expected, $result);
- }
- public function testOutWithStyles() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "{:some-style}ok\n";
- $result = $command->out('ok', 'some-style');
- $this->assertEqual($expected, $result);
- }
- public function testOutWithoutStyles() {
- $command = new MockCommand(array('request' => $this->request));
- $command->plain = true;
- $expected = "ok\n";
- $result = $command->out('ok', 'some-style');
- $this->assertEqual($expected, $result);
- }
- public function testOutWithSilent() {
- $command = new MockCommand(array('request' => $this->request));
- $command->silent = true;
- $expected = "";
- $result = $command->out('ok', 'some-style');
- $this->assertEqual($expected, $result);
- }
- public function testColumnsOnErrorOutput() {
- $command = new MockCommand(array('request' => $this->request));
- $expected = "data1\t\ndata2\t\n";
- $command->columns(array('col1' => 'data1', 'col2' => 'data2'), array('error' => true));
- $result = $command->response->error;
- $this->assertEqual($expected, $result);
- }
- }
- ?>
|