OSDN Git Service

MyTest
authorCake <cake_67@users.sourceforge.jp>
Fri, 18 Feb 2011 14:18:43 +0000 (23:18 +0900)
committerCake <cake_67@users.sourceforge.jp>
Fri, 18 Feb 2011 14:18:43 +0000 (23:18 +0900)
app/plugins/my_test/config/bootstrap.php [new file with mode: 0644]
app/plugins/my_test/config/schema/empty [new file with mode: 0644]
app/plugins/my_test/libs/component_test_case.php [new file with mode: 0644]
app/plugins/my_test/libs/empty [new file with mode: 0644]
app/plugins/my_test/libs/my_test_case.php [new file with mode: 0644]
app/plugins/my_test/libs/my_test_suite_dispatcher.php [new file with mode: 0644]
app/plugins/my_test/vendors/empty [new file with mode: 0644]
app/plugins/my_test/vendors/shells/my_test.php [new file with mode: 0644]
app/plugins/my_test/vendors/shells/tasks/empty [new file with mode: 0644]

diff --git a/app/plugins/my_test/config/bootstrap.php b/app/plugins/my_test/config/bootstrap.php
new file mode 100644 (file)
index 0000000..8bea0a6
--- /dev/null
@@ -0,0 +1,19 @@
+<?php 
+/*
+ * My Test Plugin
+ * 
+ * CakePHP 1.3 + PHP 5
+ * 
+ * @category Bootstrap
+ * @copyright Copyright 2010, Takayuki Miwa http://github.com/tkyk/
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ * @refer URL http://wp.serpere.info/archives/1750
+ *            http://wp.serpere.info/archives/1786
+ *            http://wp.serpere.info/archives/1192
+ */
+
+App::import('lib', array(
+       'MyTest.MyTestCase',
+       'MyTest.ComponentTestCase'
+));
+
diff --git a/app/plugins/my_test/config/schema/empty b/app/plugins/my_test/config/schema/empty
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app/plugins/my_test/libs/component_test_case.php b/app/plugins/my_test/libs/component_test_case.php
new file mode 100644 (file)
index 0000000..d86c58b
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * CakePHP 1.3 + PHP 5
+ * 
+ * @copyright Copyright 2010, Takayuki Miwa http://github.com/tkyk/
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+
+class ComponentTestCase_TestController extends Controller {}
+
+abstract class ComponentTestCase extends CakeTestCase {
+    var $Controller;
+    var $components = array();
+    var $uses = array();
+    var $controllerClassName = 'ComponentTestCase_TestController';
+  
+    protected function _createController($components, $controllerCn = null) {
+        $cls = empty($controllerCn) ? $this->controllerClassName : $controllerCn;
+        $Controller = new $cls;
+        if(empty($Controller->uses)) {
+            $Controller->uses = $this->uses;
+        }
+        $Controller->components = $components;
+        $Controller->constructClasses();
+        $Controller->Component->initialize($Controller);
+        return $Controller;
+    }
+
+    function startTest($m) {
+        if(!empty($this->components)) {
+            $this->Controller = $this->_createController($this->components);
+            $this->__setComponents();
+        }
+    }
+
+    private function __setComponents($unset=false) {
+        foreach(Set::normalize($this->components) as $key => $_x) {
+            list($plugin, $key) = pluginSplit($key);
+            if($unset) {
+                unset($this->{$key});
+            } else {
+                $this->{$key} = $this->Controller->{$key};
+            }
+        }
+    }
+
+   function endTest() {
+        if(!empty($this->Controller)) {
+            unset($this->Controller);
+            $this->__setComponents(true);
+        }
+        ClassRegistry::flush();
+    }
+}
diff --git a/app/plugins/my_test/libs/empty b/app/plugins/my_test/libs/empty
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app/plugins/my_test/libs/my_test_case.php b/app/plugins/my_test/libs/my_test_case.php
new file mode 100644 (file)
index 0000000..653f198
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * My Test Plugin
+ * CakePHP 1.3 + PHP 5
+ * 
+ * @copyright Copyright 2010, Takayuki Miwa http://github.com/tkyk/
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+
+abstract class MyTestCase extends CakeTestCase {
+
+       /* Auto Fixture Loader */
+       function _initDb() {
+               parent::_initDb();
+
+               $exclude = array();
+               if(isset($this->fixtures['EXCLUDE'])) {
+                       $exclude = $this->fixtures['EXCLUDE'];
+                       unset($this->fixtures['EXCLUDE']);
+               }
+
+               if(isset($this->fixtures['IMPORT'])) {
+                       $from = $this->fixtures['IMPORT'];
+                       unset($this->fixtures['IMPORT']);
+                       $this->_importTables($from);
+               }
+
+               $this->fixtures = array_diff(array_unique($this->fixtures), $exclude);
+       }
+
+       function _importTables($from) {
+               $defaultDb = ConnectionManager::getDataSource($from);
+               foreach($defaultDb->listSources() as $table) {
+var_dump($table);
+                       $this->fixtures[] = "app.". Inflector::singularize($table);
+               }
+       }
+
+       function _loadFixtures() {
+               parent::_loadFixtures();
+
+               if (empty($this->_fixtures)) {
+                       $this->_fixtures = array();
+               }
+
+               foreach($this->fixtures as $fixture) {
+                       if(isset($this->_fixtures[$fixture])) {
+                               continue;
+                       }
+                       if(preg_match('/^app\.(\w+)$/', $fixture, $m)) {
+                               $this->_generateFixture($fixture, $m[1]);
+                       }
+               }
+
+               if (empty($this->_fixtures)) {
+                       unset($this->_fixtures);
+               }
+       }
+
+       function _generateFixture($fixture, $name) {
+               $Name = Inflector::camelize($name);
+               $table = Inflector::tableize($Name);
+
+               $fixtureClass = $Name . 'Fixture';
+               $schemaVar = '$import';
+               $schema = array('table' => $table);
+
+               if(!class_exists($fixtureClass)) {
+                       $code = '
+class '. $fixtureClass .' extends CakeTestFixture {
+var $name = "'. $Name .'";
+var '. $schemaVar .' = '. var_export($schema, true) .';
+}
+';
+                       eval($code);
+               }
+               $this->_fixtures[$fixture] =& new $fixtureClass($this->db);
+               $this->_fixtureClassMap[$Name] = $fixture;
+       }
+
+}
diff --git a/app/plugins/my_test/libs/my_test_suite_dispatcher.php b/app/plugins/my_test/libs/my_test_suite_dispatcher.php
new file mode 100644 (file)
index 0000000..a1cf1af
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+/*
+ * My Test Plugin
+ * 
+ * CakePHP 1.3 + PHP 5
+ * 
+ * @category Dispatcher
+ * @copyright Copyright 2010, Takayuki Miwa http://github.com/tkyk/
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ * @refer URL http://wp.serpere.info/archives/1750
+ *            http://wp.serpere.info/archives/1786
+ *            http://wp.serpere.info/archives/1192
+ */
+
+class MyTestSuiteDispatcher extends CakeTestSuiteDispatcher {
+       function _parseParams() {
+               parent::_parseParams();
+               require_once(dirname(__FILE__) .'/../config/bootstrap.php');
+       }
+}
diff --git a/app/plugins/my_test/vendors/empty b/app/plugins/my_test/vendors/empty
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app/plugins/my_test/vendors/shells/my_test.php b/app/plugins/my_test/vendors/shells/my_test.php
new file mode 100644 (file)
index 0000000..3fbcfac
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+require_once CONSOLE_LIBS . 'testsuite.php';
+
+class MyTestShell extends TestSuiteShell {
+       function initialize() {
+               parent::initialize();
+               require_once(dirname(__FILE__) .'/../../config/bootstrap.php');
+       }
+}
+
diff --git a/app/plugins/my_test/vendors/shells/tasks/empty b/app/plugins/my_test/vendors/shells/tasks/empty
new file mode 100644 (file)
index 0000000..e69de29