Permalink
Browse files

adding some tests and stubbing others out; fixing some bugs i found w…

…he implementing tests :)
  • Loading branch information...
Ryan Nagle
Ryan Nagle committed Oct 18, 2011
1 parent a4967e2 commit a72ebc0d882554e2e2c21858216c2bf92e619e76
View
@@ -46,12 +46,12 @@ public function __construct( $kwargs=array() ) {
// Write the data in this model to permanent storage
public function save() {
- throw new NotImplementedException();
+ throw new \NotImplementedException();
}
// Validate the data in this model
public function validate() {
- throw new NotImplementedException();
+ throw new \NotImplementedException();
}
// Call initialize when the model is created
@@ -97,7 +97,7 @@ public function fetch() {
// $from_db = new Object;
// pass the results to reload
// $this->reload( $from_db );
- throw new NotImplementedException();
+ throw new \NotImplementedException();
}
/**
@@ -245,8 +245,8 @@ public static function get( $kwargs ) {
return $model;
}
- public static function get_by( $kwargs ) { throw new NotImplementedException(); }
- public static function filter( $kwargs ) { throw new NotImplementedException(); }
+ public static function get_by( $kwargs ) { throw new \NotImplementedException(); }
+ public static function filter( $kwargs ) { throw new \NotImplementedException(); }
}
/**
View
@@ -57,8 +57,8 @@ function load( $apps ) {
foreach ( $apps as $name ) {
$app = $registered_apps[$name];
- if ( $app['views'] ) include $app['views'];
- if ( $app['models'] ) include $app['models'];
+ if ( $app['views'] ) include_once $app['views'];
+ if ( $app['models'] ) include_once $app['models'];
if ( $app['templates'] ) array_push($template_dirs, $app['templates']);
}
@@ -83,9 +83,9 @@ function load( $apps ) {
# now that we have a template engine, load some goodies
foreach ( $apps as $name ) {
$app = $registered_apps[$name];
- if ( $app['tags'] ) include $app['tags'];
- if ( $app['functions'] ) include $app['functions'];
- if ( $app['filters'] ) include $app['filters'];
+ if ( $app['tags'] ) include_once $app['tags'];
+ if ( $app['functions'] ) include_once $app['functions'];
+ if ( $app['filters'] ) include_once $app['filters'];
}
}
View
@@ -0,0 +1,61 @@
+<?php
+
+require_once 'MTVTest.php';
+
+class MTVCoreTest extends MTVTest {
+
+ public function setUp() {
+ check_before_wreck();
+ reset_app_globals();
+ }
+
+ public function test_register_app() {
+ global $registered_apps;
+
+ \mtv\register_app('test_app', dirname(__DIR__) . '/test_app');
+
+ // Should have one app
+ $this->assertEquals(count($registered_apps), 1);
+
+ // App name should be 'test_app'
+ $this->assertTrue(array_key_exists('test_app', $registered_apps));
+ }
+
+ public function test_load() {
+ global $registered_apps, $twig, $apps;
+
+ // Register and load test_app
+ \mtv\register_app('test_app', dirname(__FILE__) . '/test_app');
+
+ $apps = array('test_app');
+ \mtv\load($apps);
+
+ // Make sure test_app home view function is loaded
+ $this->assertTrue(function_exists('test_app\views\home'));
+ // Make sure test_app TestModel class is loaded
+ $this->assertTrue(class_exists('test_app\models\TestModel'));
+ // Make sure Twig was reinitialized
+ $this->assertTrue(!empty($twig));
+ }
+
+ public function test_run() {
+ global $registered_apps, $apps, $url_patterns;
+
+ /*
+ * NOTE: This test checks functionality of
+ * the http.php lib. If these tests pass,
+ * we can consider the http lib functionally sound.
+ * TODO: Unit tests for http.php functions.
+ */
+
+ // Request the home page
+ $result = $this->http_get('/');
+ $this->assertEquals($result, 200);
+
+ // Request non-existent page
+ $result = $this->http_get('/404');
+ $this->assertEquals($result, 404);
+ }
+
+}
+
View
@@ -0,0 +1,23 @@
+<?php
+
+require_once 'MTVTest.php';
+
+/*
+ * TODO: Fill in the stubbed tests below.
+ */
+class MTVHttpTest extends MTVOutputTest {
+
+ public function test_resolve() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_urlresolver() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_include_urls_for() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+}
+
View
@@ -0,0 +1,115 @@
+<?php
+
+require_once 'MTVTest.php';
+
+class MTVModelTest extends MTVTest {
+
+ public function setUp() {
+ check_before_wreck();
+
+ $this->model = new \mtv\models\Model(
+ array('name' => 'test_model')
+ );
+ }
+
+ public function test_save() {
+ try {
+ $this->model->save();
+ } catch (NotImplementedException $expected) {
+ return;
+ }
+
+ $this->fail('An expected NotImplementedException has not been raised.');
+ }
+
+ public function test_validate() {
+ try {
+ $this->model->save();
+ } catch (NotImplementedException $expected) {
+ return;
+ }
+
+ $this->fail('An expected NotImplementedException has not been raised.');
+ }
+
+ public function test_initialize() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test___toString() {
+ $ret = $this->model->__toString();
+
+ $this->assertEquals($ret, 'mtv\models\Model');
+ }
+
+ public function test___get() {
+ $ret = $this->model->__get('name');
+
+ $this->assertEquals($ret, 'test_model');
+ }
+
+ public function test___set() {
+ $this->model->__set('name', 'name_changed_with_set');
+
+ $this->assertEquals($this->model->attributes['name'], 'name_changed_with_set');
+ }
+
+ public function test___unset() {
+ $this->model->__unset('name');
+
+ $this->assertTrue(empty($this->model->attributes['name']));
+ }
+
+ public function test___isset() {
+ $ret = $this->model->__isset('name');
+
+ $this->assertTrue($ret);
+ }
+
+ public function test_clear() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_set() {
+ // Set attribute values, verify
+ $attrs = array(
+ 'name' => 'changed_name',
+ 'note' => 'Just a test'
+ );
+ $this->model->set($attrs);
+
+ $this->assertEquals($this->model->attributes['name'], 'changed_name');
+ $this->assertEquals($this->model->attributes['note'], 'Just a test');
+ }
+
+ public function test_fetch() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_parse() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_reload() {
+ $this->markTestIncomplete('This test has not been implemented yet.');
+ }
+
+ public function test_to_json() {
+ // Return assoc array of jsonable fields for passing to json_encode()
+ $this->model->json_fields = array('name');
+ $ret = $this->model->to_json();
+
+ $this->assertEquals($ret, array('name' => 'test_model'));
+ }
+
+ public function test_set_from_json() {
+ // Provide data for the model as a json string,
+ // check that the attributes are set properly
+ $json = '{"name": "changed_name_with_json"}';
+ $this->model->set_from_json($json);
+
+ $this->assertEquals(
+ $this->model->attributes['name'], 'changed_name_with_json');
+ }
+
+}
@@ -0,0 +1,73 @@
+<?php
+
+/*
+ * PHPUnit is required in order to run these tests.
+ * You can install PHPUnit by running requirements.sh
+ * packaged with MTV.
+ *
+ * You'll also need to edit the accompanying settings.json file
+ * in this directory and update the hostname of the WordPress
+ * install you're using to run these tests.
+ *
+ * Note: if you encounter errors and/or failures when
+ * running tests, you should try fixing errors first. They're
+ * usually easier to tackle and might resolve failures.
+ */
+
+require_once 'lib/cli-load.php';
+require_once 'PHPUnit/Autoload.php';
+require_once 'PHPUnit/Util/ErrorHandler.php';
+
+// All tests
+$tests = array();
+
+// Printer object will eventually print results
+$printer = new PHPUnit_TextUI_ResultPrinter(
+ NULL, true, !stristr(PHP_OS, 'WIN'));
+
+// Results object tracks results for all tests
+$result = new PHPUnit_Framework_TestResult;
+$result->addListener($printer);
+
+// Test core MTV functions
+require_once 'MTVCoreTest.php';
+$tests['MTVCoreTest'] = new PHPUnit_Framework_TestSuite('MTVCoreTest');
+
+// Test MTV's http lib
+require_once 'MTVHttpTest.php';
+$tests['MTVHttpTest'] = new PHPUnit_Framework_TestSuite('MTVHttpTest');
+
+// Test Model model
+require_once 'MTVModelTest.php';
+$tests['MTVModelTest'] = new PHPUnit_Framework_TestSuite('MTVModelTest');
+
+// TODO:
+
+// Test Collection model
+// Stub
+
+// Test Post model
+// Stub
+
+// Test PostCollection model
+// Stub
+
+// Test User model
+// Stub
+
+// Test UserCollection model
+// stub
+
+// Test Site model
+// stub
+
+// Test SiteCollection model
+// stub
+
+// Add above test suites to master suite and run, printing the result
+$suite = new PHPUnit_Framework_TestSuite();
+foreach ($tests as $k => $v)
+ $suite->addTestSuite($k);
+$suite->run($result);
+$printer->printResult($result);
+
Oops, something went wrong.

0 comments on commit a72ebc0

Please sign in to comment.