From 3cf130283b054b50119f887e11b9b5a648d785a9 Mon Sep 17 00:00:00 2001 From: Anthon Pang Date: Thu, 8 May 2014 00:19:19 -0400 Subject: [PATCH] 1.4.0 - remove WebTest (test runner) --- README.md | 1 - bin/webunit | 32 -- composer.json | 7 +- lib/WebDriver/Exception.php | 2 - lib/WebDriver/Exception/WebTestAssertion.php | 33 -- lib/WebDriver/WebTest/Script.php | 110 ------ lib/WebDriver/WebTest/WebTest.php | 376 ------------------- 7 files changed, 2 insertions(+), 559 deletions(-) delete mode 100755 bin/webunit delete mode 100644 lib/WebDriver/Exception/WebTestAssertion.php delete mode 100644 lib/WebDriver/WebTest/Script.php delete mode 100755 lib/WebDriver/WebTest/WebTest.php diff --git a/README.md b/README.md index 76a37d80..24f93bc5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ Distinguishing features: * In the *master* branch, class names and file organization follow PSR-0 conventions for php 5.3+ namespaces. * Coding style follows PSR-1, PSR-2, and Symfony2 conventions. * Auto-generate API documentation via [phpDocumentor 2.x](http://phpdoc.org/). -* Includes a basic web test runner. [![Build Status](https://travis-ci.org/instaclick/php-webdriver.png)](https://travis-ci.org/instaclick/php-webdriver) [![Coverage Status](https://coveralls.io/repos/instaclick/php-webdriver/badge.png)](https://coveralls.io/r/instaclick/php-webdriver) diff --git a/bin/webunit b/bin/webunit deleted file mode 100755 index 16590671..00000000 --- a/bin/webunit +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env php - - */ - -/** - * WebDriver-based web test runner - */ -use WebDriver\WebTest\WebTest; - -require_once(__DIR__ . '/../lib/WebDriver/ClassLoader.php'); - -$rc = WebTest::main($argc, $argv); - -exit((int) !$rc); diff --git a/composer.json b/composer.json index 2a0cfe04..2be2b179 100644 --- a/composer.json +++ b/composer.json @@ -36,10 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.3.x-dev" + "dev-master": "1.4.x-dev" } - }, - "bin": [ - "bin/webunit" - ] + } } diff --git a/lib/WebDriver/Exception.php b/lib/WebDriver/Exception.php index 9330e202..b0f44f81 100644 --- a/lib/WebDriver/Exception.php +++ b/lib/WebDriver/Exception.php @@ -86,7 +86,6 @@ abstract class Exception extends \Exception const UNEXPECTED_PARAMETERS = -5; const INVALID_REQUEST = -6; const UNKNOWN_LOCATOR_STRATEGY = -7; - const WEBTEST_ASSERTION = -8; private static $errs = array( // self::SUCCESS => array('Success', 'This should never be thrown!'), @@ -123,7 +122,6 @@ abstract class Exception extends \Exception self::UNEXPECTED_PARAMETERS => array('UnexpectedParameters', 'This command does not expect this number of parameters.'), self::INVALID_REQUEST => array('InvalidRequest', 'This command does not support this HTTP request method.'), self::UNKNOWN_LOCATOR_STRATEGY => array('UnknownLocatorStrategy', 'This locator strategy is not supported.'), - self::WEBTEST_ASSERTION => array('WebTestAssertion', 'WebTest assertion failed.'), ); /** diff --git a/lib/WebDriver/Exception/WebTestAssertion.php b/lib/WebDriver/Exception/WebTestAssertion.php deleted file mode 100644 index 3b4ef042..00000000 --- a/lib/WebDriver/Exception/WebTestAssertion.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @author Anthon Pang - */ - -namespace WebDriver\Exception; - -use WebDriver\Exception as BaseException; - -/** - * WebDriver\Exception\WebTestAssertion class - * - * @package WebDriver - */ -final class WebTestAssertion extends BaseException { -} diff --git a/lib/WebDriver/WebTest/Script.php b/lib/WebDriver/WebTest/Script.php deleted file mode 100644 index 564d844a..00000000 --- a/lib/WebDriver/WebTest/Script.php +++ /dev/null @@ -1,110 +0,0 @@ - - */ - -namespace WebDriver\WebTest; - -use WebDriver\Session; -use WebDriver\Exception as WebDriverException; - -/** - * abstract WebDriver\WebTest\Script class - * - * @package WebDriver - */ -abstract class Script -{ - /** - * Session - * - * @var \WebDriver\Session - */ - protected $session; - - /** - * Tally on assertions - * - * @var array - */ - protected $assertStats; - - /** - * Constructor - * - * @param \WebDriver\Session $session - */ - public function __construct(\WebDriver\Session $session) - { - $this->session = $session; - - $this->assertStats = array( - 'pass' => 0, - 'failure' => 0, - 'total' => 0, - ); - } - - /** - * Assert (expect) value - * - * @param mixed $expression Expression - * @param mixed $expected Expected - * @param string $message Message - * - * @throw \WebDriver\Exception\WebTestAssertion if $expression is not equal to $expected - */ - protected function assert($expression, $expected, $message) - { - $this->assertStats['total']++; - - if ($expression !== $expected) { - $this->assertStats['failure']++; - - throw WebDriverException::factory(WebDriverException::WEBTEST_ASSERTION, $message); - } - - $this->assertStats['pass']++; - } - - /** - * Assert (expect) exception - * - * @param function $callback Callback function - * @param string $message Message - * - * @throw \WebDriver\Exception\WebTestAssertion if not exception is thrown - */ - protected function assertException($callback, $message) - { - $this->assertStats['total']++; - - try { - $callback(); - - $this->assertStats['failure']++; - - throw WebDriverException::factory(WebDriverException::WEBTEST_ASSERTION, $message); - } catch (\Exception $e) { - // expected exception - } - - $this->assertStats['pass']++; - } -} diff --git a/lib/WebDriver/WebTest/WebTest.php b/lib/WebDriver/WebTest/WebTest.php deleted file mode 100755 index f049970a..00000000 --- a/lib/WebDriver/WebTest/WebTest.php +++ /dev/null @@ -1,376 +0,0 @@ - - */ - -namespace WebDriver\WebTest; - -use WebDriver\WebDriver; -use WebDriver\Exception as WebDriverException; - -/** - * WebDriver\WebTest\WebTest class - test runner - * - * WebDriver-based web test runner, outputting results in TAP format. - * - * @package WebDriver - * - * @link http://testanything.org/wiki/index.php/TAP_version_13_specification - */ -class WebTest -{ - /** - * List of magic methods - * - * @var array - */ - private static $magicMethods = array( - '__construct', - '__destruct', - '__call', - '__callStatic', - '__get', - '__set', - '__isset', - '__unset', - '__sleep', - '__wakeup', - '__toString', - '__invoke', - '__set_state', - '__clone', - ); - - /** - * Error handler to instead throw exceptions - * - * @param integer $errno Error number - * @param string $errstr Error string - * @param string $errfile Source file of error - * @param integer $errline Line number in source file - * - * @throws \ErrorException - */ - static public function exception_error_handler($errno, $errstr, $errfile, $errline) - { - throw new \ErrorException($errstr, $errno, 0, $errfile, $errline); - } - - /** - * Assertion handler to instead throw exceptions - * - * @param string $file Source file - * @param integer $line Line number - * @param string $code Error code - * - * @throws \WebDriver\Exception\WebTestAssertion - */ - static public function assert_handler($file, $line, $code) - { - throw WebDriverException::factory(WebDriverException::WEBTEST_ASSERTION, "assertion failed: $file:$line: $code"); - } - - /** - * Get classes declared in the target file - * - * @param string $file File name - * - * @return array Array of class names - */ - public function getClasses($file) - { - $classes = get_declared_classes(); - - include_once($file); - - return array_diff(get_declared_classes(), $classes); - } - - /** - * Dump comment block - * - * Note: Reflection extension expects phpdocs style comments - * - * @param string $comment - */ - public function dumpComment($comment) - { - if ($comment) { - $lines = preg_replace( - array('~^\s*/[*]+\s*~', '~^\s*[*]+/?\s*~'), - '# ', - explode("\n", trim($comment)) - ); - - echo implode("\n", $lines) . "\n"; - } - } - - /** - * Dump diagnostic block (YAML format) - * - * @param mixed $diagnostic - */ - public function dumpDiagnostic($diagnostic) - { - if ($diagnostic) { - if (function_exists('yaml_emit')) { - $diagnostic = trim(yaml_emit($diagnostic)); - } else { - $diagnostic = "---\n" . trim($diagnostic) . "\n..."; - } - - if (is_string($diagnostic)) { - $lines = explode("\n", $diagnostic); - $lines = preg_replace('/^/', ' ', $lines); - echo implode("\n", $lines) . "\n"; - } - } - } - - /** - * Parse TODO/SKIP directives (if any) from comment block - * - * @param string $comment Comment - * - * @return string|null - */ - public function getDirective($comment) - { - if ($comment) { - if (preg_match('~\b(SKIP|TODO)(\s+([\S \t]+))?~', $comment, $matches)) { - return $matches[0]; - } - } - - return null; - } - - /** - * Is this a testable method? - * - * @param string $className Class name - * @param \ReflectionMethod $reflectionMethod Reflection method - * - * @return boolean False if method should not be counted - */ - protected function isTestableMethod($className, $reflectionMethod) - { - $method = $reflectionMethod->getName(); - $modifiers = $reflectionMethod->getModifiers(); - - if ($method === $className - || $modifiers !== \ReflectionMethod::IS_PUBLIC - || in_array($method, self::$magicMethods) - ) { - return false; - } - - return true; - } - - /** - * Run tests - * - * @param string $file File - * - * @return boolean True if success; false otherwise - */ - public function runTests($file) - { - $webdriver = new WebDriver(); - $session = $webdriver->session(); - $classes = $this->getClasses($file); - $success = true; - - /* - * count the number of testable methods - */ - $totalMethods = 0; - - foreach ($classes as $class) { - $parents = class_parents($class, false); - - if ($parents && in_array('WebDriver\WebTest\Script', $parents)) { - $reflectionClass = new \ReflectionClass($class); - $reflectionMethods = $reflectionClass->getMethods(); - - foreach ($reflectionMethods as $reflectionMethod) { - if ($this->isTestableMethod($class, $reflectionMethod)) { - $totalMethods++; - } - } - } - } - - if ($totalMethods) { - $i = 0; - echo "1..$totalMethods\n"; - - foreach ($classes as $class) { - $parents = class_parents($class, false); - - if ($parents && in_array('WebDriver\WebTest\Script', $parents)) { - $class = '\\' . $class; - - $objectUnderTest = new $class($session); - - $reflectionClass = new \ReflectionClass($class); - - $comment = $reflectionClass->getDocComment(); - $this->dumpComment($comment); - - $reflectionMethods = $reflectionClass->getMethods(); - - foreach ($reflectionMethods as $reflectionMethod) { - if (!$this->isTestableMethod($class, $reflectionMethod)) { - continue; - } - - $comment = $reflectionMethod->getDocComment(); - $this->dumpComment($comment); - - $directive = $this->getDirective($comment); - - $description = $method; - $reflectionParameters = $reflectionMethod->getParameters(); - - foreach ($reflectionParameters as $reflectionParameter) { - if ($reflectionParameter->getName() === 'description' - && $reflectionParameter->isDefaultValueAvailable() - ) { - $defaultValue = $reflectionParameter->getDefaultValue(); - - if (is_string($defaultValue)) { - $description = $defaultValue; - break; - } - } - } - - $diagnostic = null; - $rc = false; - $i++; - - try { - $objectUnderTest->$method(); - $rc = true; - } catch (WebDriverException\Curl $e) { - $success = false; - echo 'Bail out! ' . $e->getMessage() . "\n"; - break 2; - } catch (\Exception $e) { - $success = false; - $diagnostic = $e->getMessage(); - - // @todo check driver capability for screenshot - - $screenshot = $session->screenshot(); - - if (!empty($screenshot)) { - $imageName = basename($file) . ".$i.png"; - file_put_contents($imageName, base64_decode($screenshot)); - } - } - - echo ($rc ? 'ok' : 'not ok') . " $i - $description" . ($directive ? " # $directive" : '') . "\n"; - - $this->dumpDiagnostic($diagnostic); - } - - unset($objectUnderTest); - } - } - } else { - echo "0..0\n"; - } - - if ($session) { - $session->close(); - } - - return $success; - } - - /** - * Main dispatch routine - * - * @param integer $argc number of arguments - * @param array $argv arguments - * - * @return boolean True if success; false otherwise - */ - static public function main($argc, $argv) - { - set_error_handler(array('WebDriver\WebTest\WebTest', 'exception_error_handler')); - - assert_options(ASSERT_ACTIVE, 1); - assert_options(ASSERT_WARNING, 0); - assert_options(ASSERT_CALLBACK, array('WebDriver\WebTest\WebTest', 'assert_handler')); - - /* - * parse command line options - */ - if ($argc === 1) { - $argc++; - array_push($argv, '-h'); - } - - for ($i = 1; $i < $argc; $i++) { - $opt = $argv[$i]; - $optValue = ''; - - if (preg_match('~([-]+[^=]+)=(.+)~', $opt, $matches)) { - $opt = $matches[1]; - $optValue = $matches[2]; - } - - switch ($opt) { - case '-h': - case '--help': - echo $argv[0] . " [-d directory] [--tap] [--xml] [--disable-screenshot] test.php\n"; - exit(1); - - case '-d': - case '--output-directory': - - case '--format': - case '--tap': - case '--xml': - - case '--disable-screenshot': - - default: - } - } - - echo "TAP version 13\n"; - - $success = false; - - try { - $webtest = new self; - $success = $webtest->runTests($argv[1]); - } catch (\Exception $e) { - echo 'Bail out! ' . $e->getMessage() . "\n"; - } - - return $success; - } -}