From 0c7334329175c5e563207aa36e7277401e97542f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 26 Jun 2015 12:03:42 -0400 Subject: [PATCH] Better integrate minification scripts, and add more unit tests --- app/base/BaseApiModel.php | 11 +++- app/base/BaseModel.php | 3 +- app/base/Config.php | 60 +++++++++++++++++++ app/base/functions.php | 3 +- app/config/base_config.php | 15 +++++ app/config/config.php | 19 ++---- .../config/minify_config.php | 25 +++----- .../config/minify_css_groups.php | 0 .../config/minify_js_groups.php | 0 index.php | 8 +-- phpunit.xml | 7 ++- public/css.php | 9 +-- public/js.php | 15 +++-- public/js/anime_edit.js | 2 +- tests/base/BaseApiModelTest.php | 33 ++++++++++ tests/base/BaseDBModelTest.php | 10 ++++ tests/base/BaseModelTest.php | 10 ++++ tests/base/ConfigTest.php | 27 +++++++++ tests/base/FunctionsTest.php | 53 ++++++++++++++++ tests/bootstrap.php | 21 ++++++- 20 files changed, 273 insertions(+), 58 deletions(-) create mode 100644 app/base/Config.php create mode 100644 app/config/base_config.php rename public/config/config.php => app/config/minify_config.php (69%) rename public/config/css_groups.php => app/config/minify_css_groups.php (100%) rename public/config/js_groups.php => app/config/minify_js_groups.php (100%) create mode 100644 tests/base/BaseApiModelTest.php create mode 100644 tests/base/BaseDBModelTest.php create mode 100644 tests/base/BaseModelTest.php create mode 100644 tests/base/ConfigTest.php diff --git a/app/base/BaseApiModel.php b/app/base/BaseApiModel.php index 89e15e5a..b07584a1 100644 --- a/app/base/BaseApiModel.php +++ b/app/base/BaseApiModel.php @@ -11,15 +11,21 @@ use \GuzzleHttp\Cookie\CookieJar; */ class BaseApiModel extends BaseModel { + /** + * Base url for making api requests + * @var string + */ + protected $base_url = ''; + /** * The Guzzle http client object - * @var object $client + * @var object */ protected $client; /** * Cookie jar object for api requests - * @var object $cookieJar + * @var object */ protected $cookieJar; @@ -48,6 +54,7 @@ class BaseApiModel extends BaseModel { /** * Attempt login via the api * + * @codeCoverageIgnore * @param string $username * @param string $password * @return bool diff --git a/app/base/BaseModel.php b/app/base/BaseModel.php index 5d2b35bb..fc37c173 100644 --- a/app/base/BaseModel.php +++ b/app/base/BaseModel.php @@ -29,6 +29,7 @@ class BaseModel { * Get the path of the cached version of the image. Create the cached image * if the file does not already exist * + * @codeCoverageIgnore * @param string $api_path - The original image url * @param string $series_slug - The part of the url with the series name, becomes the image name * @param string $type - Anime or Manga, controls cache path @@ -41,7 +42,6 @@ class BaseModel { $path = current($path_parts); $ext_parts = explode('.', $path); $ext = end($ext_parts); - /*$ext = $ext = strtolower(pathinfo($api_path, PATHINFO_EXTENSION));*/ // Workaround for some broken extensions if ($ext == "jjpg") $ext = "jpg"; @@ -91,6 +91,7 @@ class BaseModel { /** * Resize an image * + * @codeCoverageIgnore * @param string $path * @param string $width * @param string $height diff --git a/app/base/Config.php b/app/base/Config.php new file mode 100644 index 00000000..7d39cc28 --- /dev/null +++ b/app/base/Config.php @@ -0,0 +1,60 @@ +config = $config; + + foreach($base_config as $key => $val) + { + $this->config[$key] = $val; + } + + } + + /** + * Getter for config values + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + if (isset($this->config[$key])) + { + return $this->config[$key]; + } + + return NULL; + } +} +// End of config.php \ No newline at end of file diff --git a/app/base/functions.php b/app/base/functions.php index 3d96fcdd..b5297c5d 100644 --- a/app/base/functions.php +++ b/app/base/functions.php @@ -41,10 +41,9 @@ function is_not_selected($a, $b) /** * Get the base url for css/js/images * - * @param string $type - (optional) The controller * @return string */ - function asset_url(/*$type="anime"*,...*/) + function asset_url(/*...*/) { global $config; diff --git a/app/config/base_config.php b/app/config/base_config.php new file mode 100644 index 00000000..dbe4ad4f --- /dev/null +++ b/app/config/base_config.php @@ -0,0 +1,15 @@ + _dir(APP_DIR, 'cache'), + 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), + + // Included config files + 'routes' => require _dir(CONF_DIR, 'routes.php'), + 'database' => require _dir(CONF_DIR, 'database.php'), +]; \ No newline at end of file diff --git a/app/config/config.php b/app/config/config.php index ef17b1e8..e2e62f2f 100644 --- a/app/config/config.php +++ b/app/config/config.php @@ -1,5 +1,5 @@ '//' . $_SERVER['HTTP_HOST'] . '/public', + // path to public directory on the server + 'asset_dir' => __DIR__ . '/../../public', + // ---------------------------------------------------------------------------- // Routing // @@ -34,18 +37,4 @@ return (object)[ // Default to list view? 'default_to_list_view' => FALSE, - - // ---------------------------------------------------------------------------- - // Lower level configuration - // - // You shouldn't generally need to change anything below this line - // ---------------------------------------------------------------------------- - - // Cache paths - 'data_cache_path' => _dir(APP_DIR, 'cache'), - 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), - - // Included config files - 'routes' => require _dir(CONF_DIR, 'routes.php'), - 'database' => require _dir(CONF_DIR, 'database.php'), ]; \ No newline at end of file diff --git a/public/config/config.php b/app/config/minify_config.php similarity index 69% rename from public/config/config.php rename to app/config/minify_config.php index 0b3d3de4..c28bbdd2 100644 --- a/public/config/config.php +++ b/app/config/minify_config.php @@ -12,23 +12,12 @@ // -------------------------------------------------------------------------- -// Should we use myth to preprocess? -$use_myth = TRUE; +/* $config = */require 'config.php'; -/* -|-------------------------------------------------------------------------- -| Document Root -|-------------------------------------------------------------------------- -| -| The folder where the index of the website exists. In most situations, -| this will not need to be changed. -| -| If the website is in a folder off of the domain name, like: -| http://example.com/website/ -| you will need to add that folder to the document root. -| -*/ -$document_root = $_SERVER['DOCUMENT_ROOT']; +$config = (object)$config; + +// Should we use myth to preprocess? +$use_myth = FALSE; /* |-------------------------------------------------------------------------- @@ -38,7 +27,7 @@ $document_root = $_SERVER['DOCUMENT_ROOT']; | The folder where css files exist, in relation to the document root | */ -$css_root = $document_root. '/public/css/'; +$css_root = $config->asset_dir. '/css/'; /* |-------------------------------------------------------------------------- @@ -68,4 +57,4 @@ $path_to = ''; | The folder where javascript files exist, in relation to the document root | */ -$js_root = $document_root. '/public/js/'; \ No newline at end of file +$js_root = $config->asset_dir. '/js/'; \ No newline at end of file diff --git a/public/config/css_groups.php b/app/config/minify_css_groups.php similarity index 100% rename from public/config/css_groups.php rename to app/config/minify_css_groups.php diff --git a/public/config/js_groups.php b/app/config/minify_js_groups.php similarity index 100% rename from public/config/js_groups.php rename to app/config/minify_js_groups.php diff --git a/index.php b/index.php index 1677be43..f99c9210 100644 --- a/index.php +++ b/index.php @@ -29,13 +29,13 @@ define('CONF_DIR', APP_DIR . DIRECTORY_SEPARATOR . 'config'); define('BASE_DIR', APP_DIR . DIRECTORY_SEPARATOR . 'base'); require BASE_DIR . DIRECTORY_SEPARATOR . 'pre_conf_functions.php'; -// Load config and global functions -$config = require _dir(APP_DIR, '/config/config.php'); -require _dir(BASE_DIR, '/functions.php'); - // Setup autoloaders _setup_autoloaders(); +// Load config and global functions +$config = new Config(); +require _dir(BASE_DIR, '/functions.php'); + session_start(); use \Whoops\Handler\PrettyPageHandler; diff --git a/phpunit.xml b/phpunit.xml index 8c240fbe..aa2cbb5c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,7 +5,9 @@ bootstrap="tests/bootstrap.php"> - app + app/base + app/controllers + app/models @@ -13,4 +15,7 @@ tests/base + + + \ No newline at end of file diff --git a/public/css.php b/public/css.php index 3e9f8a11..506f27bc 100644 --- a/public/css.php +++ b/public/css.php @@ -13,10 +13,10 @@ // -------------------------------------------------------------------------- //Get config files -require('./config/config.php'); +require('../app/config/minify_config.php'); //Include the css groups -$groups = require("./config/css_groups.php"); +$groups = require("../app/config/minify_css_groups.php"); //Function for compressing the CSS as tightly as possible function compress($buffer) { @@ -117,11 +117,6 @@ if($last_modified === $requested_time) header("HTTP/1.1 304 Not Modified"); exit(); } -else // Re-compress after running myth -{ - $cmd = "/usr/bin/myth -c {$css_root}base.myth.css {$css_root}base.css"; - exec($cmd); -} //This GZIPs the CSS for transmission to the user //making file size smaller and transfer rate quicker diff --git a/public/js.php b/public/js.php index ffacbd43..8a297ff2 100644 --- a/public/js.php +++ b/public/js.php @@ -13,14 +13,14 @@ // -------------------------------------------------------------------------- //Get config files -require('./config/config.php'); +require('../app/config/minify_config.php'); //Include the js groups -$groups_file = "./config/js_groups.php"; +$groups_file = "../app/config/minify_js_groups.php"; $groups = require($groups_file); //The name of this file -$this_file = basename(__FILE__); +$this_file = __FILE__; // -------------------------------------------------------------------------- @@ -96,6 +96,10 @@ $modified = array(); //Aggregate the last modified times of the files if(isset($groups[$_GET['g']])) { + if ( ! is_dir($js_root . 'cache')) + { + mkdir($js_root . 'cache'); + } $cache_file = $js_root.'cache/'.$_GET['g']; foreach($groups[$_GET['g']] as $file) @@ -145,10 +149,9 @@ if($last_modified === $requested_time) if($cache_modified < $last_modified) { $js = google_min(get_files()); - $cs = file_put_contents($cache_file, $js); //Make sure cache file gets created/updated - if($cs === FALSE) + if(file_put_contents($cache_file, $js) === FALSE) { die("Cache file was not created. Make sure you have the correct folder permissions."); } @@ -179,4 +182,4 @@ header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." echo $js; ob_end_flush(); -//end of js.php +//end of js.php \ No newline at end of file diff --git a/public/js/anime_edit.js b/public/js/anime_edit.js index ad0e0145..e90f41d2 100644 --- a/public/js/anime_edit.js +++ b/public/js/anime_edit.js @@ -41,7 +41,7 @@ $.post(BASE_URL + 'update', data, function(res) { if (res.status === 'completed') { - parent_sel.hide(); + this_sel.parent('article').hide(); } add_message('success', "Sucessfully updated " + title); diff --git a/tests/base/BaseApiModelTest.php b/tests/base/BaseApiModelTest.php new file mode 100644 index 00000000..26e38400 --- /dev/null +++ b/tests/base/BaseApiModelTest.php @@ -0,0 +1,33 @@ +$key; + } +} + +class BaseApiModelTest extends AnimeClient_TestCase { + + public function testBaseApiModelSanity() + { + $baseApiModel = new MockBaseApiModel(); + + // Some basic type checks for class memebers + $this->assertInstanceOf('BaseModel', $baseApiModel); + $this->assertInstanceOf('BaseApiModel', $baseApiModel); + + $this->assertInstanceOf('\GuzzleHttp\Client', $baseApiModel->client); + $this->assertInstanceOf('\GuzzleHttp\Cookie\CookieJar', $baseApiModel->cookieJar); + + $this->assertTrue(is_string($baseApiModel->base_url)); + $this->assertTrue(empty($baseApiModel->base_url)); + } + +} \ No newline at end of file diff --git a/tests/base/BaseDBModelTest.php b/tests/base/BaseDBModelTest.php new file mode 100644 index 00000000..66e8cb90 --- /dev/null +++ b/tests/base/BaseDBModelTest.php @@ -0,0 +1,10 @@ +assertTrue(is_object($baseDBModel)); + } +} \ No newline at end of file diff --git a/tests/base/BaseModelTest.php b/tests/base/BaseModelTest.php new file mode 100644 index 00000000..f7967c52 --- /dev/null +++ b/tests/base/BaseModelTest.php @@ -0,0 +1,10 @@ +assertTrue(is_object($baseModel)); + } +} \ No newline at end of file diff --git a/tests/base/ConfigTest.php b/tests/base/ConfigTest.php new file mode 100644 index 00000000..dc2017b7 --- /dev/null +++ b/tests/base/ConfigTest.php @@ -0,0 +1,27 @@ +config = new Config([ + 'config' => [ + 'foo' => 'bar' + ], + 'base_config' => [ + 'bar' => 'baz' + ] + ]); + } + + public function testConfig__get() + { + $this->assertEquals($this->config->bar, $this->config->__get('bar')); + } + + public function testGetNonExistentConfigItem() + { + $this->assertEquals(NULL, $this->config->foobar); + } + +} \ No newline at end of file diff --git a/tests/base/FunctionsTest.php b/tests/base/FunctionsTest.php index 56ba4bd4..60a7221e 100644 --- a/tests/base/FunctionsTest.php +++ b/tests/base/FunctionsTest.php @@ -12,12 +12,65 @@ class FunctionsTest extends AnimeClient_TestCase { public function testIsSelected() { + // Failure to match + $this->assertEquals('', is_selected('foo', 'bar')); + // Matches + $this->assertEquals('selected', is_selected('foo', 'foo')); } public function testIsNotSelected() { + // Failure to match + $this->assertEquals('selected', is_not_selected('foo', 'bar')); + // Matches + $this->assertEquals('', is_not_selected('foo', 'foo')); } + public function assetUrlProvider() + { + return [ + 'single argument' => [ + 'config' => (object)[ + 'asset_path' => '//localhost/assets/' + ], + 'args' => [ + 'images' + ], + 'expected' => '//localhost/assets/images', + ], + 'multiple arguments' => [ + 'config' => (object)[ + 'asset_path' => '//localhost/assets/' + ], + 'args' => [ + 'images', 'anime', 'foo.png' + ], + 'expected' => '//localhost/assets/images/anime/foo.png' + ] + ]; + } + + /** + * @dataProvider assetUrlProvider + */ + public function testAssetUrl($config, $args, $expected) + { + global $config; + $config = func_get_arg(0); + + $result = call_user_func_array('asset_url', $args); + + $this->assertEquals($expected, $result); + } + + public function testIsLoggedIn() + { + $this->assertFalse(is_logged_in()); + + $_SESSION['hummingbird_anime_token'] = 'foobarbadsessionid'; + + $this->assertTrue(is_logged_in()); + } } \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 83f050d0..415ede24 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -20,7 +20,21 @@ $defaultHandler = new MockErrorHandler(); /** * Base class for TestCases */ -class AnimeClient_TestCase extends PHPUnit_Framework_TestCase {} +class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { + + public function setUp() + { + parent::setUp(); + + global $config; + $config = new Config([ + 'config' => [], + 'base_config' => [ + 'databaase' => [] + ] + ]); + } +} // ----------------------------------------------------------------------------- // Autoloaders @@ -35,5 +49,10 @@ define('BASE_DIR', _dir(APP_DIR, 'base')); // Setup autoloaders _setup_autoloaders(); +require(_dir(BASE_DIR, 'functions.php')); + +// Pre-define some superglobals +$_SESSION = []; +$_COOKIE = []; // End of bootstrap.php \ No newline at end of file