Skip to content

Instantly share code, notes, and snippets.

@thekid
Created June 23, 2011 16:05
Show Gist options
  • Save thekid/1042863 to your computer and use it in GitHub Desktop.
Save thekid/1042863 to your computer and use it in GitHub Desktop.
XP Framework: Patch for issue #24
diff --git a/tools/src/main/php/xp/unittest/QuietListener.class.php b/tools/src/main/php/xp/unittest/QuietListener.class.php
new file mode 100644
index 0000000..72e9d7e
--- /dev/null
+++ b/tools/src/main/php/xp/unittest/QuietListener.class.php
@@ -0,0 +1,101 @@
+<?php
+/* This class is part of the XP framework
+ *
+ * $Id$
+ */
+
+ uses('unittest.TestListener');
+
+ /**
+ * Quiet listener - no output at all
+ *
+ * @purpose TestListener
+ */
+ class QuietListener extends Object implements TestListener {
+
+
+ /**
+ * Called when a test case starts.
+ *
+ * @param unittest.TestCase failure
+ */
+ public function testStarted(TestCase $case) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test fails.
+ *
+ * @param unittest.TestFailure failure
+ */
+ public function testFailed(TestFailure $failure) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test errors.
+ *
+ * @param unittest.TestError error
+ */
+ public function testError(TestError $error) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test raises warnings.
+ *
+ * @param unittest.TestWarning warning
+ */
+ public function testWarning(TestWarning $warning) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test finished successfully.
+ *
+ * @param unittest.TestSuccess success
+ */
+ public function testSucceeded(TestSuccess $success) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test is not run because it is skipped due to a
+ * failed prerequisite.
+ *
+ * @param unittest.TestSkipped skipped
+ */
+ public function testSkipped(TestSkipped $skipped) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test is not run because it has been ignored by using
+ * the @ignore annotation.
+ *
+ * @param unittest.TestSkipped ignore
+ */
+ public function testNotRun(TestSkipped $ignore) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test run starts.
+ *
+ * @param unittest.TestSuite suite
+ */
+ public function testRunStarted(TestSuite $suite) {
+ // NOOP
+ }
+
+ /**
+ * Called when a test run finishes.
+ *
+ * @param unittest.TestSuite suite
+ * @param unittest.TestResult result
+ */
+ public function testRunFinished(TestSuite $suite, TestResult $result) {
+ // NOOP
+ }
+ }
+?>
diff --git a/tools/src/main/php/xp/unittest/Runner.class.php b/tools/src/main/php/xp/unittest/Runner.class.php
index 2bec041..d6d81e1 100644
--- a/tools/src/main/php/xp/unittest/Runner.class.php
+++ b/tools/src/main/php/xp/unittest/Runner.class.php
@@ -7,8 +7,7 @@
$package= 'xp.unittest';
uses(
- 'xp.unittest.DefaultListener',
- 'xp.unittest.VerboseListener',
+ 'xp.unittest.TestListeners',
'xp.unittest.sources.PropertySource',
'xp.unittest.sources.ClassSource',
'xp.unittest.sources.ClassFileSource',
@@ -34,6 +33,7 @@
* Options is one of:
* <ul>
* <li>-v : Be verbose</li>
+ * <li>-q : Be quiet (no output)</li>
* <li>-cp: Add classpath elements</li>
* <li>-a {argument}: Define argument to pass to tests (may be used
* multiple times)</li>
@@ -158,12 +158,14 @@
// Parse arguments
$sources= new Vector();
- $verbose= FALSE;
+ $listener= TestListeners::$DEFAULT;
$arguments= array();
try {
for ($i= 0, $s= sizeof($args); $i < $s; $i++) {
if ('-v' == $args[$i]) {
- $verbose= TRUE;
+ $listener= TestListeners::$VERBOSE;
+ } else if ('-q' == $args[$i]) {
+ $listener= TestListeners::$QUIET;
} else if ('-cp' == $args[$i]) {
foreach (explode(PATH_SEPARATOR, $this->arg($args, ++$i, 'cp')) as $path) {
ClassLoader::getDefault()->registerPath($path);
@@ -203,14 +205,9 @@
return 1;
}
- $suite->addListener($verbose
- ? new VerboseListener($this->out)
- : new DefaultListener($this->out)
- );
-
- // Add test classes
+ // Set up suite
+ $suite->addListener($listener->newInstance($this->out));
foreach ($sources as $source) {
- $verbose && $this->out->writeLine('===> Adding test classes from ', $source);
try {
$tests= $source->testCasesWith($arguments);
foreach ($tests as $test) {
diff --git a/tools/src/main/php/xp/unittest/TestListeners.class.php b/tools/src/main/php/xp/unittest/TestListeners.class.php
new file mode 100644
index 0000000..06b31e8
--- /dev/null
+++ b/tools/src/main/php/xp/unittest/TestListeners.class.php
@@ -0,0 +1,58 @@
+<?php
+/* This class is part of the XP framework
+ *
+ * $Id$
+ */
+
+ uses(
+ 'xp.unittest.DefaultListener',
+ 'xp.unittest.VerboseListener',
+ 'xp.unittest.QuietListener'
+ );
+
+ /**
+ * Listeners enumeration
+ *
+ */
+ abstract class TestListeners extends Enum {
+ public static $DEFAULT, $VERBOSE, $QUIET;
+
+ static function __static() {
+ self::$DEFAULT= newinstance(__CLASS__, array(0, 'DEFAULT'), '{
+ static function __static() { }
+ public function newInstance(OutputStreamWriter $out) {
+ return new DefaultListener($out);
+ }
+ }');
+ self::$VERBOSE= newinstance(__CLASS__, array(1, 'VERBOSE'), '{
+ static function __static() { }
+ public function newInstance(OutputStreamWriter $out) {
+ return new VerboseListener($out);
+ }
+ }');
+ self::$QUIET= newinstance(__CLASS__, array(2, 'QUIET'), '{
+ static function __static() { }
+ public function newInstance(OutputStreamWriter $out) {
+ return new QuietListener();
+ }
+ }');
+ }
+
+ /**
+ * Creates a new listener instance
+ *
+ * @param io.streams.OutputStreamWriter out
+ * @return unittest.TestListener
+ */
+ public abstract function newInstance(OutputStreamWriter $out);
+
+ /**
+ * Returns all enum members
+ *
+ * @return lang.Enum[]
+ */
+ public static function values() {
+ return parent::membersOf(__CLASS__);
+ }
+ }
+?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment