├── .coveralls.yml ├── .editorconfig ├── .gitignore ├── .scrutinizer.yml ├── .sensiolabs.yml ├── .styleci.yml ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Bzip2Compressor.php ├── CompressorInterface.php ├── DummyCompressor.php ├── GzipCompressor.php └── ZipCompressor.php └── tests ├── Bzip2CompressorTest.php ├── DummyCompressorTest.php ├── Filesystem.php ├── GzipCompressorTest.php ├── ZipCompressorTest.php └── bootstrap.php /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci 2 | coverage_clover: build/coverage-clover.xml 3 | json_path: build/coveralls-upload.json 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; top-most EditorConfig file 2 | root = true 3 | 4 | ; Unix-style newlines 5 | [*] 6 | end_of_line = LF 7 | 8 | [*.php] 9 | indent_style = space 10 | indent_size = 4 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /build/ 3 | phpunit.xml 4 | composer.lock 5 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | imports: 2 | - php 3 | 4 | tools: 5 | external_code_coverage: true 6 | -------------------------------------------------------------------------------- /.sensiolabs.yml: -------------------------------------------------------------------------------- 1 | rules: 2 | php.silenced_error: 3 | enabled: true 4 | function_whitelist: 5 | - fopen 6 | - bzopen 7 | - gzopen 8 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: symfony 2 | 3 | enabled: 4 | - short_array_syntax 5 | 6 | disabled: 7 | - phpdoc_align 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | sudo: false 4 | 5 | notifications: 6 | email: deploy@peter-gribanov.ru 7 | 8 | matrix: 9 | fast_finish: true 10 | include: 11 | - php: 7.4 12 | - php: 7.3 13 | - php: 7.2 14 | - php: 7.1 15 | - php: 7.0 16 | - php: 5.6 17 | env: COVERAGE=1 18 | - php: 5.5 19 | dist: trusty 20 | - php: 5.4 21 | dist: trusty 22 | 23 | before_install: 24 | - if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$COVERAGE" != "1" ]; then phpenv config-rm xdebug.ini; fi; 25 | - if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi; 26 | - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; 27 | - composer self-update 28 | - mkdir build 29 | 30 | before_script: 31 | - composer install --prefer-dist --no-interaction --no-scripts --no-progress 32 | 33 | script: 34 | - if [ "$COVERAGE" != "1" ]; then vendor/bin/phpunit; fi; 35 | - if [ "$COVERAGE" == "1" ]; then vendor/bin/phpunit --coverage-clover build/coverage-clover.xml; fi; 36 | 37 | after_script: 38 | - if [ "$COVERAGE" == "1" ]; then vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage-clover.xml; fi; 39 | - if [ "$COVERAGE" == "1" ]; then vendor/bin/coveralls -v -c .coveralls.yml; fi; 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 GPS Lab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://img.shields.io/packagist/v/gpslab/compressor.svg?maxAge=3600&label=stable)](https://packagist.org/packages/gpslab/compressor) 2 | [![Latest Unstable Version](https://img.shields.io/packagist/vpre/gpslab/compressor.svg?maxAge=3600&label=unstable)](https://packagist.org/packages/gpslab/compressor) 3 | [![Total Downloads](https://img.shields.io/packagist/dt/gpslab/compressor.svg?maxAge=3600)](https://packagist.org/packages/gpslab/compressor) 4 | [![Build Status](https://img.shields.io/travis/gpslab/compressor.svg?maxAge=3600)](https://travis-ci.org/gpslab/compressor) 5 | [![Coverage Status](https://img.shields.io/coveralls/gpslab/compressor.svg?maxAge=3600)](https://coveralls.io/github/gpslab/compressor?branch=master) 6 | [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/gpslab/compressor.svg?maxAge=3600)](https://scrutinizer-ci.com/g/gpslab/compressor/?branch=master) 7 | [![SensioLabs Insight](https://img.shields.io/sensiolabs/i/55aa6cb4-fccc-4700-a00b-723c8f11a413.svg?maxAge=3600&label=SLInsight)](https://insight.sensiolabs.com/projects/55aa6cb4-fccc-4700-a00b-723c8f11a413) 8 | [![StyleCI](https://styleci.io/repos/79129059/shield?branch=master)](https://styleci.io/repos/79129059) 9 | [![License](https://img.shields.io/packagist/l/gpslab/compressor.svg?maxAge=3600)](https://github.com/gpslab/compressor) 10 | 11 | File compressor 12 | =============== 13 | 14 | ## License 15 | 16 | This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gpslab/compressor", 3 | "license": "MIT", 4 | "type": "library", 5 | "homepage": "https://github.com/gpslab/compressor", 6 | "description": "File compressor", 7 | "autoload": { 8 | "psr-4": { 9 | "GpsLab\\Component\\Compressor\\": "src/" 10 | } 11 | }, 12 | "autoload-dev": { 13 | "psr-4": { 14 | "GpsLab\\Component\\Compressor\\Tests\\": "tests/" 15 | } 16 | }, 17 | "require": { 18 | "php": ">=5.4.0" 19 | }, 20 | "require-dev": { 21 | "symfony/filesystem": "^2.4", 22 | "phpunit/phpunit": "^4.8.36", 23 | "scrutinizer/ocular": "~1.3", 24 | "php-coveralls/php-coveralls": "^1.0" 25 | }, 26 | "suggest": { 27 | "ext-zlib": "Support for Gzip compressor", 28 | "ext-bz2": "Support for Bzip2 compressor", 29 | "ext-zip": "Support for Zip compressor" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | ./tests 17 | 18 | 19 | 20 | 21 | ./src 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Bzip2Compressor.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor; 11 | 12 | class Bzip2Compressor implements CompressorInterface 13 | { 14 | /** 15 | * @param string $source 16 | * @param string $target 17 | * 18 | * @return bool 19 | */ 20 | public function compress($source, $target = '') 21 | { 22 | $target = $target ?: $source.'.bz2'; 23 | $fh = @fopen($source, 'rb'); 24 | $bz = @bzopen($target, 'w'); 25 | 26 | if (false === $fh || false === $bz) { 27 | return false; 28 | } 29 | 30 | while (!feof($fh)) { 31 | if (false === bzwrite($bz, fread($fh, 1024))) { 32 | return false; 33 | } 34 | } 35 | 36 | fclose($fh); 37 | bzclose($bz); 38 | 39 | return true; 40 | } 41 | 42 | /** 43 | * @param string $source 44 | * @param string $target 45 | * 46 | * @return bool 47 | */ 48 | public function uncompress($source, $target) 49 | { 50 | $bz = @bzopen($source, 'r'); 51 | $fh = @fopen($target, 'wb'); 52 | 53 | if (false === $fh || false === $bz) { 54 | return false; 55 | } 56 | 57 | while (!feof($bz)) { 58 | if (false === fwrite($fh, bzread($bz, 1024))) { 59 | return false; 60 | } 61 | } 62 | 63 | fclose($fh); 64 | bzclose($bz); 65 | 66 | return true; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/CompressorInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor; 11 | 12 | interface CompressorInterface 13 | { 14 | /** 15 | * @param string $source 16 | * @param string $target 17 | * 18 | * @return bool 19 | */ 20 | public function compress($source, $target = ''); 21 | 22 | /** 23 | * @param string $source 24 | * @param string $target 25 | * 26 | * @return bool 27 | */ 28 | public function uncompress($source, $target); 29 | } 30 | -------------------------------------------------------------------------------- /src/DummyCompressor.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor; 11 | 12 | class DummyCompressor implements CompressorInterface 13 | { 14 | /** 15 | * @param string $source 16 | * @param string $target 17 | * 18 | * @return bool 19 | */ 20 | public function compress($source, $target = '') 21 | { 22 | if ($target && $target != $source) { 23 | return copy($source, $target); 24 | } 25 | 26 | return true; 27 | } 28 | 29 | /** 30 | * @param string $source 31 | * @param string $target 32 | * 33 | * @return bool 34 | */ 35 | public function uncompress($source, $target) 36 | { 37 | if ($target != $source) { 38 | return copy($source, $target); 39 | } 40 | 41 | return true; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/GzipCompressor.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor; 11 | 12 | class GzipCompressor implements CompressorInterface 13 | { 14 | /** 15 | * @param string $source 16 | * @param string $target 17 | * 18 | * @return bool 19 | */ 20 | public function compress($source, $target = '') 21 | { 22 | $target = $target ?: $source.'.gz'; 23 | $fh = @fopen($source, 'rb'); 24 | $gz = @gzopen($target, 'wb9'); 25 | 26 | if (false === $fh || false === $gz) { 27 | return false; 28 | } 29 | 30 | while (!feof($fh)) { 31 | if (false === gzwrite($gz, fread($fh, 1024))) { 32 | return false; 33 | } 34 | } 35 | 36 | fclose($fh); 37 | gzclose($gz); 38 | 39 | return true; 40 | } 41 | 42 | /** 43 | * @param string $source 44 | * @param string $target 45 | * 46 | * @return bool 47 | */ 48 | public function uncompress($source, $target) 49 | { 50 | $gz = @gzopen($source, 'rb'); 51 | $fh = @fopen($target, 'wb'); 52 | 53 | if (false === $fh || false === $gz) { 54 | return false; 55 | } 56 | 57 | while (!feof($gz)) { 58 | if (false === fwrite($fh, gzread($gz, 1024))) { 59 | return false; 60 | } 61 | } 62 | 63 | fclose($fh); 64 | gzclose($gz); 65 | 66 | return true; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/ZipCompressor.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor; 11 | 12 | class ZipCompressor implements CompressorInterface 13 | { 14 | /** 15 | * @var \ZipArchive 16 | */ 17 | protected $zip; 18 | 19 | /** 20 | * @param \ZipArchive $zip 21 | */ 22 | public function __construct(\ZipArchive $zip) 23 | { 24 | $this->zip = $zip; 25 | } 26 | 27 | /** 28 | * @param string $source 29 | * @param string $target 30 | * 31 | * @return bool 32 | */ 33 | public function compress($source, $target = '') 34 | { 35 | $target = $target ?: $source.'.zip'; 36 | 37 | if (false === $this->zip->open($target, \ZipArchive::OVERWRITE | \ZipArchive::CREATE)) { 38 | return false; 39 | } 40 | 41 | if (false === @$this->zip->addFile($source, basename($source))) { 42 | return false; 43 | } 44 | 45 | return $this->zip->close(); 46 | } 47 | 48 | /** 49 | * @param string $source 50 | * @param string $target 51 | * 52 | * @return bool 53 | */ 54 | public function uncompress($source, $target) 55 | { 56 | if (false === $this->zip->open($source, \ZipArchive::CHECKCONS)) { 57 | return false; 58 | } 59 | 60 | if (false === @$this->zip->extractTo(dirname($target), basename($target))) { 61 | return false; 62 | } 63 | 64 | return $this->zip->close(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/Bzip2CompressorTest.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor\Tests; 11 | 12 | use GpsLab\Component\Compressor\Bzip2Compressor; 13 | use PHPUnit\Framework\TestCase; 14 | 15 | class Bzip2CompressorTest extends TestCase 16 | { 17 | /** 18 | * @var Bzip2Compressor 19 | */ 20 | private $compressor; 21 | 22 | /** 23 | * @var Filesystem 24 | */ 25 | private $fs; 26 | 27 | protected function setUp() 28 | { 29 | $this->compressor = new Bzip2Compressor(); 30 | $this->fs = new Filesystem(); 31 | } 32 | 33 | protected function tearDown() 34 | { 35 | $this->fs->clear(); 36 | } 37 | 38 | public function testCompress() 39 | { 40 | $source = $this->fs->tempnam(); 41 | $compress = $this->fs->tempnam(); 42 | $uncompress = $this->fs->tempnam(); 43 | 44 | $this->fs->put($source); 45 | 46 | $this->assertTrue($this->compressor->compress($source, $compress)); 47 | $this->assertTrue($this->fs->exists($compress)); 48 | $this->assertTrue($this->fs->equals($compress, function ($content) { 49 | return bzdecompress($content); 50 | })); 51 | 52 | $this->assertTrue($this->compressor->uncompress($compress, $uncompress)); 53 | $this->assertTrue($this->fs->exists($uncompress)); 54 | $this->assertTrue($this->fs->equals($uncompress)); 55 | } 56 | 57 | public function testCompressOnIncorrectFilePath() 58 | { 59 | $source = '/this/bzip/file/is/not/existed'; 60 | 61 | $this->assertFalse($this->compressor->compress($source)); 62 | } 63 | 64 | public function testUncompressOnIncorrectFilePath() 65 | { 66 | $source = '/this/bzip/file/is/not/existed'; 67 | $target = '/this/target/bzip/file/is/not/existed'; 68 | 69 | $this->assertFalse($this->compressor->uncompress($source, $target)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tests/DummyCompressorTest.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor\Tests; 11 | 12 | use GpsLab\Component\Compressor\DummyCompressor; 13 | use PHPUnit\Framework\TestCase; 14 | 15 | class DummyCompressorTest extends TestCase 16 | { 17 | /** 18 | * @var DummyCompressor 19 | */ 20 | private $compressor; 21 | 22 | /** 23 | * @var Filesystem 24 | */ 25 | private $fs; 26 | 27 | /** 28 | * @var string 29 | */ 30 | private $source = ''; 31 | 32 | /** 33 | * @var string 34 | */ 35 | private $target = ''; 36 | 37 | protected function setUp() 38 | { 39 | $this->compressor = new DummyCompressor(); 40 | $this->fs = new Filesystem(); 41 | 42 | $this->source = $this->fs->tempnam(); 43 | $this->target = $this->fs->tempnam(); 44 | } 45 | 46 | protected function tearDown() 47 | { 48 | $this->fs->clear(); 49 | } 50 | 51 | public function testCompressDoNothing() 52 | { 53 | $this->assertTrue($this->compressor->compress($this->source)); 54 | } 55 | 56 | public function testCompress() 57 | { 58 | $this->fs->put($this->source); 59 | 60 | $this->assertTrue($this->compressor->compress($this->source, $this->target)); 61 | $this->assertTrue($this->fs->exists($this->target)); 62 | $this->assertTrue($this->fs->equals($this->target)); 63 | } 64 | 65 | public function testUncompressDoNothing() 66 | { 67 | $this->assertTrue($this->compressor->uncompress($this->source, $this->source)); 68 | } 69 | 70 | public function testUncompress() 71 | { 72 | $this->fs->put($this->source); 73 | 74 | $this->assertTrue($this->compressor->uncompress($this->source, $this->target)); 75 | $this->assertTrue($this->fs->exists($this->target)); 76 | $this->assertTrue($this->fs->equals($this->target)); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/Filesystem.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor\Tests; 11 | 12 | use Symfony\Component\Filesystem\Filesystem as FilesystemSymfony; 13 | 14 | class Filesystem 15 | { 16 | /** 17 | * @var string 18 | */ 19 | const CONTENT = "foo\r\nbar\n"; 20 | 21 | /** 22 | * @var FilesystemSymfony 23 | */ 24 | private $fs; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $root = ''; 30 | 31 | public function __construct() 32 | { 33 | $this->fs = new FilesystemSymfony(); 34 | 35 | $this->root = sys_get_temp_dir().'/test/'; 36 | $this->fs->mkdir($this->root); 37 | } 38 | 39 | public function clear() 40 | { 41 | $this->fs->remove($this->root); 42 | } 43 | 44 | /** 45 | * @param string $file 46 | * 47 | * @return bool 48 | */ 49 | public function exists($file) 50 | { 51 | return $this->fs->exists($file); 52 | } 53 | 54 | /** 55 | * @return string 56 | */ 57 | public function tempnam() 58 | { 59 | return $this->fs->tempnam($this->root, 'test'); 60 | } 61 | 62 | /** 63 | * @param string $filename 64 | */ 65 | public function remove($filename) 66 | { 67 | $this->fs->remove($filename); 68 | } 69 | 70 | /** 71 | * @param string $filename 72 | */ 73 | public function put($filename) 74 | { 75 | file_put_contents($filename, self::CONTENT); 76 | } 77 | 78 | /** 79 | * @param string $filename 80 | * @param \Closure|null $compressor 81 | * 82 | * @return bool 83 | */ 84 | public function equals($filename, \Closure $compressor = null) 85 | { 86 | // not compress as a default 87 | if (!$compressor) { 88 | $compressor = function ($content) { 89 | return $content; 90 | }; 91 | } 92 | 93 | return self::CONTENT == call_user_func($compressor, file_get_contents($filename)); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/GzipCompressorTest.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor\Tests; 11 | 12 | use GpsLab\Component\Compressor\GzipCompressor; 13 | use PHPUnit\Framework\TestCase; 14 | 15 | class GzipCompressorTest extends TestCase 16 | { 17 | /** 18 | * @var GzipCompressor 19 | */ 20 | private $compressor; 21 | 22 | /** 23 | * @var Filesystem 24 | */ 25 | private $fs; 26 | 27 | protected function setUp() 28 | { 29 | $this->compressor = new GzipCompressor(); 30 | $this->fs = new Filesystem(); 31 | } 32 | 33 | protected function tearDown() 34 | { 35 | $this->fs->clear(); 36 | } 37 | 38 | public function testCompress() 39 | { 40 | $source = $this->fs->tempnam(); 41 | $compress = $this->fs->tempnam(); 42 | $uncompress = $this->fs->tempnam(); 43 | 44 | $this->fs->put($source); 45 | 46 | $this->assertTrue($this->compressor->compress($source, $compress)); 47 | $this->assertTrue($this->fs->exists($compress)); 48 | $this->assertFalse($this->fs->equals($compress)); 49 | 50 | $this->assertTrue($this->compressor->uncompress($compress, $uncompress)); 51 | $this->assertTrue($this->fs->exists($uncompress)); 52 | $this->assertTrue($this->fs->equals($uncompress)); 53 | } 54 | 55 | public function testCompressOnIncorrectFilePath() 56 | { 57 | $source = '/this/bzip/file/is/not/existed'; 58 | 59 | $this->assertFalse($this->compressor->compress($source)); 60 | } 61 | 62 | public function testUncompressOnIncorrectFilePath() 63 | { 64 | $source = '/this/bzip/file/is/not/existed'; 65 | $target = '/this/target/bzip/file/is/not/existed'; 66 | 67 | $this->assertFalse($this->compressor->uncompress($source, $target)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /tests/ZipCompressorTest.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2011, Peter Gribanov 7 | * @license http://opensource.org/licenses/MIT 8 | */ 9 | 10 | namespace GpsLab\Component\Compressor\Tests; 11 | 12 | use GpsLab\Component\Compressor\ZipCompressor; 13 | use PHPUnit\Framework\TestCase; 14 | 15 | class ZipCompressorTest extends TestCase 16 | { 17 | /** 18 | * @var ZipCompressor 19 | */ 20 | private $compressor; 21 | 22 | /** 23 | * @var Filesystem 24 | */ 25 | private $fs; 26 | 27 | protected function setUp() 28 | { 29 | $this->compressor = new ZipCompressor(new \ZipArchive()); 30 | $this->fs = new Filesystem(); 31 | } 32 | 33 | protected function tearDown() 34 | { 35 | $this->fs->clear(); 36 | } 37 | 38 | public function testCompress() 39 | { 40 | $source = $this->fs->tempnam(); 41 | $compress = $this->fs->tempnam(); 42 | 43 | $this->fs->put($source); 44 | 45 | $this->assertTrue($this->compressor->compress($source, $compress)); 46 | $this->assertTrue($this->fs->exists($compress)); 47 | $this->assertFalse($this->fs->equals($compress)); 48 | 49 | $this->fs->remove($source); 50 | 51 | $this->assertTrue($this->compressor->uncompress($compress, $source)); 52 | $this->assertTrue($this->fs->exists($source)); 53 | $this->assertTrue($this->fs->equals($source)); 54 | } 55 | 56 | public function testCompressOnIncorrectFilePath() 57 | { 58 | $source = '/this/bzip/file/is/not/existed'; 59 | 60 | $this->assertFalse($this->compressor->compress($source)); 61 | } 62 | 63 | public function testUncompressOnIncorrectFilePath() 64 | { 65 | $source = '/this/bzip/file/is/not/existed'; 66 | $target = '/this/target/bzip/file/is/not/existed'; 67 | 68 | $this->assertFalse($this->compressor->uncompress($source, $target)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |