PHPUnit without success / fail message

I had this problem today and finally found out that it was only a PHP error causing this. PHPUnit would terminate without a word – seemingly without error. But turns out I had a PHP error in my test.

To find out what exactly is going on just try this option for your test:

This will make sure you see the output of both PHPUnit and your potential PHP errors. Only recommended for a not-so-many tests setup, since this option will start a new PHP instance for each test.

Inject DoctrineORM EntityManager in ZF2 Form

This one took me a while to figure out so I thought I would write it up real quick in case somebody else is trying to figure  this out.

TLDR – “Cut the crap and just show me how it’s done” – LINK

I was setting up a Zend Framework 2 project with Doctrine. You can find quite a lot of tutorials for that. For starters you could check out Jason Grimes’ ZF2 Doctrine tutorial.

Setting up the entities and my edit forms was no problem. But making use of DoctrineModule\Form\Element\ObjectSelect took some more work. I wanted to use it for a category select. But the form element needs the Doctrine entityManager. So I wrote up my form like this:

Hand-ish, not very elegant way of injecting Doctrine EntityManager in Zf2 Form

Look at how I inject the entityManager and keep dragging it through the application just so I can use it in the one form field below.

And in my Controller I had to create the form like this:

Note that there is a BaseController which provides the getEntityManager() function. I created it because I need more than one form with the entityManager and providing the function at a central place seemed like the best idea at that point.

The BaseController looks something like that:

All pretty tedious and not very elegant. So I started looking for a more elegant way to do things.

(Semi)-automated injection of Doctrine EntityManager

The configuration of Zf2 is quite mighty and worth looking at if you want things to be done FOR you instead of just tediously trying to build and maintain stuff yourself. Still haven’t found a comprehensive guide to it so it took me  a lot of digging around.

First of all you instead of creating your form directly like that:

you get your form through the FormElementManager like this:

This way you can make use of the getFormElementConfig() function in your Module.php:

What you can see here is that there is a function called  getFormElementConfig() which accepts an array. Most important part here are the “initializers”. You can have more than one initializer and the names (in my case ObjectManagerInitializer) can be anything you like. Though it makes sense to chose a speaking name of course.

As soon as you get your form via the FormElementManager (don’t forget to add your FormName to the invokables part of the array) Zf2 recognizes there is some work to be done on getting your new – much cooler – form.

The initializer will check if your form implements the ObjectManagerAwareInterface and if so it will get the entityManager via the serviceLocator and set it using setObjectManager().

One thing that remains to be done is change the form accordingly. Here is the Code:

The Form now implements the ObjectManagerAwareInterface – which requires the functions getObjectManager() and setObjectManager(). When creating the formElements you only have to call the getObjectManager() function now and you are done.

Note that you have to add the fields not in __construct but in init() now. At the point of construction the objectManager will not yet be present.

Voilà! Now you can create your form without worrying about creating and injecting anything beforehand! :)


Database under git version control – basic version ;)

If you just want to backup your database with Git and not think much about it just read this post. If you want to dive a little deeper and read why Git might not be the ideal solution for doing this pls check out this thread on Stackoverflow.

Note: I am using Debian as testing environment – those scripts are only tested on this machine.

Making a backup of a mysql-database is actually a very basic thing:

  • cd into your project directory
  • call mysqldump and export the dump into a file called schema.sql
  • add the dump-file to your repo
  • done

But what if you can never remember things and want to try to automate things – what to do?

1. Solution: Cronjob

Just create a little cronjob that will do backups once in a while.

2. Solution: Git pre-commit hook

The hooks Git offers are actually a very neat thing. They enable you to do stuff before (or after, or whatever) EVERY commit. So once you have a hook set up you never have to think again – at least not about that. ;)

Just cd into the .git/hooks/ directory in your project and take a look:
There are already lots of examples for you to check out:

For now we only care about the pre-commit.sample.
Just make a copy of that:

and open it up:

Make sure you delete all the stuff that’s already in there and replace it with this content:

Not much to see in this file. Just a basic dump command and a git add command to add your schema.sql to your git repo.

Now every time you do a commit you will do a schema backup automatically. If you want to include the whole data just remove the -d option from the mysqldump command. If you do include data keep in mind  that your Git-repo can get VERY large if you have a big database. (in this case you might want to consider other backup options)


Create a new repository