A Bit About Burnout

I’ve been thinking a lot lately about burnout, I’ve had a couple of encounters with it in the last year myself and also a few people close to me have found themselves struggling internally after working too hard. I don’t really have any advice to give – the advice I offer to myself is to know my own limitations, know when I am close to them and need to tread carefully, and to stand up for myself against the sources of stress or overwork if the need arises.

However it happens, the question a burned-out person seems to ask is “how long will I feel this way?” and there is no quantifiable answer, and no guaranteed cure for that dragging fatigue. Most of the advice I’ve heard revolves around “get a hobby” – this time around I realised what I needed to hear was “give yourself permission to waste time”. The first one I don’t have a problem with, anyone who knows me knows I’m a live wire and always doing 6 things at once. The second one … that’s the killer.

Earlier in the year I did a lot of travel for a particular work project, at a time when my diary was already pretty full and I was preparing for a big conference. With one thing and another, by the time it was all over, I realised I needed to make some changes. Lots of elements of my life – friends, family, relationship and health – had all taken a beating while I was off dealing with all that I needed to do, and I realised there was a lot to do to get myself to where I wanted to be on all those other fronts. Last week I realised that although it took 10 weeks, now I feel like myself, finally picking up the strands of hobbies and having the energy to contribute to things that once interested me. Its not a big chunk of time to spend “healing” and I am glad now that I did step away, allow myself to vegetate and chill out with those close to me instead of being driven all the time to the next thing on the list.

Most of all – I hope other people come through the experience as unscathed as I have, and that I learned something for next time.

Adding PUT variables to Request Object in Zend Framework

When I wrote recently about testing web services within Zend Framework, I missed out a really key piece of information! I didn’t explain how I was reading the PUT vars in my controller code in the first place – so I’ll rectify that omission now.

Its very simple: I have extended Zend_Controller_Action with my own, and all controllers inherit from here. This has a routeAction() which grabs the incoming variables from a PUT request and sets them as parameters within the usual $this->getRequest() scope, then forwards on the request. Here is my class:

class My_Controller_Action extends Zend_Controller_Action
{
    public function routeAction()    
    {
        // handle grabbing PUT vars
        if($this->getRequest()->isPut()) {
            parse_str($this->getRequest()->getRawBody(), $params);
            foreach($params as $key => $value) {
                $this->getRequest()->setParam($key, $value);
            }
        }
        $this->_forward(strtolower($this->getRequest()->getMethod()));
    }
}

So in my controller code, I simply call out to $this->getRequest()->getParam(‘name’) or whatever, and in my tests I can set those parameters as I showed in my other article.

I hope this makes sense, its one of those things I set up once and use a lot (and now I’ll be able to refer to how I did it!), if you have any queries, comments, improvements or if this helps you then please leave a comment – I haven’t come across anyone else doing anything similar but I know there must be, so let me know!

Using Zend_Test for Web Services

Recently I had cause to develop a web service and so I wrote some tests to go along with it – or I was about to. When I looked at the asserts available in Zend_Test, they were all geared towards HTML/CSS output – but we can test just as effectively on another output.

Using Zend_Test, I set up the request object I wanted to send, and dispatched it. Then I retrieved the body and, since this service returns JSON, I json_decoded it. This gives me an object – and I can go ahead and use all the functionality of PHPUnit, with or without Zend_Test’s additions, to test my service. Its perhaps easiest to show this in a few steps.

Setting up the request object

The idea here is that you set up any parameters you need to including the HTTP verb to use and cookies if needed, then despatch the request. Here’s a few examples, first a simple GET method, with a cookie.

        $request = $this->getRequest();
        $request->setMethod('GET');
        $request->setCookie('token','xxxx');
        $this->dispatch('/user/24');

Including data with a POST request:

        $request = $this->getRequest();
        $request->setMethod('POST');
        $request->setPost(array(
            'name' => 'new user',
            'organisation' => 49
            ));
        $this->dispatch('/user');

This is a REST service, so I also tested PUT and DELETE methods. DELETE just needs the setMethod() call since it doesn’t have any data with it, but PUT was a bit trickier – here’s an example of what I used:

        $request = $this->getRequest();
        $request->setMethod('PUT');
        $params = array('name' => 'Harry Potter');
        $request->setRawBody(http_build_query($params));
        $this->dispatch('/user/48');

Decoding the Response

This is the easy part, all I do is check the status code is what was expected, and then decode the response. My web service returns JSON so this part of each test looks something like:

        $this->assertResponseCode('200');

        $response = json_decode($this->getResponse()->getBody());

Testing the content of the response

Here we get into classic PHPUnit territory and simply use the assertTrue and assertEquals calls we’d use when testing anything else, an example is included for completeness:

        $this->assertEquals($response->contentType, 'user');
        $this->assertTrue(is_numeric($response->id));
        $this->assertEquals($response->name, 'new user');
        $this->assertEquals($response->organisation, 49);

In Conclusion

By combining the request/response awareness of Zend_Test with standard PHPUnit testing strategies, its easy to test web services as well as web pages. I hope the examples are helpful – if they help you or if you have anything to add, then leave a comment!

The Writing on the Wall

I’m currently redecorating in the house and have moved my office up to the top floor temporarily – I like it up here but its a flight of stairs to the bathroom, two to answer the door and three to get a cup of coffee and I also have no wired network! I started stripping the wallpaper off my real office (which will be my office again once its done), and look what I found:

office What's under the wallpaper?

Can you tell what it is yet? Magnificent ? In His Flying Machine

I think this room is going to be a bit of a mission, I have one wall with half a missing skirting board which has had its fireplace plastered up twice, one where the plaster is falling off much faster than the old wallpaper, and one outside wall which is damp and in such bad shape I actually had to go outside and check it looked OK from there. And this is is the smallest room in the house …

Customising Screen-Profile Files

I wrote a while ago about the new version of screen in Ubuntu Jaunty. Screen is an application which lets you run lots of tabs inside one terminal window, then disconnect from screen and reconnect again later. You can turn on and off a whole bunch of notifications which appear inside screen, and I also added the pink tabs I used with older versions of screen.

The tabs line is the one in the profile file which starts “caption always”.

caption always "%{wk}%H%{Bk}|%{Mk}%?%-Lw%?%{km}[%n*%f %t]%?(%u)%?%{mk}%?%+Lw%? %=%{Bk}"

I also altered the “hardstatus string” line, which holds lots of placeholders, to remove the clock – I run screen on my laptop inside KDE mostly, so I already know what time it is. Annoyingly this isn’t customisable via the menus but I edited my profile file to get rid of it – here’s the diff

29c29
< hardstatus string '%99`%{= kw} %100`%112`%= %102`%101`%114`%115`%108`%113`%119`%117`%118`%116`%106`%104`%103`%105`%107`%Y-%m-%d %0c:%s'
---
> hardstatus string '%99`%{= kw} %100`%112`%= %102`%101`%114`%115`%108`%113`%119`%117`%118`%116`%106`%104`%103`%105`%107`'

The only thing annoying me now is that screen seems to constantly redraw itself, so Konsole thinks there is activity in that screen, when there isn’t. Suggestions on stopping this or more ways you can customise your screen file are gratefully received – just add a comment!

Upgrading Subversion Server to 1.5

I’m really excited about the magic merging features in the newer subversion versions at 1.5 and later. I’m still figuring out which are actually useful, more on those another day, but I wanted to mention something I ran into when upgrading my server.

Some time ago I upgraded the subversion server to subversion 1.5, and the clients that use it are probably mostly on 1.5 as well. We haven’t had any compatibility problems between versions on this upgrade, which is good news since a few versions ago there was a release which caused any newer client to render the repo unreadable by any older client. Predictably someone in the office upgraded their client one day and it took us a good few hours to work out why subversion had stopped working!

The Subversion 1.5 upgrade doesn’t turn on all the 1.5 features by default, but will upgrade to 1.5 and allow older clients to continue to work with it. If you want to upgrade to the 1.5 features though, you’ll need to make sure that all users have clients of version 1.5 or later, and then upgrade the repo by running:

svnadmin upgrade

Once this is done you can start using the new merge tracking features in subversion – enjoy!

5 Ways to Make Friends at a Technical Conference

These are my top tips for getting along and meeting new people at a technical conference.

Take an extension cable

Conferences are notorious for having too few and too short power leads, and everyone needs to recharge laptops, especially in hands-on sessions like tutorial day. Having an extension cable will make you instant friends as you bring power to more people.

Join in the pre-conference hype

Follow the nominated twitter tag and log into the IRC channel if there is one. Find out who is staying in the same place as you or arriving at the same time, arrange to travel together or meet for a pre-conference drink to break the ice.

Attend the extra-curricular events

Don’t think for a moment that when the official schedule ends, you are off-duty for the night! Find out about any social activities going on – and if there is an informal track such as an unconference, make sure you attend, or even participate. This is a great opportunity to meet more people and see more talks in a much less structured environment.

Take business cards

Or if you don’t have that kind of job (or even if you do!) get some moo cards to take with you so you can pass on your blog/email address, name and twitter details to people you want to stay in touch with after the event.

Stay an extra day

The party starts when the conference ends, so hang around for 24 hours or so and join in :) Especially for the speakers (whose rooms are paid for) and those travelling internationally, there’s no need to rush off straight away. Some of the best times I’ve had at conferences have been after the main event.

Keep in touch

Write up your experiences on your blog (do you have a blog? If not, this is a great place to start) and tag it appropriately. Comment on other people’s and stay in touch with the new friends that you made at the conference.

OK, so technically this is six ways to make friends, but I won’t apologise for that :) What’s your top tip for conference attendees?

PHP 5.3 Feature: Late Static Binding

