mercredi 14 novembre 2018

Column not found in Dbunit PHP

Question

I have the following test case, but when I execute it with:

./vendor/bin/phpunit --bootstrap vendor/autoload.php --configuration tests/phpunit.xml tests/ExampleTest

I get:

1) ExampleTest::testQuery PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list'

I'm sure it's connecting to the right database (MySQL 5.6.21) and I'm using PHP 5.6.3. The database has the correct schema (if I run select id, code from table_1) I get the right data set. After the error the test data set is correctly inserted, so the connection is correct.

It seems like this problem falls into this FAQ's territory, but I have no idea how to translate:

Why am I getting a "NoSuchColumnException" but I am sure that the column actually exists? This exception can occur especially with MySQL RDBMS if you forget to set the special MySqlMetadataHandler on the DatabaseConfig. See properties page for more.

into the PHP port of DbUnit.

How can I solve this problem?

Reference code

<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\TestCaseTrait;

class ExampleTest extends TestCase
{
    use PHPUnit_Extensions_Database_TestCase_Trait;

    static private $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_PASSWD']);
        }
        $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
      }

      return $this->conn;
    }

    public function getDataSet()
    {
      return new ArrayDataSet([
        'table_1' => [
          [
            'id' => 1,
            'code' => 11
          ],
        ],
      ]);
    }

    public function getActualDataSet()
    {
      $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
      $actual->addTable('table_1', 'SELECT id, code');
      return $actual;
    }

    public function testQuery()
    {
      $expected = $this->getDataSet();
      $actual = $this->getActualDataSet();
      $this->assertDataSetsEqual($expected, $actual);
    }
}

And this is the reference composer.json:

"require-dev": {
    "phpunit/phpunit": "^4.8",
    "phpunit/dbunit": "^2.0"
},
"autoload": {
  "classmap": ["src/"]
}

Aucun commentaire:

Enregistrer un commentaire