[198] r397@phpspec (orig r198): padraic.brady | 2008-01-15 10:21:38 -0700

0 views
Skip to first unread message

nor...@domain51.com

unread,
Jan 15, 2008, 12:22:07 PM1/15/08
to phpspec...@googlegroups.com
Revision
198
Author
phpspec
Date
2008-01-15 10:22:06 -0700 (Tue, 15 Jan 2008)

Log Message

 r397@phpspec (orig r198):  padraic.brady | 2008-01-15 10:21:38 -0700
 * Just need to fix context setup
 * Misc edits and fixups

Modified Paths

Added Paths

Property Changed

Diff

Property changes:


Name: svk:merge
   - d6e91ea2-e33a-0410-98df-1d493bd67c58:/:197
   + d6e91ea2-e33a-0410-98df-1d493bd67c58:/:198

Modified: branches/zend/specs/ContextZendSpec.php (197 => 198)


--- branches/zend/specs/ContextZendSpec.php	2008-01-14 22:25:03 UTC (rev 197)
+++ branches/zend/specs/ContextZendSpec.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -2,6 +2,8 @@
 
 require_once 'SpecHelper.php';
 
+require_once dirname(__FILE__) . '/_zend/application/Bootstrap.php';
+
 class DescribeContextZend extends PHPSpec_Context
 {
 
@@ -25,14 +27,6 @@
         $this->spec($context->getController())->should->be('Bar');
     }
 
-    public function itShouldCreateRequestBeforeEachExample()
-    {
-        $context = new DescribeFooController;
-        $context->beforeEach();
-        $this->spec($context->request())->should
-            ->beAnInstanceOf('PHPSpec_Context_Zend_Request');
-    }
-
     public function itShouldAllowSettingModuleDirs()
     {
         PHPSpec_Context_Zend::addModuleDirectory('/path/to/module');
@@ -40,6 +34,23 @@
             ->be(array('/path/to/module'));
     }
 
+    public function itShouldAllowSettingSetupCallbackFunction() 
+    {
+        PHPSpec_Context_Zend::setFrontControllerSetupCallback(
+            array('Boostrap','prepare')
+        );
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $this->spec(
+            Zend_Controller_Front::getInstance()->getControllerDirectory()
+            )->should->be(
+                array('default'=>dirname(__FILE__) . DIRECTORY_SEPARATOR
+                . '_zend' . DIRECTORY_SEPARATOR
+                . 'application' . DIRECTORY_SEPARATOR
+                . 'controllers')
+        );
+    }
+
     public function itShouldClearFrontControllerBeforeEachExample()
     {
         PHPSpec_Context_Zend::addModuleDirectory(
@@ -47,8 +58,9 @@
         );
         $context = new DescribeFooController;
         $context->beforeEach();
-        $this->spec(Zend_Controller_Front::getInstance()->getControllerDirectory())
-            ->should->be(
+        $this->spec(
+            Zend_Controller_Front::getInstance()->getControllerDirectory()
+            )->should->be(
                 array('Default'=>dirname(__FILE__) . DIRECTORY_SEPARATOR
                 . '_modules' . DIRECTORY_SEPARATOR
                 . 'Default' . DIRECTORY_SEPARATOR
@@ -58,12 +70,7 @@
 
     public function itShouldFormulateAndDispatchGetRequest()
     {
-        PHPSpec_Context_Zend::addControllerDirectory(
-            dirname(__FILE__)
-            . DIRECTORY_SEPARATOR . '_zend'
-            . DIRECTORY_SEPARATOR . 'application'
-            . DIRECTORY_SEPARATOR . 'controllers'
-        );
+        $this->setControllerDirectory();
         $context = new DescribeFooController;
         $context->beforeEach();
         $context->setController('index');
@@ -71,11 +78,64 @@
         $this->spec($response)->should->match("/This is Index/");
     }
 
+    public function itShouldFormulateAndDispatchPostRequest()
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $context->setController('index');
+        $response = $context->post('index');
+        $this->spec($response)->should->match("/This is Index/");
+    }
+
+    public function itShouldFormulateAndDispatchRequestWithUserParams() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $context->setController('index');
+        $response = $context->get('userparam', array(), array('text'=>'This is User Param'));
+        $this->spec($response)->should->match("/This is User Param/");
+    }
+
+    public function itShouldDispatchARelativeUriPath() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $response = $context->post('/index/userparam/text/userparampage');
+        $this->spec($response)->should->match("/userparampage/");
+    }
+
+    public function itShouldDispatchRelativePathWithoutOpeningSlash() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $response = $context->post('index/userparam/text/userparampage');
+        $this->spec($response)->should->match("/userparampage/");
+    }
+
+    public function itShould() 
+    {
+    }
+
     public function after()
     {
         PHPSpec_Context_Zend::clearModuleDirectories();
         PHPSpec_Context_Zend::clearControllerDirectories();
+        PHPSpec_Context_Zend::setFrontControllerSetupCallback(array());
     }
