php - How to test a PDO::execute() failure -


i'm using pdo , phpunit professionnal project.

here method, in codebase, want test :

public function signup(\pdo $pdo)  {     $statement = $pdo->prepare("insert user values (         default,          :firstname,          :lastname,         :salutation,         :birthday,         :email,         :password     )");      $statement->bindvalue(':firstname', $this->firstname);     $statement->bindvalue(':lastname', $this->lastname);     // etc...      if($statement->execute()) {         $this->id = $pdo->lastinsertid();         return $this->id;     }      return 0; } 

i find way $statement->execute() fails , return false according pdo documentation

and test method, implement :

testsignupinsertrowfailandreturnzerovalue() 

does make sense test ?


edit

here solution simple mock :

$pdomock = $this->getmockbuilder('pdo')             ->disableoriginalconstructor()             ->getmock();      $pdomock->method("prepare")             ->will($this->returnvalue(new \pdostatement())); 

execute returns true on success , false on failure per docs.

if understand question correctly, check if query failed like:

if($statement->execute()) {   // success } else{   // failure } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -