Database Testing with PHPUnit

Disclaimer This post is mainly meant for my personal future reference, but I thought it may be useful to other people too. So it’s not an extensive guide for using DBUnit, but more of a step-by-step list of how to get started using it. Introduction I am currently working on a small PHP project using the Silex framework, or mini-Symfony if you like. It consists of three parts: a RESTful web service (based on Silex), a cron job (also based on Silex) and a JavaScript heavy front-end.¬†The RESTful service is what I’m currently working on and for which I needed some more extensive database testing. The problem The RESTful service project consists of some controllers, which are responsible for handling the available actions that the service is able to perform. For the actual work there is a Service layer and the controllers delegate the heavy lifting to the services in that layer. So even though there are some simple tests for each of the actions in the controller, the services are the most difficult to test since they are data-heavy. For some of the methods a simple mock db adapter returning some mock data was sufficient, but when you really ...

(Read more...)

Sending e-mail with alternative parts plus attachments

Today I was working on my MailService component in a ZF2 project. For this specific project I needed to be able to support alternative parts for text and html combined with attachments. The alternative parts are to allow less sophisticated mail readers, not capable of rendering html, to still read the message. When sending mail using the Zend\Mail\Message class you have to create a Zend\Mime\Message object and assign this object to the body of Zend\Mail\Message, using setBody(). The Zend\Mime\Message object contains the actual parts of the message and the Zend\Mail\Message is responsible for the mail headers. The default content-type of a Zend\Mail\Message is 'multipart/mixed'. This is usually perfect if you're only sending one text part and optionally one or more attachments, but if you want to support alternative parts (requires content-type 'multipart/alternate') AND attachments (requires 'multipart/mixed') then you have a little ...

(Read more...)

Adding custom fields to ZfcUser register form

The ZfcUser module ZfcUser is a Zend Framework commons module that handles basic use cases related to users. It takes care of handling the registration, logging in, logging out, authenticating and user profile management. It is a very well written module, allowing the user of the module to change the behavior without having to hack in the module’s codebase. Tip: never change the code of a vendor module. The problem A common use-case for the ZfcUser module is to add more fields to the registration form (i.e. a firstname and lastname or date of birth). Of course when I needed this functionality myself, I did a quick search and figured I wasn’t the only one needing this. However there were no real¬†conclusive answers of how to do this. I did find one article[1] that helped me to get started. What I needed What I needed was the following: Add the extra fields to the registration form; Make sure the fielddata gets saved to the database when the user registers; Adding validators and filters to the custom fields; Changing the validator messages of the standard ZfcUser userfields; Changing the order of the fields in my registration form. How I accomplished it ...

(Read more...)

Dealing with alternative directory structures in ZF2

I just started diving into Zend Framework 2 after using Zend Framework 1 and even its early predecessors for years. And boy, things have changed! But after some initial reading I think I already see some major advantages, not in the last place the ModuleManager which really allows developers to create independent modules and share them with the community. Anyway on to the actual subject of this post: directory structure. A standard ZF2 application has the following directory structure: config data module public vendor init_autoloader.php public contains index.php and your .htaccess, plus your public web resources such as images javascript and css files. In a shared hosting environment running Plesk the directory structure is like this: httpdocs (this is the webroot) private statistics subdomains etc. We don’t want to put everything in the httpdocs directory, because that’s the public directory and is accessible from the outside world. In a Plesk environment the private directory is used for application level files that aren’t supposed to be accessible from outside. So the solution is pretty simple: Upload the contents from the public directory into httpdocs Upload the other directories (config, data, module, vendor and init_autoloader.php) into private After that you have to ...

(Read more...)

Using Zend_Crypt_Rsa – small example of how to use it

PHPMay 05 20120 Comment

For a recent project I needed to use RSA encryption to encrypt some sensitive data that was being sent from a client to a (JSON) service. Both the client and the service are written in PHP using Zend Framework, so the obvious place to look for classes implementing this functionality is… well the Zend Framework library of course. I was happily surprised finding the class Zend_Crypt_Rsa in the Crypt folder of the Zend Framework. When I wanted to check the online documentation at http://framework.zend.com/manual/en I was less happy, because it turns out there is no documentation for this class. Luckily it’s really not that hard to use. If you check out the code of the class itself it’s pretty self explanatory. However to save someone else some time it might be useful to post the steps I took to get it working, so here we go. 1. First of all you will need to have OpenSSL installed and the openssl PHP extension loaded, because the Zend_Crypt_Rsa class is dependent on it. 2. You have to generate the private and public keys that you’re going to use for the encryption (public key) and decryption (private key). You do this using the ...

(Read more...)