2014-05-14 13:24:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Cms\Classes\Theme;
|
2014-10-15 08:53:44 +00:00
|
|
|
use System\Classes\CombineAssets;
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
class CombineAssetsTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
2015-02-10 06:45:07 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
CombineAssets::resetCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Helpers
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
protected static function callProtectedMethod($object, $name, $params = [])
|
|
|
|
|
{
|
|
|
|
|
$className = get_class($object);
|
|
|
|
|
$class = new ReflectionClass($className);
|
|
|
|
|
$method = $class->getMethod($name);
|
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
return $method->invokeArgs($object, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getProtectedProperty($object, $name)
|
|
|
|
|
{
|
|
|
|
|
$className = get_class($object);
|
|
|
|
|
$class = new ReflectionClass($className);
|
|
|
|
|
$property = $class->getProperty($name);
|
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
return $property->getValue($object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function setProtectedProperty($object, $name, $value)
|
|
|
|
|
{
|
|
|
|
|
$className = get_class($object);
|
|
|
|
|
$class = new ReflectionClass($className);
|
|
|
|
|
$property = $class->getProperty($name);
|
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
return $property->setValue($object, $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Tests
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
public function testCombiner()
|
|
|
|
|
{
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Supported file extensions should exist
|
|
|
|
|
*/
|
|
|
|
|
$jsExt = $cssExt = self::getProtectedProperty($combiner, 'jsExtensions');
|
|
|
|
|
$this->assertInternalType('array', $jsExt);
|
|
|
|
|
|
|
|
|
|
$cssExt = self::getProtectedProperty($combiner, 'cssExtensions');
|
|
|
|
|
$this->assertInternalType('array', $cssExt);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check service methods
|
|
|
|
|
*/
|
|
|
|
|
$this->assertTrue(method_exists($combiner, 'combine'));
|
|
|
|
|
$this->assertTrue(method_exists($combiner, 'resetCache'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCombine()
|
|
|
|
|
{
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2014-05-14 13:24:20 +00:00
|
|
|
|
2015-03-02 08:01:30 +00:00
|
|
|
$url = $combiner->combine([
|
|
|
|
|
'assets/css/style1.css',
|
|
|
|
|
'assets/css/style2.css'
|
|
|
|
|
],
|
|
|
|
|
base_path().'/tests/fixtures/themes/test'
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotNull($url);
|
|
|
|
|
$this->assertRegExp('/\w+[-]\d+/i', $url); // Must contain hash-number
|
|
|
|
|
|
|
|
|
|
$url = $combiner->combine([
|
|
|
|
|
'assets/js/script1.js',
|
|
|
|
|
'assets/js/script2.js'
|
|
|
|
|
],
|
|
|
|
|
base_path().'/tests/fixtures/themes/test'
|
|
|
|
|
);
|
2014-05-14 13:24:20 +00:00
|
|
|
$this->assertNotNull($url);
|
2015-03-02 08:01:30 +00:00
|
|
|
$this->assertRegExp('/\w+[-]\d+/i', $url); // Must contain hash-number
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPutCache()
|
|
|
|
|
{
|
|
|
|
|
$sampleId = md5('testhash');
|
|
|
|
|
$sampleStore = ['version' => 12345678];
|
|
|
|
|
$samplePath = '/tests/fixtures/Cms/themes/test';
|
|
|
|
|
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2014-05-14 13:24:20 +00:00
|
|
|
$value = self::callProtectedMethod($combiner, 'putCache', [$sampleId, $sampleStore]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 10:37:55 +00:00
|
|
|
public function testGetTargetPath()
|
|
|
|
|
{
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2014-06-26 10:37:55 +00:00
|
|
|
|
|
|
|
|
$value = self::callProtectedMethod($combiner, 'getTargetPath', ['/combine']);
|
|
|
|
|
$this->assertEquals('combine/', $value);
|
|
|
|
|
|
|
|
|
|
$value = self::callProtectedMethod($combiner, 'getTargetPath', ['/index.php/combine']);
|
|
|
|
|
$this->assertEquals('index-php/combine/', $value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
public function testMakeCacheId()
|
|
|
|
|
{
|
|
|
|
|
$sampleResources = ['assets/css/style1.css', 'assets/css/style2.css'];
|
2015-03-02 08:01:30 +00:00
|
|
|
$samplePath = base_path().'/tests/fixtures/cms/themes/test';
|
2014-05-14 13:24:20 +00:00
|
|
|
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2015-03-02 08:01:30 +00:00
|
|
|
self::setProtectedProperty($combiner, 'localPath', $samplePath);
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
$value = self::callProtectedMethod($combiner, 'makeCacheId', [$sampleResources]);
|
|
|
|
|
$this->assertEquals(md5($samplePath.implode('|', $sampleResources)), $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testResetCache()
|
|
|
|
|
{
|
2015-01-12 09:08:31 +00:00
|
|
|
$combiner = CombineAssets::instance();
|
2014-05-14 13:24:20 +00:00
|
|
|
$this->assertNull($combiner->resetCache());
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-15 21:36:05 +00:00
|
|
|
}
|