Reorganized tests, updated README, added EVIRONMENT constant

This commit is contained in:
Timothy Warren 2012-01-17 11:55:42 -05:00
parent 309846ee8f
commit 55fcf167a3
8 changed files with 184 additions and 138 deletions

View File

@ -2,6 +2,15 @@
miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pure-PHP templating system. miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pure-PHP templating system.
### Requirements
* PHP 5.2+ (5.4+ for trait branch)
* PDO extensions for databases you wish to use
* Webserver that correctly handles PATH_INFO, such as:
* Apache
* IIS
* Lighttpd
* SimpleTest library for running unit tests
### Unique features ### Unique features
#### Extensive use of PHP's magic methods on the base class #### Extensive use of PHP's magic methods on the base class
* `__toString()` method allows a view of the current class object when the current class object is used as a string. If you prefer `var_dump()` or `var_export()`, you can pass the name of that function if you call the `__toString` method directly. * `__toString()` method allows a view of the current class object when the current class object is used as a string. If you prefer `var_dump()` or `var_export()`, you can pass the name of that function if you call the `__toString` method directly.
@ -12,7 +21,7 @@ miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pur
Eg. `$this->foo = function($baz){}` is callable as `$this->foo()`, with the current object as the last argument Eg. `$this->foo = function($baz){}` is callable as `$this->foo()`, with the current object as the last argument
##### Database class is an extension of PHP's PDO class. #### Database class is an extension of PHP's PDO class.
* miniMVC supports any database supported by PDO * miniMVC supports any database supported by PDO
* Database class also implements the `__toString` method * Database class also implements the `__toString` method
@ -61,7 +70,7 @@ miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pur
} }
} }
* Loading a database (From a Model) * Loading a database
`$this->db = db::get_instance($db_name);` `$this->db = db::get_instance($db_name);`
@ -71,7 +80,3 @@ miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pur
* Loading a model (From a controller) * Loading a model (From a controller)
`$this->load_model($model)` `$this->load_model($model)`
#### Disclaimer
Please treat this as alpha-level code. Structure might change, classes might appear or disappear overnight.

View File

@ -11,9 +11,19 @@
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Set as either DEVELOPMENT or PRODUCTION
// DEVELOPMENT enables error reporting
// PRODUCTION disables error reporting
define('ENVIRONMENT', 'DEVELOPMENT');
// Change this in a live environment! if(ENVIRONMENT == 'DEVELOPMENT')
error_reporting((-1) & ~(E_ERROR | E_PARSE)); {
error_reporting(-1);
}
else if(EVIRONMENT == 'PRODUCTION')
{
error_reporting(0);
}
// Set the default paths // Set the default paths
define('BASE_PATH', __DIR__); define('BASE_PATH', __DIR__);
@ -36,6 +46,8 @@ require(SYS_PATH . "common.php");
// Quercus doesn't define error_get_last... // Quercus doesn't define error_get_last...
if(function_exists('error_get_last')) if(function_exists('error_get_last'))
{ {
// Catch fatal errors, don't show them
error_reporting((-1) & ~(E_ERROR | E_PARSE));
register_shutdown_function('shutdown'); register_shutdown_function('shutdown');
} }
set_error_handler('on_error'); set_error_handler('on_error');

View File

