Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Import from http://openpear.org/package/Net_URL_Dispatcher

  • Loading branch information...
commit f37b7e1aaf322c49831d358ae6f75daf20bdeb67 0 parents
@heavenshell authored
Showing with 3,669 additions and 0 deletions.
  1. +3 −0  .gitignore
  2. +21 −0 ChangeLog
  3. +26 −0 README
  4. +4 −0 doc/ja/README
  5. +667 −0 src/Net/URL/Dispatcher.php
  6. +165 −0 src/Net/URL/Dispatcher/Abstract.php
  7. +82 −0 src/Net/URL/Dispatcher/Action.php
  8. +82 −0 src/Net/URL/Dispatcher/Controller.php
  9. +58 −0 src/Net/URL/Dispatcher/Exception.php
  10. +361 −0 tests/Dispatcher/ActionTest.php
  11. +434 −0 tests/Dispatcher/ControllerTest.php
  12. +202 −0 tests/DispatcherTest.php
  13. +75 −0 tests/apps/sample/actions/DefaultparamAction.php
  14. +75 −0 tests/apps/sample/actions/HogeAction.php
  15. +79 −0 tests/apps/sample/actions/ParamAction.php
  16. +75 −0 tests/apps/sample/actions/PostAction.php
  17. +75 −0 tests/apps/sample/actions/PreAction.php
  18. +75 −0 tests/apps/sample/actions/Stack1Action.php
  19. +75 −0 tests/apps/sample/actions/Stack2Action.php
  20. +75 −0 tests/apps/sample/actions/Stack3Action.php
  21. +75 −0 tests/apps/sample/actions/Stack4Action.php
  22. +75 −0 tests/apps/sample/actions/Stack5Action.php
  23. +75 −0 tests/apps/sample/actions/Stack6Action.php
  24. +142 −0 tests/apps/sample/controllers/HogeController.php
  25. +80 −0 tests/apps/sample/controllers/OtherController.php
  26. +75 −0 tests/apps/sample/controllers/PostController.php
  27. +75 −0 tests/apps/sample/controllers/PreController.php
  28. +75 −0 tests/apps/sample/others/FooAction.php
  29. +76 −0 tests/apps/sample/others/FooController.php
  30. +75 −0 tests/apps/sample/others/Sub/BarAction.php
  31. +76 −0 tests/apps/sample/others/Sub/BarController.php
  32. +61 −0 tests/prepare.php
