5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.1.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Runner/BaseTestRunner.php';
49 require_once 'PHPUnit/Util/Filter.php';
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54 * Writes test result and code coverage data to a database.
58 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
59 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
61 * @version Release: 3.3.17
62 * @link http://www.phpunit.de/
63 * @since Class available since Release 3.1.0
65 class PHPUnit_Util_Log_Database implements PHPUnit_Framework_TestListener
68 * @var PHPUnit_Util_Log_Database
70 protected static $instance = NULL;
75 protected $currentTestId;
85 protected $testSuites = array();
90 protected $currentTestSuccess = TRUE;
101 * @param integer $revision
102 * @param string $information
103 * @throws PDOException
104 * @throws RuntimeException
106 protected function __construct(PDO $dbh, $revision, $information = '')
110 $stmt = $this->dbh->prepare(
112 (timestamp, revision, information)
113 VALUES(:timestamp, :revision, :information);'
118 $stmt->bindParam(':timestamp', $timestamp, PDO::PARAM_INT);
119 $stmt->bindParam(':revision', $revision, PDO::PARAM_INT);
120 $stmt->bindParam(':information', $information, PDO::PARAM_STR);
123 $this->runId = $this->dbh->lastInsertId();
128 * @param integer $revision
129 * @param string $information
130 * @return PHPUnit_Util_Log_Database
131 * @throws InvalidArgumentException
132 * @throws PDOException
133 * @throws RuntimeException
135 public static function getInstance(PDO $dbh = NULL, $revision = '', $information = '')
138 if (self::$instance != NULL) {
139 return self::$instance;
145 if (self::$instance != NULL) {
146 throw new RuntimeException;
149 if (empty($revision)) {
150 throw new InvalidArgumentException;
153 self::$instance = new PHPUnit_Util_Log_Database(
154 $dbh, $revision, $information
157 return self::$instance;
161 * Returns the ID of the current test.
165 public function getCurrentTestId()
167 return $this->currentTestId;
171 * Returns the ID of this test run.
175 public function getRunId()
183 * @param PHPUnit_Framework_Test $test
184 * @param Exception $e
187 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
189 $message = PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" .
190 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
193 PHPUnit_Runner_BaseTestRunner::STATUS_ERROR,
198 $this->updateParents(
199 $time, PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
202 $this->currentTestSuccess = FALSE;
206 * A failure occurred.
208 * @param PHPUnit_Framework_Test $test
209 * @param PHPUnit_Framework_AssertionFailedError $e
212 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
214 $message = PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" .
215 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
218 PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE,
223 $this->updateParents(
224 $time, PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE
227 $this->currentTestSuccess = FALSE;
233 * @param PHPUnit_Framework_Test $test
234 * @param Exception $e
237 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
239 $message = PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" .
240 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
243 PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE,
248 $this->currentTestSuccess = FALSE;
254 * @param PHPUnit_Framework_Test $test
255 * @param Exception $e
258 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
260 $message = PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n" .
261 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
264 PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED,
269 $this->currentTestSuccess = FALSE;
273 * A test suite started.
275 * @param PHPUnit_Framework_TestSuite $suite
277 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
279 if (empty($this->testSuites)) {
280 $testSuiteId = $this->insertRootNode($suite->getName());
282 $testSuiteId = $this->insertNode($suite);
285 $this->testSuites[] = array(
286 'id' => $testSuiteId,
287 'result' => PHPUnit_Runner_BaseTestRunner::STATUS_PASSED
292 * A test suite ended.
294 * @param PHPUnit_Framework_TestSuite $suite
296 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
298 array_pop($this->testSuites);
300 if (empty($this->testSuites)) {
301 $stmt = $this->dbh->prepare(
304 WHERE run_id = :runId;'
307 $stmt->bindParam(':runId', $this->runId, PDO::PARAM_INT);
315 * @param PHPUnit_Framework_Test $test
317 public function startTest(PHPUnit_Framework_Test $test)
319 $this->insertNode($test);
320 $this->currentTestSuccess = TRUE;
326 * @param PHPUnit_Framework_Test $test
329 public function endTest(PHPUnit_Framework_Test $test, $time)
331 if ($this->currentTestSuccess) {
333 PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time
336 $this->updateParents($time);
341 * Inserts the root node into the tree.
343 * @param string $name
345 * @throws PDOException
347 protected function insertRootNode($name)
349 $this->dbh->beginTransaction();
351 $stmt = $this->dbh->prepare(
353 (run_id, test_name, node_left, node_right, node_is_leaf)
354 VALUES(:runId, :testName, 1, 2, 0);'
357 $stmt->bindParam(':runId', $this->runId, PDO::PARAM_INT);
358 $stmt->bindParam(':testName', $name, PDO::PARAM_STR);
361 $rootId = $this->dbh->lastInsertId();
363 $stmt = $this->dbh->prepare(
365 SET node_root = :root
366 WHERE test_id = :testId;'
369 $stmt->bindParam(':root', $rootId, PDO::PARAM_INT);
370 $stmt->bindParam(':testId', $rootId, PDO::PARAM_INT);
373 $this->dbh->commit();
379 * Inserts a node into the tree.
381 * @param PHPUnit_Framework_Test $test
382 * @throws PDOException
384 protected function insertNode(PHPUnit_Framework_Test $test)
386 $isLeaf = (int)!$test instanceof PHPUnit_Framework_TestSuite;
388 $this->dbh->beginTransaction();
390 $stmt = $this->dbh->prepare(
393 WHERE test_id = :testId;'
396 $stmt->bindParam(':testId', $this->testSuites[count($this->testSuites)-1]['id'], PDO::PARAM_INT);
399 $right = (int)$stmt->fetchColumn();
402 $stmt = $this->dbh->prepare(
404 SET node_left = node_left + 2
405 WHERE node_root = :root
406 AND node_left > :left;'
409 $stmt->bindParam(':root', $this->testSuites[0]['id'], PDO::PARAM_INT);
410 $stmt->bindParam(':left', $right, PDO::PARAM_INT);
413 $stmt = $this->dbh->prepare(
415 SET node_right = node_right + 2
416 WHERE node_root = :root
417 AND node_right >= :right;'
420 $stmt->bindParam(':root', $this->testSuites[0]['id'], PDO::PARAM_INT);
421 $stmt->bindParam(':right', $right, PDO::PARAM_INT);
424 $testName = $test->getName();
428 $stmt = $this->dbh->prepare(
430 (run_id, test_name, test_result, test_message,
431 test_execution_time, node_root, node_left, node_right,
433 VALUES(:runId, :testName, 0, "", 0, :root, :left, :right,
437 $stmt->bindParam(':runId', $this->runId, PDO::PARAM_INT);
438 $stmt->bindParam(':testName', $testName, PDO::PARAM_STR);
439 $stmt->bindParam(':root', $this->testSuites[0]['id'], PDO::PARAM_INT);
440 $stmt->bindParam(':left', $left, PDO::PARAM_INT);
441 $stmt->bindParam(':right', $right, PDO::PARAM_INT);
442 $stmt->bindParam(':isLeaf', $isLeaf, PDO::PARAM_INT);
445 $this->currentTestId = $this->dbh->lastInsertId();
446 $this->dbh->commit();
448 if (!$test instanceof PHPUnit_Framework_TestSuite) {
449 $test->__db_id = $this->currentTestId;
452 return $this->currentTestId;
456 * Stores a test result.
458 * @param integer $result
460 * @param string $message
461 * @throws PDOException
463 protected function storeResult($result = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED, $time = 0, $message = '')
465 $stmt = $this->dbh->prepare(
467 SET test_result = :result,
468 test_message = :message,
469 test_execution_time = :executionTime
470 WHERE test_id = :testId;'
473 $stmt->bindParam(':result', $result, PDO::PARAM_INT);
474 $stmt->bindParam(':message', $message, PDO::PARAM_STR);
475 $stmt->bindParam(':executionTime', $time);
476 $stmt->bindParam(':testId', $this->currentTestId, PDO::PARAM_INT);
482 * @param integer $result
483 * @throws PDOException
485 protected function updateParents($time, $result = NULL)
487 $stmtUpdateResultAndTime = $this->dbh->prepare(
489 SET test_result = :result,
490 test_execution_time = test_execution_time + :time
491 WHERE test_id = :testSuiteId;'
494 $stmtUpdateTime = $this->dbh->prepare(
496 SET test_execution_time = test_execution_time + :time
497 WHERE test_id = :testSuiteId;'
500 foreach ($this->testSuites as &$testSuite) {
501 if ($result > $testSuite['result']) {
502 $stmtUpdateResultAndTime->bindParam(':result', $result, PDO::PARAM_INT);
503 $stmtUpdateResultAndTime->bindParam(':testSuiteId', $testSuite['id'], PDO::PARAM_INT);
504 $stmtUpdateResultAndTime->bindParam(':time', $time);
505 $stmtUpdateResultAndTime->execute();
507 $testSuite['result'] = $result;
509 $stmtUpdateTime->bindParam(':testSuiteId', $testSuite['id'], PDO::PARAM_INT);
510 $stmtUpdateTime->bindParam(':time', $time);
511 $stmtUpdateTime->execute();