Skip to content

Commit

Permalink
Merge pull request #5 from ishanvyas22/tests
Browse files Browse the repository at this point in the history
Tests added
  • Loading branch information
ishanvyas22 authored Nov 1, 2019
2 parents 2baa9fb + 191b57b commit e39705b
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 46 deletions.
17 changes: 15 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
"name": "ishanvyas22/asset-mix",
"description": "Asset Mix plugin for CakePHP",
"type": "cakephp-plugin",
"keywords": ["cakephp", "cakephp-plugin", "assets", "laravel-mix", "minifier", "less", "javascript", "sass"],
"license": "MIT",
"homepage": "https://github.com/ishanvyas22/asset-mix",
"authors": [
{
"name": "Ishan Vyas",
"homepage": "https://github.com/ishanvyas22",
"role": "Author"
}
],
"support": {
"issues": "https://github.com/ishanvyas22/asset-mix/issues",
"source": "https://github.com/ishanvyas22/asset-mix"
},
"require": {
"cakephp/cakephp": "^3.5"
"cakephp/cakephp": ">=3.5.0 <4.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0"
Expand All @@ -18,7 +31,7 @@
"autoload-dev": {
"psr-4": {
"AssetMix\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
"TestApp\\": "tests/test_app/src"
}
},
"scripts": {
Expand Down
32 changes: 23 additions & 9 deletions src/Mix.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
*/
class Mix
{
/**
* Mix manifests array
*
* @var array
*/
private static $manifests = [];

/**
* Get the path to a versioned Mix file.
*
Expand All @@ -19,9 +26,6 @@ class Mix
*/
public function __invoke($path, $manifestDirectory = '')
{
static $manifests = [];
$publicPath = env('DOCUMENT_ROOT');

if (! starts_with($path, '/')) {
$path = "/{$path}";
}
Expand All @@ -30,29 +34,39 @@ public function __invoke($path, $manifestDirectory = '')
$manifestDirectory = "/{$manifestDirectory}";
}

if (file_exists($publicPath . $manifestDirectory . '/hot')) {
$url = rtrim(file_get_contents($publicPath . $manifestDirectory . '/hot'));
if (file_exists(WWW_ROOT . $manifestDirectory . '/hot')) {
$url = rtrim(file_get_contents(WWW_ROOT . $manifestDirectory . '/hot'));
if (starts_with($url, ['http://', 'https://'])) {
return str_after($url, ':') . $path;
}

return "//localhost:8765{$path}";
}

$manifestPath = $publicPath . $manifestDirectory . '/mix-manifest.json';
if (! isset($manifests[$manifestPath])) {
$manifestPath = WWW_ROOT . $manifestDirectory . '/mix-manifest.json';
if (! isset(self::$manifests[$manifestPath])) {
if (! file_exists($manifestPath)) {
throw new Exception('The Mix manifest does not exist.');
}

$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
self::$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
}

$manifest = $manifests[$manifestPath];
$manifest = self::$manifests[$manifestPath];
if (! isset($manifest[$path])) {
throw new Exception("Unable to locate AssetMix file: {$path}.");
}

return $manifestDirectory . $manifest[$path];
}

/**
* Reset manifests array
*
* @return void
*/
public static function reset()
{
self::$manifests = [];
}
}
124 changes: 120 additions & 4 deletions tests/TestCase/View/Helper/AssetMixHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace AssetMix\Test\TestCase\View\Helper;

use AssetMix\Mix;
use Cake\View\View;
use Cake\TestSuite\TestCase;
use AssetMix\View\Helper\AssetMixHelper;
Expand All @@ -24,8 +25,6 @@ class AssetMixHelperTest extends TestCase
*/
public function setUp()
{
$this->_compareBasePath = APP . 'tests' . DS . 'comparisons' . DS;

parent::setUp();

$view = new View();
Expand All @@ -42,15 +41,132 @@ public function tearDown()
unset($this->AssetMix);

parent::tearDown();

$this->_cleanUp();

Mix::reset();
}

/**
* Copy `mix-manifest.json` file to `test_app` webroot
*
* @return void
*/
private function _copy($withVersion = false)
{
$sourceFilename = 'mix-manifest.json';
$destinationFilename = 'mix-manifest.json';

if ($withVersion) {
$sourceFilename = 'mix-manifest-with-version.json';
}

if (! copy(COMPARE_PATH . $sourceFilename, WWW_ROOT . $destinationFilename)) {
throw new \Exception('Unable to copy mix-manifest.json file');
}
}

/**
* Copy `mix-manifest.json` file to `test_app` webroot
*
* @return void
*/
protected function _copyWithoutVersion()
{
$this->_copy(false);
}

/**
* Copy `mix-manifest-with-version.json` file to `test_app` webroot
*
* @return void
*/
protected function _copyWithVersion()
{
$this->_copy(true);
}

/**
* Clean webroot directory
*
* @return void
*/
protected function _cleanUp()
{
$files = glob(WWW_ROOT . '*');

foreach ($files as $file) {
if (! is_file($file)) {
continue;
}

unlink($file);
}
}

/**
* Test `css()` function returns proper tag without versioning
*
* @return void
*/
public function testStyleTagWithoutVersion()
{
$this->_copyWithoutVersion();

$result = $this->AssetMix->css('main');

$this->assertContains('<link', $result);
$this->assertContains('rel="stylesheet"', $result);
$this->assertContains('href="/css/main.css"', $result);
}

/**
* Test script function returns proper tag without versioning
* Test `script()` function returns proper tag without versioning
*
* @return void
*/
public function testScriptTagWithoutVersion()
{
$this->AssetMix->css('main');
$this->_copyWithoutVersion();

$result = $this->AssetMix->script('app');

$this->assertContains('<script', $result);
$this->assertContains('/js/app.js', $result);
$this->assertContains('defer="defer"', $result);
}

/**
* Test `css()` function returns proper tag
* with versioning enabled
*
* @return void
*/
public function testStyleTagWithVersion()
{
$this->_copyWithVersion();

$result = $this->AssetMix->css('main');

$this->assertContains('<link', $result);
$this->assertContains('rel="stylesheet"', $result);
$this->assertContains('href="/css/main.css?id=9c4259d5465e35535a2a"', $result);
}

/**
* Test `script()` function returns proper tag
* with versioning enabled
*
* @return void
*/
public function testScriptTagWithVersion()
{
$this->_copyWithVersion();

$result = $this->AssetMix->script('app');

$this->assertContains('<script', $result);
$this->assertContains('/js/app.js?id=f059fcadc7eba26be9ae', $result);
$this->assertContains('defer="defer"', $result);
}
}
91 changes: 60 additions & 31 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
<?php
/**
* Test suite bootstrap for AssetMix.
*
* This function is used to find the location of CakePHP whether CakePHP
* has been installed as a dependency of the plugin, or the plugin is itself
* installed as a dependency of an application.
*/
$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\I18n\I18n;

throw new Exception("Cannot find the root of the application, unable to run tests");
};
$root = $findRoot(__FILE__);
unset($findRoot);
require_once 'vendor/autoload.php';

chdir($root);
// Path constants to a few helpful things.
define('ROOT', dirname(__DIR__) . DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
define('CORE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS);
define('CAKE', CORE_PATH . 'src' . DS);
define('TESTS', ROOT . 'tests');
define('APP', ROOT . 'tests' . DS . 'test_app' . DS);
define('COMPARE_PATH', ROOT . 'tests' . DS . 'test_files' . DS);
define('APP_DIR', 'test_app');
define('WEBROOT_DIR', 'webroot');
define('TMP', sys_get_temp_dir() . DS);
define('CONFIG', APP . 'config' . DS);
define('WWW_ROOT', APP . 'webroot' . DS);
define('CACHE', TMP);
define('LOGS', TMP);

require_once $root . '/vendor/autoload.php';
require_once CORE_PATH . 'config/bootstrap.php';

/**
* Define fallback values for required constants and configuration.
* To customize constants and configuration remove this require
* and define the data required by your plugin here.
*/
require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8');

if (file_exists($root . '/config/bootstrap.php')) {
require $root . '/config/bootstrap.php';
Configure::write('debug', true);
Configure::write('App', [
'namespace' => 'TestApp',
'encoding' => 'UTF-8',
'base' => false,
'baseUrl' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
'fullBaseUrl' => 'http://localhost',
'imageBaseUrl' => 'img/',
'jsBaseUrl' => 'js/',
'cssBaseUrl' => 'css/',
'paths' => [
'plugins' => [APP . 'plugins' . DS],
'templates' => [APP . 'src' . DS . 'Template' . DS]
]
]);

return;
}
Cache::setConfig([
'_cake_core_' => [
'engine' => 'File',
'prefix' => 'cake_core_',
'serialize' => true
],
'_cake_model_' => [
'engine' => 'File',
'prefix' => 'cake_model_',
'serialize' => true
],
'default' => [
'engine' => 'File',
'prefix' => 'default_',
'serialize' => true
],
]);

Plugin::getCollection()->add(new \AssetMix\Plugin());
4 changes: 4 additions & 0 deletions tests/test_files/mix-manifest-with-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/js/app.js": "/js/app.js?id=f059fcadc7eba26be9ae",
"/css/main.css": "/css/main.css?id=9c4259d5465e35535a2a"
}
4 changes: 4 additions & 0 deletions tests/test_files/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/js/app.js": "/js/app.js",
"/css/main.css": "/css/main.css"
}

0 comments on commit e39705b

Please sign in to comment.