Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Allow input and output streams to be configured #59

Open
wants to merge 1 commit into from

1 participant

zeebinz
zeebinz

Testing is possible if STDOUT and STDIN can be replaced with other streams. The tests in cli.php are proof of concept, but there are probably better solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 25, 2013
zeebinz Allow input and output streams to be configured e5956c5
This page is out of date. Refresh to see the latest.
53  classes/Kohana/Minion/CLI.php
@@ -34,6 +34,16 @@ class Kohana_Minion_CLI {
34 34
 	);
35 35
 
36 36
 	/**
  37
+	 * @var  resource  The configured output stream
  38
+	 */
  39
+	protected static $_stdout = STDOUT;
  40
+
  41
+	/**
  42
+	 * @var  resource  The configured input stream
  43
+	 */
  44
+	protected static $_stdin = STDIN;
  45
+
  46
+	/**
37 47
 	 * Returns one or more command-line options. Options are specified using
38 48
 	 * standard CLI syntax:
39 49
 	 *
@@ -130,10 +140,10 @@ public static function read($text = '', array $options = NULL)
130 140
 			$options_output = ' [ '.implode(', ', $options).' ]';
131 141
 		}
132 142
 
133  
-		fwrite(STDOUT, $text.$options_output.': ');
  143
+		fwrite(Minion_CLI::$_stdout, $text.$options_output.': ');
134 144
 
135 145
 		// Read the input from keyboard.
136  
-		$input = trim(fgets(STDIN));
  146
+		$input = trim(fgets(Minion_CLI::$_stdin));
137 147
 
138 148
 		// If options are provided and the choice is not in the array, tell them to try again
139 149
 		if ( ! empty($options) && ! in_array($input, $options))
@@ -202,7 +212,7 @@ public static function write($text = '')
202 212
 		}
203 213
 		else
204 214
 		{
205  
-			fwrite(STDOUT, $text.PHP_EOL);
  215
+			fwrite(Minion_CLI::$_stdout, $text.PHP_EOL);
206 216
 		}
207 217
 	}
208 218
 
@@ -226,7 +236,7 @@ public static function write_replace($text = '', $end_line = FALSE)
226 236
 	{
227 237
 		// Append a newline if $end_line is TRUE
228 238
 		$text = $end_line ? $text.PHP_EOL : $text;
229  
-		fwrite(STDOUT, "\r\033[K".$text);
  239
+		fwrite(Minion_CLI::$_stdout, "\r\033[K".$text);
230 240
 	}
231 241
 
232 242
 	/**
@@ -248,7 +258,7 @@ public static function wait($seconds = 0, $countdown = false)
248 258
 
249 259
 			while ($time > 0)
250 260
 			{
251  
-				fwrite(STDOUT, $time.'... ');
  261
+				fwrite(Minion_CLI::$_stdout, $time.'... ');
252 262
 				sleep(1);
253 263
 				$time--;
254 264
 			}
@@ -312,4 +322,37 @@ public static function color($text, $foreground, $background = null)
312 322
 		return $string;
313 323
 	}
314 324
 
  325
+	/**
  326
+	 * Sets the output stream.
  327
+	 *
  328
+	 * @param   resource  $stream  the output stream
  329
+	 * @return  bool      true if the stream was set successfully
  330
+	 */
  331
+	public static function set_stdout($stream)
  332
+	{
  333
+		if (is_resource($stream) AND get_resource_type($stream) == 'stream')
  334
+		{
  335
+			Minion_CLI::$_stdout = $stream;
  336
+			return TRUE;
  337
+		}
  338
+
  339
+		return FALSE;
  340
+	}
  341
+
  342
+	/**
  343
+	 * Sets the input stream.
  344
+	 *
  345
+	 * @param   resource  $stream  the input stream
  346
+	 * @return  bool      true if the stream was set successfully
  347
+	 */
  348
+	public static function set_stdin($stream)
  349
