0

php/dbunit error when null in table I get and error when test a stored procedure or query which contains or return a NULL values in it, i have tried the same example which doest return any NULL value and it WORKS perfect , how can i represent NULL in XML file to test it

TEST CODE

        public function StarRead()
        {
            fwrite(STDOUT, __METHOD__ . " Check with valid data \n"); 
            $resultTable = $this->getConnection()->createQueryTable(
                'Star', 'CALL get_star(1,NULL,0,2)'
            );
            $expected = $this->createFlatXMLDataSet(dirname(__FILE__).'/_files/new.xml');
            $expectedTable = $expected->getTable('Star');
            //Here we check that the table in the database matches the data in the XML file
            $this->assertTablesEqual($expectedTable, $resultTable);
        }

XML FILE

        <?xml version="1.0" encoding="UTF-8"?>
        <dataset>
        <Star 
        productId ="4"
        currentPrice =""
        listPrice =""
        createdOn ="2012-12-12 12:12:12"
        originalImage ="link"
        merchantName =""
        title ="Christopher Knight H"
        url ="link"
        />
        </dataset>

OUTPUT:

PHPUnit 4.0.12 by Sebastian Bergmann.
DBTest::StarRead Check with valid data F
Time: 1.4 seconds, Memory: 8.50Mb
There was 1 failure:

1) DBTest::StarRead Failed asserting that +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | AutoStar | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | productId | currentPrice | listPrice | createdOn | originalImage | merchantName | title | url | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | 4 | NULL | NULL | 2012-12-12 12:12:12 | link | NULL | Christopher Knight H | link | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+

is equal to expected (table diff enabled) +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | AutoStar | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | productId | currentPrice | listPrice | createdOn | originalImage | merchantName | title | url | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+ | 4 | | | 2012-12-12 12:12:12 | link | | Christopher Knight H | link | +----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+

.

FAILURES! Tests: 1, Assertions: 1, Failures: 1.

I tried the same code in Ubuntu 12.04 LTS (phpunit 3.7.28) and it works fine.

And in Debian 6.0.8 (phpunit 4.0.12) and it gives me the same error. Why is this?

arco444
  • 22,002
  • 12
  • 63
  • 67
Terence
  • 729
  • 1
  • 7
  • 17
  • Just a wild guess, what happens if you delete the lines with "" from the XML file? After all, "" is something completely different from NULL. – Jasper Mar 28 '14 at 12:24
  • got an error RuntimeException: AttValue: " or ' expected attributes construct error Couldn't find end of Start Tag – Terence Mar 28 '14 at 12:28
  • thanks for the reply it really means a lot to me thanks – Terence Mar 28 '14 at 12:29
  • and yes try it that way to keep non of the entries null or "" then it runs succesfully . but then what about null or "" entites – Terence Mar 28 '14 at 12:31

2 Answers2

0

According to the documentation,

http://dbunit.sourceforge.net/faq.html#flatxmlnull

leaving out the whole attribute should do it, like so:

    <?xml version="1.0" encoding="UTF-8"?>
    <dataset>
    <Star 
      productId ="4"
      createdOn ="2012-12-12 12:12:12"
      originalImage ="link"
      title ="Christopher Knight H"
      url ="link"
    />
    </dataset>
Jasper
  • 3,939
  • 1
  • 18
  • 35
  • but, I tried the same code in Ubuntu 12.04 LTS (phpunit 3.7.28) and it works fine (including "" in xml). And in Debian 6.0.8 (phpunit 4.0.12) and it gives me the same error i guess is this because of phphunit version.. i will just try to downgrade the version of phpunit and get back – Terence Mar 28 '14 at 12:41
0

The PHPUnit provides Replacement DataSet, that is a decorator for an existing dataset and allows you to replace values in any column of the dataset by another replacement value.For example, if the XML is like

    <?xml version="1.0" encoding="UTF-8"?>
    <dataset>
    <guestbook id="1" user="joe" created="2014-04-13 17:15:23" />
    <guestbook id="2" user="##NULL##" created="2014-04-20 12:14:20" /> 
    </dataset>

Wrap the Flat XML DataSet into a Replacement DataSet as follows

    <?php
    class ReplacementTest extends PHPUnit_Extensions_Database_TestCase
    {
        public function getDataSet()
        {
            $ds = $this->createFlatXmlDataSet('myFlatXmlFixture.xml');
            $rds = new PHPUnit_Extensions_Database_DataSet_ReplacementDataSet($ds);
            $rds->addFullReplacement('##NULL##', null);
            return $rds;
        }
    }
    ?>
Vidz
  • 545
  • 1
  • 6
  • 16