How to Install Sismo
So, as easy as Fabien makes this look like, in my case it wasn’t that easy to get Sismo (his small yet very nice personal continuous integration “server”) up and running on my local machine. These were the steps I had to take:
-
Create a directory for Sismo, e.g.
/Users/matthiasnoback/Sites/sismo
-
Download sismo.php and copy the file to the directory you have just created
-
Create a
VirtualHost
for Sismo (for example, usesismo.local
as a server name
PHPUnit: Writing a Custom Assertion
When you see yourself repeating a number of assertions in your unit tests, or you have to think hard each time you make some kind of assertion, it’s time to create your own assertions, which wraps these complicated assertions into one single method call on your TestCase
class. In the example below, I will create a custom assertion which would recognize the following JSON string as a “successful response”:
{"success":true}
Inside a TestCase
we could run the following lines of code to verify the successfulness of the given JSON response string:
Wordpress & Symfony2: using the CssSelector and FluentDOM to filter HTML snippets
Recently I created my first WordPress template (the one you are looking at right now) and I was left both in shock and in great awe. This piece of software written in PHP has many users (like myself), yet it is so very outdated. The filtering mechanism by which you can modify HTML after it is generated by WordPress feels very dirty. But because a lot of the HTML snippets that WordPress generates are not suitable for decorating with Twitter Bootstrap’s beautiful CSS rules, I used those dirty filters extensively.
Silex: set up your project for testing with PHPUnit
In my previous post I wrote down a set of requirements for Silex applications. There were a few things left for another post: first of all, I want to have unit tests, nicely organized in directories that correspond to the namespaces of my project’s classes. This means that the tests for Acme\SomeNamespace\SomeClass
should be found in Acme\Tests\SomeNamespace\SomeClass
.
Organizing your unit tests
I want to write my tests for the PHPUnit framework. This allows me to use some PHPUnit best practices: first of all we define our PHPUnit configuration file in /app/phpunit.xml
.
First of all we point PHPUnit to a bootstrap file (of which we will later define it’s contents). We also set an environment variable called “env” whose value is “test”. Finally we set the location of our app.php
file inside the server variable “env”.
Silex: getting your project structure right
When I created my first Silex project, I almost felt encouraged to let go of my high standards in programming. So things were starting to look very much like my “legacy” PHP projects, in which everything was put in functions with lengthy parameter lists and those functions were called from within a single index.php
file. I ignored many of the things about high quality software development I had learned in previous years. The result of this, was a project much less maintainable than my other recent projects.
The Symfony2 Serializer Component: create a Normalizer for JSON class hinting
I was looking for a way to serialize domain objects into JSON strings and back again into objects of the original class. This is essential for webservices, especially those built around the JSON-RPC specification that allow clients to make calls to their server and, for example add new personal data to their database of contacts. It makes life a lot easier when this data matches the field names and inner structure of your own domain objects. For example, a JSON string like {"name":"Matthias Noback"}
should be easily translatable to an object of class Acme\Webservice\Entity\Person
with a private property name. But how do you know which class to instantiate, when you only have the simple JSON string above?
Symfony2: dynamically add routes
Earlier I was looking for an alternative to Symfony1’s routing.load_configuration
event, so I could add some extra routes on-the-fly. This may be useful, when routes change in more ways than only variable request parameters as part of routes do (you know, like /blog/{id}
). I got it completely wrong in my previous post about this subject. Of course, adding extra routes is a matter of creating a custom routing loader, and tell the framework about it using the service container. So, there we go.
Symfony2 & Doctrine Common: creating powerful annotations
I was looking into the Doctrine Common library; it seems to me that especially the AnnotationReader
is quite interesting. Several Symfony2 bundles use annotation for quick configuration. For example adding an @Route
annotation to your actions allows you to add them “automatically” to the route collection. The bundles that leverage the possibilities of annotation all use the Doctrine Common AnnotationReader (in fact, the cached version) for retrieving all the annotations from your classes and methods. The annotation reader works like this: it looks for annotations, for which it tries to instantiate a class. This may be a class made available by adding a use
statement to your file. That is why you have to add use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
to any PHP file in which you use the @Route
annotation.
Symfony2: Extending forms globally ("you know, like with CSRF protection")
With Symfony2, many things are managed through dependency injection. Except for forms. Oh, wait, forms can be services too of course! Remember? Any class instance can be a service… Now, as in many other cases, Symfony2’s FrameworkBundle
adds some magic to the Form component by creating services that link several parts of the Form component together. For example, depending on the value of the framework.csrf_protection
parameter in config.yml
file a hidden field “_token” will be added to all forms, site-wide. To create this kind of very general “form extension” in your own project, you need to do just a few things (I am going to use the example of a “Captcha” form extension, but I don’t give a full implementation of this idea).
Symfony2: Creating a Validator with dependencies? Make it a service!
One of the ugly things about Symfony 1 validators was that their dependencies were generally fetched from far away, mainly by calling sfContext::getInstance()
. With Symfony2 and it’s service container, this isn’t necessary anymore. Validators can be services. The example below is quite simple, but given you can inject anything you want, you can make it quite complicated. I show you how to create a validator that checks with the router of the value-under-validation is a route.