Permalink
Browse files

Added a reflection method class that checks for empty examples

  • Loading branch information...
1 parent c3cf4fd commit a633cbcc0bb7929c2dfa24fba2688536229784fd @MarcelloDuarte MarcelloDuarte committed Feb 17, 2012
Showing with 104 additions and 3 deletions.
  1. +17 −3 src/PHPSpec/Specification/Example.php
  2. +87 −0 src/PHPSpec/Util/ReflectionMethod.php
@@ -27,7 +27,8 @@
\PHPSpec\Specification\Result\Pending,
\PHPSpec\Specification\Result\DeliberateFailure,
\PHPSpec\Specification\Result\Failure,
- \PHPSpec\Util\Filter;
+ \PHPSpec\Util\Filter,
+ \PHPSpec\Util\ReflectionMethod;
/**
* @category PHPSpec
@@ -77,10 +78,10 @@ public function __construct (ExampleGroup $exampleGroup, $methodName)
public function run (Reporter $reporter)
{
try {
- $methodName = $this->_methodName;
$startTime = microtime(true);
call_user_func(array($this->_exampleGroup, 'before'));
- call_user_func(array($this->_exampleGroup, $methodName));
+ $this->markExampleAsPendingIfItIsEmpty();
+ call_user_func(array($this->_exampleGroup, $this->_methodName));
$this->closeExample($startTime);
} catch (Failure $failure) {
$reporter->addFailure($this, $failure);
@@ -173,4 +174,17 @@ private function closeExample($startTime)
\Mockery::close();
}
}
+
+ /**
+ * Marks example as pending if it is empty
+ */
+ private function markExampleAsPendingIfItIsEmpty()
+ {
+ $method = new ReflectionMethod(
+ $this->_exampleGroup, $this->_methodName
+ );
+ if ($method->isEmpty()) {
+ throw new Pending('empty example');
+ }
+ }
}
@@ -0,0 +1,87 @@
+<?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\Util;
+
+/**
+ * @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
+ */
+class ReflectionMethod
+{
+ /**
+ * Object or class name
+ *
+ * @var object|string
+ */
+ protected $_objectOrClassName;
+
+ /**
+ * The name of the method
+ *
+ * @var string
+ */
+ protected $_methodName;
+
+ /**
+ * Constructs a reflection method object
+ *
+ * @param string|object $objectOrClassName
+ * @param string $methodName
+ */
+ public function __construct($objectOrClassName, $methodName)
+ {
+ $this->_objectOrClassName = $objectOrClassName;
+ $this->_methodName = $methodName;
+ }
+
+ /**
+ * Checks whether method is empty
+ *
+ * @return bool
+ */
+ public function isEmpty()
+ {
+ $method = new \ReflectionMethod(
+ $this->_objectOrClassName,
+ $this->_methodName
+ );
+ list(,$path,) = explode(PHP_EOL, (string)$method);
+ preg_match('/(@@ )(.*\.php)( )(\d+)(\D*)(\d+)/', $path, $matches);
+ list ($path, $start, $end) = array(
+ $matches[2], $matches[4], $matches[6]
+ );
+
+ $code = '';
+ for ($i = $start; $i <= $end; $i++) {
+ $code .= Backtrace::readLine($path, $i);
+ }
+
+ $methodBodyPattern = "/(function\s+)(\w+)(\()[^\)]?(\))({)([^}]?)/";
+ preg_match($methodBodyPattern, $code, $matches);
+ $extract = isset($matches[6]) ?: '';
+ return empty($extract);
+ }
+}

0 comments on commit a633cbc

Please sign in to comment.