Skip to content

Commit

Permalink
- PHPUnit_Extensions_SeleniumTestCase
Browse files Browse the repository at this point in the history
  - 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
sebastianbergmann committed Mar 23, 2007
1 parent 9b2358d commit 0b001d5
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 25 deletions.
46 changes: 46 additions & 0 deletions PHPUnit/Extensions/SeleniumTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ abstract class PHPUnit_Extensions_SeleniumTestCase extends PHPUnit_Framework_Tes
*/
private $sessionId = NULL;

/**
* @var integer
* @access private
*/
private $runId = NULL;

/**
* @var integer
* @access private
*/
private $testId = NULL;

/**
* @access protected
*/
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion PHPUnit/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : ''
Expand Down
134 changes: 110 additions & 24 deletions PHPUnit/Util/Log/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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
Expand All @@ -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);

Expand Down Expand Up @@ -282,6 +295,64 @@ public function __construct($dsn, $revision, $information = '')
$this->runId = $this->dbh->lastInsertId();
}

/**
* @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.
*
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -396,6 +463,7 @@ public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
*/
public function startTest(PHPUnit_Framework_Test $test)
{
$this->insertNode($test);
$this->currentTestSuccess = TRUE;
}

Expand All @@ -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
);
}
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
)
);
}
}
?>

0 comments on commit 0b001d5

Please sign in to comment.