PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Testing » PHPUnit2 » Bug #5231

Make it possible to filter paths in the IncludePathTestCollector

Details

Request #5231Make it possible to filter paths in the IncludePathTestCollector
Submitted2005-08-29 13:05 UTC
Fromo dot persson at gmail dot com
Assignedsebastian
StatusNo Feedback
PackagePHPUnit2
PHP Version5.1.0
OSLinux
Roadmaps(Not assigned)

Comments

[2005-08-29 13:05 UTC] o dot persson at gmail dot com

Description:
------------
Would be a nice feature to be able to filter unwanted paths (instead of removing them from the include_path).

I use this code locally, thought it could be interesting. I chose to use strpos -- maybe preg_match would give more flexibility.

Test script:
---------------
Index: /var/www-dev/components/PEAR/PHPUnit2/Runner/IncludePathTestCollector.php
===================================================================
--- /var/www-dev/components/PEAR/PHPUnit2/Runner/IncludePathTestCollector.php (revision 48)
+++ /var/www-dev/components/PEAR/PHPUnit2/Runner/IncludePathTestCollector.php (working copy)
@@ -38,7 +38,16 @@
*/

class PHPUnit2_Runner_IncludePathTestCollector implements PHPUnit2_Runner_TestCollector {
+
/**
+ * Array containing all paths that will be filtered by the collector.
+ *
+ * @var array
+ * @access protected
+ */
+ protected $filter = array();
+
+ /**
* @return array
* @access public
*/
@@ -59,7 +68,7 @@
}

foreach ($iterator as $path => $file) {
- if ($this->isTestClass($file)) {
+ if ($this->isTestClass($file) && !$this->isFiltered($path)) {
if (substr(PHP_OS, 0, 3) == 'WIN') {
$path = str_replace('/', '\\', $path);
}
@@ -82,6 +91,33 @@
protected function isTestClass($classFileName) {
return (strpos($classFileName, 'Test') !== FALSE && substr($classFileName, -4) == '.php') ? TRUE : FALSE;
}
+
+ /**
+ * Add a pattern which will be ignored when the collector is running. The
+ * pattern added will be searched using strpos.
+ *
+ * @param string $pattern pattern to filter
+ * @return void
+ * @access public
+ */
+ public function addFilter($pattern) {
+ $this->filter[] = $pattern;
+ }
+
+ /**
+ * Compare the specified path with the internal filter rules.
+ *
+ * @param string $path path to check if it should be filtered
+ * @return boolean
+ * @access public
+ */
+ public function isFiltered($path) {
+ foreach ($this->filter as $skip) {
+ if (strpos($path, $skip) !== false)
+ return true;
+ }
+ return false;
+ }
}

[2005-10-15 08:06 UTC] sebastian at php dot net

I will implement this using a FilterIterator.

[2005-10-15 08:21 UTC] sebastian at php dot net

Please test the following patch:

Index: IncludePathTestCollector.php
===================================================================
RCS file: /repository/pear/PHPUnit2/Runner/IncludePathTestCollector.php,v
retrieving revision 1.15
diff -u -b -B -r1.15 IncludePathTestCollector.php
--- IncludePathTestCollector.php 9 Sep 2005 09:47:56 -0000 1.15
+++ IncludePathTestCollector.php 15 Oct 2005 08:20:06 -0000
@@ -40,6 +40,12 @@

class PHPUnit2_Runner_IncludePathTestCollector implements PHPUnit2_Runner_TestCollector {
/**
+ * @var string
+ * @access private
+ */
+ private $filterIterator = NULL;
+
+ /**
* @return array
* @access public
*/
@@ -65,6 +71,11 @@
);
}

+ if ($this->filterIterator !== NULL) {
+ $class = new ReflectionClass($this->filterIterator);
+ $iterator = $class->newInstance($iterator);
+ }
+
foreach ($iterator as $path => $file) {
if ($this->isTestClass($file)) {
if (substr(PHP_OS, 0, 3) == 'WIN') {
@@ -79,6 +90,31 @@
}

/**
+ * Adds a FilterIterator to filter the source files to be collected.
+ *
+ * @param string $filterIterator
+ * @throws InvalidArgumentException
+ * @access public
+ */
+ public function setFilterIterator($filterIterator) {
+ if (is_string($filterIterator) && class_exists($filterIterator)) {
+ try {
+ $class = new ReflectionClass($filterIterator);
+
+ if ($class->isSubclassOf('FilterIterator')) {
+ $this->filterIterator = $filterIterator;
+ }
+ }
+
+ catch (ReflectionException $e) {
+ throw new InvalidArgumentException;
+ }
+ } else {
+ throw new InvalidArgumentException;
+ }
+ }
+
+ /**
* Considers a file to contain a test class when it contains the
* pattern "Test" in its name and its name ends with ".php".
*

[2005-12-19 05:50 UTC] sebastian at php dot net

No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.