array( 'in' => array( 'color' => 'blue', ), 'out' => "\033[34m%s\033[0m" ), 'color_only_2' => array( 'in' => array( 'color' => 'red', ), 'out' => "\033[31m%s\033[0m" ), 'bgcolor_only_1' => array( 'in' => array( 'bgcolor' => 'green', ), 'out' => "\033[42m%s\033[0m" ), 'bgcolor_only_2' => array( 'in' => array( 'bgcolor' => 'yellow', ), 'out' => "\033[43m%s\033[0m" ), 'style_only_1' => array( 'in' => array( 'style' => 'bold', ), 'out' => "\033[1m%s\033[0m" ), 'style_only_2' => array( 'in' => array( 'style' => 'negative', ), 'out' => "\033[7m%s\033[0m" ), ); /** * consoleOutput * * @var mixed */ private $consoleOutput; public static function suite() { return new PHPUnit_Framework_TestSuite( "ezcConsoleOutputTest" ); } protected function setUp() { $this->consoleOutput = new ezcConsoleOutput(); foreach ( $this->testFormats as $name => $inout ) { foreach ( $inout['in'] as $formatName => $val ) { $this->consoleOutput->formats->$name->$formatName = $val; } } } protected function tearDown() { unset( $this->consoleOutput ); } /** * testFormatText * * @access public */ public function testFormatText() { foreach ( $this->testFormats as $name => $inout ) { $realRes = $this->consoleOutput->formatText( $this->testString, $name ); $fakeRes = ezcBaseFeatures::os() !== "Windows" ? sprintf( $inout['out'], $this->testString ) : $this->testString; $this->assertEquals( $realRes, $fakeRes, "Test <{$name}> failed. String <{$realRes}> (real) is not equal to <{$fakeRes}> (fake)." ); } } /** * testOutputText * * @access public */ public function testOutputText() { foreach ( $this->testFormats as $name => $inout ) { ob_start(); $this->consoleOutput->outputText( $this->testString, $name ); $realRes = ob_get_contents(); ob_end_clean(); $fakeRes = ezcBaseFeatures::os() !== "Windows" ? sprintf( $inout['out'], $this->testString ) : $this->testString; $this->assertEquals( $fakeRes, $realRes, "Test <{$name}> failed. String <{$realRes}> (real) is not equal to <{$fakeRes}> (fake)." ); } } /** * testOutputTextAutobreak * * @access public */ public function testOutputTextAutobreak() { $this->consoleOutput->options->autobreak = 20; $testText = 'Some text which is obviously longer than 20 characters and should be broken.'; $testResText = <<testFormats as $name => $inout ) { ob_start(); $this->consoleOutput->outputText( $testText, $name ); $realRes = ob_get_contents(); ob_end_clean(); $fakeRes = ezcBaseFeatures::os() !== "Windows" ? sprintf( $inout['out'], $testResText ) : $testResText; $this->assertEquals( $fakeRes, $realRes, 'Test "' . $name . ' failed. String <' . $realRes . '> (real) is not equal to <' . $fakeRes . '> (fake).' ); } } public function testOutputColorAliases() { $this->consoleOutput->formats->aliasBG->bgcolor = "gray"; $this->consoleOutput->formats->aliasBG->color = "white"; $this->consoleOutput->formats->realBG->bgcolor = "black"; $this->consoleOutput->formats->realBG->color = "white"; $this->consoleOutput->formats->realFG->color = "gray"; $this->consoleOutput->formats->realFG->bgcolor = "white"; $this->consoleOutput->formats->aliasFG->color = "black"; $this->consoleOutput->formats->aliasFG->bgcolor = "white"; $this->assertEquals( $this->consoleOutput->formatText( "I am black!", "aliasBG" ), $this->consoleOutput->formatText( "I am black!", "realBG" ), "Backgroundcolor not correctly aliased to ." ); $this->assertEquals( $this->consoleOutput->formatText( "I am gray!", "aliasFG" ), $this->consoleOutput->formatText( "I am gray!", "realFG" ), "Foregroundcolor not correctly aliased to ." ); } public function testOutputToTarget() { $outFile = $this->createTempDir( __FUNCTION__ ) . "/outfile"; touch( $outFile ); $this->consoleOutput->formats->targetFile->target = $outFile; $this->consoleOutput->formats->targetFile->color = "blue"; $this->consoleOutput->outputText( "Hello, I'm a cool text, written to a file!", "targetFile" ); $this->assertEquals( $this->consoleOutput->formatText( "Hello, I'm a cool text, written to a file!", "targetFile" ), file_get_contents( $outFile ) ); } public function testSetOptionsSuccess() { $optObj = new ezcConsoleOutputOptions(); $optObj->verbosityLevel = 10; $optObj->autobreak = 80; $optObj->useFormats = false; $output = new ezcConsoleOutput(); $output->setOptions( $optObj ); $this->assertEquals( $optObj, $output->options ); $output = new ezcConsoleOutput(); $output->setOptions( array( "verbosityLevel" => 10, "autobreak" => 80, "useFormats" => false, ) ); $this->assertEquals( $optObj, $output->options ); } public function testSetOptionsFailure() { $output = new ezcConsoleOutput(); try { $output->setOptions( true ); } catch ( ezcBaseValueException $e ) { return; } $this->fail( "Exception not thrown on invalid parameter to ezcConsoleOutput->setOptions()." ); } public function testGetOptions() { $output = new ezcConsoleOutput(); $this->assertEquals( new ezcConsoleOutputOptions(), $output->getOptions() ); } public function testGetAccessSuccess() { $output = new ezcConsoleOutput(); $this->assertEquals( new ezcConsoleOutputOptions(), $output->options ); $this->assertEquals( new ezcConsoleOutputFormats(), $output->formats ); } public function testGetAccessFailure() { $output = new ezcConsoleOutput(); try { echo $output->foo; } catch ( ezcBasePropertyNotFoundException $e ) { return; } $this->fail( "Exception not thrown on get access to invalid property foo." ); } public function testSetAccessSuccess() { $optObj = new ezcConsoleOutputOptions(); $forObj = new ezcConsoleOutputFormats(); $output = new ezcConsoleOutput(); $output->options = $optObj; $output->formats = $forObj; $this->assertSame( $optObj, $output->options ); $this->assertSame( $forObj, $output->formats ); } public function testSetAccessFailure() { $output = new ezcConsoleOutput(); $exceptionThrown = false; try { $output->options = 23; } catch ( ezcBaseValueException $e ) { $exceptionThrown = true; } $this->assertTrue( $exceptionThrown, "Exception not thrown on invalid value for property options." ); $exceptionThrown = false; try { $output->formats = 23; } catch ( ezcBaseValueException $e ) { $exceptionThrown = true; } $this->assertTrue( $exceptionThrown, "Exception not thrown on invalid value for property formats." ); $exceptionThrown = false; try { $output->foo = 23; } catch ( ezcBasePropertyNotFoundException $e ) { $exceptionThrown = true; } $this->assertTrue( $exceptionThrown, "Exception not thrown on set access to invalid property foo." ); } public function testIssetAccess() { $output = new ezcConsoleOutput(); $this->assertTrue( isset( $output->options ) ); $this->assertTrue( isset( $output->formats ) ); $this->assertFalse( isset( $output->foo ) ); } public function testOutputTextFailure() { $output = new ezcConsoleOutput(); $output->formats->invalid->target = "foo://bar"; $exceptionThrown = false; try { $output->outputText( "Foo Bar", "invalid" ); } catch ( ezcConsoleInvalidOutputTargetException $e ) { $exceptionThrown = true; } $this->assertTrue( $exceptionThrown, "Exception not thrown on output to invalid output target." ); } public function testToPos() { $output = new ezcConsoleOutput(); ob_start(); $output->outputText( "Test 123" ); $output->toPos( 6 ); $output->outputText( "456" ); $res = ob_get_clean(); $exp = 'Test 123456'; $this->assertEquals( $exp, $res, "Position code not generated correctly." ); } public function testRestorePosFailure() { $output = new ezcConsoleOutput(); try { $output->restorePos(); } catch ( ezcConsoleNoPositionStoredException $e ) { return; } $this->fail( "Exception not thrown on restore position without stored position." ); } } ?>