views:

42

answers:

1

I'm trying to use PHPUnit to unit test some class methods that return SQL. These classes should work with any Zend_Db adapter, so I would like the tests to do the same. My tests look a little like this:

public function testEtcGeneratesCorrectSQL()
{
    $model = new ClassBeingTested();

    // do some stuff

    $sql = $model->__toString();

    $this->assertEquals('SELECT foo.* FROM foo WHERE bar = 1', $sql);
}

the problem is differences in escaping between adapters. If I run this test using Pdo_Mysql, I'll get an error like this:

--- Expected
+++ Actual
@@ @@
-SELECT foo.* FROM foo WHERE bar = 1
+SELECT `foo`.* FROM `foo` WHERE `bar` = 1

if I use the Sqlite adapter:

--- Expected
+++ Actual
@@ @@
-SELECT foo.* FROM foo WHERE bar = 1
+SELECT "foo".* FROM "foo" WHERE "bar" = 1

so what's the right thing to do here? Is there a way to disable the escaping in Zend_Db I can turn on just for the purpose of these tests? Do I hardcode in the adapter type and then adjust my expected output to match? Or strip out the different quote characters before doing the assertion?

+2  A: 

Use a constant rather than hardcoding either set of quotes, so for MySQL:

$this->assertEquals('SELECT ' . DB_QUOTE . 'foo' . DB_QUOTE . '.* FROM '
                    . DB_QUOTE . 'foo' . DB_QUOTE . ' WHERE '
                    . DB_QUOTE . 'bar' . DB_QUOTE . ' = 1');

Which looks absolutely horrid, but it will work if you set DB_QUOTE depending on the driver you're using.

Alan
And then what happens if you want to test more than one driver? You're forced to do it in a new PHP process. If you wanted to go with this idea, use variables as it would allow for more flexibility when testing...
ircmaxell
Thanks to you both. It seems the adapters have a getQuoteIdentifierSymbol() method, so I'll be able to put this into a variable and include in my assertions
Tim Fountain