Skip to content

Commit

Permalink
Make test suite pass for all PHP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmullie committed Jan 6, 2021
1 parent b88dcc6 commit 45fd3b0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 140 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,3 @@ after_success:

after_script:
- make down PHP=$(phpenv version-name)

matrix:
allow_failures:
- php: 7.4
- php: 8.0
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 7 additions & 3 deletions src/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function add($data /* $data = null, ... */)
* @param string|string[] $data
*
* @return static
*
*
* @throws IOException
*/
public function addFile($data /* $data = null, ... */)
Expand Down Expand Up @@ -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.');
}

Expand All @@ -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.');
}
}
Expand Down
15 changes: 14 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<?php

require __DIR__.'/../vendor/autoload.php';
namespace {
require __DIR__.'/../vendor/autoload.php';
}

namespace PHPUnit\Framework
{
// compatibility for when these tests are run with PHPUnit<6.0 (which we
// still do because PHPUnit=6.0 stopped supporting a lot of PHP versions)
if (!class_exists('PHPUnit\Framework\TestCase')) {
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
}
}
}
84 changes: 33 additions & 51 deletions tests/css/CSSTest.php
Original file line number Diff line number Diff line change
@@ -1,118 +1,100 @@
<?php

use MatthiasMullie\Minify;
namespace MatthiasMullie\Minify\Test;

use PHPUnit\Framework\TestCase;
use ReflectionObject;

/**
* CSS minifier test case.
*/
class CSSTest extends PHPUnit_Framework_TestCase
class CSSTest extends TestCase
{
/**
* @var Minify\CSS
*/
private $minifier;

/**
* Prepares the environment before running a test.
*/
protected function setUp()
protected function mockMinifier()
{
parent::setUp();

// override save method, there's no point in writing the result out here
$this->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'));
}

/**
Expand All @@ -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}',
);

Expand Down
59 changes: 18 additions & 41 deletions tests/js/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

namespace MatthiasMullie\Minify\Test;

use MatthiasMullie\Minify;
use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
use MatthiasMullie\Scrapbook\Psr6\Pool;
use PHPUnit\Framework\TestCase;
use ReflectionObject;

/**
* Tests common functions of abstract Minify class by using JS implementation.
*/
class AbstractTest extends PHPUnit_Framework_TestCase
class AbstractTest extends TestCase
{
/**
* @test
*/
public function construct()
public function testConstruct()
{
$path1 = __DIR__.'/sample/source/script1.js';
$path2 = __DIR__.'/sample/source/script2.js';
Expand Down Expand Up @@ -44,10 +45,7 @@ public function construct()
$this->assertEquals($content1.';'.$content2, $result);
}

/**
* @test
*/
public function add()
public function testAdd()
{
$path1 = __DIR__.'/sample/source/script1.js';
$path2 = __DIR__.'/sample/source/script2.js';
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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 = '';

Expand All @@ -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 = '';

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 45fd3b0

Please sign in to comment.