Better integrate minification scripts, and add more unit tests

This commit is contained in:
Timothy Warren 2015-06-26 12:03:42 -04:00
parent 5e5c41fe05
commit 0c73343291
20 changed files with 273 additions and 58 deletions

View File

@ -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

View File

@ -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

60
app/base/Config.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/**
* Wrapper for configuration values
*/
class Config {
/**
* Config object
*
* @var array
*/
protected $config;
/**
* Constructor
*
* @param array $config_files
*/
public function __construct(Array $config_files=[])
{
// @codeCoverageIgnoreStart
if (empty($config_files))
{
/* $config = */require_once _dir(CONF_DIR, 'config.php');
/* $base_config = */require_once _dir(CONF_DIR, 'base_config.php');
}
// @codeCoverageIgnoreEnd
else
{
$config = $config_files['config'];
$base_config = $config_files['base_config'];
}
$this->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

View File

@ -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;

View File

@ -0,0 +1,15 @@
<?php
// ----------------------------------------------------------------------------
// Lower level configuration
//
// You shouldn't generally need to change anything below this line
// ----------------------------------------------------------------------------
$base_config = [
// 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'),
];

View File

@ -1,5 +1,5 @@
<?php
return (object)[
$config = [
// ----------------------------------------------------------------------------
// Username for anime and manga lists
// ----------------------------------------------------------------------------
@ -15,6 +15,9 @@ return (object)[
// path to public directory
'asset_path' => '//' . $_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'),
];

View File

@ -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/';
$js_root = $config->asset_dir. '/js/';

View File

@ -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;

View File

@ -5,7 +5,9 @@
bootstrap="tests/bootstrap.php">
<filter>
<whitelist>
<directory suffix=".php">app</directory>
<directory suffix=".php">app/base</directory>
<directory suffix=".php">app/controllers</directory>
<directory suffix=".php">app/models</directory>
</whitelist>
</filter>
<testsuites>
@ -13,4 +15,7 @@
<directory>tests/base</directory>
</testsuite>
</testsuites>
<php>
<server name="HTTP_USER_AGENT" value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0" />
</php>
</phpunit>

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -0,0 +1,33 @@
<?php
class MockBaseApiModel extends BaseApiModel {
public function __construct()
{
parent::__construct();
}
public function __get($key)
{
return $this->$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));
}
}

View File

@ -0,0 +1,10 @@
<?php
class BaseDBModelTest extends AnimeClient_TestCase {
public function testBaseDBModelSanity()
{
$baseDBModel = new BaseDBModel();
$this->assertTrue(is_object($baseDBModel));
}
}

View File

@ -0,0 +1,10 @@
<?php
class BaseModelTest extends AnimeClient_TestCase {
public function testBaseModelSanity()
{
$baseModel = new BaseModel();
$this->assertTrue(is_object($baseModel));
}
}

27
tests/base/ConfigTest.php Normal file
View File

@ -0,0 +1,27 @@
<?php
class ConfigTest extends AnimeClient_TestCase {
public function setUp()
{
$this->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);
}
}

View File

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

View File

@ -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