Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…he implementing tests :)
  • Loading branch information...
commit a72ebc0d882554e2e2c21858216c2bf92e619e76 1 parent a4967e2
@rnagle rnagle authored
View
10 mtv/models.php
@@ -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
10 mtv/mtv.php
@@ -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
61 mtv/tests/MTVCoreTest.php
@@ -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
23 mtv/tests/MTVHttpTest.php
@@ -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
115 mtv/tests/MTVModelTest.php
@@ -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');
+ }
+
+}
View
73 mtv/tests/MTVRunAllTests.php
@@ -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);
+
View
75 mtv/tests/MTVTest.php
@@ -0,0 +1,75 @@
+<?php
+
+require_once 'PHPUnit/Autoload.php';
+require_once 'PHPUnit/Util/ErrorHandler.php';
+
+class MTVTest extends PHPUnit_Framework_TestCase {
+
+ protected $backupGlobals = false;
+
+ protected $backupStaticAttributes = false;
+
+ public function setUp() {
+ // Make sure we have a WP install and MTV is active
+ check_before_wreck();
+ }
+
+ public function http_get($route = '') {
+ global $settings;
+
+ $ch = curl_init();
+
+ curl_setopt(
+ $ch, CURLOPT_URL, 'http://' . $settings['hostname'] . '/' . $route);
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+ curl_exec($ch);
+
+ $result = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+ curl_close($ch);
+
+ return $result;
+ }
+
+}
+
+class MTVOutputTest extends PHPUnit_Extensions_OutputTestCase {
+
+ protected $backupGlobals = false;
+
+ protected $backupStaticAttributes = false;
+
+ public function setUp() {
+ // Make sure we have a WP install and MTV is active
+ check_before_wreck();
+ }
+
+}
+
+function check_before_wreck() {
+ require_once dirname(__FILE__) . '/lib/cli-load.php';
+
+ $plugin_file = ABSPATH . 'wp-admin/includes/plugin.php';
+
+ // Check for WordPress
+ if (file_exists($plugin_file))
+ require_once $plugin_file;
+ else
+ throw new Exception('Tests require a working installation of WordPress.');
+
+ // Make sure plugin is active
+ if (!is_plugin_active('mtv/wp-plugin.php'))
+ throw new Exception('Tests require MTV plugin is installed and activated.');
+}
+
+function reset_app_globals() {
+ global $registered_apps, $twig, $apps;
+
+ // Reset registered and installed apps
+ $apps = array();
+ $registered_apps = array();
+ $twig = null;
+}
+
View
32 mtv/tests/lib/cli-load.php
@@ -0,0 +1,32 @@
+<?php
+
+/*
+ * This script load WordPress for use in cli
+ */
+
+if ( !defined('STDIN') )
+ die("Please run this script from the command line.");
+
+$settings_file = 'settings.json';
+
+global $settings;
+$settings_path = dirname( __DIR__ ) .'/'. $settings_file;
+$settings = json_decode( file_get_contents( $settings_path ), true );
+
+define('DOING_AJAX', true);
+define('WP_USE_THEMES', false);
+
+global $_SERVER;
+$_SERVER["HTTP_HOST"] = $settings['hostname'];
+$_SERVER["SERVER_NAME"] = $settings['hostname'];
+$_SERVER["REQUEST_URI"] = "/cli";
+$_SERVER["REQUEST_METHOD"] = "GET";
+
+$abspath = dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))));
+define( 'ABSPATH', $abspath . '/' );
+
+require_once(ABSPATH.'wp-config.php');
+
+if ( ! defined('WP_SITEURL') )
+ define( 'WP_SITEURL', "http://".$settings['hostname']);
+
View
3  mtv/tests/settings.json
@@ -0,0 +1,3 @@
+{
+ "hostname": "mtv.dev"
+}
View
8 mtv/tests/test_app/models.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace test_app\models;
+
+use mtv\models\Model;
+
+class TestModel extends Model {}
+
View
6 mtv/tests/test_app/urls.php
@@ -0,0 +1,6 @@
+<?php
+
+$url_patterns = array(
+ '/^$/' =>
+ 'test_app\views\home',
+);
View
7 mtv/tests/test_app/views.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace test_app\views;
+
+function home() {
+ print "OK: Home";
+}
View
22 mtv/wp/templates/404.html
@@ -0,0 +1,22 @@
+<!DOCTYPE HTML>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>Page Not Found</title>
+ <style type="text/css">
+ body {font-family:Helevetica Neue, Helevetica, Arial,
+ sans-serif; font-size:14px; line-height:1.4em;
+ background-color:#efefef; color:#333; text-shadow:0 1px 0
+ white; padding:0 20%;}
+ pre {font-family:Monaco, fixed; border:1px solid #dfdfdf;
+ background-color:white; padding:10px; margin:5px 0 20px 0;
+ overflow:auto;}
+ h1 {line-height:1.2em;}
+ h4 {margin:20px 0 0 0;}
+ </style>
+</head>
+<body>
+ <h3>404 Page not found</h3>
+</body>
+</html>
+
View
3  requirements.sh
@@ -0,0 +1,3 @@
+# May need to sudo this
+pear channel-discover pear.phpunit.de
+pear install --force --alldeps phpunit/PHPUnit
Please sign in to comment.
Something went wrong with that request. Please try again.