Skip to content

Commit

Permalink
Use decorators in writer, close #263.
Browse files Browse the repository at this point in the history
  • Loading branch information
mageekguy committed Sep 23, 2013
1 parent 4c9420e commit e88ce0e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 55 deletions.
32 changes: 31 additions & 1 deletion classes/writer.php
Expand Up @@ -2,9 +2,14 @@

namespace mageekguy\atoum;

use
mageekguy\atoum\writer\decorator
;

abstract class writer
{
protected $adapter = null;
protected $decorators = array();

public function __construct(adapter $adapter = null)
{
Expand All @@ -28,6 +33,31 @@ public function reset()
return $this;
}

public abstract function write($string);
public function addDecorator(decorator $decorator)
{
$this->decorators[] = $decorator;

return $this;
}

public function getDecorators()
{
return $this->decorators;
}

public function write($string)
{
foreach ($this->decorators as $decorator)
{
$string = $decorator->decorate($string);
}

$this->doWrite($string);

return $this;
}

public abstract function clear();

protected abstract function doWrite($string);
}
25 changes: 13 additions & 12 deletions classes/writers/file.php
Expand Up @@ -29,18 +29,6 @@ public function __destruct()
$this->closeFile();
}

public function write($something)
{
if (strlen($something) != $this->openFile()->adapter->fwrite($this->resource, $something))
{
throw new exceptions\runtime('Unable to write in file \'' . $this->filename . '\'');
}

$this->adapter->fflush($this->resource);

return $this;
}

public function clear()
{
if ($this->openFile()->adapter->ftruncate($this->resource, 0) === false)
Expand Down Expand Up @@ -80,6 +68,19 @@ public function reset()
return parent::reset();
}

protected function doWrite($something)
{
if (strlen($something) != $this->openFile()->adapter->fwrite($this->resource, $something))
{
throw new exceptions\runtime('Unable to write in file \'' . $this->filename . '\'');
}

$this->adapter->fflush($this->resource);

return $this;
}


private function openFile()
{
if ($this->resource === null)
Expand Down
55 changes: 27 additions & 28 deletions classes/writers/http.php
Expand Up @@ -9,7 +9,6 @@
mageekguy\atoum\report\writers
;


class http extends atoum\writer implements writers\asynchronous
{
protected $url = null;
Expand All @@ -29,33 +28,6 @@ public function writeAsynchronousReport(reports\asynchronous $report)
return $this->write((string) $report);
}

public function write($string)
{
if ($this->url === null)
{
throw new exceptions\runtime('No URL set for HTTP writer');
}

$headers = array();

foreach ($this->headers as $name => $value)
{
$headers[] = sprintf('%s: %s', $name, $value);
}

$context = $this->adapter->stream_context_create(array(
'http' => array(
'method' => $this->method,
'header' => join("\r\n", $headers),
'content' => $this->parameter ? http_build_query(array($this->parameter => $string)) : $string
)
));

$this->adapter->file_get_contents($this->url, false, $context);

return $this;
}

public function clear()
{
return $this;
Expand Down Expand Up @@ -108,4 +80,31 @@ public function getUrl()
{
return $this->url;
}

protected function doWrite($string)
{
if ($this->url === null)
{
throw new exceptions\runtime('No URL set for HTTP writer');
}

$headers = array();

foreach ($this->headers as $name => $value)
{
$headers[] = sprintf('%s: %s', $name, $value);
}

$context = $this->adapter->stream_context_create(array(
'http' => array(
'method' => $this->method,
'header' => join("\r\n", $headers),
'content' => $this->parameter ? http_build_query(array($this->parameter => $string)) : $string
)
));

$this->adapter->file_get_contents($this->url, false, $context);

return $this;
}
}
14 changes: 7 additions & 7 deletions classes/writers/mail.php
Expand Up @@ -47,13 +47,6 @@ public function getLocale()
return $this->locale;
}

public function write($something)
{
$this->mailer->send($something);

return $this;
}

public function clear()
{
return $this;
Expand All @@ -77,4 +70,11 @@ public function writeAsynchronousReport(reports\asynchronous $report)

return $this->write((string) $report);
}

protected function doWrite($something)
{
$this->mailer->send($something);

return $this;
}
}
15 changes: 8 additions & 7 deletions classes/writers/std.php
Expand Up @@ -41,13 +41,6 @@ public function getCli()
return $this->cli;
}

public function write($something)
{
$this->init()->adapter->fwrite($this->resource, $something);

return $this;
}

public function clear()
{
return $this->write($this->cli->isTerminal() === false ? PHP_EOL : "\033[1K\r");
Expand All @@ -63,5 +56,13 @@ public function writeAsynchronousReport(reports\asynchronous $report)
return $this->write((string) $report);
}

protected function doWrite($something)
{
$this->init()->adapter->fwrite($this->resource, $something);

return $this;
}


protected abstract function init();
}
38 changes: 38 additions & 0 deletions tests/units/classes/writer.php
Expand Up @@ -22,9 +22,11 @@ public function test__construct()
->if($writer = new testedClass())
->then
->object($writer->getAdapter())->isEqualTo(new atoum\adapter())
->array($writer->getDecorators())->isEmpty()
->if($writer = new testedClass($adapter = new atoum\test\adapter()))
->then
->object($writer->getAdapter())->isIdenticalTo($adapter)
->array($writer->getDecorators())->isEmpty()
;
}

Expand All @@ -42,6 +44,42 @@ public function testSetAdapter()
;
}

public function testAddDecorator()
{
$this
->if($writer = new testedClass())
->then
->object($writer->addDecorator($decorator1 = new \mock\mageekguy\atoum\writer\decorator()))->isIdenticalTo($writer)
->array($writer->getDecorators())->isEqualTo(array($decorator1))
->object($writer->addDecorator($decorator1))->isIdenticalTo($writer)
->array($writer->getDecorators())->isEqualTo(array($decorator1, $decorator1))
->object($writer->addDecorator($decorator2 = new \mock\mageekguy\atoum\writer\decorator()))->isIdenticalTo($writer)
->array($writer->getDecorators())->isEqualTo(array($decorator1, $decorator1, $decorator2))
;
}

public function testWrite()
{
$this
->if($writer = new \mock\mageekguy\atoum\writer())
->then
->object($writer->write($message = uniqid()))->isIdenticalTo($writer)
->mock($writer)->call('doWrite')->withArguments($message)->once()
->if($writer->addDecorator($decorator1 = new \mock\mageekguy\atoum\writer\decorator()))
->and($this->calling($decorator1)->decorate = $decoratedMessage1 = uniqid())
->then
->object($writer->write($message = uniqid()))->isIdenticalTo($writer)
->mock($writer)->call('doWrite')->withArguments($decoratedMessage1)->once()
->if($writer->addDecorator($decorator2 = new \mock\mageekguy\atoum\writer\decorator()))
->and($this->calling($decorator2)->decorate = $decoratedMessage2 = uniqid())
->then
->object($writer->write($message = uniqid()))->isIdenticalTo($writer)
->mock($decorator1)->call('decorate')->withArguments($message)->once()
->mock($decorator2)->call('decorate')->withArguments($decoratedMessage1)->once()
->mock($writer)->call('doWrite')->withArguments($decoratedMessage2)->once()
;
}

public function testReset()
{
$this
Expand Down

0 comments on commit e88ce0e

Please sign in to comment.