diff --git a/src/TarStreamer.php b/src/TarStreamer.php index 4c73f65..7d167cb 100644 --- a/src/TarStreamer.php +++ b/src/TarStreamer.php @@ -38,24 +38,16 @@ public function __construct($options = []){ * * @param string $archiveName Filename of archive to be created (optional, default 'archive.tar') * @param string $contentType Content mime type to be set (optional, default 'application/x-tar') + * @throws \Exception */ public function sendHeaders($archiveName = 'archive.tar', $contentType = 'application/x-tar'){ $encodedArchiveName = rawurlencode($archiveName); if (headers_sent($headerFile, $headerLine)){ - die( - "

Error: Unable to send file " . - "$encodedArchiveName. HTML Headers have already been sent from " . - "$headerFile in line $headerLine" . - "

" - ); + throw new \Exception("Unable to send file $encodedArchiveName. HTML Headers have already been sent from $headerFile in line $headerLine"); } $buffer = ob_get_contents(); if (!empty($buffer)){ - die( - "\n

Error: Unable to send file " . - "$encodedArchiveName. Output buffer " . - "already contains text (typically warnings or errors).

" - ); + throw new \Exception("Unable to send file $encodedArchiveName. Output buffer already contains text (typically warnings or errors)."); } $headers = [ diff --git a/tests/Streamer.php b/tests/Streamer.php index 3c3cbd0..caea3aa 100644 --- a/tests/Streamer.php +++ b/tests/Streamer.php @@ -14,8 +14,9 @@ class Streamer extends PHPUnit_Framework_TestCase /** @var TarStreamer */ private $streamer; - public function setUp() { - $this->archive = tempnam('/tmp' , 'tar'); + public function setUp() + { + $this->archive = tempnam('/tmp', 'tar'); $this->streamer = new TarStreamer( ['outstream' => fopen($this->archive, 'w')] ); @@ -26,25 +27,83 @@ public function setUp() { * @param $fileName * @param $data */ - public function testSimpleFile($fileName, $data) { - $dataStream = fopen('data://text/plain,'.$data, 'r'); - $this->streamer->addFileFromStream($dataStream, $fileName, 10); + public function testSimpleFile($fileName, $data) + { + $dataStream = fopen('data://text/plain,' . $data, 'r'); + $ret = $this->streamer->addFileFromStream($dataStream, $fileName, 10); + $this->assertTrue($ret); + $this->streamer->finalize(); - $this->assertTar($fileName, $data); + $this->assertFileInTar($fileName); } - public function providesNameAndData() { + /** + * @dataProvider providesNameAndData + * @param $fileName + * @param $data + */ + public function testAddingNoResource($fileName, $data) + { + $ret = $this->streamer->addFileFromStream($data, $fileName, 10); + $this->assertFalse($ret); + + $this->streamer->finalize(); + + $this->assertFileNotInTar($fileName); + } + + public function testDir() + { + $folderName = 'foo-folder'; + $this->streamer->addEmptyDir($folderName); + + $this->streamer->finalize(); + + $this->assertFolderInTar($folderName); + } + + public function providesNameAndData() + { return [ ['foo.bar', '1234567890'], // ['foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234.txt', 'abcdefgh'] ]; } - private function assertTar($file, $data) + private function assertFileInTar($file) + { + $elem = $this->getElementFromTar($file); + $this->assertNotNull($elem); + $this->assertEquals('0', $elem['typeflag']); + } + + private function assertFileNotInTar($file) { $arc = new Archive_Tar($this->archive); $content = $arc->extractInString($file); - $this->assertEquals($data, $content); + $this->assertNull($content); + } + + private function assertFolderInTar($folderName) + { + $elem = $this->getElementFromTar($folderName . '/'); + $this->assertNotNull($elem); + $this->assertEquals('5', $elem['typeflag']); + } + + /** + * @param $folderName + * @param $list + * @return array + */ + private function getElementFromTar($folderName) + { + $arc = new Archive_Tar($this->archive); + $list = $arc->listContent(); + $elem = array_filter($list, function ($element) use ($folderName) { + return $element['filename'] == $folderName; + }); + return isset($elem[0]) ? $elem[0] : null; } }