@ -72,6 +72,8 @@ class JSObject extends Object{
* @return string * @return string
*/ */
function __toString() function __toString()
{
if(ENVIRONMENT == 'DEVELOPMENT')
{ {
$args = func_get_args(); $args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r"; $method = ( ! empty($args)) ? $args[0] : "print_r";
@ -105,6 +107,11 @@ class JSObject extends Object{
return $output . '</pre>'; return $output . '</pre>';
} }
else
{
return '';
}
}
/** /**
* PHP magic method to facilitate dynamic class loading * PHP magic method to facilitate dynamic class loading

32
tests/JSObjectTest.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/**
* Test Class for JSObject class
*/
class JSObjectTest extends UnitTestCase {
function __construct()
{
parent::__construct('JSObject Class Tests');
}
function setUp()
{
$this->JSO = new JSObject(array(
'foo' => 54,
'bar' => 'baz',
));
}
function tearDown()
{
unset($this->JSO);
}
function testToStringDevel()
{
// __toString produces same output as print_r by default
$this->assertIdentical($this->JSO->__toString(), '<pre>'.print_r($this->JSO, TRUE).'</pre>');
}
}

39
tests/commonTest.php Normal file
View File

@ -0,0 +1,39 @@
<?php
/**
* Test class for /src/sys/common.php
*/
class commonTest extends UnitTestCase {
function __construct()
{
parent::__construct('Common.php Tests');
}
function setUp()
{
$this->empty = array();
$this->object = new JSObject();
$this->array_like = new JSObject(array('foo' => 'bar'));
}
function tearDown()
{
unset($this->empty);
unset($this->object);
unset($this->array_like);
}
function testEmptyArrayNotLikeArray()
{
// Empty is not array like
$this->assertFalse(is_like_array($this->empty));
}
function testEmptyObjectIsLikeArray()
{
// Empty object is array like - because objects are truthy
$this->assertTrue(is_like_array($this->object));
}
}

28
tests/dbTest.php Normal file
View File

@ -0,0 +1,28 @@
<?php
/**
* Database class testing class
*/
class DBTest extends UnitTestCase {
function __construct()
{
}
function setUp()
{
}
function tearDown()
{
}
function testCreation()
{
}
}

16
tests/index.php Normal file
View File

@ -0,0 +1,16 @@
<?php
//Specify test environment
define('ENVIRONMENT', 'DEVELOPMENT');
//Include simpletest
require_once('simpletest/autorun.php');
//Include src files
require_once('../src/sys/common.php');
//Include test files
require_once('commonTest.php');
require_once('JSObjectTest.php');
require_once('miniMVCTest.php');
require_once('dbTest.php');

View File

@ -1,79 +1,5 @@
<?php <?php
require_once('simpletest/autorun.php');
require_once('../src/sys/common.php');
/**
* Test class for /src/sys/common.php
*/
class commonTest extends UnitTestCase {
function __construct()
{
parent::__construct('Common.php Tests');
}
function setUp()
{
$this->empty = array();
$this->object = new JSObject();
$this->array_like = new JSObject(array('foo' => 'bar'));
}
function tearDown()
{
unset($this->empty);
unset($this->object);
unset($this->array_like);
}
function testCreation()
{
// Empty is not array like
$this->assertFalse(is_like_array($this->empty));
// Empty object is array like - because objects are truthy
$this->assertTrue(is_like_array($this->object));
}
}
// --------------------------------------------------------------------------
/**
* Test Class for JSObject class
*/
class JSObjectTest extends UnitTestCase {
function __construct()
{
parent::__construct('JSObject Class Tests');
}
function setUp()
{
$this->JSO = new JSObject(array(
'foo' => 54,
'bar' => 'baz',
));
}
function tearDown()
{
unset($this->JSO);
}
function testCreation()
{
// __toString produces same output as print_r by default
$this->assertIdentical($this->JSO->__toString(), '<pre>'.print_r($this->JSO, TRUE).'</pre>');
}
}
// --------------------------------------------------------------------------
/** /**
* Test class for miniMVC class * Test class for miniMVC class
*/ */
@ -94,50 +20,31 @@ class miniMVCTest extends UnitTestCase {
unset($this->mm); unset($this->mm);
} }
function testCreation() function testNoClone()
{
// Expect an error trying to clone the miniMVC object
$this->expectError("Clone is not allowed.");
$mm2 = clone $this->mm;
}
function testReferences()
{ {
// miniMVC::get_instance returns reference to latest miniMVC object // miniMVC::get_instance returns reference to latest miniMVC object
$this->assertReference($this->mm, miniMVC::get_instance()); $this->assertReference($this->mm, miniMVC::get_instance());
// Expect an error trying to clone the miniMVC object
$this->expectError("Clone is not allowed.");
$mm2 = clone $this->mm;
// miniMVC extends JSObject, right? // miniMVC extends JSObject, right?
$this->assertIsA($this->mm, 'JSObject'); $this->assertIsA($this->mm, 'JSObject');
}
function testInvoke()
{
// Invoke function should return the same reference than miniMVC::get_instance() does // Invoke function should return the same reference than miniMVC::get_instance() does
$mm = $this->mm; $mm = $this->mm;
$this->assertIdentical($mm(), miniMVC::get_instance()); $this->assertIdentical($mm(), miniMVC::get_instance());
$this->assertIdentical($this->mm->__invoke(), miniMVC::get_instance()); $this->assertIdentical($this->mm->__invoke(), miniMVC::get_instance());
} }
} }
// --------------------------------------------------------------------------
class DBTest extends UnitTestCase {
function __construct()
{
}
function setUp()
{
}
function tearDown()
{
}
function testCreation()
{
}
}