With the release of PHP 5.3 comes the functionality “late static binding”, a feature which I’ve been impatient to see for quite some time! This is something a lot of developers have been puzzled by in earlier versions of PHP 5, probably without being aware it had a name. In PHP 5.3 there is functionality for dealing with this situation so here’s an outline the problem and then a look at how to avoid it with PHP 5.3.

The issue arises because of the way PHP classes refer to themselves. Keywords like self are resolved at compile time, which means that where classes inherit from one another, self will always relate to the class where it is mentioned and not the class which is inheriting it. Many class hierarchies will have common functionality across classes but which need some form of “which class is this?” awareness, either to access the class name or a variable declared in the class so that a method declared in the parent can be used in all inheriting classes. Without late static binding, this will always resolve to the declaring class, leaving developers the choice between copy/pasting the same function into every class that needs it or turning their static method into a dynamic one, since $this resolves as expected.

It’s perhaps clearer to illustrate the problem by showing an example. Here I have a base class Record, which all my other classes will extend from (in the tradition of these things, its an over-simplified and not-terribly-useful example but I think it shows the point):

class Record {
    
    protected static $tableName = 'base';

    public static function getTableName() {
        return self::$tableName;
    }
}

So, I’m ready to write my first useful class, which will be my user class – it will have its own variables but can inherit the function since they’re identical.

class User extends Record {
    protected static $tableName = 'users';
}

So if we call the getTableName method against the User class – what would you expect the output to be?

User::getTableName();  // returns "base"

That isn’t what I expected when I first ran into this scenario. This is a pretty common thing to want to do, I’ve seen people have this problem when implementing active record patterns, when creating re-usable form widgets with their own templates, and in countless other applications. With PHP 5.3, the static keyword has been implemented to allow us to get the value of the class the code is actually executing inside rather than where it was inherited from. We simply replace the “self” in our Record class with “static”:

class Record {

    protected static $tableName = 'base';

    public static function getTableName() {
        return static::$tableName;
    }
}

This keyword evaluates differently and has awareness of its calling context – so if we repeat our call to the getTableName method against the User class:

User::getTableName(); // returns "users"

That’s better :)

This feature doesn’t make this problem go away – the self keyword still resolves to parent so as PHP developers we will still see this slightly unexpected behaviour where the parent values are returned. However there is now a solution to it in the shape of the new static keyword. Its a feature I’m delighted to see included and I’m sure it’ll be helpful in a wide range of applications!

Does this help you? Have you run into this behaviour before? And how did you solve it? Leave a comment!

PHPUnit with Zend_Controller_Action_Helper

I’m currently working on a REST service built in Zend Framework and I ran into problems very quickly – when I tried to write the first unit test for the first action in fact! PHPUnit was just dying when I asked it to dispatch() any URL which didn’t return HTML, it wasn’t even giving its usual output.

What was actually happening was I was making use of Zend_Controller_Action_Helper_Json, my service returns JSON and this action helper takes input, transforms it into JSON, sets the content-type correctly and tells ZF not to look for a view since we don’t need one. I thought this was pretty neat.

But look at the start of the declaration for the action helper:

class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Suppress exit when sendJson() called
     * @var boolean
     */
    public $suppressExit = false;
...

By default the JSON action helper will exit() – which of course when run from phpunit causes that to exit as well! There is the class variable there so it was simple to turn off – I just extended my class and changed the value of that $suppressExit variable.

class Zend_Controller_Action_Helper_ServiceJson extends Zend_Controller_Action_Helper_Json {
    public $suppressExit = true;
}

My tests now run successfully and I can build my application, hopefully next time I’ll realise what I’m doing wrong faster!

Ubuntu Netbook Remix on Aspire One

A while ago, actually quite a while ago in May, I put the Ubuntu Netbook Remix version of Ubuntu 9.04 Jaunty Jackalope onto my lovely blue Acer Aspire One netbook.

The way this works is that on another machine (or I guess on an existing OS on the netbook) you download a bootable USB key image. With some trepidation (not a whole lot, I did back up first), I put in the USB key and settled in to see how far I could get with the installation.

Well, a short time later I realised I’d finished installing and was really just fiddling! Straight out of the box the wifi worked, the hibernate worked, sound (in and out) worked, the webcam worked, and there is this great window handler thing which amalgamates title bar and task bar into one. There’s also a cute menu on the desktop – all in all its really neat:

The working hibernate in particular has really made a big difference, at home the netbook just gets used for short bursts and lives next to my bed, usually plugged in. When it comes into its own though is at conferences! I can flip this thing open, use it, and flip it shut, pretty much all day. The startup time is really small from suspend and so long as I’m only dipping in and out (at conferences, I’m mostly in talks so I’m only ever checking mail etc), the battery life easily lasts a day.

Thanks to the Ubuntu folks – this is one quality piece of software and now I love my little netbook even more. Anyone else using the netbook remix? Were your experiences as good as mine?