Skip to content

Commit

Permalink
Add GraveyardInterface and BufferedGraveyard
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Dec 25, 2018
1 parent bb1aace commit f25c8b5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/BufferedGraveyard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Scheb\Tombstone;

class BufferedGraveyard implements GraveyardInterface
{
/**
* @var GraveyardInterface
*/
private $graveyard;

/**
* @var array
*/
private $tombstoneCalls = [];

public function __construct(GraveyardInterface $graveyard)
{
$this->graveyard = $graveyard;
}

public function tombstone(string $date, ?string $author, ?string $label, array $trace): void
{
$this->tombstoneCalls[] = func_get_args();
}

public function flush(): void
{
foreach ($this->tombstoneCalls as $args) {
$this->graveyard->tombstone(... $args);
}
$this->graveyard->flush();
}
}
2 changes: 1 addition & 1 deletion src/Graveyard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Scheb\Tombstone\Tracing\PathNormalizer;
use Scheb\Tombstone\Tracing\TraceProvider;

class Graveyard
class Graveyard implements GraveyardInterface
{
/**
* @var HandlerInterface[]
Expand Down
10 changes: 10 additions & 0 deletions src/GraveyardInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Scheb\Tombstone;

interface GraveyardInterface
{
public function tombstone(string $date, ?string $author, ?string $label, array $trace): void;

public function flush(): void;
}
6 changes: 3 additions & 3 deletions src/GraveyardProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
class GraveyardProvider
{
/**
* @var Graveyard
* @var GraveyardInterface
*/
private static $graveyard;

public static function getGraveyard(): Graveyard
public static function getGraveyard(): GraveyardInterface
{
if (null === self::$graveyard) {
self::$graveyard = new Graveyard();
Expand All @@ -18,7 +18,7 @@ public static function getGraveyard(): Graveyard
return self::$graveyard;
}

public static function setGraveyard(Graveyard $graveyard): void
public static function setGraveyard(GraveyardInterface $graveyard): void
{
self::$graveyard = $graveyard;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/BufferedGraveyardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Scheb\Tombstone\Test;

use Scheb\Tombstone\BufferedGraveyard;
use Scheb\Tombstone\Graveyard;
use Scheb\Tombstone\GraveyardInterface;

class BufferedGraveyardTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $innerGraveyard;

/**
* @var Graveyard
*/
private $graveyard;

protected function setUp()
{
$this->innerGraveyard = $this->createMock(GraveyardInterface::class);
$this->graveyard = new BufferedGraveyard($this->innerGraveyard);
}

/**
* @test
*/
public function tombstone_tombstoneInvoked_notAddToInnerGraveyard(): void
{
$this->innerGraveyard
->expects($this->never())
->method($this->anything());

$this->graveyard->tombstone('2018-01-01', 'author1', 'label1', ['trace1']);
}

/**
* @test
*/
public function flush_tombstonesBuffered_addTombstonesAndFlush(): void
{
$this->graveyard->tombstone('2018-01-01', 'author1', 'label1', ['trace1']);
$this->graveyard->tombstone('2018-02-01', 'author2', 'label2', ['trace2']);

$this->innerGraveyard
->expects($this->exactly(2))
->method('tombstone')
->withConsecutive(
['2018-01-01', 'author1', 'label1', ['trace1']],
['2018-02-01', 'author2', 'label2', ['trace2']]
);

$this->innerGraveyard
->expects($this->once())
->method('flush');

$this->graveyard->flush();

}
}
3 changes: 2 additions & 1 deletion tests/GraveyardProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Scheb\Tombstone\Test;

use Scheb\Tombstone\Graveyard;
use Scheb\Tombstone\GraveyardInterface;
use Scheb\Tombstone\GraveyardProvider;

class GraveyardProviderTest extends TestCase
Expand All @@ -21,7 +22,7 @@ public function getGraveyard_notSet_returnDefaultGraveyard(): void
*/
public function setGraveyard_newGraveyard_exchangeGraveyard(): void
{
$graveyard = $this->createMock(Graveyard::class);
$graveyard = $this->createMock(GraveyardInterface::class);
GraveyardProvider::setGraveyard($graveyard);
$returnValue = GraveyardProvider::getGraveyard();
$this->assertSame($graveyard, $returnValue);
Expand Down

0 comments on commit f25c8b5

Please sign in to comment.