Skip to content

Commit

Permalink
Merge pull request #3 from owncloud/dont-die
Browse files Browse the repository at this point in the history
Dont die
  • Loading branch information
LukasReschke committed Sep 28, 2015
2 parents 0b9e800 + b771bbb commit 07b940c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
14 changes: 3 additions & 11 deletions src/TarStreamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<p><strong>Error:</strong> Unable to send file " .
"$encodedArchiveName. HTML Headers have already been sent from " .
"<strong>$headerFile</strong> in line <strong>$headerLine" .
"</strong></p>"
);
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<p><strong>Error:</strong> Unable to send file " .
"<strong>$encodedArchiveName</strong>. Output buffer " .
"already contains text (typically warnings or errors).</p>"
);
throw new \Exception("Unable to send file $encodedArchiveName. Output buffer already contains text (typically warnings or errors).");
}

$headers = [
Expand Down
77 changes: 68 additions & 9 deletions tests/Streamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
);
Expand All @@ -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;
}
}

0 comments on commit 07b940c

Please sign in to comment.