0

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:

  1. 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.

Coder4Fun
  • 135
  • 1
  • 14
  • *"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."* - could you please link those you found? – Definitely not Rafal Feb 03 '21 at 10:03
  • @DefinitelynotRafal they don't relate to my question. But OK: https://stackoverflow.com/questions/8700702/pdoexception-you-cannot-serialize-or-unserialize-pdo-instances https://habr.com/ru/post/120661/ https://github.com/krakjoe/pthreads/issues/209 https://www.drupal.org/project/drupal/issues/977460 – Coder4Fun Feb 03 '21 at 10:11
  • None of them relate to what I encountered with – Coder4Fun Feb 03 '21 at 10:11

0 Answers0