PHPUnit testing singleton dependencies

Unit testing should be simple, but when you have a code base which uses static methods, singletons and a lack of dependency injection, you can run into some serious problems.

The key to writing maintainable and testable code is dependency injection. Without dependency injection, our objects have to rely on systems that may or may not be under our control. We want our unit tests to be executable in an environment which does require a database or a file system or another systems API etc. Dependency injection allows us to provide mocks of these external and frankly irrelevant sources. The mocks will simply tell us what we want to hear and then we can test our own system with this data. Normally we would use PHPUnits getMockBuilder method to create mock objects, this however cannot be used in the case of a singleton for a few reasons:

Continue reading

500 The FastCGI process exited unexpectedly

Recently at work, I’ve came across the error “500 The FastCGI process exited unexpectedly” error code: 0xc0000005, trying to execute a PHP script on an IIS6 and IIS7 server. Knowing how annoying it was to resolve and how many people have the same problem, I thought I would answer your questions.

The problem is caused by excessively requiring more memory than is allocated to your script, which depending on the version of PHP you are running, defaults between 8 and 128 megabytes. To work out how much memory your script is actually using, you should log usage at all points in your script (especially in large loops), this can be done using the functions:

Continue reading

Symfony 2 docs and tutorials

symfony2

The last few week I’ve been working quite a lot with Symfony 2, it’s not the simplest of frameworks and for my next project I think I’ll try another framework – But thats another story. Here are some links and tips to help bail you out, when you inevitably get stuck.

Links

  1. Symblog – This in my opinion is the best place to start learning Symfony 2. Dsyph3r – The author, has created a bunch of tutorials, to walk you through the process of creating a blog. Sadly the tutorials arn’t complete but the amount you can learn quick, makes this the ideal place to get going.
  2. Symfony 2 Cheat Sheet – This is the biggest cheat sheet I’ve ever seen, but Symfony 2 is pretty huge. Here you will find tonnes of snipets to help you with your controllers, routing, Twig templating, Doctrine 2, unit and functional testing, forms & validation, security, caching, translations and the service container.
  3. Symfony 2 Official Website – The amount of docs on this website are extreme, however I find it to boring to read through. However for some areas of Symfony 2, this is the only place you’tre going to get docs which actually make sense.
  4. Symfony 2 API – Exactly what it says on the tin. This is useful, however I find it easier just looking through the Symfony 2 classes myself.

Continue reading

Reset your Git repository back to a previous commit

Reset your Git repository back to a previous push

Earlier today I accidentally deleted quite a lot of code from a Git repo, quickly realising I had made a catastrophic error, I scoured Stack Overflow for a way to reset by entire repository back to a previous commit. Here’s what I found:

First things first make sure your current checkout has no local changes, if it does stash them or just create another clone of the branch you are using.

git stash -u save “My Wicked Cool Feature to stash”

or

git clone repo_url

Reset your working copy, so that it matches exactly what was in your branch during the commit you are reseting to.

git reset --hard sha1_hash

Although you won’t be able to see any changes when you run “git status”, running the below command, will publish the old revision back to HEAD.

git push -f

That’s all, your online repository will now be reset back to the previous commit.