From 45fd3b0f1dfa2c965857c6d4a470bea52adc31a6 Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Wed, 6 Jan 2021 15:52:02 +0100 Subject: [PATCH] Make test suite pass for all PHP versions --- .travis.yml | 5 --- Dockerfile | 4 +- composer.json | 4 +- src/JS.php | 2 +- src/Minify.php | 10 +++-- tests/bootstrap.php | 15 ++++++- tests/css/CSSTest.php | 84 +++++++++++++++------------------------ tests/js/AbstractTest.php | 59 +++++++++------------------ tests/js/JSTest.php | 51 ++++++++---------------- 9 files changed, 94 insertions(+), 140 deletions(-) diff --git a/.travis.yml b/.travis.yml index 218b08f0..a99049a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,8 +31,3 @@ after_success: after_script: - make down PHP=$(phpenv version-name) - -matrix: - allow_failures: - - php: 7.4 - - php: 8.0 diff --git a/Dockerfile b/Dockerfile index fc36a814..5917e05f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,8 @@ COPY . /var/www WORKDIR /var/www RUN apt-get update -RUN apt-get install -y zip unzip zlib1g-dev -RUN if [[ `php-config --vernum` -ge 73000 ]]; then docker-php-ext-install zip; fi +RUN apt-get install -y zip unzip libzip-dev git +RUN docker-php-ext-install zip RUN docker-php-ext-install pcntl RUN curl -sS https://getcomposer.org/installer | php RUN mv composer.phar /usr/local/bin/composer diff --git a/composer.json b/composer.json index 6d81b4f9..a6dd4ab6 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ "matthiasmullie/path-converter": "~1.1" }, "require-dev": { - "matthiasmullie/scrapbook": "~1.0", - "phpunit/phpunit": "~4.8", + "matthiasmullie/scrapbook": "dev-master", + "phpunit/phpunit": ">=4.8", "friendsofphp/php-cs-fixer": "~2.0" }, "suggest": { diff --git a/src/JS.php b/src/JS.php index 92389cdd..a0fa649d 100644 --- a/src/JS.php +++ b/src/JS.php @@ -254,7 +254,7 @@ protected function extractRegex() // of the RegExp methods (a `\` followed by a variable or value is // likely part of a division, not a regex) $keywords = array('do', 'in', 'new', 'else', 'throw', 'yield', 'delete', 'return', 'typeof'); - $before = '([=:,;\+\-\*\/\}\(\{\[&\|!]|^|'.implode('|', $keywords).')\s*'; + $before = '(^|[=:,;\+\-\*\/\}\(\{\[&\|!]|'.implode('|', $keywords).')\s*'; $propertiesAndMethods = array( // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Properties_2 'constructor', diff --git a/src/Minify.php b/src/Minify.php index 3f40bc15..4d8dcf40 100644 --- a/src/Minify.php +++ b/src/Minify.php @@ -105,7 +105,7 @@ public function add($data /* $data = null, ... */) * @param string|string[] $data * * @return static - * + * * @throws IOException */ public function addFile($data /* $data = null, ... */) @@ -472,7 +472,7 @@ protected function canImportFile($path) */ protected function openFileForWriting($path) { - if (($handler = @fopen($path, 'w')) === false) { + if ($path === '' || ($handler = @fopen($path, 'w')) === false) { throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.'); } @@ -490,7 +490,11 @@ protected function openFileForWriting($path) */ protected function writeToFile($handler, $content, $path = '') { - if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) { + if ( + !is_resource($handler) || + ($result = @fwrite($handler, $content)) === false || + ($result < strlen($content)) + ) { throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 991ea439..d8d174cc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,3 +1,16 @@ minifier = $this->getMockBuilder('\MatthiasMullie\Minify\CSS') + return $this->getMockBuilder('\MatthiasMullie\Minify\CSS') ->setMethods(array('save')) ->getMock(); } - /** - * Cleans up the environment after running a test. - */ - protected function tearDown() - { - $this->minifier = null; - parent::tearDown(); - } - /** * Test CSS minifier rules, provided by dataProvider. * - * @test * @dataProvider dataProvider */ - public function minify($input, $expected) + public function testMinify($input, $expected) { - $this->minifier->add($input); - $result = $this->minifier->minify(); + $minifier = $this->mockMinifier(); + $minifier->add($input); + $result = $minifier->minify(); $this->assertEquals($expected, $result); } /** * Test conversion of relative paths, provided by dataProviderPaths. * - * @test * @dataProvider dataProviderPaths */ - public function convertRelativePath($source, $target, $expected) + public function testConvertRelativePath($source, $target, $expected) { + $minifier = $this->mockMinifier(); $source = (array) $source; foreach ($source as $path => $css) { - $this->minifier->add($css); + $minifier->add($css); // $source also accepts an array where the key is a bogus path if (is_string($path)) { - $object = new ReflectionObject($this->minifier); + $object = new ReflectionObject($minifier); $property = $object->getProperty('data'); $property->setAccessible(true); - $data = $property->getValue($this->minifier); + $data = $property->getValue($minifier); // keep content, but make it appear from the given path $data[$path] = array_pop($data); - $property->setValue($this->minifier, $data); + $property->setValue($minifier, $data); $property->setAccessible(false); } } - $result = $this->minifier->minify($target); + $result = $minifier->minify($target); $this->assertEquals($expected, $result); } /** * Test loop while importing file. - * - * @test - * - * @expectedException MatthiasMullie\Minify\Exceptions\FileImportException */ - public function fileImportLoop() + public function testFileImportLoop() { + $this->expectException('MatthiasMullie\Minify\Exceptions\FileImportException'); + $testFile = __DIR__.'/sample/loop/first.css'; - $this->minifier->add($testFile); + $minifier = $this->mockMinifier(); + $minifier->add($testFile); - $this->minifier->minify(); + $minifier->minify(); } /** * Test minifier import configuration methods. - * - * @test */ - public function setConfig() + public function testSetConfig() { - $this->minifier->setMaxImportSize(10); - $this->minifier->setImportExtensions(array('gif' => 'data:image/gif')); + $minifier = $this->mockMinifier(); + $minifier->setMaxImportSize(10); + $minifier->setImportExtensions(array('gif' => 'data:image/gif')); - $object = new ReflectionObject($this->minifier); + $object = new ReflectionObject($minifier); $property = $object->getProperty('maxImportSize'); $property->setAccessible(true); - $this->assertEquals($property->getValue($this->minifier), 10); + $this->assertEquals($property->getValue($minifier), 10); $property = $object->getProperty('importExtensions'); $property->setAccessible(true); - $this->assertEquals($property->getValue($this->minifier), array('gif' => 'data:image/gif')); + $this->assertEquals($property->getValue($minifier), array('gif' => 'data:image/gif')); } /** @@ -124,11 +106,11 @@ public function dataProvider() // passing in an array of css inputs $tests[] = array( - [ + array( __DIR__.'/sample/combine_imports/index.css', __DIR__.'/sample/bom/bom.css', 'p { width: 55px , margin: 0 0 0 0}', - ], + ), 'body{color:red}body{color:red}p{width:55px,margin:0 0 0 0}', ); diff --git a/tests/js/AbstractTest.php b/tests/js/AbstractTest.php index 201d022b..9bb8b94a 100644 --- a/tests/js/AbstractTest.php +++ b/tests/js/AbstractTest.php @@ -1,18 +1,19 @@ assertEquals($content1.';'.$content2, $result); } - /** - * @test - */ - public function add() + public function testAdd() { $path1 = __DIR__.'/sample/source/script1.js'; $path2 = __DIR__.'/sample/source/script2.js'; @@ -106,10 +104,7 @@ public function add() $this->assertEquals($content1.';'.$content2.';'.$content3, $result); } - /** - * @test - */ - public function loadBigString() + public function testLoadBigString() { // content greater than PHP_MAXPATHLEN // https://github.com/matthiasmullie/minify/issues/90 @@ -120,10 +115,7 @@ public function loadBigString() $this->assertEquals($minifier->minify(), $content); } - /** - * @test - */ - public function loadOpenBaseDirRestricted() + public function testLoadOpenBaseDirRestricted() { if (!function_exists('pcntl_fork') || defined('HHVM_VERSION')) { $this->markTestSkipped("Can't fork, skip open_basedir test"); @@ -166,10 +158,7 @@ public function loadOpenBaseDirRestricted() } } - /** - * @test - */ - public function save() + public function testSave() { $path = __DIR__.'/sample/source/script1.js'; $content = file_get_contents($path); @@ -181,13 +170,10 @@ public function save() $this->assertEquals(file_get_contents($savePath), $content); } - /** - * @test - * - * @expectedException MatthiasMullie\Minify\Exceptions\IOException - */ - public function checkFileOpenFail() + public function testCheckFileOpenFail() { + $this->expectException('MatthiasMullie\Minify\Exceptions\IOException'); + $minifier = new Minify\JS(); $wrongPath = ''; @@ -198,13 +184,10 @@ public function checkFileOpenFail() $method->invokeArgs($minifier, array($wrongPath)); } - /** - * @test - * - * @expectedException MatthiasMullie\Minify\Exceptions\IOException - */ - public function checkFileWriteFail() + public function testCheckFileWriteFail() { + $this->expectException('MatthiasMullie\Minify\Exceptions\IOException'); + $minifier = new Minify\JS(); $wrongPath = ''; @@ -215,10 +198,7 @@ public function checkFileWriteFail() $method->invokeArgs($minifier, array($wrongPath, '')); } - /** - * @test - */ - public function gzip() + public function testGzip() { $path = __DIR__.'/sample/source/script1.js'; $content = file_get_contents($path); @@ -230,10 +210,7 @@ public function gzip() $this->assertEquals(file_get_contents($savePath), gzencode($content, 9, FORCE_GZIP)); } - /** - * @test - */ - public function cache() + public function testCache() { $path = __DIR__.'/sample/source/script1.js'; $content = file_get_contents($path); diff --git a/tests/js/JSTest.php b/tests/js/JSTest.php index f5c1b6c0..db43308d 100644 --- a/tests/js/JSTest.php +++ b/tests/js/JSTest.php @@ -1,59 +1,42 @@ minifier = $this->getMockBuilder('\MatthiasMullie\Minify\JS') + return $this->getMockBuilder('\MatthiasMullie\Minify\JS') ->setMethods(array('save')) ->getMock(); } - /** - * Cleans up the environment after running a test. - */ - protected function tearDown() - { - $this->minifier = null; - parent::tearDown(); - } - /** * Test minifier on files that doesn't exist. - * - * @expectedException MatthiasMullie\Minify\Exceptions\IOException */ public function testAddFileException() { - $this->minifier->addFile('/sample/source/nothing'); + $this->expectException('MatthiasMullie\Minify\Exceptions\IOException'); + + $minifier = $this->mockMinifier(); + $minifier->addFile('/sample/source/nothing'); } /** * Test minifier addFile method. - * - * @test */ public function testAddFile() { - $this->minifier->addFile(__DIR__.'/sample/source/script1.js'); + $minifier = $this->mockMinifier(); + $minifier->addFile(__DIR__.'/sample/source/script1.js'); - $result = $this->minifier->minify(); + $result = $minifier->minify(); $this->assertEquals('var test=1', $result); } @@ -61,13 +44,13 @@ public function testAddFile() /** * Test JS minifier rules, provided by dataProvider. * - * @test * @dataProvider dataProvider */ - public function minify($input, $expected) + public function testMinify($input, $expected) { - $this->minifier->add($input); - $result = $this->minifier->minify(); + $minifier = $this->mockMinifier(); + $minifier->add($input); + $result = $minifier->minify(); $this->assertEquals($expected, $result); }