+	{
  350
+		if (is_resource($stream) AND get_resource_type($stream) == 'stream')
  351
+		{
  352
+			Minion_CLI::$_stdin = $stream;
  353
+			return TRUE;
  354
+		}
  355
+
  356
+		return FALSE;
  357
+	}
315 358
 }
96  tests/minion/cli.php
... ...
@@ -0,0 +1,96 @@
  1
+<?php
  2
+/**
  3
+ * Test case for Minion_CLI.
  4
+ *
  5
+ * @package    Kohana/Minion
  6
+ * @group      kohana
  7
+ * @group      kohana.minion
  8
+ * @category   Test
  9
+ * @author     Kohana Team
  10
+ * @copyright  (c) 2009-2012 Kohana Team
  11
+ * @license    http://kohanaframework.org/license
  12
+ */
  13
+class Minion_CLITest extends Unittest_TestCase
  14
+{
  15
+	/**
  16
+	 * Tests that only valid streams can be set.
  17
+	 *
  18
+	 * @covers Minion_CLI::set_stdout
  19
+	 * @covers Minion_CLI::set_stdin
  20
+	 */
  21
+	public function test_setting_streams()
  22
+	{
  23
+		$this->assertFalse(Minion_CLI::set_stdout('invalid'));
  24
+		$this->assertFalse(Minion_CLI::set_stdout(1));
  25
+
  26
+		$stream = fopen('php://memory', 'rw');
  27
+		$this->assertTrue(Minion_CLI::set_stdout($stream));
  28
+		fclose($stream);
  29
+
  30
+		$this->assertFalse(Minion_CLI::set_stdin('invalid'));
  31
+		$this->assertFalse(Minion_CLI::set_stdin(1));
  32
+
  33
+		$stream = fopen('php://memory', 'rw');
  34
+		$this->assertTrue(Minion_CLI::set_stdin($stream));
  35
+		fclose($stream);
  36
+	}
  37
+
  38
+	/**
  39
+	 * Tests that any output can be captured effectively.
  40
+	 *
  41
+	 * @covers Minion_CLI::write
  42
+	 */
  43
+	public function test_writing_to_and_reading_from_output_stream()
  44
+	{
  45
+		$stream = fopen('php://memory', 'rw');
  46
+		Minion_CLI::set_stdout($stream);
  47
+
  48
+		$msg = 'first test message';
  49
+		Minion_CLI::write($msg);
  50
+		rewind($stream);
  51
+
  52
+		$this->assertSame($msg, trim(fgets($stream)));
  53
+		rewind($stream);
  54
+
  55
+		$msg = 'second test message';
  56
+		Minion_CLI::write($msg);
  57
+		rewind($stream);
  58
+
  59
+		$this->assertSame($msg, trim(fgets($stream)));
  60
+		fclose($stream);
  61
+	}
  62
+
  63
+	/**
  64
+	 * Tests that any input can be handled effectively.
  65
+	 *
  66
+	 * @covers Minion_CLI::read
  67
+	 */
  68
+	public function test_writing_to_and_reading_from_input_stream()
  69
+	{
  70
+		$output = fopen('php://memory', 'rw');
  71
+		$input  = fopen('php://memory', 'rw');
  72
+		$this->assertNotSame($output, $input);
  73
+
  74
+		Minion_CLI::set_stdout($output);
  75
+		Minion_CLI::set_stdin($input);
  76
+
  77
+		fwrite($input, 'y'.PHP_EOL.'red'.PHP_EOL);
  78
+		rewind($input);
  79
+
  80
+		$read = Minion_CLI::read('Choose', array('y', 'n'));
  81
+		$this->assertSame('y', $read);
  82
+
  83
+		$read = Minion_CLI::read('Choose', array('red', 'blue'));
  84
+		$this->assertSame('red', $read);
  85
+
  86
+		fclose($output);
  87
+		fclose($input);
  88
+	}
  89
+
  90
+	public static function tearDownAfterClass()
  91
+	{
  92
+		// Restore the default streams
  93
+		Minion_CLI::set_stdin(STDIN);
  94
+		Minion_CLI::set_stdout(STDOUT);
  95
+	}
  96
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.