Say I have two php files (they are fictional but display the problem). The first one func.php contains a function, and the second one funcTest.php is for testing the function. First:
<?php
require_once "pdo_set/pdo.php";
$db_name = "db";
try {
$pdo = new PDO("$driver:host=$host;dbname=$db_name;charset=$charset", $db_user, $db_pass,
$options);
} catch (Exception $ex) {
echo "<script>alert('Error')</script>";
die();
}
function func(int $a, int $b, $data) {
echo "$a, $b, $data";
return $a + $b;
}
Second:
<?php
require_once "PHPUnit/Autoload.php";
require "func.php";
use PHPUnit\Framework\TestCase;
$data = "Some data";
var_dump($data);
final class funcTest extends PHPUnit_Framework_TestCase {
public function testFunc() {
$this->assertEquals(3, func(1, 2, $data));
}
}
When I run test using PHPUnit, I get this error:
There was 1 error:
- FuncTest::testFunc PDOException: You cannot serialize or unserialize PDO instances
Why do I get the error. How to workaround this?
P.S. I've found many qeustions on forums including SO about problems with "PDO serialize deserialize", but none of them helped me. So please DO NOT mark it as a duplicate.