I'm learning PHPUnit and found a strange behavior in one of my tests.
I'm running all tests together from command line by passing folder with tests as an argument to PHPUnit:
phpunit examples
Everything went fine until I started testing my database using DbUnit. It seems like my database test has conflict with one of my another tests. I got message:
Time: 1.69 seconds, Memory: 4.50Mb
There was 1 error:
1) BackupStaticPropertyTest::test_PropertyShouldBeEqualToSix
PDOException: You cannot serialize or unserialize PDO instances
C:\Apache24\htdocs\phpunit-examples\vendor\sebastian\global-state\src\Snapshot.php:392
C:\Apache24\htdocs\phpunit-examples\vendor\sebastian\global-state\src\Snapshot.php:162
FAILURES!
My BackupStaticPropertyTest:
class BackupStaticPropertyTest extends PHPUnit_Framework_TestCase
{
public function test_PropertyShouldBeEqualToFive()
{
$this->assertEquals(5, StaticProperty::$prop);
StaticProperty::$prop = 6;
}
/**
* @backupStaticAttributes enabled
*/
public function test_PropertyShouldBeEqualToSix()
{
$this->assertEquals(6, StaticProperty::$prop);
StaticProperty::$prop = 7;
}
public function test_PropertyShouldBeEqualToSeven()
{
$this->assertEquals(StaticProperty::$prop, 6);
}
}
In this test I'm using @backupStaticAttributes annotation. Looks like it's the reason of error. But how does this test can be connected to my Database test?
My database test:
class CustomDatabaseTest extends DefaultDatabaseTestCase
{
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
protected function getDataSet()
{
$compositeDataSet = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
$compositeDataSet->addDataSet($this->createXMLDataSet('examples/Database-Test-Example/users-table.xml'));
$compositeDataSet->addDataSet($this->createXMLDataSet('examples/Database-Test-Example/messages-table.xml'));
return $compositeDataSet;
}
public function test_RowCountForUserTableShouldBeEqualToTwo()
{
$this->assertEquals(2, $this->getConnection()->getRowCount('users'));
}
}
DefaultDatabaseTestCase:
abstract class DefaultDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
private static $pdo = null;
private $conn = null;
final public function getConnection()
{
if ($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'],
$GLOBALS['DB_PWD']);
$this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DATABASE']);
}
}
return $this->conn;
}
}
I tried to use:
/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/
annotations above my Database testcase class, but still getting an error. Why do I get PDO exception in test that is not about database testing at all?
Aucun commentaire:
Enregistrer un commentaire