From ebd3e024d5354f951f1506237d928b08ed6e999c Mon Sep 17 00:00:00 2001 From: woxxy Date: Fri, 21 Dec 2012 00:31:39 +0100 Subject: [PATCH] Updating unit tests to work with new getAssetManager --- classes/Foolz/Theme/AssetManager.php | 20 ++++--- classes/Foolz/Theme/Loader.php | 86 ++++++++++++++++++++++++++++ classes/Foolz/Theme/Theme.php | 12 +++- tests/classes/ThemeTest.php | 11 +++- 4 files changed, 119 insertions(+), 10 deletions(-) diff --git a/classes/Foolz/Theme/AssetManager.php b/classes/Foolz/Theme/AssetManager.php index 0544717..ae50a3f 100644 --- a/classes/Foolz/Theme/AssetManager.php +++ b/classes/Foolz/Theme/AssetManager.php @@ -36,17 +36,14 @@ class AssetManager * Create a new instance of the asset manager * * @param \Foolz\Theme\Theme $theme The reference to the theme creating this asset manager - * @param string $public_dir The directory where files can be accessed via URL - * @param string $base_url The URL that points to the public directory * * @return \Foolz\Theme\AssetManager */ - public function __construct(\Foolz\Theme\Theme $theme, $public_dir, $base_url) + public function __construct(\Foolz\Theme\Theme $theme) { $this->theme = $theme; - $this->public_dir = $public_dir; - $this->public_dir_mtime = filemtime($this->getPublicDir()); - $this->base_url = $base_url; + $this->public_dir = $this->getTheme()->getLoader()->getPublicDir(); + $this->base_url = $this->getTheme()->getLoader()->getBaseUrl(); // load the assets if ( ! file_exists($this->getPublicDir())) @@ -54,6 +51,8 @@ public function __construct(\Foolz\Theme\Theme $theme, $public_dir, $base_url) $this->loadAssets(); } + $this->public_dir_mtime = filemtime($this->getPublicDir()); + // reload the assets if the assets directory is more recent if (filemtime($this->getTheme()->getDir().'assets') > $this->public_dir_mtime) { @@ -99,7 +98,14 @@ public function getAssetLink($path) */ protected function loadAssets() { - copy($this->getTheme()->getDir().'assets', $this->getPublicDir()); + if ( ! file_exists($this->getPublicDir())) + { + mkdir($this->getPublicDir(), 0777, true); + } + + // damned copy doesn't work with directories + //copy($this->getTheme()->getDir().'assets', $this->getPublicDir()); + system('cp -R '.$this->getTheme()->getDir().'assets/*'.' '.$this->getPublicDir()); } /** diff --git a/classes/Foolz/Theme/Loader.php b/classes/Foolz/Theme/Loader.php index 3b17108..784ab7e 100644 --- a/classes/Foolz/Theme/Loader.php +++ b/classes/Foolz/Theme/Loader.php @@ -27,6 +27,32 @@ class Loader extends \Foolz\Package\Loader */ protected $type_class = 'Foolz\Theme\Theme'; + /** + * Path to public directory where assets are copied + * + * @var string|string + */ + protected $public_dir = null; + + /** + * URL that points to the public directory + * + * @var string|null + */ + protected $base_url = null; + + /** + * Creates or returns a named instance of Loader + * + * @param string $instance The name of the instance to use or create + * + * @return \Foolz\Theme\Loader + */ + public static function forge($instance = 'default') + { + return parent::forge($instance); + } + /** * Gets all the themes or the themes from the directory * @@ -53,4 +79,64 @@ public function get($dir_name, $slug) { return parent::get($dir_name, $slug); } + + /** + * Set the public directory where files can be copied into + * + * @param $public_dir The path + * + * @return \Foolz\Theme\Loader + */ + public function setPublicDir($public_dir) + { + $this->public_dir = rtrim($public_dir, '/').'/'; + + return $this; + } + + /** + * Returns the public directory where files are copied into + * + * @return string The path + * @throws \BadMethodCallException If the public dir wasn't set + */ + public function getPublicDir() + { + if ($this->public_dir === null) + { + throw new \BadMethodCallException('The public dir wasn\'t set'); + } + + return $this->public_dir; + } + + /** + * Set the base URL that points to the public directory + * + * @param $base_url The URL + * + * @return \Foolz\Theme\Loader + */ + public function setBaseUrl($base_url) + { + $this->base_url = rtrim($base_url, '/').'/'; + + return $this; + } + + /** + * Returns the base URL that points to the public directory + * + * @return string The URL + * @throws \BadMethodCallException If the base url wasn't set + */ + public function getBaseUrl() + { + if ($this->base_url === null) + { + throw new \BadMethodCallException('The base url wasn\'t set'); + } + + return $this->base_url; + } } \ No newline at end of file diff --git a/classes/Foolz/Theme/Theme.php b/classes/Foolz/Theme/Theme.php index 454a416..766d9a5 100644 --- a/classes/Foolz/Theme/Theme.php +++ b/classes/Foolz/Theme/Theme.php @@ -17,6 +17,16 @@ public function __construct($dir) } + /** + * Gets the loader that created this object + * + * @return \Foolz\Theme\Loader + */ + public function getLoader() + { + return parent::getLoader(); + } + /** * Returns a new Builder object * @@ -34,7 +44,7 @@ public function createBuilder() */ public function getAssetManager() { - return new AssetManager($this, "", ""); + return new AssetManager($this); } /** diff --git a/tests/classes/ThemeTest.php b/tests/classes/ThemeTest.php index 947296d..290afd6 100644 --- a/tests/classes/ThemeTest.php +++ b/tests/classes/ThemeTest.php @@ -11,8 +11,15 @@ class ThemeTest extends PHPUnit_Framework_TestCase */ public function load() { + if ( ! file_exists(__DIR__.'/../public')) + { + mkdir(__DIR__.'/../public'); + } + $loader = Loader::forge(); $loader->addDir('themes', __DIR__.'/../mock/'); + $loader->setBaseUrl(""); + $loader->setPublicDir(__DIR__."/../public"); return $loader; } @@ -37,8 +44,8 @@ public function testCreateBuilder() $this->assertInstanceOf('Foolz\Theme\Builder', $this->theme()->createBuilder()); } - public function testCreateAssetManager() + public function testGetAssetManager() { - $this->assertInstanceOf('Foolz\Theme\AssetManager', $this->theme()->createAssetManager()); + $this->assertInstanceOf('Foolz\Theme\AssetManager', $this->theme()->getAssetManager()); } } \ No newline at end of file