1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
<?php
/**
* ezcConsoleOutputTest class.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package ConsoleTools
* @subpackage Tests
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* Test suite for ezcConsoleStatusbar class.
*
* @package ConsoleTools
* @subpackage Tests
*/
class ezcConsoleStatusbarTest extends ezcTestCase
{
private $stati = array(
true,
false,
true,
true,
false,
true,
true,
true,
false,
false,
true,
true,
false,
true,
true,
false,
true,
false,
true,
true,
false,
false,
false,
true,
false,
);
private $errorCaught = false;
public static function suite()
{
return new PHPUnit_Framework_TestSuite( "ezcConsoleStatusbarTest" );
}
public function testStatusbar1()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
ob_start();
foreach ( $this->stati as $statusVal )
{
$status->add($statusVal);
}
$res = ob_get_contents();
ob_end_clean();
$this->assertEquals(
file_get_contents( dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . 'testStatusbar1.dat' ),
$res,
"Formated statusbar not generated correctly."
);
// To prepare test files use this:
// file_put_contents( dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . 'testStatusbar1.dat', $res );
}
public function testStatusbar2()
{
$out = new ezcConsoleOutput();
$out->options->useFormats = false;
$status = new ezcConsoleStatusbar( $out );
ob_start();
foreach ( $this->stati as $statusVal )
{
$status->add($statusVal);
}
$res = ob_get_contents();
ob_end_clean();
$this->assertEquals(
file_get_contents( dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . 'testStatusbar2.dat' ),
$res,
"Unformated statusbar not generated correctly."
);
// To prepare test files use this:
// file_put_contents( dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . 'testStatusbar2.dat', $res );
}
public function testGetAccessSuccess()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$this->assertEquals( new ezcConsoleStatusbarOptions(), $status->options );
$this->assertEquals( "+", $status->successChar );
$this->assertEquals( "-", $status->failureChar );
}
public function testGetAccessFailure()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
try
{
echo $status->foo;
}
catch ( ezcBasePropertyNotFoundException $e )
{
return;
}
$this->fail( "ezcBasePropertyNotFoundException not thrown on get access to invalid property ezcConsoleStatusbar->foo." );
}
public function testSetAccessSuccess()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$refOpt = new ezcConsoleStatusbarOptions();
$refOpt->successChar = "*";
$refOpt->failureChar = "#";
$status->successChar = "*";
$status->failureChar = "#";
$this->assertEquals( $refOpt, $status->options, "Options not set correctly through direct set access on ezcConsoleStatusbar." );
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$status->options = $refOpt;
$this->assertSame( $refOpt, $status->options, "Property ezcConsoleStatusbar->options not set correctly." );
}
public function testSetAccessFailure()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$exceptionThrown = false;
try
{
$status->successChar = 23;
}
catch ( ezcBaseValueException $e )
{
$exceptionThrown = true;
}
$this->assertTrue( $exceptionThrown, "ezcBaseValueException not thrown on invalid value for property ezcConsoleStatusbar->successChar." );
$exceptionThrown = false;
try
{
$status->failureChar = 23;
}
catch ( ezcBaseValueException $e )
{
$exceptionThrown = true;
}
$this->assertTrue( $exceptionThrown, "ezcBaseValueException not thrown on invalid value for property ezcConsoleStatusbar->failureChar." );
$exceptionThrown = false;
try
{
$status->options = 23;
}
catch ( ezcBaseValueException $e )
{
$exceptionThrown = true;
}
$this->assertTrue( $exceptionThrown, "ezcBaseValueException not thrown on invalid value for property ezcConsoleStatusbar->options." );
$exceptionThrown = false;
try
{
$status->foo = 23;
}
catch ( ezcBasePropertyNotFoundException $e )
{
$exceptionThrown = true;
}
$this->assertTrue( $exceptionThrown, "ezcBasePropertyNotFoundException not thrown on set access of invalid property ezcConsoleStatusbar->foo." );
}
public function testIssetAccess()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$this->assertTrue( isset( $status->successChar ) );
$this->assertTrue( isset( $status->failureChar ) );
$this->assertTrue( isset( $status->options ) );
$this->assertFalse( isset( $status->foo ) );
}
public function testSetOptionsSuccess()
{
$refOpt = new ezcConsoleStatusbarOptions();
$refOpt->successChar = "*";
$refOpt->failureChar = "#";
$optArr = array(
"successChar" => "*",
"failureChar" => "#",
);
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$status->setOptions( $optArr );
$this->assertEquals( $refOpt, $status->options, "Options not correctly set from array." );
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$status->setOptions( $refOpt );
$this->assertSame( $refOpt, $status->options, "Options not correctly set from array." );
}
public function testSetOptionsFailure()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
try
{
$status->setOptions( 23 );
}
catch ( ezcBaseValueException $e )
{
return;
}
$this->fail( "ezcBaseValueException not thrown on invalid parameter to ezcConsoleStatusbar->setOptions()." );
}
public function testGetOptions()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$this->assertEquals( new ezcConsoleStatusbarOptions(), $status->getOptions() );
}
public function testReset()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
ob_start();
foreach ( $this->stati as $statusVal )
{
$status->add($statusVal);
}
ob_end_clean();
$counter = $this->readAttribute( $status, "counter" );
$this->assertEquals( 14, $counter[true], "Success values not counted correctly." );
$this->assertEquals( 11, $counter[false], "Failure values not counted correctly." );
$status->reset();
$counter = $this->readAttribute( $status, "counter" );
$this->assertEquals( 0, $counter[true], "Success values not reset correctly." );
$this->assertEquals( 0, $counter[false], "Failure values not reset correctly." );
}
public function testGetSuccessCount()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$this->assertEquals( 0, $status->getSuccessCount() );
ob_start();
foreach ( $this->stati as $statusVal )
{
$status->add($statusVal);
}
ob_end_clean();
$this->assertEquals( 14, $status->getSuccessCount() );
}
public function testGetFailureCount()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
$this->assertEquals( 0, $status->getFailureCount() );
ob_start();
foreach ( $this->stati as $statusVal )
{
$status->add($statusVal);
}
ob_end_clean();
$this->assertEquals( 11, $status->getFailureCount() );
}
public function testAddIncorrectStatus()
{
$out = new ezcConsoleOutput();
$status = new ezcConsoleStatusbar( $out );
set_error_handler( array( $this, "catchWarning" ), E_USER_WARNING );
ob_start();
$status->add( "foo" );
ob_end_clean();
restore_error_handler();
$this->assertTrue( $this->errorCaught, "Warning not caught on invalid status 'foo'." );
}
public function catchWarning( $errno, $errstr, $errfile, $errline, $errcontext )
{
$this->assertEquals( "Unknown status 'foo'.", $errstr );
$this->assertEquals( realpath( "/usr/share/php/ezc/ConsoleTools/statusbar.php" ), $errfile );
$this->errorCaught = true;
}
}
?>
|