diff --git a/src/BufferedGraveyard.php b/src/BufferedGraveyard.php new file mode 100644 index 0000000..0b744ac --- /dev/null +++ b/src/BufferedGraveyard.php @@ -0,0 +1,34 @@ +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(); + } +} diff --git a/src/Graveyard.php b/src/Graveyard.php index 63952a0..149617e 100644 --- a/src/Graveyard.php +++ b/src/Graveyard.php @@ -6,7 +6,7 @@ use Scheb\Tombstone\Tracing\PathNormalizer; use Scheb\Tombstone\Tracing\TraceProvider; -class Graveyard +class Graveyard implements GraveyardInterface { /** * @var HandlerInterface[] diff --git a/src/GraveyardInterface.php b/src/GraveyardInterface.php new file mode 100644 index 0000000..75c1745 --- /dev/null +++ b/src/GraveyardInterface.php @@ -0,0 +1,10 @@ +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(); + + } +} diff --git a/tests/GraveyardProviderTest.php b/tests/GraveyardProviderTest.php index 7608afa..758b3e7 100644 --- a/tests/GraveyardProviderTest.php +++ b/tests/GraveyardProviderTest.php @@ -3,6 +3,7 @@ namespace Scheb\Tombstone\Test; use Scheb\Tombstone\Graveyard; +use Scheb\Tombstone\GraveyardInterface; use Scheb\Tombstone\GraveyardProvider; class GraveyardProviderTest extends TestCase @@ -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);