Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Add tests for Console::writeLine(), fix typo in path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thinkscape committed Mar 16, 2013
1 parent 87ced58 commit 6d8649c
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 103 deletions.
103 changes: 0 additions & 103 deletions test/Adapater/AbstractAdapterTest.php

This file was deleted.

166 changes: 166 additions & 0 deletions test/Adapter/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link https://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Console\Adapater;

use ZendTest\Console\TestAssets\ConsoleAdapter;

/**
* @group Zend_Console
*/
class AbstractAdapterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ConsoleAdapter
*/
protected $adapter;

public function setUp()
{
$this->adapter = new ConsoleAdapter();
$this->adapter->stream = fopen('php://memory', 'w+');
}

public function tearDown()
{
fclose($this->adapter->stream);
}

public function testWriteChar()
{
ob_start();
$this->adapter->write('foo');
$this->assertEquals('foo', ob_get_clean());
}

public function testWriteText()
{
ob_start();
$this->adapter->writeText('foo');
$this->assertEquals('foo', ob_get_clean());
}

public function testWriteLine()
{
ob_start();
$this->adapter->writeLine('foo');
$this->assertEquals("foo" . PHP_EOL, ob_get_clean());

ob_start();
$this->adapter->writeLine("foo\nbar");
$this->assertEquals("foo bar" . PHP_EOL, ob_get_clean());

ob_start();
$this->adapter->writeLine("\rfoo\r");
$this->assertEquals("foo" . PHP_EOL, ob_get_clean());
}

/**
* @issue ZF2-4051
* @link https://github.com/zendframework/zf2/issues/4051
*/
public function testWriteLineOverflowAndWidthMatch()
{
// make sure console width is reported as 80
$this->adapter->setTestWidth(80);

ob_start();
$line = str_repeat('#',80);
$this->adapter->writeLine($line);
$this->assertEquals($line, ob_get_clean(), 'No newline because text matches console window width');

ob_start();
$line2 = $line . '#';
$this->adapter->writeLine($line2);
$this->assertEquals($line . '#' . PHP_EOL, ob_get_clean());

ob_start();
$line3 = $line . $line;
$this->adapter->writeLine($line3);
$this->assertEquals($line3, ob_get_clean(), 'No newline because text matches console window width');


// make sure console is reported as utf8 compatible
$this->adapter->setTestUtf8(true);

ob_start();
$line = str_repeat('ź',80);
$this->adapter->writeLine($line);
$this->assertEquals($line, ob_get_clean(), 'No newline because text matches console window width');

ob_start();
$line2 = $line . 'ź';
$this->adapter->writeLine($line2);
$this->assertEquals($line . 'ź' . PHP_EOL, ob_get_clean());

ob_start();
$line3 = $line . $line;
$this->adapter->writeLine($line3);
$this->assertEquals($line3, ob_get_clean(), 'No newline because text matches console window width');


// attempt to test utf8 string on a non-utf8 console (which should result in utf8 decoding)
$this->adapter->setTestUtf8(false);

ob_start();
$line = str_repeat('ź',80);
$this->adapter->writeLine($line);
$this->assertEquals($line, ob_get_clean(), 'No newline because text matches console window width');

ob_start();
$line2 = $line . 'ź';
$this->adapter->writeLine($line2);
$this->assertEquals($line . 'ź' . PHP_EOL, ob_get_clean());

ob_start();
$line3 = $line . $line;
$this->adapter->writeLine($line3);
$this->assertEquals($line3, ob_get_clean(), 'No newline because text matches console window width');
}

public function testReadLine()
{
fwrite($this->adapter->stream, 'baz');

$line = $this->adapter->readLine();
$this->assertEquals($line, 'baz');
}

public function testReadLineWithLimit()
{
fwrite($this->adapter->stream, 'baz, bar, foo');

$line = $this->adapter->readLine(6);
$this->assertEquals($line, 'baz, b');
}

public function testReadChar()
{
fwrite($this->adapter->stream, 'bar');

$char = $this->adapter->readChar();
$this->assertEquals($char, 'b');
}

public function testReadCharWithMask()
{
fwrite($this->adapter->stream, 'bar');

$char = $this->adapter->readChar('ar');
$this->assertEquals($char, 'a');
}

public function testReadCharWithMaskInsensitiveCase()
{
fwrite($this->adapter->stream, 'bAr');

$char = $this->adapter->readChar('ar');
$this->assertEquals($char, 'r');
}
}
35 changes: 35 additions & 0 deletions test/TestAssets/ConsoleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ConsoleAdapter extends AbstractAdapter

public $autoRewind = true;

public $testWidth = 80;

public $testIsUtf8 = true;

/**
* Read a single line from the console input
*
Expand Down Expand Up @@ -51,4 +55,35 @@ public function readChar($mask = null)
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
return $char;
}

/**
* Force reported width for testing purposes.
*
* @param int $width
* @return int
*/
public function setTestWidth($width)
{
$this->testWidth = $width;
}

/**
* Force reported utf8 capability.
*
* @param bool $isUtf8
*/
public function setTestUtf8($isUtf8)
{
$this->testIsUtf8 = $isUtf8;
}

public function isUtf8()
{
return $this->testIsUtf8;
}

public function getWidth()
{
return $this->testWidth;
}
}

0 comments on commit 6d8649c

Please sign in to comment.