+
+    public function setControllerDirectory() 
+    {
+        PHPSpec_Context_Zend::addControllerDirectory(
+            dirname(__FILE__)
+            . DIRECTORY_SEPARATOR . '_zend'
+            . DIRECTORY_SEPARATOR . 'application'
+            . DIRECTORY_SEPARATOR . 'controllers'
+        );
+    }
 }
 
 /**

Added: branches/zend/specs/_zend/application/Bootstrap.php (0 => 198)


--- branches/zend/specs/_zend/application/Bootstrap.php	                        (rev 0)
+++ branches/zend/specs/_zend/application/Bootstrap.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -0,0 +1,24 @@
+<?php
+
+require_once 'Zend/Controller/Front.php';
+
+class Bootstrap
+{
+
+    public static function prepare()
+    {
+        error_reporting(E_ALL|E_STRICT);
+        date_default_timezone_set('Europe/London');
+
+        // setup front controller
+        $frontController = Zend_Controller_Front::getInstance();
+        $frontController->throwExceptions(true);
+        $frontController->setControllerDirectory('./application/controllers');
+    }
+
+    public static function dispatch($frontController) 
+    {
+        Zend_Controller_Front::getInstance()->dispatch();
+    }
+
+}
\ No newline at end of file

Modified: branches/zend/specs/_zend/application/controllers/IndexController.php (197 => 198)


--- branches/zend/specs/_zend/application/controllers/IndexController.php	2008-01-14 22:25:03 UTC (rev 197)
+++ branches/zend/specs/_zend/application/controllers/IndexController.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -8,4 +8,9 @@
         $this->view->text = 'This is Index';
     }
 
+    public function userparamAction() 
+    {
+        $this->view->text = $this->getRequest()->text;
+    }
+
 }
\ No newline at end of file

Added: branches/zend/specs/_zend/application/run.php (0 => 198)


--- branches/zend/specs/_zend/application/run.php	                        (rev 0)
+++ branches/zend/specs/_zend/application/run.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -0,0 +1,6 @@
+<?php
+
+require_once dirname(__FILE__) . '/Bootstrap.php';
+
+$front = Bootstrap::prepare();
+Bootstrap::dispatch($front);
\ No newline at end of file

Added: branches/zend/specs/_zend/application/views/scripts/index/userparam.phtml (0 => 198)


--- branches/zend/specs/_zend/application/views/scripts/index/userparam.phtml	                        (rev 0)
+++ branches/zend/specs/_zend/application/views/scripts/index/userparam.phtml	2008-01-15 17:22:06 UTC (rev 198)
@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>user param</title>
+</head>
+<body>
+<p><?php echo $this->escape($this->text); ?></p>
+</body>
+</html>
\ No newline at end of file

Modified: branches/zend/specs/_zend/index.php (197 => 198)


--- branches/zend/specs/_zend/index.php	2008-01-14 22:25:03 UTC (rev 197)
+++ branches/zend/specs/_zend/index.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -1,23 +1,3 @@
 <?php
 
-class Psz_Bootstrap
-{
-
-    public static function bootstrap()
-    {
-        error_reporting(E_ALL|E_STRICT);
-        date_default_timezone_set('Europe/London');
-        // assume Zend Framework on default include_path
-        require_once 'Zend/Controller/Front.php';
-
-        // setup front controller
-        $frontController = Zend_Controller_Front::getInstance();
-        $frontController->throwExceptions(true);
-        $frontController->setControllerDirectory('./application/controllers');
-
-        $frontController->dispatch();
-    }
-
-}
-
-Psz_Bootstrap::bootstrap();
\ No newline at end of file
+include_once './application/Bootstrap.php';
\ No newline at end of file

Modified: branches/zend/src/PHPSpec/Context/Zend.php (197 => 198)


--- branches/zend/src/PHPSpec/Context/Zend.php	2008-01-14 22:25:03 UTC (rev 197)
+++ branches/zend/src/PHPSpec/Context/Zend.php	2008-01-15 17:22:06 UTC (rev 198)
@@ -1,199 +1,220 @@
-<?php
-/**
- * PHPSpec
- *
- * LICENSE
- *
- * This file is subject to the GNU Lesser General Public License Version 3
- * that is bundled with this package in the file LICENSE.
- * It is also available through the world-wide-web at this URL:
- * http://www.gnu.org/licenses/lgpl-3.0.txt
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to lic...@phpspec.org so we can send you a copy immediately.
- *
- * @category   PHPSpec
- * @package    PHPSpec
- * @copyright  Copyright (c) 2007 Pádraic Brady, Travis Swicegood
- * @license    http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
- */
-
-require_once 'Zend/Controller/Front.php';
-
-/**
- * @category   PHPSpec
- * @package    PHPSpec
- * @copyright  Copyright (c) 2007 Pádraic Brady, Travis Swicegood
- * @license    http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
- */
-class PHPSpec_Context_Zend extends PHPSpec_Context
-{
-    
-    protected static $_moduleDirectories = array();
-    
-    protected static $_controllerDirectories = array();
-    
-    /**
-     * Name of the Controller being specified; captured usually
-     * from the Context classname.
-     *
-     * @var string
-     */
-    protected $_controller = '';
-    
-    /**
-     * Zend Framework; instance of Front Controller
-     *
-     * @var Zend_Controller_Front
-     */
-    protected $_frontController = null;
-    
-    /**
-     * Zend Framework; instance of HTTP Request
-     *
-     * @var Zend_Controller_Request_Http
-     */
-    protected $_request = null;
-    
-    /**
-     * Zend Framework; instance of HTTP Response
-     *
-     * @var Zend_Controller_Response_Http
-     */
-    protected $_response = null;
-    
-    public function __construct()
-    {
-        parent::__construct();
-        $this->_setController();
-        $this->_frontController = Zend_Controller_Front::getInstance();
-    }
-    
-    public static function addModuleDirectory($path)
-    {
-        self::$_moduleDirectories[] = $path;
-    }
-    
-    public static function getModuleDirectories()
-    {
-        return self::$_moduleDirectories;
-    }
-    
-    public static function clearModuleDirectories()
-    {
-        self::$_moduleDirectories = array();
-    }
-    
-    public static function addControllerDirectory($path, $module = null)
-    {
+<?php
+/**
+ * PHPSpec
+ *
+ * LICENSE
+ *
+ * This file is subject to the GNU Lesser General Public License Version 3
+ * that is bundled with this package in the file LICENSE.
+ * It is also available through the world-wide-web at this URL:
+ * http://www.gnu.org/licenses/lgpl-3.0.txt
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to lic...@phpspec.org so we can send you a copy immediately.
+ *
+ * @category   PHPSpec
+ * @package    PHPSpec
+ * @copyright  Copyright (c) 2007 Pádraic Brady, Travis Swicegood
+ * @license    http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
+ */
+
+require_once 'Zend/Controller/Front.php';
+
+require_once 'Zend/Controller/Request/Http.php';
+
+/**
+ * @category   PHPSpec
+ * @package    PHPSpec
+ * @copyright  Copyright (c) 2007 Pádraic Brady, Travis Swicegood
+ * @license    http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
+ */
+class PHPSpec_Context_Zend extends PHPSpec_Context
+{
+
+    const BASE_URL = 'http://www.example.com';
+    
+    protected static $_moduleDirectories = array();
+    
+    protected static $_controllerDirectories = array();
+
+    protected static $_frontControllerSetupCallback = array();
+    
+    /**
+     * Name of the Controller being specified; captured usually
+     * from the Context classname.
+     *
+     * @var string
+     */
+    protected $_controller = '';
+    
+    /**
+     * Zend Framework; instance of Front Controller
+     *
+     * @var Zend_Controller_Front
+     */
+    protected $_frontController = null;
+    
+    /**
+     * Zend Framework; instance of HTTP Request
+     *
+     * @var Zend_Controller_Request_Http
+     */
+    protected $_request = null;
+    
+    /**
+     * Zend Framework; instance of HTTP Response
+     *
+     * @var Zend_Controller_Response_Http
+     */
+    protected $_response = null;
+    
+    public function __construct()
+    {
+        parent::__construct();
+        $this->_setController();
+        $this->_frontController = Zend_Controller_Front::getInstance();
+    }
+
+    public static function setFrontControllerSetupCallback(array $callback) 
+    {
+        self::$_frontControllerSetupCallback = $callback;
+    }
+    
+    public static function addModuleDirectory($path)
+    {
+        self::$_moduleDirectories[] = $path;
+    }
+    
+    public static function getModuleDirectories()
+    {
+        return self::$_moduleDirectories;
+    }
+    
+    public static function clearModuleDirectories()
+    {
+        self::$_moduleDirectories = array();
+    }
+    
+    public static function addControllerDirectory($path, $module = null)
+    {
         if (!isset($module)) {
         	$module = 'NULL';
         }
-        
-        self::$_controllerDirectories[$module] = $path;
-    }
-    
-    public static function getControllerDirectories()
-    {
-        return self::$_controllerDirectories;
-    }
-    
-    public static function clearControllerDirectories()
-    {
-        self::$_controllerDirectories = array();
-    }
-    
-    public function beforeEach()
-    {
-        $_GET = array();
-        $_POST = array();
-        $_COOKIE = array();
-        $this->_request = new PHPSpec_Context_Zend_Request;
-        $this->_clearFrontController();
-    }
-    
-    public function get($actionName, array $getArray = null, array $paramArray = null)
-    {
+        
+        self::$_controllerDirectories[$module] = $path;
+    }
+    
+    public static function getControllerDirectories()
+    {
+        return self::$_controllerDirectories;
+    }
+    
+    public static function clearControllerDirectories()
+    {
+        self::$_controllerDirectories = array();
+    }
+    
+    public function beforeEach()
+    {
+        $_GET = array();
+        $_POST = array();
+        $_COOKIE = array();
+        $this->_clearFrontController();
+    }
+    
+    public function get($actionName, array $getArray = null, array $paramArray = null)
+    {
         if (!empty($getArray)) {
         	$_GET = $getArray;
-        }
-        $this->_response = $this->_makeRequest($actionName, $paramArray);
-        return $this->response();
-    }
-    
-    public function post($actionName, array $postArray = null, array $paramArray = null)
-    {
-        if (!empty($postArray)) {
-            $_POST = $postArray;
-        }
-        $this->_response = $this->_makeRequest($actionName, $paramArray);
-        return $this->response();
-    }
-    
-    /**
-     * Returns current Request_Http object
-     *
-     * @return Zend_Controller_Request_Http
-     */
-    public function request()
-    {
-        return $this->_request;
-    }
-    
-    public function response()
-    {
+        }
+        $this->_response = $this->_makeRequest($actionName, $paramArray);
+        return $this->response();
+    }
+    
+    public function post($actionName, array $postArray = null, array $paramArray = null)
+    {
+        if (!empty($postArray)) {
+            $_POST = $postArray;
+        }
+        $this->_response = $this->_makeRequest($actionName, $paramArray);
+        return $this->response();
+    }
+    
+    /**
+     * Returns current Request_Http object
+     *
+     * @return Zend_Controller_Request_Http
+     */
+    public function request()
+    {
+        return $this->_request;
+    }
+    
+    public function response()
+    {
         if (!isset($this->_response)) {
-        	throw new PHPSpec_Exception('No response has been retrieved yet;
+        	throw new PHPSpec_Exception('No response has been retrieved yet;
         	make a get or post request first');
-        }
-        return $this->_response;
-    }
-    
-    public function setController($controllerName)
-    {
-        $this->_controller = $controllerName;
-    }
-    
-    public function getController()
-    {
-        return $this->_controller;
-    }
-    
-    public function getFrontController()
-    {
-        return $this->_frontController;
-    }
-    
-    protected function _makeRequest($actionName, array $paramArray = null)
-    {
-        $this->request()->setControllerName($this->getController());
-        $this->request()->setActionName($actionName);
+        }
+        return $this->_response;
+    }
+    
+    public function setController($controllerName)
+    {
+        $this->_controller = $controllerName;
+    }
+    
+    public function getController()
+    {
+        return $this->_controller;
+    }
+    
+    public function getFrontController()
+    {
+        return $this->_frontController;
+    }
+    
+    protected function _makeRequest($actionName, array $paramArray = null)
+    {
+        if (preg_match("%/%", $actionName)) {
+            $uri = self::BASE_URL 
+                . (substr($actionName, 0, 1) == '/' ? $actionName : '/' . $actionName);
+        } else {
+            $uri = self::BASE_URL . '/' . $this->getController()
+            . (!empty($actionName) ? '/' . $actionName : '');
+        }
+        $this->_request = new Zend_Controller_Request_Http($uri);
         if (!empty($paramArray)) {
         	$this->request()->setParams($paramArray);
-        }
-        $response = $this->_frontController->dispatch($this->request());
-        return $response;
-    }
-    
-    protected function _clearFrontController() {
-        $this->_frontController->resetInstance();
-        $this->_frontController->returnResponse(true);
-        $this->_frontController->throwExceptions(true);
-        foreach (self::getControllerDirectories() as $module=>$path) {
-            if ($module == 'NULL') {
-            	$module = null;
-            }
-            $this->_frontController->addControllerDirectory($path, $module);
-        }
-        foreach (self::getModuleDirectories() as $path) {
-        	$this->_frontController->addModuleDirectory($path);
-        }
-    }
-    
-    protected function _setController()
-    {
-        $controllerClassName = substr(get_class($this), 8);
-        $this->_controller = substr($controllerClassName, 0, strlen($controllerClassName)-10);
-    }
-    
+        }
+        $response = $this->_frontController->dispatch($this->request());
+        return $response;
+    }
+    
+    protected function _clearFrontController() {
+        $this->_frontController->resetInstance();
+        if (!empty(self::$_frontControllerSetupCallback)) {
+            call_user_func(self::$_frontControllerSetupCallback);
+        } else {
+            $this->_frontController->returnResponse(true);
+            $this->_frontController->throwExceptions(true);
+            foreach (self::getControllerDirectories() as $module=>$path) {
+                if ($module == 'NULL') {
+                    $module = null;
+                }
+                $this->_frontController->addControllerDirectory($path, $module);
+            }
+            foreach (self::getModuleDirectories() as $path) {
+                $this->_frontController->addModuleDirectory($path);
+            }
+        }
+        
+    }
+    
+    protected function _setController()
+    {
+        $controllerClassName = substr(get_class($this), 8);
+        $this->_controller = substr($controllerClassName, 0, strlen($controllerClassName)-10);
+    }
+    
 }
\ No newline at end of file
Reply all
Reply to author
Forward
0 new messages