Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Testing Acceptance Tests Loading Datasets for DBUnit

Alan Fox
Alan Fox
10,214 Points

PHPUnit_Extensions_Database_Exception: The root element of an xml data set file must be called <dataset>

Should the code to load the data in that example not be:

createMySQLXMLDataSet instead of createXMLDataSet

seeing as you are trying to load a mysql dump file?

I get the following error PHPUnit_Extensions_Database_Exception: The root element of an xml data set file must be called <dataset>

Although if I do change it to createMySQLXMLDataSet I get this error Argument 1 passed to PHPUnit_Extensions_Database_DataSet_DefaultTableIterator::__construct() must be of the type array, null given, called in /Applications/MAMP/htdocs/3XM/vendor/phpunit/dbunit/src/Extensions/Database/DataSet/AbstractXmlDataSet.php on line 106 and defined

Alena Holligan
Alena Holligan
Treehouse Teacher

does your xml have the following line? <database name="cfp_test">

can you share your code somewhere like gethub or upload to a workspace you can snapshot?

Alan Fox
Alan Fox
10,214 Points

This is my load code:

return $this->createMySQLXMLDataSet(dirname(__FILE__).'/data/phpmyadmin.xml');

This is my data file:

<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <database name="phpunit_testdb">
        <table name="guestbook">
            <column name="id">1</column>
            <column name="content">Hello buddy!</column>
            <column name="user">joe</column>
            <column name="created">2010-04-24 17:15:23</column>
        </table>

        <table name="guestbook">
            <column name="id">2</column>
            <column name="content">I like it!</column>
            <column name="user">joe</column>
            <column name="created">2010-04-26 12:14:20</column>
        </table>
    </database>
</mysqldump>

This is my full page code:

require 'vendor/autoload.php';

class MyGuestbookTest extends PHPUnit_Extensions_Database_TestCase
{
    /**
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    public function getConnection()
    {
        // $dsn = "mysql:host=127.0.0.1:8888;dbname=phpUnit_testDB;use=root;password=root";
        $this->db = new PDO("mysql:host=127.0.0.1:8889;dbname=phpUnit_testDB", "root", "root");
        return $this->createDefaultDBConnection($this->db, $dsn);
    }

    /**
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet()
    {
        // return $this->createXMLDataSet(dirname(__FILE__).'/data/admin.xml');
        return $this->createMySQLXMLDataSet(dirname(__FILE__).'/data/phpmyadmin.xml');
        // return new PHPUnit_Extensions_Database_DataSet_YamlDataSet( dirname(__FILE__)."/data/yamlfile.yml" );
    }

    public function testLoad()
    {
        $this->assertEquals(2, $this->getConnection()->getRowCount('guestbook'), "Pre-Condition");
    }

    public function testAnything()
    {
        $value = return10();
        $this->assertEquals('joe', $value);

        $this->assertEquals(2, $this->getConnection()->getRowCount('guestbook'), "Pre-Condition");

        addRecord();
        $this->assertEquals(3, $this->getConnection()->getRowCount('guestbook'), "Pre-Condition");

    }

}

$db_test = new MyGuestbookTest;

function addRecord()
{
    global $db_test;

    $db = $db_test->getConnection()->getConnection();

    $details_query = $db -> prepare("INSERT INTO guestbook SET id=4, content='asjiodasj das doajd', user='alan'");
    $details_query->bindParam(1, $user);
    return  $details_query->execute();

}

function return10()
{
    global $db_test;

    $db = $db_test->getConnection()->getConnection();
    $user = 1;

    $details_query = $db -> prepare("SELECT * FROM guestbook WHERE id = ?");
    $details_query->bindParam(1, $user);
    $details_query->execute();
    $addresses = $details_query->fetch(PDO::FETCH_ASSOC);

    return $addresses['user'];

}

When I use the following load code:

return $this->createXMLDataSet(dirname(__FILE__).'/data/admin.xml');

with this data file

<?xml version="1.0" ?>
<dataset>
    <table name="guestbook">
        <column>id</column>
        <column>content</column>
        <column>user</column>
        <column>created</column>
        <row>
            <value>1</value>
            <value>Hello buddy!</value>
            <value>joe</value>
            <value>2010-04-24 17:15:23</value>
        </row>
        <row>
            <value>2</value>
            <value>I like it!</value>
            <value>joe</value>
            <value>2010-04-26 12:14:20</value>
        </row>
    </table>
</dataset>

it works perfectly.

I'm running the test with vendor/bin/phpunit tests/db_test.php

But I need it to work with mysql dumps from phpmyadmin.

I hope you can help me out!

Thanks

Al

Alan Fox
Alan Fox
10,214 Points

Any ideas Alena?

Alena Holligan
Alena Holligan
Treehouse Teacher

not sure how you created your xml dump from mysql. I used

/Applications/MAMP/Library/bin/mysqldump -uroot -p --xml phpunit_testdb > phpmyadmin.xml

which created the following file which seemed to work

<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="phpunit_testdb">
    <table_structure name="guestbook">
        <field Field="id" Type="int(11)" Null="NO" Key="" Extra="" Comment="" />
        <field Field="content" Type="varchar(100)" Null="NO" Key="" Extra="" Comment="" />
        <field Field="user" Type="varchar(100)" Null="NO" Key="" Extra="" Comment="" />
        <field Field="created" Type="datetime" Null="NO" Key="" Extra="" Comment="" />
        <options Name="guestbook" Engine="InnoDB" Version="10" Row_format="Compact" Rows="2" Avg_row_length="8192" Data_length="16384" Max_data_length="0" Index_length="0" Data_free="4194304" Create_time="2016-07-18 06:44:10" Collation="utf8_general_ci" Create_options="" Comment="" />
    </table_structure>
    <table_data name="guestbook">
    <row>
        <field name="id">1</field>
        <field name="content">Hello buddy!</field>
        <field name="user">joe</field>
        <field name="created">2010-04-24 17:15:23</field>
    </row>
    <row>
        <field name="id">2</field>
        <field name="content">I like it!</field>
        <field name="user">joe</field>
        <field name="created">2010-04-26 12:14:20</field>
    </row>
    </table_data>
</database>
</mysqldump>
Alan Fox
Alan Fox
10,214 Points

I'm using phpmyadmin to export the file as a cvs. I don't have terminal access to the server. That seems to be the issue - they both return different files.

 <row>
     <field name="id">1</field>
     <field name="content">Hello buddy!</field>
     <field name="user">joe</field>
     <field name="created">2010-04-24 17:15:23</field>
     </row>

v

     <column name="id">1</column>
     <column name="content">Hello buddy!</column>
     <column name="user">joe</column>
     <column name="created">2010-04-24 17:15:23</column>

I'll write something to convert the phpmyadmin export to the correct format.

Thanks for your help

Al