Permalink
Browse files

Add support for macros

  • Loading branch information...
1 parent 27b15ca commit fe0efeebeee4706220360d6df8df2f214b0f3e85 @MarcelloDuarte MarcelloDuarte committed Jul 25, 2012
@@ -0,0 +1,4 @@
+Feature: Developer can reuse methods from macros
+ As a developer
+ I can use a macro
+ So that I don't need to repeat methods in many groups
@@ -3,60 +3,60 @@ Feature: Developer can reuse examples from another spec
I can use a shared example
So that I don't need to repeat descriptions common to more than one group
-Scenario: shared examples group included in one file
- Given a file named "MyArraySpec.php" with:
- """
- <?php
-
- class Collection {
- protected $items = array();
- public function add() {
- $this->items = array_merge($this->items, func_get_args());
- }
- public function size() {
- return count($this->items);
- }
- }
-
- class MyArray extends Collection {}
-
- use PHPSpec\Specification\SharedExample;
-
- class ACollection extends SharedExample
- {
- protected $object;
- function before()
- {
- $this->object = $this->spec(new Collection);
- }
-
- function itSaysItHasThreeItems()
- {
- $this->object->add('one', 'two', 'three');
- $this->object->size()->should->equal(3);
- }
- }
-
- use PHPSpec\Context;
-
- class DescribeMyArray extends Context
- {
- public $itBehavesLike = 'ACollection';
-
- protected $object;
-
- function before()
- {
- $this->object = $this->spec(new MyArray);
- }
- }
- """
- When I run "phpspec MyArraySpec.php -f d"
- Then the output should contain:
- """
- MyArray
- behaves like a collection
- says it has three items
- """
-
-
+ Scenario: shared examples group included in one file
+ Given a file named "MyArraySpec.php" with:
+ """
+ <?php
+
+ class Collection {
+ protected $items = array();
+ public function add() {
+ $this->items = array_merge($this->items, func_get_args());
+ }
+ public function size() {
+ return count($this->items);
+ }
+ }
+
+ class MyArray extends Collection {}
+
+ use PHPSpec\Specification\SharedExample;
+
+ class ACollection extends SharedExample
+ {
+ protected $object;
+ function before()
+ {
+ $this->object = $this->spec(new Collection);
+ }
+
+ function itSaysItHasThreeItems()
+ {
+ $this->object->add('one', 'two', 'three');
+ $this->object->size()->should->equal(3);
+ }
+ }
+
+ use PHPSpec\Context;
+
+ class DescribeMyArray extends Context
+ {
+ public $itBehavesLike = 'ACollection';
+
+ protected $object;
+
+ function before()
+ {
+ $this->object = $this->spec(new MyArray);
+ }
+ }
+ """
+ When I run "phpspec MyArraySpec.php -f d"
+ Then the output should contain:
+ """
+ MyArray
+ behaves like a collection
+ says it has three items
+ """
+
+
View
@@ -0,0 +1,89 @@
+<?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 license@phpspec.net so we can send you a copy immediately.
+ *
+ * @category PHPSpec
+ * @package PHPSpec
+ * @copyright Copyright (c) 2007-2009 Pádraic Brady, Travis Swicegood
+ * @copyright Copyright (c) 2010-2012 Pádraic Brady, Travis Swicegood,
+ * Marcello Duarte
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
+ */
+namespace PHPSpec;
+
+use PHPSpec\Specification\ExampleGroup;
+
+/**
+ * @category PHPSpec
+ * @package PHPSpec
+ * @copyright Copyright (c) 2007-2009 Pádraic Brady, Travis Swicegood
+ * @copyright Copyright (c) 2010-2012 Pádraic Brady, Travis Swicegood,
+ * Marcello Duarte
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
+ */
+abstract class Macro
+{
+ /**
+ * The example Group
+ *
+ * @var ExampleGroup
+ */
+ protected $_exampleGroup;
+
+ /**
+ * Decorates the example group
+ *
+ * @param ExampleGroup $exampleGroup
+ */
+ public function setExampleGroup(ExampleGroup $exampleGroup)
+ {
+ $this->_exampleGroup = $exampleGroup;
+ }
+
+ /**
+ * Proxies all methods to example group
+ *
+ * @param string $method
+ * @param array $args
+ * @return mixed
+ */
+ public function __call($method, $args)
+ {
+ return call_user_func_array(
+ array($this->_exampleGroup, $method), $args
+ );
+ }
+
+ /**
+ * Proxies all calls to public properties
+ *
+ * @param string $property
+ * @return mixed
+ */
+ public function __get($property)
+ {
+ return $this->_exampleGroup->$property;
+ }
+
+ /**
+ * Proxies all calls to public properties
+ *
+ * @param string $property
+ * @param mixed $value
+ * @return mixed
+ */
+ public function __set($property, $value)
+ {
+ return $this->_exampleGroup->$property = $value;
+ }
+}
@@ -142,6 +142,7 @@ protected function runExamples(World $world)
continue;
}
$exampleGroup->setMatcherFactory($world->getMatcherFactory());
+ $exampleGroup->setMacros($world->getMacros());
$exampleGroup->beforeAll();
$this->getExampleRunner($exampleGroup)->run(
$world->getReporter()
@@ -47,6 +47,8 @@ class ExampleGroup
*/
private $_matcherFactory;
+ private $_macros;
+
/**
* Shared example a example group can behave like
*
@@ -204,6 +206,16 @@ public function getMatcherFactory()
return $this->_matcherFactory;
}
+ public function setMacros($macros)
+ {
+ $this->_macros = $macros;
+ }
+
+ public function getMacros()
+ {
+ return $this->_macros;
+ }
+
/**
* Checks if the example group behaves like a shared example
*
@@ -290,4 +302,28 @@ public function runSharedExample($example)
call_user_func(array($sharedExample, 'after'));
}
}
+
+ public function __call($method, $args)
+ {
+ if ($this->interceptedHasAMacro($method)) {
+ return $this->runMacro($method, $args);
+ }
+ }
+
+ protected function interceptedHasAMacro($method)
+ {
+ include_once ($this->_macros);
+ $macro = basename($this->_macros, '.php');
+ $macro = new $macro;
+ return method_exists($macro, $method);
+ }
+
+ protected function runMacro($method, $args)
+ {
+ include_once ($this->_macros);
+ $macro = basename($this->_macros, '.php');
+ $macro = new $macro;
+ $macro->setExampleGroup($this);
+ call_user_func_array(array($macro, $method), $args);
+ }
}
View
@@ -65,6 +65,13 @@ class World
private $_matcherFactory;
/**
+ * Macros file
+ *
+ * @var string
+ */
+ protected $_macrosFile;
+
+ /**
* Gets a option
*
* @param string $name
@@ -265,4 +272,24 @@ public function getMatcherFactory()
}
return $this->_matcherFactory;
}
+
+ /**
+ * Includes macros
+ *
+ * @param string $macrosFile
+ */
+ public function includeMacros($macrosFile)
+ {
+ $this->_macrosFile = $macrosFile;
+ }
+
+ /**
+ * Retrieves the macros file
+ *
+ * @return string
+ */
+ public function getMacros()
+ {
+ return $this->_macrosFile;
+ }
}

0 comments on commit fe0efee

Please sign in to comment.