3  .gitignore
@@ -0,0 +1,3 @@
+.swp
+*~
+.DS_Store
21 ChangeLog
@@ -0,0 +1,21 @@
+2010-08-29 Heavens hell <sohyanagi@gmail.com>
+ * Change version number.
+ * Use spl_autoload_register and load() insted of __autload function.
+
+2010-08-08 Heavens hell <sohyanagi@gmail.com>
+ * Change version number.
+ * Add setControllerDirectoryName() enable to set controller directory name.
+ * Add getControllerDirectoryName() enable to get controller directory name.
+ * Add setActionDirectoryName() enable to set action directory name.
+ * Add getActionDirectoryName() enable to get action directory name.
+
+2010-08-05 Heavens hell <sohyanagi@gmail.com>
+ * Change Testing framework PHPUnit.
+ * Change split() to explode().
+
+2009-01-07 Heavens hell <sohyanagi@gmail.com>
+
+ * Add __call method in Net_URL_Dispatcher class.
+ * Add __call specs in DispatcherSpec.php.
+ * Fixed typo arraty to array in Net_URL_Dispatcher _match method's comment.
+ * Fixed Missing return value in Net_URL_Dispatcher dispatchAction method.
26 README
@@ -0,0 +1,26 @@
+Dispatcher library for PHP.
+
+This module can dispatch to controller class or action class from PATH_INFO.
+
+ex)
+ <?php
+ require_once 'Net/URL/Dispatcher.php';
+ $dispatcher = new Net_URL_Dispatcher();
+ $dispatcher->connect(':controller/:action/*params')
+ ->dispatch('/path/to/directory');
+
+
+ <?php
+ class SampleController extends Net_URL_Dispatcher_Controller
+ {
+ public function fooAction()
+ {
+ echo $this->getParam('bar'); // baz
+ }
+ }
+
+
+System requirement
+ PHP 5.2
+ PHPUnit
+ Net_URL_Mapper
4 doc/ja/README
@@ -0,0 +1,4 @@
+PATH_INFO から取得した値をパースしてディスパッチするライブラリです。
+
+コントローラクラスもしくはアクションクラスいずれかを使用できます。
+Net_URL_Mapper に依存しています。
667 src/Net/URL/Dispatcher.php
@@ -0,0 +1,667 @@
+<?php
+/**
+ * Simple dispatcher class
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see Net_URL_Dispatcher_Exception
+ */
+require_once 'Net/URL/Dispatcher/Exception.php';
+
+/**
+ * Net_URL_Dispatcher
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher
+{
+ /**
+ * Net_URL_Dispatcher VERSION
+ */
+ const VERSION = '0.2';
+
+ /**
+ * Router
+ *
+ * @var mixed
+ * @access protected
+ */
+ protected $_router = null;
+
+ /**
+ * Path to controller directory
+ *
+ * @var mixed
+ * @access protected
+ */
+ protected $_directory = null;
+
+ /**
+ * Path info
+ *
+ * @var mixed
+ * @access protected
+ */
+ protected $_pathInfo = null;
+
+ /**
+ * Net_URL_Mapper requested instance id
+ *
+ * @var mixed
+ * @access private
+ */
+ private $_mapperId = null;
+
+ /**
+ * Params
+ *
+ * @var mixed
+ * @access private
+ */
+ private $_params = null;
+
+ /**
+ * Controller directory name
+ *
+ * @var mixed
+ * @access private
+ */
+ private $_controllerDirectryName = null;
+
+ /**
+ * Action directory name
+ *
+ * @var mixed
+ * @access private
+ */
+ private $_actionDirectoryName = null;
+
+ /**
+ * Constructor
+ *
+ * @param string $id Requested instance name
+ * @access public
+ * @return void
+ */
+ public function __construct($id = '__default__')
+ {
+ spl_autoload_register(array('Net_URL_Dispatcher', 'load'));
+ $this->_mapperId = $id;
+ $this->_setRouter();
+ $this->setErrorReporting();
+ }
+
+ /**
+ * Call Net_URL_Mapper's method
+ *
+ * @param mixed $method Net_URL_Mapper's method name
+ * @param mixed $args arguments
+ * @access public
+ * @return mixed Net_URL_Dispatcher Fluent interface or result of method
+ */
+ public function __call($method, $args = null)
+ {
+ $ret = null;
+ if (method_exists($this->_router, $method)) {
+ $ret = call_user_func_array(array($this->_router, $method), $args);
+ if (!is_null($ret)) {
+ return $ret;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Get params
+ *
+ * @access public
+ * @return void
+ */
+ public function getParams()
+ {
+ return $this->_params;
+ }
+
+ /**
+ * Set params
+ *
+ * @param array $value
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setParams(array $value)
+ {
+ $this->_params = $value;
+ return $this;
+ }
+
+ /**
+ * Get controller directory name
+ *
+ * @access public
+ * @return string Controller directory name
+ */
+ public function getControllerDirectoryName()
+ {
+ if (is_null($this->_controllerDirectryName)) {
+ $this->_controllerDirectryName = 'controllers';
+ }
+ return $this->_controllerDirectryName;
+ }
+
+ /**
+ * Set controller directory name
+ *
+ * @param mixed $value Controller path name
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setControllerDirectoryName($value)
+ {
+ $this->_controllerDirectryName = $value;
+ return $this;
+ }
+
+ /**
+ * Get action directory name
+ *
+ * @access public
+ * @return string Action directory name
+ */
+ public function getActionDirectoryName()
+ {
+ if (is_null($this->_actionDirectoryName)) {
+ $this->_actionDirectoryName = 'actions';
+ }
+
+ return $this->_actionDirectoryName;
+ }
+
+ /**
+ * Set action directory name
+ *
+ * @param mixed $value Action directory name
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setActionDirectoryName($value)
+ {
+ $this->_actionDirectoryName = $value;
+ return $this;
+ }
+
+ /**
+ * Set error reporting
+ *
+ * @param mixed $value Error reporting
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setErrorReporting($value = null)
+ {
+ if (is_null($value)) {
+ error_reporting(E_ALL);
+ } else {
+ error_reporting($value);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Set router
+ *
+ * @access protected
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ protected function _setRouter()
+ {
+ require_once 'Net/URL/Mapper.php';
+ $this->_router = Net_URL_Mapper::getInstance($this->_mapperId);
+ return $this;
+ }
+
+ /**
+ * Parses a path and creates a connection
+ *
+ * @param mixed $path The path to connect
+ * @param array $defaults Default values for path parts
+ * @param array $rules Regular expressions for path parts
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function connect($path = null, $defaults = array(), $rules = array())
+ {
+ if (is_null($this->_router)
+ && !$this->_router instanceof Net_URL_Mapper) {
+ throw new Net_URL_Dispatcher_Exception('Router is invalid.');
+ }
+
+ if (is_null($path)) {
+ $path = ':controller/:action/*params';
+ }
+
+ $controller = null;
+ if (is_string($path)) {
+ $map = explode('/', $path);
+ if (substr($map[0], 0, 1) !== ':') {
+ $controller = $map[0];
+ }
+ }
+
+ if (is_array($path)) {
+ $map = '';
+ if (isset($path['controller'])) {
+ $map = $path['controller'];
+ }
+ if (isset($path['action'])) {
+ $map = $map . '/' . $path['action'];
+ }
+ $path = $map;
+ }
+
+ if (count($defaults) === 0) {
+ $defaults = array(
+ 'controller' => $controller,
+ 'action' => 'index',
+ 'params' => null
+ );
+ }
+
+ $this->_router->connect($path, $defaults, $rules);
+ return $this;
+ }
+
+ /**
+ * Matching url to path
+ *
+ * @param mixed $path
+ * @access protected
+ * @return mixed Result of matching
+ */
+ protected function _match($path)
+ {
+ return $this->_router->match($path);
+ }
+
+ /**
+ * Set directory
+ *
+ * @param mixed $path
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setDirectory($path)
+ {
+ $this->_directory = $path;
+ return $this;
+ }
+
+ /**
+ * Get directory
+ *
+ * @access public
+ * @return string Path to directory
+ */
+ public function getDirectory()
+ {
+ return $this->_directory;
+ }
+
+ /**
+ * Set pathinfo
+ *
+ * @param mixed $pathInfo Pathinfo
+ * @access public
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function setPathInfo($pathInfo)
+ {
+ $this->_pathInfo = $pathInfo;
+ return $this;
+ }
+
+ /**
+ * Get pathinfo
+ *
+ * @access public
+ * @return mixed Pathinfo
+ */
+ public function getPathInfo()
+ {
+ $env = (isset($_ENV['PATH_INFO'])) ? $_ENV['PATH_INFO'] : getEnv('PATH_INFO');
+ return is_null($this->_pathInfo) ? ltrim($env, '/') : $this->_pathInfo;
+ }
+
+ /**
+ * Dispatch to controller or action
+ *
+ * @param mixed $directory Path to controllers or actions
+ * @access public
+ * @throws Net_URL_Dispatcher_Exception
+ * @return Net_URL_Dispatcher Fluent interface
+ */
+ public function dispatch($directory = null)
+ {
+ if (is_null($directory)) {
+ $directory = rtrim($this->_directory, '\//') . DIRECTORY_SEPARATOR;
+ } else {
+ $directory = rtrim($directory, '\//') . DIRECTORY_SEPARATOR;
+ $this->_directory = $directory;
+ }
+
+ if (!is_dir($directory)) {
+ throw new Net_URL_Dispatcher_Exception('Directory found.');
+ }
+
+ $path = $this->getPathInfo();
+ $match = $this->_match($path);
+
+ if (is_null($match)) {
+ throw new Net_URL_Dispatcher_Exception('Could not dispatch.');
+ }
+
+ // Get controller
+ $controller = isset($match['controller'])
+ ? ucfirst($match['controller']) . 'Controller'
+ : null;
+
+ // Get action
+ $action = isset($match['action'])
+ ? $match['action'] . 'Action'
+ : null;
+
+ // Get patameters
+ $params = isset($match['params'])
+ ? $match['params']
+ : null;
+
+ $params = $this->parseRequests($params);
+ $params = self::deleteNullByte($params);
+
+ if (!is_null($this->_params)) {
+ if (is_array($this->_params) && is_array($params)) {
+ $params = array_merge($params, $this->_params);
+ }
+ }
+
+ // If $controller is null, use action as class.
+ if (is_null($controller)) {
+ $this->dispatchAction(ucfirst($action), $params, $directory);
+ } else {
+ $this->dispatchController($controller, $action, $params, $directory);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Dispatch to controller class
+ *
+ * @param mixed $controller Controller name
+ * @param mixed $action Action name
+ * @param mixed $params Parameters
+ * @param mixed $directory Path to controllers
+ * @access public
+ * @throws Net_URL_Dispatcher_Exception
+ * @return Net_URL_Dispatcher_Controller
+ */
+ public function dispatchController($controller, $action, $params, $directory = null)
+ {
+ if (!class_exists($controller, false)) {
+ $controllerPath = $directory . $this->getControllerDirectoryName()
+ . DIRECTORY_SEPARATOR . $controller . '.php';
+
+ self::securityCheck($controllerPath);
+ if (!is_file($controllerPath)) {
+ throw new Net_URL_Dispatcher_Exception('File not found.');
+ }
+ require_once $controllerPath;
+ }
+
+ // Create instance
+ $instance = new $controller;
+
+ if (!method_exists($instance, $action)) {
+ throw new Net_URL_Dispatcher_Exception('Action metod not found.');
+ }
+
+ if (method_exists($instance, 'setParams')) {
+ $instance->setParams($params);
+ }
+
+ // Execute preDispatch
+ if (method_exists($instance, 'preDispatch')) {
+ $instance->preDispatch();
+ }
+
+ // Execute action
+ $instance->{$action}();
+
+ // Execute postDispatch
+ if (method_exists($instance, 'postDispatch')) {
+ $instance->postDispatch();
+ }
+
+ if (method_exists($instance, 'getActionStack')) {
+ $actionStack = $instance->getActionStack();
+ if (isset($actionStack['controller'])
+ && !is_null($actionStack['controller'])) {
+ $controller = $actionStack['controller'] . 'Controller';
+ }
+
+ if (isset($actionStack['action'])) {
+ $action = $actionStack['action'] . 'Action';
+ }
+
+ if (isset($actionStack['params'])) {
+ if (is_array($params)) {
+ $params = array_merge($params, $actionStack['params']);
+ } else {
+ $params = $actionStack['params'];
+ }
+ }
+
+ if (!is_null($actionStack)) {
+ $this->dispatchController($controller, $action, $params, $directory);
+ }
+ }
+
+ return $instance;
+ }
+
+ /**
+ * Dispatch to action
+ *
+ * @param mixed $action Action name
+ * @param mixed $params Parameters
+ * @param mixed $directory Path to directory
+ * @access public
+ * @throws Net_URL_Dispatcher_Exception
+ * @return Net_URL_Dispatcher_Action Fluent interface
+ */
+ public function dispatchAction($action, $params, $directory = null)
+ {
+ if (!class_exists($action, false)) {
+ $actionPath = $directory . $this->getActionDirectoryName()
+ . DIRECTORY_SEPARATOR . $action . '.php';
+
+ self::securityCheck($actionPath);
+ if (!is_file($actionPath)) {
+ throw new Net_URL_Dispatcher_Exception('File not found.');
+ }
+ require_once $actionPath;
+ }
+
+ // Create instance
+ $instance = new $action;
+
+ if (method_exists($instance, 'setParams')) {
+ $instance->setParams($params);
+ }
+
+ // Before Execute
+ if (method_exists($instance, 'preExecute')) {
+ $instance->preExecute();
+ }
+
+ // Execute
+ if (method_exists($instance, 'execute')) {
+ $instance->execute();
+ }
+
+ // After Exectute
+ if (method_exists($instance, 'postExecute')) {
+ $instance->postExecute();
+ }
+
+ if (method_exists($instance, 'getActionStack')) {
+ $actionStack = $instance->getActionStack();
+
+ if (isset($actionStack['action'])) {
+ if (isset($actionStack['params'])
+ && is_array($actionStack['params'])) {
+ if (is_array($params)) {
+ $params = array_merge($params, $actionStack['params']);
+ } else {
+ $params = $actionStack['params'];
+ }
+ }
+
+ $stackAction = $actionStack['action'] . 'Action';
+ $this->dispatchAction($stackAction, $params, $directory);
+ }
+ }
+
+ return $instance;
+ }
+
+ /**
+ * Parse requests
+ *
+ * @param mixed $value Path info
+ * @access public
+ * @return mixed array or null
+ */
+ public function parseRequests($value = null)
+ {
+ $requests = array();
+ if (is_string($value)) {
+ $value = ltrim($value, '/');
+ // key/value -> array('key' => 'value');
+ // key/value/key2 -> array('key => 'value', 'key2' => null);
+ $params = explode('/', $value);
+ if (is_array($params)) {
+ $i = 1;
+ foreach ($params as $key => $val) {
+ if (($key % 2) === 0) {
+ $requests[$val] = isset($params[$i]) ? $params[$i] : null;
+ }
+ $i ++;
+ }
+ }
+ }
+
+ if (isset($_GET) && count($_GET) > 0) {
+ $requests = array_merge($requests, $_GET);
+ }
+
+ if (isset($_POST) && count($_POST) > 0) {
+ $requests = array_merge($requests, $_POST);
+ }
+
+ return (is_array($requests) && count($requests) > 0) ? $requests : null;
+ }
+
+ /**
+ * Delete null byte
+ *
+ * @param mixed $value String values
+ * @access public
+ * @return mixed Strings which delete null byte
+ */
+ public static function deleteNullByte($value)
+ {
+ if (is_array($value)) {
+ return array_map('Net_URL_Dispatcher::deleteNullByte', $value);
+ }
+ return str_replace(pack('c', 0), '', $value);
+ }
+
+ /**
+ * Ensure that filename does not contain exploits
+ *
+ * @param mixed $filename
+ * @access public
+ * @throws Net_URL_Dispatcher_Exception
+ * @return void
+ */
+ public static function securityCheck($filename)
+ {
+ if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', $filename)) {
+ throw new Net_URL_Dispatcher_Exception('Illegal chatacter in filename');
+ }
+ }
+
+ /**
+ * Autoload class
+ *
+ * @param mixed $className Class name
+ * @access public
+ * @return void
+ */
+ public static function load($className)
+ {
+ if (!class_exists($className, false)) {
+ $path = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
+ self::securityCheck($path);
+ require_once $path;
+ }
+ }
+}
165 src/Net/URL/Dispatcher/Abstract.php
@@ -0,0 +1,165 @@
+<?php
+/**
+ * Abstract class for controller | action class
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * Net_URL_Dispatcher_Abstract
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+abstract class Net_URL_Dispatcher_Abstract
+{
+ /**
+ * Parameters
+ *
+ * @var mixed
+ * @access protected
+ */
+ protected $_params = null;
+
+ /**
+ * Actions
+ *
+ * @var mixed
+ * @access private
+ */
+ private $_actions = null;
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @return void
+ */
+ public function __construct()
+ {
+ }
+
+ /**
+ * Stack action
+ *
+ * @param mixed $action Action name
+ * @param mixed $controller Controller name
+ * @param mixed $params Parameters
+ * @access public
+ * @return Net_URL_Dispatcher_Abstract Fluent interface
+ */
+ public function actionStack($action, $controller = null, $params = null)
+ {
+ $this->_actions[] = array(
+ 'action' => $action,
+ 'controller' => $controller,
+ 'params' => $params
+ );
+
+ return $this;
+ }
+
+ /**
+ * Get action
+ *
+ * @access public
+ * @return array or null
+ */
+ public function getActionStack()
+ {
+ return is_array($this->_actions) ? array_shift($this->_actions) : null;
+ }
+
+ /**
+ * Set parameters
+ *
+ * @param mixed $value
+ * @access public
+ * @return Net_URL_Dispatcher_Abstract Fluent interface
+ */
+ public function setParams($value)
+ {
+ $this->_params = $value;
+ return $this;
+ }
+
+ /**
+ * Get parameters
+ *
+ * @access public
+ * @return array All parameters
+ */
+ public function getParams()
+ {
+ return $this->_params;
+ }
+
+ /**
+ * Set parameter
+ *
+ * @param mixed $key Key name
+ * @param mixed $value Value
+ * @access public
+ * @return Net_URL_Dispatcher_Abstract Fluent interface
+ */
+ public function setParam($key, $value)
+ {
+ $this->_params[$key] = $value;
+ return $this;
+ }
+
+ /**
+ * Get parameter
+ *
+ * @param mixed $key Parameter key name
+ * @param mixed $default Default value
+ * @access public
+ * @return mixed Parameter
+ */
+ public function getParam($key, $default = null)
+ {
+ return isset($this->_params[$key]) ? $this->_params[$key] : $default;
+ }
+}
82 src/Net/URL/Dispatcher/Action.php
@@ -0,0 +1,82 @@
+<?php
+/**
+ * Base class of action
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see Net_URL_Dispatcher_Abstract
+ */
+require_once 'Abstract.php';
+
+/**
+ * Net_Dispatcher_Action
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher_Action extends Net_URL_Dispatcher_Abstract
+{
+ /**
+ * Pre execute
+ *
+ * @access public
+ * @return void
+ */
+ public function preExecute()
+ {
+ }
+
+ /**
+ * Post execute
+ *
+ * @access public
+ * @return void
+ */
+ public function postExecute()
+ {
+ }
+}
82 src/Net/URL/Dispatcher/Controller.php
@@ -0,0 +1,82 @@
+<?php
+/**
+ * Base class of controller
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see Net_URL_Dispatcher_Abstract
+ */
+require_once 'Abstract.php';
+
+/**
+ * Base class of controller
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher_Controller extends Net_URL_Dispatcher_Abstract
+{
+ /**
+ * Pre dispatch
+ *
+ * @access public
+ * @return void
+ */
+ public function preDispatch()
+ {
+ }
+
+ /**
+ * Post dispatch
+ *
+ * @access public
+ * @return void
+ */
+ public function postDispatch()
+ {
+ }
+}
58 src/Net/URL/Dispatcher/Exception.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Exception
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * Net_URL_Dispatcher_Exception
+ *
+ * @package Net
+ * @category Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher_Exception extends Exception
+{
+}
361 tests/Dispatcher/ActionTest.php
@@ -0,0 +1,361 @@
+<?php
+/**
+ * Simple dispatcher class
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see prepare
+ */
+require_once dirname(dirname(__FILE__)) . '/prepare.php';
+
+/**
+ * DescribeDispatcher
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher_ActionTest extends PHPUnit_Framework_TestCase
+{
+ public function testShouldDispatchToActionClassByServerEnv()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeAction_execute');
+ }
+
+ public function testShouldDispatchToActionClassBySetterMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setPathInfo('hoge');
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeAction_execute');
+ }
+
+ public function testShouldOccurExceptionByWrongActionClassName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'fuga';
+
+ $e = null;
+ try {
+ $dispatcher->connect(':action')->dispatch($path);
+ } catch (Net_URL_Dispatcher_Exception $ex) {
+ $e = $ex;
+ }
+
+ $this->assertTrue($e instanceof Net_URL_Dispatcher_Exception);
+ }
+
+ public function testShouldOccurExceptionByWrongParamter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'fuga/hoge';
+
+ $e = null;
+ try {
+ $dispatcher->connect(':action')->dispatch($path);
+ } catch (Net_URL_Dispatcher_Exception $ex) {
+ $e = $ex;
+ }
+
+ $this->assertTrue($e instanceof Net_URL_Dispatcher_Exception);
+ }
+
+ public function testShouldDispatchToActionClassWithoutConnectMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+
+ ob_start();
+ $dispatcher->dispatchAction('HogeAction', null, $path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeAction_execute');
+ }
+
+ public function testShouldDispatchToActionClassAndRunPreExecuteMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'pre';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'PreAction_preExecute');
+ }
+
+ public function testShouldDispatchToActionClassAndRunPostExecuteMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'post/';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'PostAction_postExecute');
+ }
+
+ public function testShouldDispatchToActionClassWithPathinfoParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param/foo/bar';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'bar');
+ }
+
+ public function testShouldDispatchToActionClassWithGetParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param';
+ $_GET['foo'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'hoge');
+ unset($_GET);
+ }
+
+ public function testShouldDispatchToActionClassWithPostParamter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param';
+ $_POST['foo'] = 'baz';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'baz');
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToActionClassWithPathinfoAndGetParamters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param/hoge/fuga';
+ $_GET['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fugabar');
+ }
+
+ public function testShouldDispatchToActionClassWithPathinfoAndPostParamters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param/hoge/fuga';
+ $_POST['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fugabar');
+ }
+
+ public function testShouldDispatchToActionClassWithGetAndPostParameters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param';
+ $_GET['hoge'] = 'fuga';
+ $_POST['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fugabar');
+ unset($_GET);
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToActionClassWithPathinfoAndGetAndPostParameters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'param/hoge/fuga';
+ $_GET['foo'] = 'bar';
+ $_POST['baz'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fugabarhoge');
+ unset($_GET);
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToActionClassAndCanUseActionStackMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'stack1';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'Stack2Action_execute');
+ }
+
+ public function testShouldDispatchToActionClassAndCanUseActionStackMethodWithParamter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'stack3';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'bar');
+ }
+
+ public function testShouldDispatchToActionClassAndCanUseActionStackMethodWithMetgedParameters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'stack5/hoge/fuga';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar');
+ }
+
+ public function testShouldDispatchToActionClassAndGetDefaultParam()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'defaultparam';
+
+ ob_start();
+ $dispatcher->connect(':action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'bar');
+ }
+
+ public function testShouldDispatchToAnotherDirectory()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setActionDirectoryName('others');
+ $_ENV['PATH_INFO'] = 'foo';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'FooAction_execute');
+ }
+
+ public function testShouldDispatchToAnotherSubDirectory()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setActionDirectoryName('others/Sub');
+ $_ENV['PATH_INFO'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'BarAction_execute');
+ }
+}
434 tests/Dispatcher/ControllerTest.php
@@ -0,0 +1,434 @@
+<?php
+/**
+ * Simple dispatcher class
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see prepare
+ */
+require_once dirname(dirname(__FILE__)) . '/prepare.php';
+
+/**
+ * DescribeDispatchToController
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_Dispatcher_ControllerTest extends PHPUnit_Framework_TestCase
+{
+ public function testShouldDispatchToControllerClassByServerEnv()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = '/hoge/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassBySetterMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setPathInfo('hoge/index');
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassWhichCallsStatic()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setPathInfo('hoge/index');
+
+ ob_start();
+ $dispatcher->connect('hoge/:action/')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassByServerEnvWhichCallStatic()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/index';
+
+ ob_start();
+ $dispatcher->connect('hoge/:action/')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassByDefaultAction()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldOccurExceptionByWrongControllerClassName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'fuga';
+
+ $e = null;
+ try {
+ $dispatcher->connect(':controller/:action/')->dispatch($path);
+ } catch (Net_URL_Dispatcher_Exception $ex) {
+ $e = $ex;
+ }
+
+ $this->assertTrue($e instanceof Net_URL_Dispatcher_Exception);
+ }
+
+ public function testShouldOccurExceptionByWrongActionMethodName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/fuga';
+
+ $e = null;
+ try {
+ $dispatcher->connect(':controller/:action/')->dispatch($path);
+ } catch (Net_URL_Dispatcher_Exception $ex) {
+ $e = $ex;
+ }
+
+ $this->assertTrue($e instanceof Net_URL_Dispatcher_Exception);
+ }
+
+ public function testShouldOccurExceptionByWrongParamter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/index/foo/bar';
+
+ $e = null;
+ try {
+ $dispatcher->connect(':controller/:action/id')->dispatch($path);
+ } catch (Net_URL_Dispatcher_Exception $ex) {
+ $e = $ex;
+ }
+ $this->assertTrue($e instanceof Net_URL_Dispatcher_Exception);
+ }
+
+ public function testShouldDispatchToControllerClassWithoutConnectMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+
+ ob_start();
+ $dispatcher->dispatchController('HogeController', 'indexAction', null, $path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassAndRunPredispatchMethod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'pre/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'PreController_preDispatch');
+ }
+
+ public function testShouldDispatchToControllerClassAndRunPostdispatchMedhod()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'post/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'PostController_postDispatch');
+ }
+
+ public function testShouldDispatchToControllerClassWithPathinfoParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/param/foo/bar';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'bar');
+ }
+
+ public function testShouldDispatchToControllerClassWithGetParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/get';
+ $_GET['bar'] = 'foo';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'foo');
+ unset($_GET);
+ }
+
+ public function testShouldDispatchToControllerClassWithPostParamter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/post';
+ $_POST['fuga'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'hoge');
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToControllerClassWithPathinfoAndGetParamters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/mix/hoge/fuga';
+ $_GET['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar');
+ unset($_GET);
+ }
+
+ public function testShouldDispatchToControllerClassWithPathinfoAndPostParamters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/mix/hoge/fuga';
+ $_POST['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar');
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToControllerClassWithGetAndPostParameters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/mix/';
+ $_GET['hoge'] = 'fuga';
+ $_POST['foo'] = 'bar';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar');
+ unset($_GET);
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToControllerClassWithPathinfoAndGetAndPostParameters()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/pathinfogetpost/hoge/fuga';
+ $_GET['foo'] = 'bar';
+ $_POST['baz'] = 'hoge';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar_hoge');
+ unset($_GET);
+ unset($_POST);
+ }
+
+ public function testShouldDispatchToControllerClassAndCanUseActionStackToSameClass()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/stack1';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'HogeController_stack2Action');
+ }
+
+ public function testShouldDispatchToControllerClassAndCanUseActionStackToSameCalssWithParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/stack3';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'bar');
+ }
+
+ public function testShouldDispatchToControllerClassAndCanUseActionStackToOtherClass()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/stack5';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'OtherController_indexAction');
+ }
+
+ public function testShouldDispatchToControllerClassAndCanUseActionStackToOtherClassWithParameter()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/stack6';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'foo');
+ }
+
+ public function testShouldDispatchToControllerClassAndGetDefaultParam()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/getdefaultparam';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga');
+ }
+
+ public function testShouldDispatchToAnotherDirectory()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setControllerDirectoryName('others');
+ $_ENV['PATH_INFO'] = '/foo/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'FooController_indexAction');
+ }
+
+ public function testShouldDispatchToAnotherSubDirectory()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setControllerDirectoryName('others/Sub');
+ $_ENV['PATH_INFO'] = '/bar/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'BarController_indexAction');
+ }
+}
202 tests/DispatcherTest.php
@@ -0,0 +1,202 @@
+<?php
+/**
+ * Simple dispatcher class
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see prepare
+ */
+require_once 'prepare.php';
+
+/**
+ * DescribeDispatcher
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class Net_URL_DispatcherTest extends PHPUnit_Framework_TestCase
+{
+ public function testShouldGetVersion()
+ {
+ $this->assertSame(Net_URL_Dispatcher::VERSION, '0.2');
+ }
+
+ public function testShouldCreateInstance()
+ {
+ $dispatcher = new Net_URL_Dispatcher();
+ $this->assertTrue($dispatcher instanceof Net_URL_Dispatcher);
+ }
+
+ public function testShouldSetDirecotryPath()
+ {
+ $path = dirname(__FILE__) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setDirectory($path);
+ $exceptPath = $dispatcher->getDirectory();
+
+ $this->assertSame($exceptPath, $path);
+ }
+
+ public function testShouldSetDirecotryPathInDispatchMethod()
+ {
+ $path = dirname(__FILE__) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/index';
+
+ ob_start();
+ $dispatcher->connect(':controller/:action')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($dispatcher->getDirectory(), $path);
+ }
+
+ public function testShouldDefaultErrorReportingBeE_all()
+ {
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $this->assertSame(error_reporting(), E_ALL);
+ }
+
+ public function testShouldSetErrorReporting()
+ {
+ $dispatcher = new Net_URL_Dispatcher();
+ $dispatcher->setErrorReporting(E_ALL|E_NOTICE);
+
+ $this->assertSame(error_reporting(), E_ALL|E_NOTICE);
+
+ }
+
+ public function testShouldSetParams()
+ {
+ $path = dirname(__FILE__) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $_ENV['PATH_INFO'] = 'hoge/mix/hoge/fuga';
+
+ ob_start();
+ $dispatcher->setParams(array('foo' => 'bar'))->connect(':controller/:action/*params')->dispatch($path);
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ $this->assertSame($buffer, 'fuga_bar');
+ }
+
+ public function testShouldCallNet_url_mapperClassMethodWhichHasRetrurnValue()
+ {
+ $path = dirname(__FILE__) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $buffer = $dispatcher->getId();
+
+ $this->assertSame($buffer, __METHOD__);
+ }
+
+ public function testShouldCallNet_url_mapperClassMethodWhichHasNoReturnValue()
+ {
+ $path = dirname(__FILE__) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $buffer = $dispatcher->setScriptname('index.php');
+
+ $this->assertTrue($buffer instanceof Net_URL_Dispatcher);
+ }
+
+ public function testShouldSetControllerDirectoryName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setControllerDirectoryName('foo');
+ $directroy = $dispatcher->getControllerDirectoryName();
+
+ $this->assertSame('foo', $directroy);
+ }
+
+ public function testShouldGetControllerDirectroryName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setControllerDirectoryName('bar');
+ $directroy = $dispatcher->getControllerDirectoryName();
+
+ $this->assertSame('bar', $directroy);
+ }
+
+ public function testShouldGetDefaultControllerDirectroyName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $directroy = $dispatcher->getControllerDirectoryName();
+
+ $this->assertSame('controllers', $directroy);
+ }
+
+ public function testShouldSetActionDirectoryName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setActionDirectoryName('foo');
+ $directroy = $dispatcher->getActionDirectoryName();
+
+ $this->assertSame('foo', $directroy);
+ }
+
+ public function testShouldGetActionDirectroryName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $dispatcher->setActionDirectoryName('bar');
+ $directroy = $dispatcher->getActionDirectoryName();
+
+ $this->assertSame('bar', $directroy);
+ }
+
+ public function testShouldGetDefaultActionDirectroyName()
+ {
+ $path = dirname(dirname(__FILE__)) . '/apps/sample/';
+ $dispatcher = new Net_URL_Dispatcher(__METHOD__);
+ $directroy = $dispatcher->getActionDirectoryName();
+
+ $this->assertSame('actions', $directroy);
+ }
+}
75 tests/apps/sample/actions/DefaultparamAction.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Spec for DefaultparamAction
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Shinya Ohyanagi nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+
+/**
+ * @see Net_URL_Dispatcher_Action
+ */
+require_once 'Net/URL/Dispatcher/Action.php';
+
+/**
+ * HogeAction
+ *
+ * @category Net
+ * @package Net_URL_Dispatcher
+ * @version $id$
+ * @copyright 2009-2010 Shinya Ohyanagi
+ * @author Shinya Ohyanagi <sohyanagi@gmail.com>
+ * @license New BSD License
+ */
+class DefaultparamAction extends Net_URL_Dispatcher_Action
+{
+ public function preExecute()
+ {
+ }
+
+ public function execute()
+ {
+ echo $this->getParam('foo', 'bar');
+ }
+
+ public function postExecute()
+ {
+ }
+}
75 tests/apps/sample/actions/HogeAction.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Spec for HogeAction
+ *
+ * PHP version 5.2
+ *
+ * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+