Skip to content

Commit 45fd3b0

Browse files
Make test suite pass for all PHP versions
1 parent b88dcc6 commit 45fd3b0

File tree

9 files changed

+94
-140
lines changed

9 files changed

+94
-140
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,3 @@ after_success:
3131

3232
after_script:
3333
- make down PHP=$(phpenv version-name)
34-
35-
matrix:
36-
allow_failures:
37-
- php: 7.4
38-
- php: 8.0

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ COPY . /var/www
55
WORKDIR /var/www
66

77
RUN apt-get update
8-
RUN apt-get install -y zip unzip zlib1g-dev
9-
RUN if [[ `php-config --vernum` -ge 73000 ]]; then docker-php-ext-install zip; fi
8+
RUN apt-get install -y zip unzip libzip-dev git
9+
RUN docker-php-ext-install zip
1010
RUN docker-php-ext-install pcntl
1111
RUN curl -sS https://getcomposer.org/installer | php
1212
RUN mv composer.phar /usr/local/bin/composer

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"matthiasmullie/path-converter": "~1.1"
2020
},
2121
"require-dev": {
22-
"matthiasmullie/scrapbook": "~1.0",
23-
"phpunit/phpunit": "~4.8",
22+
"matthiasmullie/scrapbook": "dev-master",
23+
"phpunit/phpunit": ">=4.8",
2424
"friendsofphp/php-cs-fixer": "~2.0"
2525
},
2626
"suggest": {

src/JS.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ protected function extractRegex()
254254
// of the RegExp methods (a `\` followed by a variable or value is
255255
// likely part of a division, not a regex)
256256
$keywords = array('do', 'in', 'new', 'else', 'throw', 'yield', 'delete', 'return', 'typeof');
257-
$before = '([=:,;\+\-\*\/\}\(\{\[&\|!]|^|'.implode('|', $keywords).')\s*';
257+
$before = '(^|[=:,;\+\-\*\/\}\(\{\[&\|!]|'.implode('|', $keywords).')\s*';
258258
$propertiesAndMethods = array(
259259
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Properties_2
260260
'constructor',

src/Minify.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function add($data /* $data = null, ... */)
105105
* @param string|string[] $data
106106
*
107107
* @return static
108-
*
108+
*
109109
* @throws IOException
110110
*/
111111
public function addFile($data /* $data = null, ... */)
@@ -472,7 +472,7 @@ protected function canImportFile($path)
472472
*/
473473
protected function openFileForWriting($path)
474474
{
475-
if (($handler = @fopen($path, 'w')) === false) {
475+
if ($path === '' || ($handler = @fopen($path, 'w')) === false) {
476476
throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.');
477477
}
478478

@@ -490,7 +490,11 @@ protected function openFileForWriting($path)
490490
*/
491491
protected function writeToFile($handler, $content, $path = '')
492492
{
493-
if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) {
493+
if (
494+
!is_resource($handler) ||
495+
($result = @fwrite($handler, $content)) === false ||
496+
($result < strlen($content))
497+
) {
494498
throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.');
495499
}
496500
}

tests/bootstrap.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
<?php
22

3-
require __DIR__.'/../vendor/autoload.php';
3+
namespace {
4+
require __DIR__.'/../vendor/autoload.php';
5+
}
6+
7+
namespace PHPUnit\Framework
8+
{
9+
// compatibility for when these tests are run with PHPUnit<6.0 (which we
10+
// still do because PHPUnit=6.0 stopped supporting a lot of PHP versions)
11+
if (!class_exists('PHPUnit\Framework\TestCase')) {
12+
abstract class TestCase extends \PHPUnit_Framework_TestCase
13+
{
14+
}
15+
}
16+
}

tests/css/CSSTest.php

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,100 @@
11
<?php
22

3-
use MatthiasMullie\Minify;
3+
namespace MatthiasMullie\Minify\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ReflectionObject;
47

58
/**
69
* CSS minifier test case.
710
*/
8-
class CSSTest extends PHPUnit_Framework_TestCase
11+
class CSSTest extends TestCase
912
{
10-
/**
11-
* @var Minify\CSS
12-
*/
13-
private $minifier;
14-
15-
/**
16-
* Prepares the environment before running a test.
17-
*/
18-
protected function setUp()
13+
protected function mockMinifier()
1914
{
20-
parent::setUp();
21-
2215
// override save method, there's no point in writing the result out here
23-
$this->minifier = $this->getMockBuilder('\MatthiasMullie\Minify\CSS')
16+
return $this->getMockBuilder('\MatthiasMullie\Minify\CSS')
2417
->setMethods(array('save'))
2518
->getMock();
2619
}
2720

28-
/**
29-
* Cleans up the environment after running a test.
30-
*/
31-
protected function tearDown()
32-
{
33-
$this->minifier = null;
34-
parent::tearDown();
35-
}
36-
3721
/**
3822
* Test CSS minifier rules, provided by dataProvider.
3923
*
40-
* @test
4124
* @dataProvider dataProvider
4225
*/
43-
public function minify($input, $expected)
26+
public function testMinify($input, $expected)
4427
{
45-
$this->minifier->add($input);
46-
$result = $this->minifier->minify();
28+
$minifier = $this->mockMinifier();
29+
$minifier->add($input);
30+
$result = $minifier->minify();
4731
$this->assertEquals($expected, $result);
4832
}
4933

5034
/**
5135
* Test conversion of relative paths, provided by dataProviderPaths.
5236
*
53-
* @test
5437
* @dataProvider dataProviderPaths
5538
*/
56-
public function convertRelativePath($source, $target, $expected)
39+
public function testConvertRelativePath($source, $target, $expected)
5740
{
41+
$minifier = $this->mockMinifier();
5842
$source = (array) $source;
5943
foreach ($source as $path => $css) {
60-
$this->minifier->add($css);
44+
$minifier->add($css);
6145

6246
// $source also accepts an array where the key is a bogus path
6347
if (is_string($path)) {
64-
$object = new ReflectionObject($this->minifier);
48+
$object = new ReflectionObject($minifier);
6549
$property = $object->getProperty('data');
6650
$property->setAccessible(true);
67-
$data = $property->getValue($this->minifier);
51+
$data = $property->getValue($minifier);
6852

6953
// keep content, but make it appear from the given path
7054
$data[$path] = array_pop($data);
71-
$property->setValue($this->minifier, $data);
55+
$property->setValue($minifier, $data);
7256
$property->setAccessible(false);
7357
}
7458
}
7559

76-
$result = $this->minifier->minify($target);
60+
$result = $minifier->minify($target);
7761

7862
$this->assertEquals($expected, $result);
7963
}
8064

8165
/**
8266
* Test loop while importing file.
83-
*
84-
* @test
85-
*
86-
* @expectedException MatthiasMullie\Minify\Exceptions\FileImportException
8767
*/
88-
public function fileImportLoop()
68+
public function testFileImportLoop()
8969
{
70+
$this->expectException('MatthiasMullie\Minify\Exceptions\FileImportException');
71+
9072
$testFile = __DIR__.'/sample/loop/first.css';
9173

92-
$this->minifier->add($testFile);
74+
$minifier = $this->mockMinifier();
75+
$minifier->add($testFile);
9376

94-
$this->minifier->minify();
77+
$minifier->minify();
9578
}
9679

9780
/**
9881
* Test minifier import configuration methods.
99-
*
100-
* @test
10182
*/
102-
public function setConfig()
83+
public function testSetConfig()
10384
{
104-
$this->minifier->setMaxImportSize(10);
105-
$this->minifier->setImportExtensions(array('gif' => 'data:image/gif'));
85+
$minifier = $this->mockMinifier();
86+
$minifier->setMaxImportSize(10);
87+
$minifier->setImportExtensions(array('gif' => 'data:image/gif'));
10688

107-
$object = new ReflectionObject($this->minifier);
89+
$object = new ReflectionObject($minifier);
10890

10991
$property = $object->getProperty('maxImportSize');
11092
$property->setAccessible(true);
111-
$this->assertEquals($property->getValue($this->minifier), 10);
93+
$this->assertEquals($property->getValue($minifier), 10);
11294

11395
$property = $object->getProperty('importExtensions');
11496
$property->setAccessible(true);
115-
$this->assertEquals($property->getValue($this->minifier), array('gif' => 'data:image/gif'));
97+
$this->assertEquals($property->getValue($minifier), array('gif' => 'data:image/gif'));
11698
}
11799

118100
/**
@@ -124,11 +106,11 @@ public function dataProvider()
124106

125107
// passing in an array of css inputs
126108
$tests[] = array(
127-
[
109+
array(
128110
__DIR__.'/sample/combine_imports/index.css',
129111
__DIR__.'/sample/bom/bom.css',
130112
'p { width: 55px , margin: 0 0 0 0}',
131-
],
113+
),
132114
'body{color:red}body{color:red}p{width:55px,margin:0 0 0 0}',
133115
);
134116

tests/js/AbstractTest.php

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22

3+
namespace MatthiasMullie\Minify\Test;
4+
35
use MatthiasMullie\Minify;
46
use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
57
use MatthiasMullie\Scrapbook\Psr6\Pool;
8+
use PHPUnit\Framework\TestCase;
9+
use ReflectionObject;
610

711
/**
812
* Tests common functions of abstract Minify class by using JS implementation.
913
*/
10-
class AbstractTest extends PHPUnit_Framework_TestCase
14+
class AbstractTest extends TestCase
1115
{
12-
/**
13-
* @test
14-
*/
15-
public function construct()
16+
public function testConstruct()
1617
{
1718
$path1 = __DIR__.'/sample/source/script1.js';
1819
$path2 = __DIR__.'/sample/source/script2.js';
@@ -44,10 +45,7 @@ public function construct()
4445
$this->assertEquals($content1.';'.$content2, $result);
4546
}
4647

47-
/**
48-
* @test
49-
*/
50-
public function add()
48+
public function testAdd()
5149
{
5250
$path1 = __DIR__.'/sample/source/script1.js';
5351
$path2 = __DIR__.'/sample/source/script2.js';
@@ -106,10 +104,7 @@ public function add()
106104
$this->assertEquals($content1.';'.$content2.';'.$content3, $result);
107105
}
108106

109-
/**
110-
* @test
111-
*/
112-
public function loadBigString()
107+
public function testLoadBigString()
113108
{
114109
// content greater than PHP_MAXPATHLEN
115110
// https://github.com/matthiasmullie/minify/issues/90
@@ -120,10 +115,7 @@ public function loadBigString()
120115
$this->assertEquals($minifier->minify(), $content);
121116
}
122117

123-
/**
124-
* @test
125-
*/
126-
public function loadOpenBaseDirRestricted()
118+
public function testLoadOpenBaseDirRestricted()
127119
{
128120
if (!function_exists('pcntl_fork') || defined('HHVM_VERSION')) {
129121
$this->markTestSkipped("Can't fork, skip open_basedir test");
@@ -166,10 +158,7 @@ public function loadOpenBaseDirRestricted()
166158
}
167159
}
168160

169-
/**
170-
* @test
171-
*/
172-
public function save()
161+
public function testSave()
173162
{
174163
$path = __DIR__.'/sample/source/script1.js';
175164
$content = file_get_contents($path);
@@ -181,13 +170,10 @@ public function save()
181170
$this->assertEquals(file_get_contents($savePath), $content);
182171
}
183172

184-
/**
185-
* @test
186-
*
187-
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
188-
*/
189-
public function checkFileOpenFail()
173+
public function testCheckFileOpenFail()
190174
{
175+
$this->expectException('MatthiasMullie\Minify\Exceptions\IOException');
176+
191177
$minifier = new Minify\JS();
192178
$wrongPath = '';
193179

@@ -198,13 +184,10 @@ public function checkFileOpenFail()
198184
$method->invokeArgs($minifier, array($wrongPath));
199185
}
200186

201-
/**
202-
* @test
203-
*
204-
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
205-
*/
206-
public function checkFileWriteFail()
187+
public function testCheckFileWriteFail()
207188
{
189+
$this->expectException('MatthiasMullie\Minify\Exceptions\IOException');
190+
208191
$minifier = new Minify\JS();
209192
$wrongPath = '';
210193

@@ -215,10 +198,7 @@ public function checkFileWriteFail()
215198
$method->invokeArgs($minifier, array($wrongPath, ''));
216199
}
217200

218-
/**
219-
* @test
220-
*/
221-
public function gzip()
201+
public function testGzip()
222202
{
223203
$path = __DIR__.'/sample/source/script1.js';
224204
$content = file_get_contents($path);
@@ -230,10 +210,7 @@ public function gzip()
230210
$this->assertEquals(file_get_contents($savePath), gzencode($content, 9, FORCE_GZIP));
231211
}
232212

233-
/**
234-
* @test
235-
*/
236-
public function cache()
213+
public function testCache()
237214
{
238215
$path = __DIR__.'/sample/source/script1.js';
239216
$content = file_get_contents($path);

0 commit comments

Comments
 (0)