Skip to content

Commit

Permalink
reset stream counters on close it for fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Feb 8, 2018
1 parent 97fce8d commit d1c21e6
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Stream/LoggerStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function open()
public function close()
{
// do nothing
$this->counter = 0;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Stream/MultiStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function close()
foreach ($this->streams as $stream) {
$stream->close();
}
$this->counter = 0;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Stream/OutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

class OutputStream implements Stream
{
const LINKS_LIMIT = 50000;

const BYTE_LIMIT = 52428800; // 50 Mb

/**
* @var SitemapRender
*/
Expand Down Expand Up @@ -68,6 +64,8 @@ public function close()
{
$this->state->close();
$this->send($this->end_string);
$this->counter = 0;
$this->used_bytes = 0;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Stream/RenderBzip2FileStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

class RenderBzip2FileStream implements FileStream
{
const LINKS_LIMIT = 50000;

const BYTE_LIMIT = 52428800; // 50 Mb

/**
* @var SitemapRender
*/
Expand Down Expand Up @@ -91,6 +87,7 @@ public function close()
$this->state->close();
$this->write($this->end_string);
bzclose($this->handle);
$this->counter = 0;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Stream/RenderFileStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

class RenderFileStream implements FileStream
{
const LINKS_LIMIT = 50000;

const BYTE_LIMIT = 52428800; // 50 Mb

/**
* @var SitemapRender
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Stream/RenderGzipFileStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

class RenderGzipFileStream implements FileStream
{
const LINKS_LIMIT = 50000;

const BYTE_LIMIT = 52428800; // 50 Mb

/**
* @var SitemapRender
*/
Expand Down Expand Up @@ -104,6 +100,7 @@ public function close()
$this->state->close();
$this->write($this->end_string);
gzclose($this->handle);
$this->counter = 0;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Stream/RenderIndexFileStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function close()

file_put_contents($this->filename, $this->buffer.$this->render->end());
$this->buffer = '';
$this->counter = 0;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Stream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

interface Stream extends \Countable
{
const LINKS_LIMIT = 50000;

const BYTE_LIMIT = 52428800; // 50 Mb

public function open();

public function close();
Expand Down
13 changes: 11 additions & 2 deletions tests/Unit/Stream/LoggerStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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));
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Stream/MultiStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Stream/OutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Stream/RenderBzip2FileStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Stream/RenderFileStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Stream/RenderGzipFileStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Stream/RenderIndexFileStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d1c21e6

Please sign in to comment.