Unit Testing with Zend Framework 1.11 and PHPUnit

Either I failed in my Google-Foo, or there is not a lot of current documentation on setting up Unit Testing for Zend Framework 1.11. So, having worked through the process, here’s my approach.

Most of my notes are based on the excellent ZendCast Unit Testing with the Zend Framework with Zend_Test and PHPUnit. While this video is very educational and helpful, it was made with pre 1.8 versions of Zend Framework. There have been many changes since then – to ZF, PHPUnit, and even the underlying systems. Some of those changes break the routine defined in the video. Also, the video assumes everything is done right, and doesn’t necessarily handle troubleshooting. Continue reading

Advertisements

Building RESTful Services with Zend Framework

As a followup to my previous post, I now turn to RESTful web services. I originally encountered the term when attending php|tropics in 2005, where George Schlossnaggle likened it to simple GET and POST requests. Since then, the architectural style — and developer understanding of the architectural style — has improved a bit, and a more solid definition can be made.

At its heart, REST simply dictates that a given resource have a unique address, and that you interact with that resource using HTTP verbs. The standard verbs utilized are:

  • GET: retrieve a list of resources, or, if an identifier is present, view a single resource
  • POST: create a new resource with the data provided in the POST
  • PUT: update an existing resource as specified by an identifier, using the PUT data
  • DELETE: delete an existing resource as specified by an identifier

Continue reading

Supporting PUT & DELETE in the Zend Framework

Creating a RESTful Web service is not simply about serving read-only content through HTTP GET requests. It’s about using the full range of HTTP’s constrained interface to allow clients to consume or create resources within your service. Take a look at CouchDB, for example. Its initial releases look very promising, and the server accepts GET, POST, PUT, and DELETE requests to manipulate resources in the system. I can’t wait until the project implements authentication and authorization features; then, it will look much more attractive for real-world use.

But I digress… Continue reading

PUT or POST: The REST of the Story

Web service designers have tried for some time now to correlate CRUD (Create, Retrieve, Update and Delete) semantics with the Representational State Transfer (REST) verbs defined by the HTTP specification–GET, PUT, POST, DELETE, HEAD, etc.

So often, developers will try to correlate these two concepts–CRUD and REST–using a one-to-one mapping of verbs from the two spaces, like this:

  • Create = PUT
  • Retrieve = GET
  • Update = POST
  • Delete = DELETE

“How to Create a REST Protocol” is an example of a very well-written article about REST, but which makes this faulty assumption. (In fairness to the author, he may well have merely “simplified REST for the masses”, as his article doesn’t specifically state that this mapping is the ONLY valid mapping. And indeed, he makes the statement that the reader should not assume the mapping indicates a direct mapping to SQL operations.) Continue reading

Safely Handling MySQL Transactions in Zend Framework

Preface

If you’re using Zend MVC and attempting to store transactional data, below is a quick and easy way to handle exceptions while still allowing rollbacks.

The code will essentially ensure that your transaction safely fails if an exception is thrown (i.e. the dreaded Integrity constraint violation: 1062 Duplicate entry) and fall back on your error controller for handling the exception. You can then deal with these exceptions in any manner you see fit by utilising Zend_Log_Writer_Stream. Continue reading

PHP Constructor Best Practices And The Prototype Pattern

If your knowledge of constructors ends with “the place where I put my object initialization code,” read on. While this is mostly what a constructor is, the way a developer crafts their class constructor greatly impacts the initial API of a particular class/object; which ultimately affects usability and extensibility. After all, the constructor is the first impression a particular class can make.

Constructors, in their current form, have been in PHP since 5.0.0. Previous to 5.0, PHP loosely followed the style similar to that of C++ where the name of the method matching the name of the class would act as the class constructor. PHP 5 brought us the __construct() “magic method” which greatly formalized the new object initialization routine. Continue reading

Convert DOC to PDF in PHP

Since the initial release of LiveDocx last week, one of the most common questions that I keep receiving via e-mail is:

How can I convert DOC to PDF in PHP?

Although it was not the original intention of LiveDocx to offer such file format conversion, it is very possible. In this post, I would like to present a little class, which does exactly that. Continue reading