From 55fcf167a393ff0970738ba945c08ac0c320865f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Jan 2012 11:55:42 -0500 Subject: [PATCH] Reorganized tests, updated README, added EVIRONMENT constant --- README.md | 19 ++++--- src/index.php | 16 +++++- src/sys/miniMVC.php | 55 ++++++++++--------- tests/JSObjectTest.php | 32 +++++++++++ tests/commonTest.php | 39 ++++++++++++++ tests/dbTest.php | 28 ++++++++++ tests/index.php | 16 ++++++ tests/miniMVCTest.php | 117 +++++------------------------------------ 8 files changed, 184 insertions(+), 138 deletions(-) create mode 100644 tests/JSObjectTest.php create mode 100644 tests/commonTest.php create mode 100644 tests/dbTest.php create mode 100644 tests/index.php diff --git a/README.md b/README.md index 09fb54f..661ad44 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file + `$this->load_model($model)` \ No newline at end of file diff --git a/src/index.php b/src/index.php index 347ac40..5339c37 100644 --- a/src/index.php +++ b/src/index.php @@ -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'); diff --git a/src/sys/miniMVC.php b/src/sys/miniMVC.php index ca2fdcc..21d6617 100644 --- a/src/sys/miniMVC.php +++ b/src/sys/miniMVC.php @@ -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 = '
';
+			if(empty($data))
+			{
+				$data =& $this;
+			}
+			
+			$output = '
';
+			
+			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 . '
'; } - 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 . '
'; } /** diff --git a/tests/JSObjectTest.php b/tests/JSObjectTest.php new file mode 100644 index 0000000..a016ad4 --- /dev/null +++ b/tests/JSObjectTest.php @@ -0,0 +1,32 @@ +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(), '
'.print_r($this->JSO, TRUE).'
'); + } + +} \ No newline at end of file diff --git a/tests/commonTest.php b/tests/commonTest.php new file mode 100644 index 0000000..599aa89 --- /dev/null +++ b/tests/commonTest.php @@ -0,0 +1,39 @@ +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)); + } + +} \ No newline at end of file diff --git a/tests/dbTest.php b/tests/dbTest.php new file mode 100644 index 0000000..c306c28 --- /dev/null +++ b/tests/dbTest.php @@ -0,0 +1,28 @@ +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(), '
'.print_r($this->JSO, TRUE).'
'); - } - -} - -// -------------------------------------------------------------------------- - /** * 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() - { - - } - -}