1

I'm new to database testing with PhpUnit. I'm starting with simple test that compare PostgreSQL table's content with xml file.

The problem is that assertion doesn't work as expected:

Please take a look of the result:

http://www3.picturepush.com/photo/a/5540556/1024/Anonymous/Screenshot.png

As you see the tables are equal, but the content in db table(first at screen) has an extra spaces...

I've no idea what is wrong.

Here is PHP code:

public function testGetSourceData()
{
    include_once(
            sfConfig::get('sf_lib_dir')
            . '/task/ShopCategoryTreeUpdateTask.class.php'
    );

    $method = new ReflectionMethod(
      'ShopCategoryTreeUpdateTask', '_getSourceData'
    );

    $method->setAccessible(TRUE);

    $res = $method->invoke(new ShopCategoryTreeUpdateTask(new sfEventDispatcher, new sfFormatter),123);

    $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
    $actual->addTable('shop');

    $expected = $this->getDataSet();

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

}

and here the XML: http://pastebin.com/5MmtJDr6

Thanks for any goals!

2

1 Answer 1

1

I think you may be over complicating the test. In your method it is likely that QueryDataSet and getDataSet are rendering the result to string differently.

You shouldn't really be testing private and public methods by reflection because these should be testable and accessible by your public methods. If they are not accessibly by public methods then they can never be called. Your code coverage report comes in handy here.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, IMHO that is all correct with testing private and protected methods: sebastian-bergmann.de/archives/881-Testing-Your-Privates.html The result of your suggestions will be good code coverage but not real unit testing. The other thing is that I've answeared about problem with db testing not about sense/problem of testing private or protected methods. Tom
Actually, if you read the paragraph near the bottom, he explains his stance on testing private/protected methods and properties "Most of the time, you should be able to test a class by exercising its public methods. If there is significant functionality that is hidden behind private or protected access, that might be a warning sign that there's another class in there struggling to get out" and... "So: Just because the testing of protected and private attributes and methods is possible does not mean that this is a "good thing"."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.