Saturday, March 29, 2014

First clumsy tests using Phake test doubles framework

Two weeks ago I started learning PHP for my new job at Runroom.

It's my first job as a web developer so I'm still a bit "lost".

Today I tried to write unit test for this class in order to learn how to use Phake test doubles framework:
These were the resulting tests:
Although I like the readability of the tests, I'm not very happy with them because I think they will be too fragile. At the same time I'm not sure how else I could test the behavior of SendingFormByEmailAndRedirecting class...

To give you a bit more of context this is how I'm using it from my code:


  1. I don't really like the name of the class but it's because I don't have any other context. It may be OK if you are using the Command pattern. As a consumer of the API I would expect the "form" object to be passed in to "execute".

    Maybe the tests are trying to tell you something about your design. It could be an interesting experiment, using mocks rather than spies. In the tests, I would put lines 32 and 33 in different tests. Of just remove line 32. Ideally tests should fail for a single reason.

    Interesting problem and interesting post, thank you :-)

    1. Thanks for your feedback, Carlos.

      I don't like the class name either, I'm still struggling with it.
      I imagined the class as representing an "action" so the name tried to describe the action. I don't have more types of actions yet so I'm not using a Command pattern.

      I'll try passing the form too see how it feels.
      This will also allow me to remove the word form from the names of both the "action" and its collaborators.

      You're totally right, I could perfectly remove line 32 (already removed).

      I also added an example of how I'm using the class.

      Thank you very much.

      Best regards,