Created
July 6, 2011 16:37
-
-
Save mgroves/1067692 to your computer and use it in GitHub Desktop.
now with html and color!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Formatting | |
| { | |
| public static function BeginLine($color = null) | |
| { | |
| if(defined('STDIN') ) { | |
| return ""; | |
| } | |
| if($color == null) { | |
| return "<span>"; | |
| } | |
| return "<span style=\"color: " . $color . "\">"; | |
| } | |
| public static function NewLine() | |
| { | |
| if(defined('STDIN') ) { | |
| return "\n"; | |
| } | |
| return "</span><br />"; | |
| } | |
| public static function Indent() | |
| { | |
| if(defined('STDIN') ) { | |
| return "\t"; | |
| } | |
| return " "; | |
| } | |
| } | |
| class TestRunner | |
| { | |
| public function Run() | |
| { | |
| $classes = get_declared_classes(); | |
| foreach($classes as $class) | |
| { | |
| if(substr($class,0,4) == "Test" && ($class != "TestRunner")) { | |
| $this->RunTests($class); | |
| } | |
| } | |
| } | |
| private function RunTests($className) | |
| { | |
| echo(Formatting::BeginLine() . $className . Formatting::NewLine()); | |
| $class = new ReflectionClass($className); | |
| $obj = $class->newInstance(); | |
| if($class->hasMethod("startUp")) { | |
| $startUp = $class->getMethod("startUp"); | |
| $startUp->invoke($obj); | |
| } | |
| $beforeEach = null; | |
| $afterEach = null; | |
| if($class->hasMethod("beforeEach")) { | |
| $beforeEach = $class->getMethod("beforeEach"); | |
| } | |
| if($class->hasMethod("afterEach")) { | |
| $afterEach = $class->getMethod("afterEach"); | |
| } | |
| $allMethods = $class->getMethods(ReflectionMethod::IS_PUBLIC); | |
| foreach($allMethods as $method) | |
| { | |
| $methodName = $method->getName(); | |
| if(substr($methodName,0,4) == "test") { | |
| echo(Formatting::BeginLine() . Formatting::Indent() . $methodName . Formatting::NewLine()); | |
| if($beforeEach != null) $beforeEach->invoke($obj); | |
| $method->invoke($obj); | |
| if($afterEach != null) $afterEach->invoke($obj); | |
| } | |
| } | |
| if($class->hasMethod("cleanUp")) { | |
| $startUp = $class->getMethod("cleanUp"); | |
| $startUp->invoke($obj); | |
| } | |
| echo(Formatting::NewLine()); | |
| } | |
| } | |
| $t = new TestRunner(); | |
| $t->Run(); | |
| class Assert | |
| { | |
| public static function Equals($var1, $var2, $message = null) | |
| { | |
| if($var1 != $var2) { | |
| self::ShowMessage($message, "red"); | |
| echo(" - Failed (Expected '" . $var1 . "' but was '" . $var2 . "')" . Formatting::NewLine()); | |
| } else { | |
| self::ShowMessage($message, "green"); | |
| echo(" - Passed!" . Formatting::NewLine()); | |
| } | |
| } | |
| public static function NotNull($var, $message = null) | |
| { | |
| if($var == null) { | |
| self::ShowMessage($message, "red"); | |
| echo(" - Failed (Expected not null, but was null)" . Formatting::NewLine()); | |
| } else { | |
| self::ShowMessage($message, "green"); | |
| echo(" - Passed!" . Formatting::NewLine()); | |
| } | |
| } | |
| public static function NotEmpty($var, $message) | |
| { | |
| if(count($var) <= 0) { | |
| self::ShowMessage($message, "red"); | |
| echo(" - Failed (Expected not empty, but was empty)" . Formatting::NewLine()); | |
| } else { | |
| self::ShowMessage($message, "green"); | |
| echo(" - Passed!" . Formatting::NewLine()); | |
| } | |
| } | |
| private static function ShowMessage($message, $color = null) | |
| { | |
| if($message != null) { | |
| echo(Formatting::BeginLine($color) . Formatting::Indent() . Formatting::Indent() . $message); | |
| } else { | |
| echo(Formatting::BeginLine($color) . Formatting::Indent() . Formatting::Indent()); | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment