[201] r403@phpspec (orig r201): padraic.brady | 2008-01-16 09:38:22 -0700

0 views
Skip to first unread message

nor...@domain51.com

unread,
Jan 16, 2008, 11:38:47 AM1/16/08
to phpspec...@googlegroups.com
Revision
201
Author
phpspec
Date
2008-01-16 09:38:47 -0700 (Wed, 16 Jan 2008)

Log Message

 r403@phpspec (orig r201):  padraic.brady | 2008-01-16 09:38:22 -0700
 * Added Context_Zend reponse class for matching
 * Added BeSuccess matcher to context Zend
 * Misc Specification.php updates to facilitate two sources of matchers (requires Refactoring to a generic solution)

Modified Paths

Added Paths

Property Changed

Diff

Property changes:


Name: svk:merge
   - d6e91ea2-e33a-0410-98df-1d493bd67c58:/:200
   + d6e91ea2-e33a-0410-98df-1d493bd67c58:/:201

Modified: branches/zend/specs/ContextZendSpec.php (200 => 201)


--- branches/zend/specs/ContextZendSpec.php	2008-01-15 21:38:13 UTC (rev 200)
+++ branches/zend/specs/ContextZendSpec.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -81,6 +81,37 @@
         $this->spec($response)->should->match("/This is Index/");
     }
 
+    public function itShouldPreserveInstanceOfPhpspecRequest() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $context->setController('index');
+        $response = $context->get('index');
+        $this->spec($context->request())->should->beAnInstanceOf('Zend_Controller_Request_Http');
+    }
+
+    public function itShouldReturnInstanceOfPhpspecResponseFromRequest() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $context->setController('index');
+        $response = $context->get('index');
+        $this->spec($response)->should->beAnInstanceOf('PHPSpec_Context_Zend_Response');
+    }
+
+    public function itShouldPreserveInstanceOfPhpspecResponse() 
+    {
+        $this->setControllerDirectory();
+        $context = new DescribeFooController;
+        $context->beforeEach();
+        $context->setController('index');
+        $context->get('index');
+        $this->spec($context->response())
+            ->should->beAnInstanceOf('PHPSpec_Context_Zend_Response');
+    }
+
     public function itShouldFormulateAndDispatchRequestWithUserParams()
     {
         $this->setControllerDirectory();
@@ -109,6 +140,15 @@
         $this->spec($response)->should->match("/userparampage/");
     }
 
