Last active
January 27, 2021 15:44
-
-
Save yehchge/609cdff06fc7ac0ea37bec1484d5d0b4 to your computer and use it in GitHub Desktop.
phpunit PDOException Assert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
use PHPUnit\Framework\TestCase; | |
class DBA | |
{ | |
public function getRowCount($table) | |
{ | |
$pdo = new PDO("mysql:host=localhost;dbname=test",'root',''); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// try { | |
$res = $pdo->query("SELECT COUNT(1) FROM $table"); | |
$count = $res->fetchColumn(); | |
return $count; | |
// } catch (PDOException $e) { | |
// die($e->getMessage().PHP_EOL); | |
// } | |
} | |
} | |
class MyTest extends TestCase | |
{ | |
public function testPDOException() : void | |
{ | |
$pdo = new DBA(); | |
// Assert | |
$this->expectException(PDOException::class); | |
// Act | |
$row = $pdo->getRowCount('guestbookerrrer'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
must remove getRowCount try...catch , the phpunit test will can run ok. but .....