lundi 27 juillet 2015

Phpunit database testing and Travis CI: Cant't truncate table

I'm writing phpunit tests to test Mysql database filling. My tests work fine locally, but are failing on Travis CI with the following error:

PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query: 

   TRUNCATE `simple_table`

   using args: Array

   [SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.simple_table' doesn't exist]

I have a DatabaseTestCase parent class and a FillCommandTest testing class.

DatabaseTestCase

class DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
    private static $pdo = null;

    private $connection = null;

    /**
     * Construct.
     */
    public function __construct()
    {
        $env = new Environment();

        $this->dsn = "mysql:dbname=". $env->get('DB_DATABASE', 'test') . ";host=" . $env->get('DB_HOST', '127.0.0.1');
        $this->username = $env->get('DB_USERNAME', 'travis');
        $this->password = $env->get('DB_PASSWORD', '');
        $this->db_name = $env->get('DB_DATABASE', 'test');
    }

    /**
     * Get database connection.
     * 
     * @return  PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
     */
    final public function getConnection()
    {
        if ($this->connection === null) {
            if (self::$pdo == null) {
                self::$pdo = new PDO($this->dsn, $this->username, $this->password);
            }

            $this->connection = $this->createDefaultDBConnection(self::$pdo, $this->db_name);
        }

        return $this->connection;
    }

    /**
     * Get XML dataset.
     * 
     * @param   string $file
     * @return  string
     */
    public function getDataSet($file = 'empty.xml')
    {
        $file = 'tests/datasets/' . $file;

        return $this->createXmlDataSet($file);
    }

    /**
     * Set up the test database table
     * 
     * @param   PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection $connection
     */
    protected function setUpTable($connection)
    {
        $pdo = $connection->getConnection();

        $sql = "CREATE TABLE IF NOT EXISTS simple_table (
            id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(50) NOT NULL,
            email VARCHAR(50) NOT NULL,
            address VARCHAR(100)
        )";

        $pdo->exec($sql);
    }
}

FillCommandTest

class FillCommandTest extends DatabaseTestCase
{
    protected $app;

    protected $command;

    protected $command_tester;

    /**
     * 
     */
    public function setUp()
    {
        $this->app = new Application();
        $this->app->add(new FillCommand());
        $this->command = $this->app->find('fill');
        $this->command_tester = new CommandTester($this->command);

        parent::setUp();

        $connection = $this->getConnection();
        $this->setUpTable($connection);
    }

    /**
     * 
     */
    public function test_parse_and_fill_simple_table()
    {
        copy(getcwd() . '/tests/files/FillSimpleTable.php', getcwd() . '/src/Fillers/FillSimpleTable.php');

        copy(getcwd() . '/tests/files/simple.txt', getcwd() . '/src/Files/simple.txt');

        $this->command_tester->execute(array(
            'command' => $this->command->getName(),
            'name' => 'simple_table'
        ));

        $actual = $this->getConnection()->createQueryTable('simple_table', 'SELECT * FROM simple_table');

        $expected = $this->getDataSet('simple_table.xml')->getTable('simple_table');

        $this->assertTablesEqual($expected, $actual);

        unlink(getcwd() . '/src/Fillers/FillSimpleTable.php');

        unlink(getcwd() . '/src/Files/simple.txt');
    }
}

What am I doing wrong? Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire