Skip to content

Commit

Permalink
fix: test phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Apr 3, 2023
1 parent cf440db commit 2487a27
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 76 deletions.
44 changes: 26 additions & 18 deletions src/Minifyku.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Minifyku
*/
protected string $error = '';

/**
* Versioning
*/
protected array $versioning = [];

/**
* Prepare config to use
*/
Expand All @@ -53,7 +58,7 @@ public function __construct(MinifykuConfig $config)
public function load(string $filename)
{
// determine file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

if (! in_array($ext, ['js', 'css'], true)) {
throw MinifykuException::forWrongFileExtension($ext);
Expand Down Expand Up @@ -219,20 +224,17 @@ protected function prepareOutput(array $filenames, string $dir = '', string $tag
*/
public function getVersion(string $dir): array
{
static $versions = null;

// load all versions numbers
if ($versions === null) {
$dir = rtrim($dir, '/');
if ($this->versioning !== []) {
return $this->versioning;
}

if (! is_file($dir . '/versions.json')) {
throw MinifykuException::forNoVersioningFile(); // @codeCoverageIgnore
}
$dir = rtrim($dir, '/');

$versions = (array) json_decode(file_get_contents($dir . '/versions.json'), true);
if (! is_file($dir . '/versions.json')) {
throw MinifykuException::forNoVersioningFile(); // @codeCoverageIgnore
}

return $versions;
return (array) json_decode(file_get_contents($dir . '/versions.json'), true);
}

/**
Expand All @@ -242,21 +244,27 @@ public function getVersion(string $dir): array
* @param array $files Files
* @param string $dir Directory
*/
protected function setVersion(string $mode, array $files, string $dir)
protected function setVersion(string $mode, array $files, string $dir): array
{
$dir = rtrim($dir, '/');

if (is_file($dir . '/versions.json')) {
$versions = json_decode(file_get_contents($dir . '/versions.json'), true);
}

if ($mode === 'all') {
$versions = $files;
$this->versioning = $files;

// Rewrite Versioning File
file_put_contents($dir . '/versions.json', json_encode($this->versioning));

return $this->versioning;
}

$versions[$mode] = $files;

return file_put_contents($dir . '/versions.json', json_encode($versions));
// Rewrite Versioning File
file_put_contents($dir . '/versions.json', json_encode($versions));

$this->versioning = $versions;

return $this->versioning;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/_support/assets/css/main.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
body {
background-color: #000;
color: #fff;
}

.hello-minifyku {
text-align: center;
color: crimson;
}
2 changes: 2 additions & 0 deletions tests/_support/assets/js/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$(function() {
console.log('I am Minifyku!');

alert('I am Minifyku!');
});
2 changes: 1 addition & 1 deletion tests/_support/public/css/all.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/_support/public/js/all.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 72 additions & 56 deletions tests/unit/MinifykuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class MinifykuTest extends CIUnitTestCase
private MinifykuConfig $config;
private Minifyku $minifyku;
private array $ver = [
'js' => 'bc3d0dc779f1a0b521b69ed3a2b85de8',
'css' => 'ec8d57dd8de143d7ef822a90fca20957',
'js' => '0561b3110b4682b4c0a67ea9741be28d',
'css' => '621c512df406dc8d923a3fa756087d9d',
];

protected function setUp(): void
Expand Down Expand Up @@ -127,6 +127,7 @@ public function testLoadJs()
{
$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<script defer type="text/javascript"', $result);
Expand All @@ -137,17 +138,77 @@ public function testLoadCss()
{
$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<link rel="stylesheet"', $result);
$this->assertStringContainsString('assets/css/all.min.css?v=' . $this->ver['css'], $result);
}

public function testDeployJsWithDirMinJs()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('js');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js));
}

public function testDeployCssWithDirMinCss()
{
if (file_exists($this->config->dirMinCss . '/all.min.css')) {
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('css');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css));
}

public function testDeployAllWithDirMinJsAndCss()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

if (file_exists($this->config->dirMinCss . '/all.min.css')) {
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('all');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js));
$this->assertFileExists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css));
}

public function testLoadJsWithDirMinJs()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<script defer type="text/javascript"', $result);
Expand All @@ -156,9 +217,14 @@ public function testLoadJsWithDirMinJs()

public function testLoadCssWithDirMinCss()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<link rel="stylesheet"', $result);
Expand All @@ -171,6 +237,7 @@ public function testLoadJsWithBaseJsUrl()

$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<script defer type="text/javascript"', $result);
Expand All @@ -183,6 +250,7 @@ public function testLoadCssWithBaseCssUrl()

$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<link rel="stylesheet"', $result);
Expand All @@ -196,6 +264,7 @@ public function testLoadJsWithBaseJsUrlAndDirMinJs()

$this->minifyku = new Minifyku($this->config);

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

$this->assertStringContainsString('<script defer type="text/javascript"', $result);
Expand All @@ -209,63 +278,10 @@ public function testLoadCssWithBaseCssUrlAndDirMinCss()

$this->minifyku = new Minifyku($this->config);

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

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

public function testDeployJsWithDirMinJs()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('js');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js));
}

public function testDeployCssWithDirMinCss()
{
if (file_exists($this->config->dirMinCss . '/all.min.css')) {
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('css');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css));
}

public function testDeployAllWithDirMinJsAndCss()
{
if (file_exists($this->config->dirMinJs . '/all.min.js')) {
unlink($this->config->dirMinJs . '/all.min.js');
}

if (file_exists($this->config->dirMinCss . '/all.min.css')) {
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifyku = new Minifyku($this->config);

$result = $this->minifyku->deploy('all');

$this->assertTrue($result);

$this->assertFileExists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js));
$this->assertFileExists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css));
}
}

0 comments on commit 2487a27

Please sign in to comment.