1

I use PHPUnit DataBase to test some class using MDB2.

All is well, since I encounter the second test, which returns an error:

Caught exception: Object of class MDB2_Error could not be converted to string

When I place the second test in place of the first one, the new first test is OK, but the second one returns the same error! And the next ones also!

Maybe the MDB2 connection is closed after the first test?

Here is my constructor:

public function __construct()
{
    $this->pdo = new PDO('connectionstring', 'user', 'passwd');
    try {
        $this->mdb2 = new MyDBA($this->dsn);
    }
    catch (Exception $e) {
        error_log(' __construct Caught exception: '.$e->getMessage());
    }
}

MyDBA returns a singleton. No exception is raised inside the constructor...

Here are the two first tests:

public function testTranslationAdd()
{
    try {
        $id = $this->mdb2->addTranslation("This is the second english translation.","en");
    }
    catch (Exception $e) {
        error_log(' testTranslationAdd Caught exception: '.$e->getMessage());
    }

    $xml_dataset = $this->createFlatXMLDataSet(dirname(__FILE__).'/state/twotranslations.xml');
    $this->assertDataSetsEqual($xml_dataset,
                               $this->getConnection()->createDataSet(array("translation")));
}

public function testTranslationGet()
{
    try {
        $text = $this->mdb2->getTranslation(1,"en");
    }
    catch (Exception $e) {
        error_log(' testTranslationGet Caught exception: '.$e->getMessage());
    }

    $this->assertEquals("This is the first english translation.",$text);
}
hakre
  • 193,403
  • 52
  • 435
  • 836
H_I
  • 385
  • 3
  • 8

1 Answers1

2

You should really add assertions that your mdb2 result is no error:

$this->assertFalse(MDB2::isError($this->mdb2), 'MDB2 error');

That unfortunately does not give you any hint what the error is, and using getMessage() directly in the assertion will fail badly if you don't have an error. That why you should write something along that way:

if (MDB2::isError($this->mdb2)) {
    $this->fail('MDB2 error: ' . $this->mdb2->getMessage());
}
cweiske
  • 30,033
  • 14
  • 133
  • 194