Permalink
Browse files

Adding unit test for AssetPipline. Closes #428

  • Loading branch information...
1 parent a421a53 commit 91003a963bf8e8b1a238c4b93889fc99e3e934d8 @jmathai jmathai committed Jan 6, 2012
Showing with 153 additions and 16 deletions.
  1. +16 −16 src/libraries/models/AssetPipeline.php
  2. +137 −0 src/tests/libraries/models/AssetPipelineTest.php
@@ -13,7 +13,7 @@ class AssetPipeline
const js = 'js';
const minified = 'm';
const combined = 'c';
- private $assets, $assetsRel, $docroot, $cacheDir, $mode;
+ protected $assets, $assetsRel, $docroot, $cacheDir, $mode;
public function __construct()
{
@@ -28,6 +28,21 @@ public function __construct()
$this->mode = self::combined;
}
+ public function addCss($src)
+ {
+ if(file_exists($path = sprintf('%s%s', $this->docroot, $src)))
+ $this->addAsset($path, 'css');
+ return $this;
+ }
+
+ public function addJs($src)
+ {
+ if(file_exists($path = sprintf('%s%s', $this->docroot, $src)))
+ $this->addAsset($path, 'js');
+
+ return $this;
+ }
+
public function getCombined($type)
{
$retval = '';
@@ -66,21 +81,6 @@ public function getUrl($type, $version = 'a')
return $url;
}
- public function addCss($src)
- {
- if(file_exists($path = sprintf('%s%s', $this->docroot, $src)))
- $this->addAsset($path, 'css');
- return $this;
- }
-
- public function addJs($src)
- {
- if(file_exists($path = sprintf('%s%s', $this->docroot, $src)))
- $this->addAsset($path, 'js');
-
- return $this;
- }
-
private function addAsset($src, $type)
{
// verify this file exists
@@ -0,0 +1,137 @@
+<?php
+$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
+foreach($paths as $path)
+{
+ if(file_exists("{$path}/vfsStream/vfsStream.php"))
+ require_once 'vfsStream/vfsStream.php';
+}
+$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
+require_once sprintf('%s/tests/helpers/init.php', $baseDir);
+require_once sprintf('%s/libraries/models/AssetPipeline.php', $baseDir);
+
+class AssetPipelineOverride extends AssetPipeline
+{
+ public $docroot;
+ public function __construct()
+ {
+ $config = new stdClass;
+ $config->paths = new stdClass;
+ $config->paths->docroot = null;
+ $config->site = new stdClass;
+ $config->site->mode = 'prod';
+
+ if(class_exists('vfsStream'))
+ {
+ vfsStreamWrapper::register();
+ vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));
+ $config->paths->docroot = vfsStream::url('testDir');
+ }
+
+ $this->docroot = $config->paths->docroot;
+ $this->cacheDir = sprintf('%s/assets/cache', $this->docroot);;
+ $this->assets = $this->assetsRel = array('js' => array(), 'css' => array());
+ $siteMode = $config->site->mode;
+ if($siteMode === 'prod')
+ $this->mode = self::minified;
+ else
+ $this->mode = self::combined;
+ }
+}
+
+class AssetPipelineTest extends PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ // to test the write methods
+ $this->assetPipeline = new AssetPipelineOverride;
+ }
+
+ public function testValidateVfsFunctionSuccess()
+ {
+ if(!class_exists('vfsStream'))
+ {
+ $this->fail('The vfsStream package was not found. Skipping tests in FileSysemLocalTest. Install using `sudo pear channel-discover pear.php-tools.net && sudo pear install pat/vfsStream-beta`');
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testAddCssWhenNotExists()
+ {
+ $res = $this->assetPipeline->addCss('/PATH/TO/CSS');
+ $this->assertTrue($res instanceof AssetPipelineOverride, 'addCss should return $this');
+ $assets = $this->assetPipeline->getUrl(AssetPipeline::css);
+ $this->assertTrue(strstr($assets, 'PATH/TO/CSS') === false, 'When file does not exist it should be omitted');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testAddCssWhenExists()
+ {
+ file_put_contents($this->assetPipeline->docroot.'/CSS', 'foobar');
+ $res = $this->assetPipeline->addCss('/CSS');
+ $this->assertTrue($res instanceof AssetPipelineOverride, 'addCss should return $this');
+ $assets = $this->assetPipeline->getUrl(AssetPipeline::css);
+ $this->assertTrue(strstr($assets, 'CSS') !== false, 'When file exists it should NOT be omitted');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testAddJsWhenNotExists()
+ {
+ $res = $this->assetPipeline->addJs('/PATH/TO/JS');
+ $this->assertTrue($res instanceof AssetPipelineOverride, 'addJs should return $this');
+ $assets = $this->assetPipeline->getUrl(AssetPipeline::css);
+ $this->assertTrue(strstr($assets, 'PATH/TO/JS') === false, 'When file does not exist it should be omitted');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testAddJsWhenExists()
+ {
+ file_put_contents($this->assetPipeline->docroot.'/JS', 'foobar');
+ $res = $this->assetPipeline->addJs('/JS');
+ $this->assertTrue($res instanceof AssetPipelineOverride, 'addJs should return $this');
+ $assets = $this->assetPipeline->getUrl(AssetPipeline::js);
+ $this->assertTrue(strstr($assets, 'JS') !== false, 'When file exists it should NOT be omitted');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testGetCombined()
+ {
+ file_put_contents($this->assetPipeline->docroot.'/JS', 'foobar');
+ $this->assetPipeline->addJs('/JS');
+ $res = $this->assetPipeline->getCombined(AssetPipeline::js);
+ $this->assertEquals('foobar', trim($res), 'response of combined should be foobar');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testGetMinified()
+ {
+ file_put_contents($this->assetPipeline->docroot.'/CSS', 'foobar');
+ $this->assetPipeline->addCss('/CSS');
+ $res = $this->assetPipeline->getCombined(AssetPipeline::css);
+ $this->assertEquals('foobar', trim($res), 'response of combined should be foobar');
+ }
+
+ /**
+ * @depends testValidateVfsFunctionSuccess
+ */
+ public function testGetUrl()
+ {
+ file_put_contents($this->assetPipeline->docroot.'/JS', 'foobar');
+ $this->assetPipeline->addJs('/JS');
+ $url = $this->assetPipeline->getUrl(AssetPipeline::js);
+ $this->assertTrue(strstr($url, 'JS') !== false, 'When file exists it should NOT be omitted');
+ }
+}

0 comments on commit 91003a9

Please sign in to comment.