Category Archives: joind.in

Database-Testing with PHPUnit and sqlite

Today I wanted to test a database-handling class using PHPUnits PHPUnit_Extensions_Database_TestCase and an sqlite in-memory database.

On running the tests I always got the following error-messages:

MyDatabaseTest::testDatabaseConnection with data set #0 ()
PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query:
                DELETE FROM "[tablename]"
             using args: Array
(
)
 [SQLSTATE[HY000]: General error: 1 no such table: [tablename]]

WTF…

After some checking what might help I got different solutions:

  • Use MySQL as test-backend – No, I want to use an sqlite with in-memory storage as it’s fast and doesn’t require too much setup on a CI-Server
  • Use a pre-configured sqlite-file and therefore a file-based sqlite. That might have been possible, but I just wanted to use in-memory….

The solution was rather simple. It seems that PHPUnit deletes the content of the table in question before setting up the table to execute a test. That only works, if the table already exists. Therefore I’ve added a create table xyz statement right after setting up the in-memory-table. So the test-class now looks like that:

class MyDatabaseTest extends PHPUnit_Extensions_Database_TestCase
{
    protected $pdo = null;

    /**
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    public function getConnection()
    {
        if (null === $this->pdo) {
            $this->pdo = new PDO('sqlite::memory:');
            $this->pdo->exec('create table [tablename]([table-definition])');
        }
        return $this->createDefaultDBConnection($this->pdo, ':memory:');
    }

    /**
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet()
    {
        return $this->createXMLDataSet('[path/to/xml-seed-file]');
    }

    public function testDatabaseConnection()
    {
        $pdo = $this->getConnection()->getConnection();
        // Do your database-tests here using the required pdo-object
    }
}

Perhaps this can help someone to find a solution earlier 😉

Displaying Date-Ranges

Recently a new issue popped up on the joind.in issue-tracker which called for nicer date-ranges. As I did something like that a few weeks prior I grabbed the issue and starded working.

The output is a small library that enables you to display a date-range like 12. – 13. March 2015 instead of 12. March 2015 – 13. March 2015.

Simple thing.

It also displays March 12th – March 13th 2015 if you want to.

Not that easy any more.

I won’t bother you with the details on how it’s done (it seemed more difficult than it actually was) as you can see that in the source-code if you are interested.

Continue reading Displaying Date-Ranges

joind.in – a personal plea

Joind.in is a community driven project to give feedback to speakers at conferences and events.

And besides that it is a great ressource for all those that want to participate in the community. Not only as it’s an opensource project that everyone can help to make even better! But to provide feedback it also contains a list of almost every conference and event that is of any significance to the (PHP-)community.

And as the driving force underneath the hood of joind.in is a great API everyone interested can do a lot of cool things with the data no one has ever thought of.

To get the most out of joind.in (and therefore that API) some things should to be considered when creating or editing an event in joind.in. Some of these I’ll list here:

Continue reading joind.in – a personal plea