Skip to content

Commit

Permalink
Fixed PDOException::getCode() string vs. int mismatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
sun committed Jul 26, 2014
1 parent 6529e40 commit 1aefe3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Framework/ExceptionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class PHPUnit_Framework_ExceptionWrapper extends PHPUnit_Framework_Exception

public function __construct(Exception $e)
{
parent::__construct($e->getMessage(), $e->getCode());
// PDOException::getCode() is a string.
// @see http://php.net/manual/en/class.pdoexception.php#95812
parent::__construct($e->getMessage(), (int) $e->getCode());

$this->classname = get_class($e);
$this->file = $e->getFile();
$this->line = $e->getLine();
Expand Down
10 changes: 7 additions & 3 deletions tests/Regression/GitHub/1351.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ PHPUnit_TextUI_Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann.

F.E.
F.E.E

Time: %s, Memory: %sMb

There was 1 error:
There were 2 errors:

1) Issue1351Test::testExceptionPre
RuntimeException: Expected rethrown exception.
Expand All @@ -27,6 +27,10 @@ Caused by
LogicException: Expected exception.
%A

2) Issue1351Test::testPhpCoreLanguageException
PDOException: SQLSTATE[HY000]: General error: 1 no such table: php_wtf
%A

--

There was 1 failure:
Expand All @@ -35,4 +39,4 @@ There was 1 failure:
Expected failure.
%A
FAILURES!
Tests: 4, Assertions: 5, Failures: 1, Errors: 1.
Tests: 5, Assertions: 5, Failures: 1, Errors: 2.
9 changes: 9 additions & 0 deletions tests/Regression/GitHub/1351/Issue1351Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ public function testExceptionPost()
$this->assertNull($this->instance);
$this->assertFalse(class_exists('ChildProcessClass1351', false), 'ChildProcessClass1351 is not loaded.');
}

public function testPhpCoreLanguageException()
{
// User-space code cannot instantiate a PDOException with a string code,
// so trigger a real one.
$connection = new PDO('sqlite::memory:');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->query("DELETE FROM php_wtf WHERE exception_code = 'STRING'");
}
}

0 comments on commit 1aefe3a

Please sign in to comment.