Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- PHPUnit_Extensions_SeleniumTestCase

  - Use cookies to "communicate" with server-side code coverage collection.

- PHPUnit_Util_Log_PDO
  - Create test node in startTest() and store the test's result later.
  - Add getCurrentTestId() and getRunId().
  - Add Singleton implementation that only optionally creates the object.

- PHPUnit_TextUI_TestRunner
  - Update to use PHPUnit_Util_Log_PDO::getInstance().
  • Loading branch information...
commit 0b001d53d27b154c04c78d53485656a61b891dd5 1 parent 9b2358d
@sebastianbergmann authored
View
46 PHPUnit/Extensions/SeleniumTestCase.php
@@ -101,6 +101,18 @@
private $sessionId = NULL;
/**
+ * @var integer
+ * @access private
+ */
+ private $runId = NULL;
+
+ /**
+ * @var integer
+ * @access private
+ */
+ private $testId = NULL;
+
+ /**
* @access protected
*/
protected function runTest()
@@ -141,6 +153,40 @@ public function start()
array($this->browser, $this->browserUrl)
);
+ if ($this->runId === NULL) {
+ $pdoListener = PHPUnit_Util_Log_PDO::getInstance();
+
+ if ($pdoListener !== FALSE) {
+ $this->runId = $pdoListener->getRunId();
+ $this->testId = $pdoListener->getCurrentTestId();
+ } else {
+ $this->runId = FALSE;
+ $this->testId = FALSE;
+ }
+ }
+
+ if ($this->runId !== FALSE && $this->runId !== NULL) {
+ $this->createCookie(
+ 'PHPUNIT_SELENIUM_RUN_ID=' . $this->runId,
+ 'path=/'
+ );
+
+ $this->createCookie(
+ 'PHPUNIT_SELENIUM_SESSION_ID=' . $this->sessionId,
+ 'path=/'
+ );
+
+ $this->createCookie(
+ 'PHPUNIT_SELENIUM_TEST_ID=' . $this->testId,
+ 'path=/'
+ );
+
+ $this->createCookie(
+ 'PHPUNIT_SELENIUM_TEST_NAME=' . $this->getName(),
+ 'path=/'
+ );
+ }
+
return $this->sessionId;
}
View
2  PHPUnit/TextUI/TestRunner.php
@@ -255,7 +255,7 @@ public function doRun(PHPUnit_Framework_Test $suite, array $parameters = array()
if (isset($parameters['pdoDSN']) && isset($parameters['pdoRevision']) &&
extension_loaded('pdo')) {
- $pdoListener = new PHPUnit_Util_Log_PDO(
+ $pdoListener = PHPUnit_Util_Log_PDO::getInstance(
$parameters['pdoDSN'],
$parameters['pdoRevision'],
isset($parameters['pdoInfo']) ? $parameters['pdoInfo'] : ''
View
134 PHPUnit/Util/Log/PDO.php
@@ -220,6 +220,19 @@ class PHPUnit_Util_Log_PDO implements PHPUnit_Framework_TestListener
CREATE UNIQUE INDEX IF NOT EXISTS code_coverage_test_id_code_line_id ON code_coverage (test_id, code_line_id);';
/**
+ * @var PHPUnit_Util_Log_PDO
+ * @access protected
+ * @static
+ */
+ protected static $instance = NULL;
+
+ /**
+ * @var integer
+ * @access protected
+ */
+ protected $currentTestId;
+
+ /**
* @var integer
* @access protected
*/
@@ -245,9 +258,9 @@ class PHPUnit_Util_Log_PDO implements PHPUnit_Framework_TestListener
* @param string $information
* @throws PDOException
* @throws RuntimeException
- * @access public
+ * @access protected
*/
- public function __construct($dsn, $revision, $information = '')
+ protected function __construct($dsn, $revision, $information = '')
{
$this->dbh = new PDO($dsn);
@@ -283,6 +296,64 @@ public function __construct($dsn, $revision, $information = '')
}
/**
+ * @param string $dsn
+ * @param integer $revision
+ * @param string $information
+ * @return PHPUnit_Util_Log_PDO
+ * @throws InvalidArgumentException
+ * @throws PDOException
+ * @throws RuntimeException
+ * @access public
+ * @static
+ */
+ public static function getInstance($dsn = '', $revision = '', $information = '')
+ {
+ if (empty($dsn)) {
+ if (self::$instance != NULL) {
+ return self::$instance;
+ } else {
+ return FALSE;
+ }
+ }
+
+ if (self::$instance != NULL) {
+ throw new RuntimeException;
+ }
+
+ if (empty($revision)) {
+ throw new InvalidArgumentException;
+ }
+
+ self::$instance = new PHPUnit_Util_Log_PDO(
+ $dsn, $revision, $information
+ );
+
+ return self::$instance;
+ }
+
+ /**
+ * Returns the ID of the current test.
+ *
+ * @return integer
+ * @access public
+ */
+ public function getCurrentTestId()
+ {
+ return $this->currentTestId;
+ }
+
+ /**
+ * Returns the ID of this test run.
+ *
+ * @return integer
+ * @access public
+ */
+ public function getRunId()
+ {
+ return $this->runId;
+ }
+
+ /**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
@@ -292,8 +363,7 @@ public function __construct($dsn, $revision, $information = '')
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
- $this->insertNode(
- $test,
+ $this->storeResult(
PHPUnit_Runner_BaseTestRunner::STATUS_ERROR,
$time,
$e->getMessage()
@@ -312,8 +382,7 @@ public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
- $this->insertNode(
- $test,
+ $this->storeResult(
PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE,
$time,
$e->getMessage()
@@ -332,8 +401,7 @@ public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_Asser
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
- $this->insertNode(
- $test,
+ $this->storeResult(
PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE,
$time,
$e->getMessage()
@@ -352,8 +420,7 @@ public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $t
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
- $this->insertNode(
- $test,
+ $this->storeResult(
PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED,
$time,
$e->getMessage()
@@ -396,6 +463,7 @@ public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
*/
public function startTest(PHPUnit_Framework_Test $test)
{
+ $this->insertNode($test);
$this->currentTestSuccess = TRUE;
}
@@ -409,8 +477,8 @@ public function startTest(PHPUnit_Framework_Test $test)
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($this->currentTestSuccess) {
- $this->insertNode(
- $test, PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time
+ $this->storeResult(
+ PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time
);
}
}
@@ -628,14 +696,10 @@ protected function insertRootNode($name)
* Inserts a node into the tree.
*
* @param PHPUnit_Framework_Test $test
- * @param integer $result
- * @param float $time
- * @param string $message
- * @return integer
* @throws PDOException
* @access protected
*/
- protected function insertNode(PHPUnit_Framework_Test $test, $result = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time = 0, $message = '')
+ protected function insertNode(PHPUnit_Framework_Test $test)
{
$this->dbh->beginTransaction();
@@ -681,28 +745,50 @@ protected function insertNode(PHPUnit_Framework_Test $test, $result = PHPUnit_Ru
'INSERT INTO test
(run_id, test_name, test_result, test_message,
test_execution_time, node_root, node_left, node_right)
- VALUES(%d, "%s", %d, "%s", %f, %d, %d, %d);',
+ VALUES(%d, "%s", 0, "", 0, %d, %d, %d);',
$this->runId,
$test->getName(),
- $result,
- $message,
- $time,
$this->testSuites[0],
$right,
$right + 1
)
);
- $testId = $this->dbh->lastInsertId();
+ $this->currentTestId = $this->dbh->lastInsertId();
$this->dbh->commit();
if (!$test instanceof PHPUnit_Framework_TestSuite) {
- $test->__db_id = $testId;
+ $test->__db_id = $this->currentTestId;
}
+ }
- return $testId;
+ /**
+ * Stores a test result.
+ *
+ * @param integer $result
+ * @param float $time
+ * @param string $message
+ * @throws PDOException
+ * @access protected
+ */
+ protected function storeResult($result = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time = 0, $message = '')
+ {
+ $this->dbh->exec(
+ sprintf(
+ 'UPDATE test
+ SET test_result = %d,
+ test_message = "%s",
+ test_execution_time = %f
+ WHERE test_id = %d;',
+
+ $result,
+ $message,
+ $time,
+ $this->currentTestId
+ )
+ );
}
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.