More tests and 100% coverage achievements!
Also fixed a few minor bugs and reworked Browser class to not use the constant UNIT_TESTING so I could get the class to 100% coverage. Adds a dependency of testing_helpers which I believe is available on Travis CI by default. Up to 75% coverage, w00t w00t!
This commit is contained in:
parent
faaefc1b82
commit
8db383601e
15 changed files with 136 additions and 62 deletions
|
@ -96,7 +96,7 @@ class Browser extends Object
|
|||
*/
|
||||
public static function goHome()
|
||||
{
|
||||
Browser::redirect('/');
|
||||
return Browser::redirect('/');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,15 +134,7 @@ class Browser extends Object
|
|||
}
|
||||
|
||||
header('Location: ' . $destination);
|
||||
|
||||
if (defined('UNIT_TESTING'))
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
else
|
||||
{
|
||||
exit;
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,7 +189,6 @@ class Browser extends Object
|
|||
case 102: $message = '102 Processing'; break;
|
||||
// }}}
|
||||
// {{{ 2xx Success
|
||||
case 200: $message = '200 OK'; break;
|
||||
case 201: $message = '201 Created'; break;
|
||||
case 202: $message = '202 Accepted'; break;
|
||||
case 203: $message = '203 Non-Authoritative Information'; break;
|
||||
|
|
|
@ -53,7 +53,11 @@ class Config extends Object
|
|||
if (file_exists($filename) && is_file($filename) && is_readable($filename))
|
||||
{
|
||||
require_once $filename;
|
||||
}
|
||||
|
||||
// Checks that we have the config array
|
||||
if (isset($config))
|
||||
{
|
||||
// Determines the environment
|
||||
if (isset($config['environment']))
|
||||
{
|
||||
|
|
|
@ -220,6 +220,11 @@ class Controller extends Object
|
|||
|
||||
// Redirect to login page
|
||||
Browser::redirect('/login');
|
||||
|
||||
// header() updates are a bitch to test, returning
|
||||
// halts execution so we don't have any output in our
|
||||
// testing results.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
*/
|
||||
class HTML extends Object
|
||||
{
|
||||
private $self_closing = array('br', 'hr', 'img', 'input', 'link', 'meta');
|
||||
private $self_closing = ['br', 'hr', 'img', 'input', 'link', 'meta'];
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
|
@ -58,7 +58,7 @@ class HTML extends Object
|
|||
}
|
||||
else
|
||||
{
|
||||
$attributes = array('type' => $type);
|
||||
$attributes = ['type' => $type];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,14 +66,15 @@ class HTML extends Object
|
|||
{
|
||||
if (isset($attributes['name']))
|
||||
{
|
||||
$label = $this->label(array('for' => $attributes['name']), $attributes['label']);
|
||||
unset($attributes['label']);
|
||||
$label = $this->label(['for' => $attributes['name']], $attributes['label']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$label = $this->label($attributes['label']);
|
||||
}
|
||||
|
||||
unset($attributes['label']);
|
||||
|
||||
return $label . $this->$method($attributes, $contents);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -122,33 +122,26 @@ class Log
|
|||
{
|
||||
$log_path = LOG_PATH . date('Y/m/d/', ($time == false ? time() : $time));
|
||||
|
||||
try
|
||||
if (!file_exists($log_path))
|
||||
{
|
||||
if (!file_exists($log_path))
|
||||
{
|
||||
mkdir($log_path, 0755, true);
|
||||
}
|
||||
|
||||
$log_file = $log_path . $log_type . '.log';
|
||||
|
||||
$message .= "\n";
|
||||
|
||||
if ($format == true)
|
||||
{
|
||||
$backtrace = debug_backtrace();
|
||||
rsort($backtrace);
|
||||
$frame = $backtrace[strpos($backtrace[0]['file'], 'index.php') === false ? 0 : 1];
|
||||
|
||||
return file_put_contents($log_file, date('H:i:s') . ' ' . str_replace(getcwd(), '', $frame['file']) . ':' . $frame['line'] . ' ' . $message, FILE_APPEND);
|
||||
}
|
||||
else
|
||||
{
|
||||
return file_put_contents($log_file, $message, FILE_APPEND);
|
||||
}
|
||||
mkdir($log_path, 0755, true);
|
||||
}
|
||||
catch (ErrorException $exception)
|
||||
|
||||
$log_file = $log_path . $log_type . '.log';
|
||||
|
||||
$message .= "\n";
|
||||
|
||||
if ($format == true)
|
||||
{
|
||||
return false;
|
||||
$backtrace = debug_backtrace();
|
||||
rsort($backtrace);
|
||||
$frame = $backtrace[strpos($backtrace[0]['file'], 'index.php') === false ? 0 : 1];
|
||||
|
||||
return file_put_contents($log_file, date('H:i:s') . ' ' . str_replace(getcwd(), '', $frame['file']) . ':' . $frame['line'] . ' ' . $message, FILE_APPEND);
|
||||
}
|
||||
else
|
||||
{
|
||||
return file_put_contents($log_file, $message, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,15 +37,25 @@ class Number
|
|||
*/
|
||||
public static function ordinalIndicator($number, $superscript = false)
|
||||
{
|
||||
$suffix = 'th';
|
||||
|
||||
if (!in_array(($number % 100), array(11, 12, 13)))
|
||||
{
|
||||
switch ($number % 10)
|
||||
{
|
||||
case 1: $suffix = 'st'; break;
|
||||
case 2: $suffix = 'nd'; break;
|
||||
case 3: $suffix = 'rd'; break;
|
||||
case 1:
|
||||
$suffix = 'st';
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$suffix = 'nd';
|
||||
break;
|
||||
|
||||
case 3:
|
||||
$suffix = 'rd';
|
||||
break;
|
||||
|
||||
default:
|
||||
$suffix = 'th';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -123,6 +123,7 @@ class Object
|
|||
public static function getInstance($class = false)
|
||||
{
|
||||
// In < 5.3 arguments must match in child, hence defaulting $class
|
||||
// @todo Remove this, as we're no longer supporting 5.3
|
||||
if ($class == false)
|
||||
{
|
||||
return false;
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
<?php
|
||||
|
||||
set_exit_overload(function(){ return false; });
|
||||
|
||||
ob_start();
|
||||
session_start();
|
||||
@session_start();
|
||||
|
||||
require_once 'vendors/composer/autoload.php';
|
||||
|
||||
|
@ -11,8 +13,6 @@ if (!defined('SITE_PATH'))
|
|||
{
|
||||
define('SECURITY_LEVEL_USER', 10);
|
||||
define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/'));
|
||||
// This isn't ideal but it helps a ton when testing the Browser class.
|
||||
define('UNIT_TESTING', true);
|
||||
}
|
||||
|
||||
require_once 'pickles.php';
|
||||
|
|
|
@ -13,17 +13,14 @@ class BrowserTest extends PHPUnit_Framework_TestCase
|
|||
$this->assertEquals('bar', Browser::get('foo'));
|
||||
}
|
||||
|
||||
public function testMissingVariable()
|
||||
{
|
||||
$this->assertFalse(Browser::get('missing'));
|
||||
}
|
||||
|
||||
public function testGoHome()
|
||||
{
|
||||
try
|
||||
{
|
||||
Browser::goHome();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Browser::goHome();
|
||||
$this->assertTrue(in_array('Location: http://testsite.com/', xdebug_get_headers()));
|
||||
}
|
||||
|
||||
|
@ -34,7 +31,7 @@ class BrowserTest extends PHPUnit_Framework_TestCase
|
|||
$this->assertTrue(Browser::isMobile());
|
||||
}
|
||||
|
||||
public function testRedirect()
|
||||
public function testIsNotMobile()
|
||||
{
|
||||
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11';
|
||||
|
||||
|
|
13
tests/classes/ConfigTest.php
Normal file
13
tests/classes/ConfigTest.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
class ConfigTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConfigProperty()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$this->assertTrue(PHPUnit_Framework_Assert::readAttribute($config, 'config'));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -100,7 +100,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
file_put_contents(SITE_MODULE_PATH . 'notauth.php', $module);
|
||||
|
||||
new Controller();
|
||||
@new Controller();
|
||||
|
||||
$this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers()));
|
||||
}
|
||||
|
|
|
@ -68,7 +68,10 @@ JS;
|
|||
|
||||
public function testReference()
|
||||
{
|
||||
$this->assertRegExp('/^\/images\/image\.\d{10}\.png$/', $this->dynamic->reference('/images/image.png'));
|
||||
$this->assertRegExp(
|
||||
'/^\/images\/image\.\d{10}\.png$/',
|
||||
$this->dynamic->reference('/images/image.png'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,6 +109,14 @@ JS;
|
|||
$this->dynamic->reference('../images/relative.png');
|
||||
}
|
||||
|
||||
public function testReferenceWithQueryString()
|
||||
{
|
||||
$this->assertRegExp(
|
||||
'/^\/images\/image\.\d{10}\.png\?foo=bar$/',
|
||||
$this->dynamic->reference('/images/image.png?foo=bar'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Filename must have an extension (e.g. /path/to/file.css)
|
||||
|
@ -123,6 +134,18 @@ JS;
|
|||
$this->assertRegExp('/^\/css\/stylesheet\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.css'));
|
||||
}
|
||||
|
||||
public function testCSSWithoutMinifyFileMinifiedFileExists()
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$config->data['pickles']['minify'] = false;
|
||||
|
||||
touch('/tmp/pickles-fs/public/css/stylesheet.min.css');
|
||||
|
||||
$this->assertRegExp('/^\/css\/stylesheet\.min\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.css'));
|
||||
|
||||
unlink('/tmp/pickles-fs/public/css/stylesheet.min.css');
|
||||
}
|
||||
|
||||
public function testCSSWithMinify()
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
|
@ -197,6 +220,18 @@ JS;
|
|||
|
||||
$this->assertRegExp('/^\/js\/script\.min\.\d{10}\.js$/', $this->dynamic->js('/js/script.js'));
|
||||
}
|
||||
|
||||
public function testJSWithoutMinifyFileMinifiedFileExists()
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$config->data['pickles']['minify'] = false;
|
||||
|
||||
touch('/tmp/pickles-fs/public/js/script.min.css');
|
||||
|
||||
$this->assertRegExp('/^\/js\/script\.min\.\d{10}\.js$/', $this->dynamic->js('/js/script.js'));
|
||||
|
||||
unlink('/tmp/pickles-fs/public/js/script.min.css');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -94,6 +94,16 @@ class HTMLTest extends PHPUnit_Framework_TestCase
|
|||
$this->html->div('string', ['class' => 'fancy'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testLabelWithInputWithoutName()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'<label>Label</label><input type="text">',
|
||||
$this->html->input([
|
||||
'label' => 'Label',
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
class LogTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
private $config;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$config->data['pickles']['logging'] = true;
|
||||
$this->config = Config::getInstance();
|
||||
$this->config->data['pickles']['logging'] = true;
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass()
|
||||
|
@ -89,6 +91,13 @@ class LogTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
$this->assertRegExp('/^\d{2}:\d{2}:\d{2} .+ query$/', $line);
|
||||
}
|
||||
|
||||
public function testLoggingDisabled()
|
||||
{
|
||||
$this->config->data['pickles']['logging'] = false;
|
||||
|
||||
$this->assertFalse(Log::error('should return false'));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -15,6 +15,11 @@ class ObjectTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
$this->assertInstanceOf('Cache', PHPUnit_Framework_Assert::readAttribute($object, 'cache'));
|
||||
}
|
||||
|
||||
public function testGetInstanceWithoutClass()
|
||||
{
|
||||
$this->assertFalse(Object::getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue