Skip to content

Commit

Permalink
rename interfaces and mark input observer as untestable
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Aug 26, 2018
1 parent 8ba2365 commit 652dc41
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function __construct($resource)
$this->resource = $resource;
}

/**
* @return resource
*/
public function getResource()
{
return $this->resource;
}

/**
* @param $resource
* @return bool|void
Expand Down
38 changes: 24 additions & 14 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Hugga;

use Hugga\Input\ReadlineHandler;
use Hugga\Input\ResourceHandler as InputHandler;
use Hugga\Output\ResourceHandler as OutputHandler;
use Hugga\Input\FileHandler as InputHandler;
use Hugga\Output\FileHandler as OutputHandler;
use Hugga\Output\TtyHandler;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -38,13 +38,13 @@ class Console
/** @var bool */
protected $logMessages = false;

/** @var OutputInterface */
/** @var OutputHandlerInterface */
protected $stdout;

/** @var InputInterface */
/** @var InputHandlerInterface */
protected $stdin;

/** @var OutputInterface */
/** @var OutputHandlerInterface */
protected $stderr;

/** @var bool */
Expand Down Expand Up @@ -219,12 +219,12 @@ public function disableAnsi(bool $disabled = true)
/**
* Set the resource for stdout
*
* @param resource|OutputInterface $stdout
* @param resource|OutputHandlerInterface $stdout
* @return $this
*/
public function setStdout($stdout)
{
if ($stdout instanceof OutputInterface) {
if ($stdout instanceof OutputHandlerInterface) {
$this->stdout = $stdout;
return $this;
}
Expand All @@ -235,20 +235,20 @@ public function setStdout($stdout)
return $this;
}

public function getStdout(): OutputInterface
public function getStdout(): OutputHandlerInterface
{
return $this->stdout;
}

/**
* Set the resource for stdin
*
* @param resource|InputInterface $stdin
* @param resource|InputHandlerInterface $stdin
* @return $this
*/
public function setStdin($stdin)
{
if ($stdin instanceof InputInterface) {
if ($stdin instanceof InputHandlerInterface) {
$this->stdin = $stdin;
return $this;
}
Expand All @@ -259,20 +259,30 @@ public function setStdin($stdin)
return $this;
}

public function getStdin(): InputInterface
public function getStdin(): InputHandlerInterface
{
return $this->stdin;
}

public function getInputObserver()
{
$resource = $this->stdin->getResource();
if (!InputObserver::isCompatible($resource)) {
throw new \LogicException('Stdin resource is not compatible for input observer');
}
// @codeCoverageIgnoreStart
return new InputObserver($resource);
}

/**
* Set the resource for stderr
*
* @param resource|OutputInterface $stderr
* @param resource|OutputHandlerInterface $stderr
* @return $this
*/
public function setStderr($stderr)
{
if ($stderr instanceof OutputInterface) {
if ($stderr instanceof OutputHandlerInterface) {
$this->stderr = $stderr;
return $this;
}
Expand All @@ -283,7 +293,7 @@ public function setStderr($stderr)
return $this;
}

public function getStderr(): OutputInterface
public function getStderr(): OutputHandlerInterface
{
return $this->stderr;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Input/AbstractInputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Hugga\Input;

use Hugga\AbstractHandler;
use Hugga\InputInterface;
use Hugga\InputHandlerInterface;

abstract class AbstractInputHandler extends AbstractHandler implements InputInterface
abstract class AbstractInputHandler extends AbstractHandler implements InputHandlerInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hugga\Input;

class ResourceHandler extends AbstractInputHandler
class FileHandler extends AbstractInputHandler
{
const BUFFER_SIZE = 4096;

Expand Down
4 changes: 3 additions & 1 deletion src/InputInterface.php → src/InputHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Hugga;

interface InputInterface
interface InputHandlerInterface
{
public function readLine(string $prompt = null): string;

public function read(int $count = 1, string $prompt = null): string;

public function readUntil(string $sequence, string $prompt = null): string;

public function getResource();
}
9 changes: 8 additions & 1 deletion src/TTyObserver.php → src/InputObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace Hugga;

class TTyObserver
/**
* Class InputObserver
*
* @package Hugga
* @author Thomas Flori <thflori@gmail.com>
* @codeCoverageIgnore not testable
*/
class InputObserver
{
protected $stdin;

Expand Down
4 changes: 2 additions & 2 deletions src/Output/AbstractOutputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Hugga\Output;

use Hugga\AbstractHandler;
use Hugga\OutputInterface;
use Hugga\OutputHandlerInterface;

abstract class AbstractOutputHandler extends AbstractHandler implements OutputInterface
abstract class AbstractOutputHandler extends AbstractHandler implements OutputHandlerInterface
{
public static function isCompatible($resource)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hugga\Output;

class ResourceHandler extends AbstractOutputHandler
class FileHandler extends AbstractOutputHandler
{
const BUFFER_SIZE = 4096;

Expand Down
4 changes: 3 additions & 1 deletion src/OutputInterface.php → src/OutputHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Hugga;

interface OutputInterface
interface OutputHandlerInterface
{
public function write(string $str);

public function delete(int $count);

public function deleteLine();

public function getResource();
}
12 changes: 10 additions & 2 deletions tests/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Hugga\Test;

use Hugga\Console;
use Hugga\Input\ResourceHandler as InputHandler;
use Hugga\Output\ResourceHandler as OutputHandler;
use Hugga\Input\FileHandler as InputHandler;
use Hugga\Output\FileHandler as OutputHandler;
use Mockery as m;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -233,4 +233,12 @@ public function acceptsOutputInterfaceForStderr()

self::assertSame($outputHandler, $this->console->getStderr());
}

/** @test */
public function throwsWhenStdIsNotCompatible()
{
self::expectException(\LogicException::class);

$this->console->getInputObserver();
}
}
4 changes: 2 additions & 2 deletions tests/Input/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hugga\Test\Input;

use Hugga\Input\ResourceHandler;
use Hugga\Input\FileHandler;
use Hugga\Test\TestCase;

class CommonTest extends TestCase
Expand All @@ -13,6 +13,6 @@ public function throwsWithoutResource()
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('has to be of the type resource');

new ResourceHandler('php://memory');
new FileHandler('php://memory');
}
}
2 changes: 1 addition & 1 deletion tests/Input/ResourceHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hugga\Test\Input;

use Hugga\Input\ResourceHandler as InputHandler;
use Hugga\Input\FileHandler as InputHandler;
use Hugga\Test\TestCase;

class ResourceHandlerTest extends TestCase
Expand Down
16 changes: 8 additions & 8 deletions tests/Output/ResourceHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

namespace Hugga\Test\Output;

use Hugga\Output\ResourceHandler;
use Hugga\Output\FileHandler;
use Hugga\Test\TestCase;

class ResourceHandlerTest extends TestCase
{
/** @test */
public function doesNotRequireATty()
{
self::assertTrue(ResourceHandler::isCompatible($this->stdout));
self::assertTrue(FileHandler::isCompatible($this->stdout));
}

/** @test */
public function requiresWriteableResource()
{
self::assertFalse(ResourceHandler::isCompatible(fopen('php://memory', 'r')));
self::assertFalse(FileHandler::isCompatible(fopen('php://memory', 'r')));
}

/** @test */
public function writesToResource()
{
$handler = new ResourceHandler($this->stdout);
$handler = new FileHandler($this->stdout);

$handler->write('any string');

Expand All @@ -33,7 +33,7 @@ public function writesToResource()
/** @test */
public function deletesTheLastBytes()
{
$handler = new ResourceHandler($this->stdout);
$handler = new FileHandler($this->stdout);
$handler->write('Calculating something ... ⚬');

$handler->delete(1);
Expand All @@ -45,7 +45,7 @@ public function deletesTheLastBytes()
/** @test */
public function deletesTheCurrentLine()
{
$handler = new ResourceHandler($this->stdout);
$handler = new FileHandler($this->stdout);
$handler->write('Text before the last line' . PHP_EOL . 'text to be removed...');

$handler->deleteLine();
Expand All @@ -57,7 +57,7 @@ public function deletesTheCurrentLine()
/** @test */
public function deletesInChunks()
{
$handler = new ResourceHandler($this->stdout);
$handler = new FileHandler($this->stdout);
$handler->write('Text before the last line' . PHP_EOL . 'foo bar');

$handler->deleteLine(3); // buffer size 3 -> 3 steps
Expand All @@ -69,7 +69,7 @@ public function deletesInChunks()
/** @test */
public function deletesEverythingWithoutLinebreak()
{
$handler = new ResourceHandler($this->stdout);
$handler = new FileHandler($this->stdout);
$handler->write('the first line of the file without line breaks');

$handler->deleteLine();
Expand Down

0 comments on commit 652dc41

Please sign in to comment.