Skip to content

Commit

Permalink
Updating unit tests to work with new getAssetManager
Browse files Browse the repository at this point in the history
  • Loading branch information
woxxy committed Dec 20, 2012
1 parent 1acc702 commit ebd3e02
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 deletions.
20 changes: 13 additions & 7 deletions classes/Foolz/Theme/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@ 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()))
{
$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)
{
Expand Down Expand Up @@ -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());
}

/**
Expand Down
86 changes: 86 additions & 0 deletions classes/Foolz/Theme/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
}
12 changes: 11 additions & 1 deletion classes/Foolz/Theme/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -34,7 +44,7 @@ public function createBuilder()
*/
public function getAssetManager()
{
return new AssetManager($this, "", "");
return new AssetManager($this);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions tests/classes/ThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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());
}
}

0 comments on commit ebd3e02

Please sign in to comment.