diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..5b63c76 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,9 @@ +filter: + excluded_paths: + - 'vendor/*' + +imports: + - php + +tools: + external_code_coverage: true diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..081521d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +language: php +php: +# - 5.3 + - 5.4 + - 5.5 + - 5.6 + - 7.0 + +branches: + only: + - master + +install: + - composer install --dev --no-interaction + +script: + - mkdir -p build/logs + - cd tests + - phpunit --coverage-clover ../build/logs/clover.xml --configuration phpunit.xml + +after_script: + # Create coverage report + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml diff --git a/composer.json b/composer.json index 59dcfe1..2e26538 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,32 @@ { - "name": "deepdiver1975/tarstreamer", - "type": "library", - "description": "A library for dynamically streaming dynamic tar files without the need to have the complete file stored on the server.", - "keywords": ["tar", "archive", "stream", "php"], - "homepage": "https://github.com/DeepDiver1975/TarStreamer", - "license": "MIT", - "require": { - "php": ">=5.3.8" - }, - "autoload": { - "psr-4": { - "DeepDiver1975\\TarStreamer\\": "src/" - } - } + "name": "owncloud/tarstreamer", + "type": "library", + "description": "A library for dynamically streaming dynamic tar files without the need to have the complete file stored on the server.", + "keywords": [ + "tar", + "archive", + "stream", + "php" + ], + "homepage": "https://github.com/owncloud/TarStreamer", + "license": "MIT", + "require": { + "php": ">=5.3.8" + }, + "autoload": { + "psr-4": { + "ownCloud\\TarStreamer\\": "src/" + } + }, + "archive": { + "exclude": [ + "test", + "tests" + ] + }, + "require-dev": { + "phpunit/phpunit": "^4.8", + "pear/pear-core-minimal": "v1.10.0alpha2", + "pear/archive_tar": "~1.4" + } } diff --git a/src/TarStreamer.php b/src/TarStreamer.php index c90c12e..4c73f65 100644 --- a/src/TarStreamer.php +++ b/src/TarStreamer.php @@ -1,6 +1,6 @@ outstream = $options['outstream']; + $this->outStream = $options['outstream']; } else { - $this->outstream = fopen('php://output', 'w'); + $this->outStream = fopen('php://output', 'w'); // turn off output buffering while (ob_get_level() > 0){ ob_end_flush(); @@ -78,9 +78,10 @@ public function sendHeaders($archiveName = 'archive.tar', $contentType = 'applic /** * Add a file to the archive at the specified location and file name. * - * @param string $stream Stream to read data from - * @param string $filePath Filepath and name to be used in the archive. - * @param array $options Optional, additional options + * @param resource $stream Stream to read data from + * @param string $filePath Filepath and name to be used in the archive. + * @param int $size + * @param array $options Optional, additional options * Valid options are: * * int timestamp: timestamp for the file (default: current time) * @return bool $success @@ -133,7 +134,7 @@ public function finalize(){ $this->send(pack('a1024', '')); // flush the data to the output - fflush($this->outstream); + fflush($this->outStream); return true; } @@ -205,19 +206,19 @@ protected function initFileStreamTransfer($name, $type, $size, $opt = []){ /** * Stream the next part of the current file stream. * - * @param $data raw data to send + * @param string $data raw data to send */ protected function streamFilePart($data){ // send data $this->send($data); // flush the data to the output - fflush($this->outstream); + fflush($this->outStream); } /** * Complete the current file stream - * + * @param $size */ protected function completeFileStream($size){ // ensure we pad the last block so that it is 512 bytes @@ -226,7 +227,7 @@ protected function completeFileStream($size){ } // flush the data to the output - fflush($this->outstream); + fflush($this->outStream); } /** @@ -240,13 +241,13 @@ protected function send($data){ } $this->needHeaders = false; - fwrite($this->outstream, $data); + fwrite($this->outStream, $data); } /** * Create a format string and argument list for pack(), then call pack() and return the result. * - * @param array key being the format string and value being the data to pack + * @param array $fields key being the format string and value being the data to pack * @return string binary packed data returned from pack() */ protected function packFields($fields){ diff --git a/test/testdata/lorem.txt b/test/testdata/lorem.txt deleted file mode 100644 index ccd0041..0000000 --- a/test/testdata/lorem.txt +++ /dev/null @@ -1,5 +0,0 @@ -Duis a blandit nulla. Proin velit est, consectetur vel sapien non, elementum semper odio. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id eros a tellus posuere laoreet eget at mauris. Nullam tincidunt bibendum metus, a porttitor lorem elementum in. Curabitur sit amet ultricies enim. Phasellus ullamcorper nibh vel odio luctus dignissim.Nam imperdiet leo eros. Sed et est ut justo accumsan egestas nec id leo. - -Quisque bibendum ultrices orci vitae rhoncus. Etiam nec quam posuere, malesuada lorem eget, convallis arcu. Integer lacinia consectetur dui ac dictum. Sed fermentum nulla vitae dui consequat, vitae lobortis neque euismod. Nullam commodo imperdiet fringilla. Nunc ultricies nisl neque, nec ullamcorper turpis feugiat id. Nulla ut rutrum felis, ut tincidunt diam. In interdum erat tristique, mattis velit nec, lacinia enim. Fusce vehicula vitae mi id maximus. Aliquam suscipit mi a felis dignissim ultricies. - -Suspendisse eu nisi eu ipsum vulputate ultricies. Donec feugiat varius eros, vitae semper lorem blandit ac. Suspendisse id gravida odio. Vestibulum eu leo quis velit ultrices molestie. Vestibulum eget eros quis erat auctor dapibus ut sit amet lectus. Donec eu efficitur ex. Fusce ultricies diam a ex venenatis fringilla. Cras interdum purus a accumsan lobortis. Praesent consectetur felis eget tellus tempus, eu posuere metus pulvinar. Morbi odio purus, finibus quis tincidunt sit amet, consectetur quis metus. \ No newline at end of file diff --git a/test/testdata/lorem2 b/test/testdata/lorem2 deleted file mode 100644 index 3b24fe6..0000000 --- a/test/testdata/lorem2 +++ /dev/null @@ -1,3 +0,0 @@ -Maecenas eu quam vel diam rutrum rhoncus vel placerat dui. In luctus, diam at malesuada tristique, turpis enim consequat sem, vel posuere dolor ligula et erat. Maecenas vulputate turpis lorem, accumsan tristique dolor tristique a. Morbi nec fringilla dolor. Sed placerat nulla sapien, nec vulputate turpis tincidunt id. Vivamus sit amet diam neque. Duis neque felis, mollis eget nisi a, placerat dapibus lectus. Sed vestibulum, ex id viverra convallis, ligula libero aliquam nibh, eu fermentum mi ipsum eu est. Donec ullamcorper maximus purus in faucibus. Donec a vehicula erat. Cras ac gravida turpis. Aliquam erat volutpat. Quisque egestas risus id mi ullamcorper vulputate. Maecenas in auctor ipsum, eget iaculis velit. - -Vivamus non tortor eget erat pulvinar ornare nec a elit. Ut tincidunt malesuada mi, eu efficitur diam posuere sed. Sed scelerisque a sem egestas semper. Donec pretium, ante quis luctus lobortis, dolor nibh suscipit nisl, sit amet lobortis ante nunc a elit. Nullam pulvinar lacus eu ultricies dapibus. Aenean bibendum, quam eu gravida lobortis, eros est feugiat neque, sit amet convallis diam purus nec massa. Quisque viverra, dui eget interdum scelerisque, velit mi fermentum turpis, maximus porta est libero id dolor. \ No newline at end of file diff --git a/test/testdata/more ipsum/lorem2.txt b/test/testdata/more ipsum/lorem2.txt deleted file mode 100644 index f0e218f..0000000 --- a/test/testdata/more ipsum/lorem2.txt +++ /dev/null @@ -1,24 +0,0 @@ -Як умру, то поховайте -Мене на могилі -Серед степу широкого -На Вкраїні милій, -Щоб лани широкополі, -І Дніпро, і кручі -Було видно, було чути, -Як реве ревучий. -Як понесе з України -У синєє море -Кров ворожу... отойді я -І лани і гори — -Все покину, і полину -До самого Бога -Молитися... а до того -Я не знаю Бога. -Поховайте та вставайте, -Кайдани порвіте -І вражою злою кров’ю -Волю окропіте. -І мене в сем’ї великій, -В сем’ї вольній, новій, -Не забудьте пом’янути -Незлим тихим словом. diff --git a/test/usage.php b/test/usage.php deleted file mode 100644 index 3c59da9..0000000 --- a/test/usage.php +++ /dev/null @@ -1,72 +0,0 @@ -sendHeaders('testdir.tar'); - -// Iterate though the directory tree -foreach($objects as $path => $object){ - - // Find a relative path inside the package - $internalPath = substr($path, strlen($basePath)); - - if (is_file($path)) { - // Path a file descriptor, relative path and file size - $fh = fopen($path, 'r'); - $tarStdoutStreamer->addFileFromStream($fh, $internalPath, filesize($path)); - fclose($fh); - } elseif(is_dir($path)) { - // Just a path - $tarStdoutStreamer->addEmptyDir($internalPath); - } -} - -// Send the end marker -$tarStdoutStreamer->finalize(); -// And that's it - - -// Another Usecase: Stream into file -$newTarFileDescriptor = fopen('test.tar', 'w+'); - -// Passing a descriptor to a brand new object -$tarFileStreamer = new TarStreamer(['outstream' => $newTarFileDescriptor]); - -// Iterate though the directory tree -foreach($objects as $path => $object){ - // Just for the sake of debugging - echo "adding $path\n"; - - // Find a relative path inside the package - $internalPath = substr($path, strlen($basePath)); - - if (is_file($path)) { - $fh = fopen($path, 'r'); - $tarFileStreamer->addFileFromStream($fh, $internalPath, filesize($path)); - fclose($fh); - } elseif(is_dir($path)) { - $tarFileStreamer->addEmptyDir($internalPath); - } -} - -// Send the end marker -$tarFileStreamer->finalize(); - -// Close output file -fclose($newTarFileDescriptor); diff --git a/tests/Streamer.php b/tests/Streamer.php new file mode 100644 index 0000000..3c3cbd0 --- /dev/null +++ b/tests/Streamer.php @@ -0,0 +1,50 @@ +archive = tempnam('/tmp' , 'tar'); + $this->streamer = new TarStreamer( + ['outstream' => fopen($this->archive, 'w')] + ); + } + + /** + * @dataProvider providesNameAndData + * @param $fileName + * @param $data + */ + public function testSimpleFile($fileName, $data) { + $dataStream = fopen('data://text/plain,'.$data, 'r'); + $this->streamer->addFileFromStream($dataStream, $fileName, 10); + $this->streamer->finalize(); + + $this->assertTar($fileName, $data); + } + + public function providesNameAndData() { + return [ + ['foo.bar', '1234567890'], +// ['foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234.txt', 'abcdefgh'] + ]; + } + + private function assertTar($file, $data) + { + $arc = new Archive_Tar($this->archive); + $content = $arc->extractInString($file); + $this->assertEquals($data, $content); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..e91cc12 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ + + + + ./ + +