+    public function itShouldAttachBesuccessMatcherToResponse() 
+    {
+        $context = new DescribeFooController;
+        $response = new PHPSpec_Context_Zend_Response;
+        $response->setContext($context);
+        $response->setHttpResponseCode(200);
+        $response->should->beSuccess();
+    }
+
     public function after()
     {
         PHPSpec_Context_Zend::clearModuleDirectories();

Added: branches/zend/src/PHPSpec/Context/Zend/Matcher/BeSuccess.php (0 => 201)


--- branches/zend/src/PHPSpec/Context/Zend/Matcher/BeSuccess.php	                        (rev 0)
+++ branches/zend/src/PHPSpec/Context/Zend/Matcher/BeSuccess.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -0,0 +1,62 @@
+<?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
+ */
+
+/**
+ * @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_Matcher_BeSuccess implements PHPSpec_Matcher_Interface
+{
+
+    protected $_expected = null;
+
+    protected $_actual = null;
+
+    public function __construct($expected)
+    {
+        $this->_expected = '200';
+    }
+
+    public function matches($actual)
+    {
+        $this->_actual = $actual;
+        if (!$actual instanceof PHPSpec_Context_Zend_Response) {
+            return false;
+        }
+        return $this->_expected == $this->_actual->getHttpResponseCode();
+    }
+
+    public function getFailureMessage()
+    {
+        return 'expected success (200), got response code (' . $this->_actual->getHttpResponseCode() . ') or invalid response object (using beSuccess())';
+    }
+
+    public function getNegativeFailureMessage()
+    {
+        return 'expected response code not to be (200), but got success (200) (using beSuccess())';
+    }
+
+    public function getDescription()
+    {
+        return 'be success';
+    }
+}
\ No newline at end of file

Added: branches/zend/src/PHPSpec/Context/Zend/Response.php (0 => 201)


--- branches/zend/src/PHPSpec/Context/Zend/Response.php	                        (rev 0)
+++ branches/zend/src/PHPSpec/Context/Zend/Response.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -0,0 +1,22 @@
+<?php
+
+require_once 'Zend/Controller/Response/Http.php';
+
+class PHPSpec_Context_Zend_Response extends Zend_Controller_Response_Http
+{
+
+    protected $_context = null;
+
+    public function setContext(PHPSpec_Context_Zend $context) 
+    {
+        $this->_context = $context;
+    }
+
+    protected function __get($name) 
+    {
+        if (preg_match("/should/", $name)) {
+            return $this->_context->spec($this)->$name;
+        }
+    }
+
+}
\ No newline at end of file

Modified: branches/zend/src/PHPSpec/Context/Zend.php (200 => 201)


--- branches/zend/src/PHPSpec/Context/Zend.php	2008-01-15 21:38:13 UTC (rev 200)
+++ branches/zend/src/PHPSpec/Context/Zend.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -76,15 +76,15 @@
     }
 
     public static function setFrontControllerSetupCallback($callback) 
-    {
+    {
         self::$_frontControllerSetupCallback = $callback;
-    }
-    
-    public static function clearFrontControllerSetupCallback() 
-    {
-        self::$_frontControllerSetupCallback = null;
     }
     
+    public static function clearFrontControllerSetupCallback() 
+    {
+        self::$_frontControllerSetupCallback = null;
+    }
+    
     public static function addModuleDirectory($path)
     {
         self::$_moduleDirectories[] = $path;
@@ -192,16 +192,20 @@
         if (!empty($paramArray)) {
         	$this->request()->setParams($paramArray);
         }
-        $response = $this->_frontController->dispatch($this->request());
+        $response = $this->_frontController->dispatch(
+            $this->request(),
+            new PHPSpec_Context_Zend_Response
+        );
+        $response->setContext($this);
         return $response;
     }
     
     protected function _clearFrontController() {
         $this->_frontController->resetInstance();
         if (count(self::$_frontControllerSetupCallback) > 0) {
-            call_user_func(array(
-                self::$_frontControllerSetupCallback[0],
-                self::$_frontControllerSetupCallback[1]
+            call_user_func(array(
+                self::$_frontControllerSetupCallback[0],
+                self::$_frontControllerSetupCallback[1]
             ));
         } else {
             $this->_frontController->returnResponse(true);

Modified: branches/zend/src/PHPSpec/Framework.php (200 => 201)


--- branches/zend/src/PHPSpec/Framework.php	2008-01-15 21:38:13 UTC (rev 200)
+++ branches/zend/src/PHPSpec/Framework.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -38,7 +38,12 @@
             return false;
         }
         $path = dirname(dirname(__FILE__));
-        include_once $path . '/' . str_replace('_', '/', $class) . '.php';
+        $file = $path . '/' . str_replace('_', '/', $class) . '.php';
+        if (!file_exists($file)) {
+            throw new PHPSpec_Exception('include_once("' . $file . '"): file does not exist');
+        } else {
+            include_once $file;
+        }
     }
 
 }

Modified: branches/zend/src/PHPSpec/Specification.php (200 => 201)


--- branches/zend/src/PHPSpec/Specification.php	2008-01-15 21:38:13 UTC (rev 200)
+++ branches/zend/src/PHPSpec/Specification.php	2008-01-16 16:38:47 UTC (rev 201)
@@ -122,7 +122,9 @@
 
         // check for Matcher references
         $matchers = array(
-            'equal', 'be', 'beEqualTo', 'beAnInstanceOf', 'beGreaterThan', 'beTrue', 'beFalse', 'beEmpty', 'beLessThan', 'beGreaterThanOrEqualTo', 'beLessThanOrEqualTo', 'beSet', 'beNull', 'beOfType', 'beIdenticalTo', 'match', 'throw'
+            'equal', 'be', 'beEqualTo', 'beAnInstanceOf', 'beGreaterThan', 'beTrue', 'beFalse', 'beEmpty', 'beLessThan', 'beGreaterThanOrEqualTo', 'beLessThanOrEqualTo', 'beSet', 'beNull', 'beOfType', 'beIdenticalTo', 'match', 'throw',
+
+            'beSuccess'
         );
         if (in_array($method, $matchers)) {
             $this->setExpectedValue(array_shift($args));
@@ -338,11 +340,19 @@
      *
      * @param DSL method call which was found to be a Matcher reference
      * @return null
+     * @todo Refactor Matcher inclusion into a more extensible system
      */
     protected function _createMatcher($method)
     {
         $matcherClass = 'PHPSpec_Matcher_' . ucfirst($method);
-        $this->_matcher = new $matcherClass( $this->getExpectedValue() );
+        try {
+            if (class_exists($matcherClass, true)) {
+                $this->_matcher = new $matcherClass( $this->getExpectedValue() );
+            }
+        } catch (PHPSpec_Exception $e) {
+            $matcherClass = 'PHPSpec_Context_Zend_Matcher_' . ucfirst($method);
+            $this->_matcher = new $matcherClass( $this->getExpectedValue() );
+        }
     }
 
     /**
Reply all
Reply to author
Forward
0 new messages