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.
### 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
#### 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.
@ -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
##### 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
* 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);`
@ -70,8 +79,4 @@ miniMVC is a minimalistic Modular MVC framework, with built-in minifier, and pur
* Loading a model (From a controller)
`$this->load_model($model)`
#### Disclaimer
Please treat this as alpha-level code. Structure might change, classes might appear or disappear overnight.
`$this->load_model($model)`

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!
error_reporting((-1) & ~(E_ERROR | E_PARSE));
if(ENVIRONMENT == 'DEVELOPMENT')
{
error_reporting(-1);
}
else if(EVIRONMENT == 'PRODUCTION')
{
error_reporting(0);
}
// Set the default paths
define('BASE_PATH', __DIR__);
@ -36,6 +46,8 @@ require(SYS_PATH . "common.php");
// Quercus doesn't define 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');
}
set_error_handler('on_error');

View File

@ -73,37 +73,44 @@ class JSObject extends Object{
*/
function __toString()
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : array();
if(empty($data))
if(ENVIRONMENT == 'DEVELOPMENT')
{
$data =& $this;
}
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : array();
$output = '<pre>';
if(empty($data))
{
$data =& $this;
}
$output = '<pre>';
if($method == "var_dump")
{
ob_start();
var_dump($data);
$output .= ob_get_contents();
ob_end_clean();
}
else if($method == "var_export")
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($data, TRUE);
}
if($method == "var_dump")
{
ob_start();
var_dump($data);
$output .= ob_get_contents();
ob_end_clean();
return $output . '</pre>';
}
else if($method == "var_export")
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($data, TRUE);
return '';
}
return $output . '</pre>';
}
/**

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
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
*/
@ -94,50 +20,31 @@ class miniMVCTest extends UnitTestCase {
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
$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?
$this->assertIsA($this->mm, 'JSObject');
}
function testInvoke()
{
// Invoke function should return the same reference than miniMVC::get_instance() does
$mm = $this->mm;
$this->assertIdentical($mm(), miniMVC::get_instance());
$this->assertIdentical($this->mm->__invoke(), miniMVC::get_instance());
}
}
// --------------------------------------------------------------------------
class DBTest extends UnitTestCase {
function __construct()
{
}
function setUp()
{
}
function tearDown()
{
}
function testCreation()
{
}
}