Skip to content

Commit

Permalink
tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Jan 1, 2021
1 parent 2c73bb0 commit 246e199
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
37 changes: 25 additions & 12 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@ name: PHP Tests

on:
push:
branches:
branches:
- master

jobs:
build:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-versions: ['7.2', '7.3', '7.4']

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2

- name: setup PHP
uses: shivammathur/setup-php@master
with:
php-version: '7.2'
extension-csv: intl, json, mbstring, mysqlnd. xdebug, xml
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate
- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
run: composer install --ansi --no-interaction

- name: Run test suite
run: composer run-script test
- name: Test with PHPUnit
run: script -e -c "vendor/bin/phpunit -v"
10 changes: 3 additions & 7 deletions src/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ public function load(string $filename)
throw MinifierException::forWrongFileExtension($ext);
}

$versions = $this->getVersion($this->config->dirVersion);

if (empty($versions))
{
throw MinifierException::forNoVersioningFile();
}

if (! in_array($this->config->returnType, ['html', 'array', 'json']))
{
throw MinifierException::forWrongReturnType($this->config->returnType);
Expand All @@ -84,6 +77,9 @@ public function load(string $filename)
$this->autoDeployCheck($filename, $ext);
}

// load versions
$versions = $this->getVersion($this->config->dirVersion);

$filenames = [];

// do we use combined+minified+versioned assets?
Expand Down
41 changes: 29 additions & 12 deletions tests/unit/MinifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class MinifierTest extends \CodeIgniter\Test\CIUnitTestCase

protected $minifier;

protected $ver = [
'js' => '9ef881911da8d7c4a1c2f19c4878d122',
'css' => '95cb11cf55b3f1164e80ae9393644ae3'
];

public function setUp(): void
{
parent::setUp();
Expand All @@ -22,6 +27,15 @@ public function setUp(): void
$this->config->js = ['all.min.js' => ['bootstrap.js', 'jquery-3.4.1.js', 'main.js']];
$this->config->css = ['all.min.css' => ['bootstrap.css', 'font-awesome.css', 'main.css']];

if (file_exists($this->config->dirJs . '/new.js'))
{
unlink($this->config->dirJs . '/new.js');
}

if (file_exists($this->config->dirCss . '/new.css'))
{
unlink($this->config->dirCss . '/new.css');
}

/*
if (file_exists($this->config->dirJs . '/all.min.js'))
Expand Down Expand Up @@ -120,7 +134,7 @@ public function testLoadJs()

$result = $this->minifier->load('all.min.js');

$this->assertEquals('<script type="text/javascript" src="http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=9ef881911da8d7c4a1c2f19c4878d122"></script>' . PHP_EOL, $result);
$this->assertEquals('<script type="text/javascript" src="http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=' . $this->ver['js'] . '"></script>' . PHP_EOL, $result);
}

public function testLoadCss()
Expand All @@ -129,7 +143,8 @@ public function testLoadCss()

$result = $this->minifier->load('all.min.css');

$this->assertEquals('<link rel="stylesheet" href="http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=50a35b0b1d1c3798aa556b8245314930">' . PHP_EOL, $result);
$this->assertEquals('<link rel="stylesheet" href="http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);

}

public function testLoadJsWithBaseJsUrl()
Expand All @@ -140,7 +155,7 @@ public function testLoadJsWithBaseJsUrl()

$result = $this->minifier->load('all.min.js');

$this->assertEquals('<script type="text/javascript" src="http://js.localhost/all.min.js?v=9ef881911da8d7c4a1c2f19c4878d122"></script>' . PHP_EOL, $result);
$this->assertEquals('<script type="text/javascript" src="http://js.localhost/all.min.js?v=' . $this->ver['js'] . '"></script>' . PHP_EOL, $result);
}

public function testLoadCssWithBaseCssUrl()
Expand All @@ -151,7 +166,7 @@ public function testLoadCssWithBaseCssUrl()

$result = $this->minifier->load('all.min.css');

$this->assertEquals('<link rel="stylesheet" href="http://css.localhost/all.min.css?v=50a35b0b1d1c3798aa556b8245314930">' . PHP_EOL, $result);
$this->assertEquals('<link rel="stylesheet" href="http://css.localhost/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);
}

public function testJsonReturnTypeWithLoadJs()
Expand All @@ -161,7 +176,7 @@ public function testJsonReturnTypeWithLoadJs()

$result = $this->minifier->load('all.min.js');

$this->assertEquals(json_encode(['http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=9ef881911da8d7c4a1c2f19c4878d122']), $result);
$this->assertEquals(json_encode(['http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=' . $this->ver['js']]), $result);
}

public function testJsonReturnTypeWithLoadCss()
Expand All @@ -171,7 +186,7 @@ public function testJsonReturnTypeWithLoadCss()

$result = $this->minifier->load('all.min.css');

$this->assertEquals(json_encode(['http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=50a35b0b1d1c3798aa556b8245314930']), $result);
$this->assertEquals(json_encode(['http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=' . $this->ver['css']]), $result);
}

public function testArrayReturnTypeWithLoadJs()
Expand All @@ -181,7 +196,7 @@ public function testArrayReturnTypeWithLoadJs()

$result = $this->minifier->load('all.min.js');

$this->assertEquals(['http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=9ef881911da8d7c4a1c2f19c4878d122'], $result);
$this->assertEquals(['http://localhost' . SUPPORTPATH . 'assets/js/all.min.js?v=' . $this->ver['js']], $result);
}

public function testArrayReturnTypeWithLoadCss()
Expand All @@ -191,7 +206,7 @@ public function testArrayReturnTypeWithLoadCss()

$result = $this->minifier->load('all.min.css');

$this->assertEquals(['http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=50a35b0b1d1c3798aa556b8245314930'], $result);
$this->assertEquals(['http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=' . $this->ver['css']], $result);
}

public function testLoadExceptionForWrongReturnType()
Expand Down Expand Up @@ -222,11 +237,12 @@ public function testAutoDeployOnChangeJsTrue()
$this->config->js = ['all.min.js' => ['bootstrap.js', 'jquery-3.4.1.js', 'main.js', 'new.js']];
$this->minifier = new Minifier($this->config);

sleep(1);
file_put_contents($this->config->dirJs . '/new.js', '//data;');

$method = $this->getPrivateMethodInvoker($this->minifier, 'autoDeployCheckJs');

file_put_contents($this->config->dirJs . '/new.js', '//data;');
$this->assertTrue($method('all.min.js'));
unlink($this->config->dirJs . '/new.js');
}

public function testAutoDeployOnChangeCssFalse()
Expand All @@ -246,11 +262,12 @@ public function testAutoDeployOnChangeCssTrue()
$this->config->css = ['all.min.css' => ['bootstrap.css', 'font-awesome.css', 'main.css', 'new.css']];
$this->minifier = new Minifier($this->config);

sleep(1);
file_put_contents($this->config->dirCss . '/new.css', '//data;');

$method = $this->getPrivateMethodInvoker($this->minifier, 'autoDeployCheckCss');

file_put_contents($this->config->dirCss . '/new.css', '//data;');
$this->assertTrue($method('all.min.css'));
unlink($this->config->dirCss . '/new.css');
}

}

0 comments on commit 246e199

Please sign in to comment.