sebastianbergmann / dbunit

DbUnit port for PHP/PHPUnit.
https://phpunit.de/
Other
225 stars 186 forks source link

Empty fixture returns 'Undefined index: <table name>' #166

Closed gknapp closed 9 years ago

gknapp commented 9 years ago

Hi,

I guess my first question is, should dbunit create my schema from MySQL dump XML files? If not, that's where I'm going wrong. Otherwise ...

One of my DB tests verifies my application does the correct thing on index collision, attempts an update.

My starting state is an empty table, and my test is to insert the same row twice to see that duplicate keys are handled correctly (second insert should insert perform a merge update).

I've dumped my table structure using mysqldump --xml but the fixture file is purely structural.

As there is no database/table_data I think $tableValues may be empty, causing this error?

https://github.com/sebastianbergmann/dbunit/blob/master/PHPUnit/Extensions/Database/DataSet/MysqlXmlDataSet.php#L24 https://github.com/sebastianbergmann/dbunit/blob/master/PHPUnit/Extensions/Database/DataSet/AbstractXmlDataSet.php#L60

PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

Configuration read from /srv/share/sites/project/phpunit.xml

..........................................E..........

Time: 1.59 seconds, Memory: 11.75Mb

There was 1 error:

1) Project\Test\Stats\Task\Fact\InteractionLiveTest::testSessionIdCollisionThrowsDuplicateKeyException
Undefined index: f_interaction

/srv/share/sites/project/test/phpunit/Stats/Task/Fact/InteractionLiveTest.php:18

FAILURES!
Tests: 53, Assertions: 348, Errors: 1.

Fixture file:

<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="stats_test">
    <table_structure name="f_interaction">
        <field Field="id_interaction" Type="int(10) unsigned" Null="NO" Key="PRI" Extra="auto_increment" Comment="" />
        <field Field="id_session" Type="varchar(36)" Null="NO" Key="UNI" Extra="" Comment="" />
        <field Field="id_date" Type="int(10) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="id_time" Type="mediumint(8) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="id_origin" Type="mediumint(8) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="id_destination" Type="mediumint(8) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="id_outcome" Type="mediumint(8) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="occurred" Type="datetime" Null="NO" Key="" Extra="" Comment="" />
        <field Field="session_duration" Type="mediumint(8) unsigned" Null="NO" Key="" Extra="" Comment="" />
        <field Field="interacted" Type="tinyint(1)" Null="NO" Key="" Extra="" Comment="" />
        <field Field="flag_0" Type="bigint(20)" Null="NO" Key="" Default="0" Extra="" Comment="" />
        <field Field="flag_1" Type="bigint(20)" Null="NO" Key="" Default="0" Extra="" Comment="" />
        <field Field="flag_2" Type="bigint(20)" Null="NO" Key="" Default="0" Extra="" Comment="" />
        <field Field="flag_3" Type="bigint(20)" Null="YES" Key="" Default="0" Extra="" Comment="" />
        <key Table="f_interaction" Non_unique="0" Key_name="PRIMARY" Seq_in_index="1" Column_name="id_interaction" Collation="A" Cardinality="2901" Null="" Index_type="BTREE" Comment="" Index_comment="" />
        <key Table="f_interaction" Non_unique="0" Key_name="idx_session" Seq_in_index="1" Column_name="id_session" Collation="A" Cardinality="2901" Null="" Index_type="BTREE" Comment="" Index_comment="" />
        <options Name="f_interaction" Engine="InnoDB" Version="10" Row_format="Compact" Rows="2831" Avg_row_length="121" Data_length="344064" Max_data_length="0" Index_length="163840" Data_free="0" Auto_increment="0" Create_time="2015-07-07 09:42:56" Collation="latin1_swedish_ci" Create_options="" Comment="Interaction facts" />
    </table_structure>
</database>
</mysqldump>

Thanks in advance.

elazar commented 9 years ago

I guess my first question is, should dbunit create my schema from MySQL dump XML files? If not, that's where I'm going wrong.

DbUnit does not create your schema, per the documentation:

PHPUnit assumes that the database schema with all its tables, triggers, sequences and views is created before a test is run. This means you as developer have to make sure that the database is correctly setup before running the suite.

gknapp commented 9 years ago

I missed that thanks.

I create my table if it doesn't exist changed my fixture to insert a row of data instead of creating the table. Works now.