From d1c21e63841c42742180224367e93d28501d7258 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 8 Feb 2018 12:28:17 +0300 Subject: [PATCH] reset stream counters on close it for fix #6 --- src/Stream/LoggerStream.php | 1 + src/Stream/MultiStream.php | 1 + src/Stream/OutputStream.php | 6 ++--- src/Stream/RenderBzip2FileStream.php | 5 +--- src/Stream/RenderFileStream.php | 6 ++--- src/Stream/RenderGzipFileStream.php | 5 +--- src/Stream/RenderIndexFileStream.php | 1 + src/Stream/Stream.php | 4 ++++ tests/Unit/Stream/LoggerStreamTest.php | 13 ++++++++-- tests/Unit/Stream/MultiStreamTest.php | 24 +++++++++++++++++++ tests/Unit/Stream/OutputStreamTest.php | 9 +++++++ .../Unit/Stream/RenderBzip2FileStreamTest.php | 9 +++++++ tests/Unit/Stream/RenderFileStreamTest.php | 9 +++++++ .../Unit/Stream/RenderGzipFileStreamTest.php | 9 +++++++ .../Unit/Stream/RenderIndexFileStreamTest.php | 9 +++++++ 15 files changed, 93 insertions(+), 18 deletions(-) diff --git a/src/Stream/LoggerStream.php b/src/Stream/LoggerStream.php index a117813..73468e7 100644 --- a/src/Stream/LoggerStream.php +++ b/src/Stream/LoggerStream.php @@ -40,6 +40,7 @@ public function open() public function close() { // do nothing + $this->counter = 0; } /** diff --git a/src/Stream/MultiStream.php b/src/Stream/MultiStream.php index c97ed08..42e0c89 100644 --- a/src/Stream/MultiStream.php +++ b/src/Stream/MultiStream.php @@ -55,6 +55,7 @@ public function close() foreach ($this->streams as $stream) { $stream->close(); } + $this->counter = 0; } /** diff --git a/src/Stream/OutputStream.php b/src/Stream/OutputStream.php index 76e03ac..bdf313f 100644 --- a/src/Stream/OutputStream.php +++ b/src/Stream/OutputStream.php @@ -18,10 +18,6 @@ class OutputStream implements Stream { - const LINKS_LIMIT = 50000; - - const BYTE_LIMIT = 52428800; // 50 Mb - /** * @var SitemapRender */ @@ -68,6 +64,8 @@ public function close() { $this->state->close(); $this->send($this->end_string); + $this->counter = 0; + $this->used_bytes = 0; } /** diff --git a/src/Stream/RenderBzip2FileStream.php b/src/Stream/RenderBzip2FileStream.php index 98ceee7..d13900e 100644 --- a/src/Stream/RenderBzip2FileStream.php +++ b/src/Stream/RenderBzip2FileStream.php @@ -18,10 +18,6 @@ class RenderBzip2FileStream implements FileStream { - const LINKS_LIMIT = 50000; - - const BYTE_LIMIT = 52428800; // 50 Mb - /** * @var SitemapRender */ @@ -91,6 +87,7 @@ public function close() $this->state->close(); $this->write($this->end_string); bzclose($this->handle); + $this->counter = 0; } /** diff --git a/src/Stream/RenderFileStream.php b/src/Stream/RenderFileStream.php index 351b891..0714c33 100644 --- a/src/Stream/RenderFileStream.php +++ b/src/Stream/RenderFileStream.php @@ -19,10 +19,6 @@ class RenderFileStream implements FileStream { - const LINKS_LIMIT = 50000; - - const BYTE_LIMIT = 52428800; // 50 Mb - /** * @var SitemapRender */ @@ -97,6 +93,8 @@ public function close() $this->state->close(); $this->write($this->end_string); fclose($this->handle); + $this->counter = 0; + $this->used_bytes = 0; } /** diff --git a/src/Stream/RenderGzipFileStream.php b/src/Stream/RenderGzipFileStream.php index b168e07..878df3e 100644 --- a/src/Stream/RenderGzipFileStream.php +++ b/src/Stream/RenderGzipFileStream.php @@ -19,10 +19,6 @@ class RenderGzipFileStream implements FileStream { - const LINKS_LIMIT = 50000; - - const BYTE_LIMIT = 52428800; // 50 Mb - /** * @var SitemapRender */ @@ -104,6 +100,7 @@ public function close() $this->state->close(); $this->write($this->end_string); gzclose($this->handle); + $this->counter = 0; } /** diff --git a/src/Stream/RenderIndexFileStream.php b/src/Stream/RenderIndexFileStream.php index 3f28992..5f3f46d 100644 --- a/src/Stream/RenderIndexFileStream.php +++ b/src/Stream/RenderIndexFileStream.php @@ -94,6 +94,7 @@ public function close() file_put_contents($this->filename, $this->buffer.$this->render->end()); $this->buffer = ''; + $this->counter = 0; } /** diff --git a/src/Stream/Stream.php b/src/Stream/Stream.php index 8f75f8c..ae1875a 100644 --- a/src/Stream/Stream.php +++ b/src/Stream/Stream.php @@ -13,6 +13,10 @@ interface Stream extends \Countable { + const LINKS_LIMIT = 50000; + + const BYTE_LIMIT = 52428800; // 50 Mb + public function open(); public function close(); diff --git a/tests/Unit/Stream/LoggerStreamTest.php b/tests/Unit/Stream/LoggerStreamTest.php index 89650e2..6d9b46d 100644 --- a/tests/Unit/Stream/LoggerStreamTest.php +++ b/tests/Unit/Stream/LoggerStreamTest.php @@ -45,7 +45,7 @@ public function testPush() $this->logger ->expects($this->at(0)) ->method('debug') - ->with(sprintf('URL "%s" is added to sitemap', $url1->getLoc()), [ + ->with(sprintf('URL "%s" was added to sitemap.xml', $url1->getLoc()), [ 'changefreq' => $url1->getChangeFreq(), 'lastmod' => $url1->getLastMod(), 'priority' => $url1->getPriority(), @@ -54,7 +54,7 @@ public function testPush() $this->logger ->expects($this->at(1)) ->method('debug') - ->with(sprintf('URL "%s" is added to sitemap', $url2->getLoc()), [ + ->with(sprintf('URL "%s" was added to sitemap.xml', $url2->getLoc()), [ 'changefreq' => $url2->getChangeFreq(), 'lastmod' => $url2->getLastMod(), 'priority' => $url2->getPriority(), @@ -66,4 +66,13 @@ public function testPush() $this->assertEquals(2, count($this->stream)); } + + public function testReset() + { + $this->stream->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->stream->close(); + $this->assertEquals(0, count($this->stream)); + } } diff --git a/tests/Unit/Stream/MultiStreamTest.php b/tests/Unit/Stream/MultiStreamTest.php index 3e99dc0..46923bf 100644 --- a/tests/Unit/Stream/MultiStreamTest.php +++ b/tests/Unit/Stream/MultiStreamTest.php @@ -107,6 +107,30 @@ public function testPush(array $substreams) $this->assertEquals(count($urls), count($stream)); } + /** + * @dataProvider streams + * + * @param \PHPUnit_Framework_MockObject_MockObject[]|Stream[] $substreams + */ + public function testReset(array $substreams) + { + $url = new Url('/foo'); + + $stream = $this->getMultiStream($substreams); + foreach ($substreams as $substream) { + $substream + ->expects($this->at(0)) + ->method('push') + ->with($url) + ; + } + $stream->push($url); + + $this->assertEquals(1, count($stream)); + $stream->close(); + $this->assertEquals(0, count($stream)); + } + /** * @param Stream[] $substreams * diff --git a/tests/Unit/Stream/OutputStreamTest.php b/tests/Unit/Stream/OutputStreamTest.php index 42baeb2..32a12b8 100644 --- a/tests/Unit/Stream/OutputStreamTest.php +++ b/tests/Unit/Stream/OutputStreamTest.php @@ -201,6 +201,15 @@ public function testOverflowSize() } } + public function testReset() + { + $this->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->close(); + $this->assertEquals(0, count($this->stream)); + } + private function open() { $this->render diff --git a/tests/Unit/Stream/RenderBzip2FileStreamTest.php b/tests/Unit/Stream/RenderBzip2FileStreamTest.php index 3917932..93ec891 100644 --- a/tests/Unit/Stream/RenderBzip2FileStreamTest.php +++ b/tests/Unit/Stream/RenderBzip2FileStreamTest.php @@ -199,6 +199,15 @@ public function testNotWritable() } } + public function testReset() + { + $this->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->close(); + $this->assertEquals(0, count($this->stream)); + } + private function open() { $this->render diff --git a/tests/Unit/Stream/RenderFileStreamTest.php b/tests/Unit/Stream/RenderFileStreamTest.php index 6f4970a..66e1375 100644 --- a/tests/Unit/Stream/RenderFileStreamTest.php +++ b/tests/Unit/Stream/RenderFileStreamTest.php @@ -233,6 +233,15 @@ public function testNotWritable() } } + public function testReset() + { + $this->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->close(); + $this->assertEquals(0, count($this->stream)); + } + private function open() { $this->render diff --git a/tests/Unit/Stream/RenderGzipFileStreamTest.php b/tests/Unit/Stream/RenderGzipFileStreamTest.php index ad2c033..a7ecae5 100644 --- a/tests/Unit/Stream/RenderGzipFileStreamTest.php +++ b/tests/Unit/Stream/RenderGzipFileStreamTest.php @@ -223,6 +223,15 @@ public function testNotWritable() } } + public function testReset() + { + $this->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->close(); + $this->assertEquals(0, count($this->stream)); + } + private function open() { $this->render diff --git a/tests/Unit/Stream/RenderIndexFileStreamTest.php b/tests/Unit/Stream/RenderIndexFileStreamTest.php index 2b249f1..62f0922 100644 --- a/tests/Unit/Stream/RenderIndexFileStreamTest.php +++ b/tests/Unit/Stream/RenderIndexFileStreamTest.php @@ -187,6 +187,15 @@ public function testPush() $this->close(); } + public function testReset() + { + $this->open(); + $this->stream->push(new Url('/')); + $this->assertEquals(1, count($this->stream)); + $this->close(); + $this->assertEquals(0, count($this->stream)); + } + private function open() { ++$this->index;