Test your code like a pro – PHPUnit in practice at PHPNW12

PHP NW12 conference is nearly upon us, so I guess it’s about time to tell you more about the workshop that I will be having there on the 5th October morning. I wanted to do that for a very long time, especially that I am a big fan of quality assurance in software engineering. You can read the official abstract at the conference homepage, but here I am gonna tell you a little bit more about what we gonna do during the 3 hour tutorial.

My main focus will be to teach you and lead you through main concepts of unit testing PHP code. It’s a beginners course, so we will go though all the basics of organising your test suite, understanding the benefits of unit tests and making you feel comfortable and confident with the code you write. We’ll do it all big band TDD style – so we will write the tests first and implement the functionality afterwards. It’s very important to get this right and to set up the mindset from the very beginning.

PHPUnit is de-facto a standard in PHP world, so it is also important that you are aware of the main features of the tool. We’ll go through the set up, configuration and options that are the most useful at the beginning – so you can get the most out of the tool. The concepts that I am gonna go through will include:

  • working with typical test suites
  • basic assertions – strings, integers, booleans, object
  • PHPUnit from the command line
  • using phpunit.xml configuration file
  • testing exceptions
  • testing PHP errors, warnings and notices
  • asserting output
  • data driving your tests by using data providers
  • generating code coverage report

If you have never had a chance to unit test your code, but you’d like to try now, it’s the best time to get started. I will do my best to make you feel confident and to help you start your own Journey Towards Continuous Integration, but what you have to do is to go to your boss and convince him to buy you a ticket to my tutorial ( and the conference). There is still plenty of time and there are still some tickets left.

No matter whether you have already bought a ticker or you’re thinking about right now, if there is anything specific you you want me to go through during the tutorial please let me know. I’ll do my best to fit that in. We can also change the course of the tutorial if the group will prefer to touch on some slightly different subjects of unit testing – I am open for suggestions. And I will be accessible after the tutorial too, for any questions, help or even quick hacks. You will even get more chances to speak to me over the next 2 days, during the main conference days – on the 6th and 7th October.

It’s a lot of things to go through so it is very important that you come prepared and focused on the day. We only have 3 hours and I’d like to avoid situation where the hardware/operational issues will stand in our way. While I will try to help everybody out if there are any issues I will encourage you to install PHPUnit on your laptops before, so it is working without a problem. I want to focus on writing the tests and using PHPUnit, not on trying to make PHP itself working on your machines.  I strongly recommend using a non-Windows system. Windows has been proved to be very unreliable if it comes to setting up PHP and PEAR. It does work, but it requires some strong skills and patience. So you’d rather install ubuntu or debian, as this is an environment that everything just works out of the box.

During the course of the next 2 days I will post more detailed instructions about how you can prepare your laptops to make sure you can run PHPUnit on them without any issues, so watch this space closely.

I hope to see you at my tutorial in about 3 weeks!

Symfony2 Live! London – aftermatch

Symfony Live LondonI am sitting on the train back to Sheffield coming back from an awesome Symfony Live London event. It was my first time at Symfony Live conference, and at the same time it was first edition of the conference held in London. Conference was organised perfectly, but was else could you expect from  people like Lorna Jane and Wes Thompson! Great event guys and very well done!

For a person like me, who only started working with Symfony2 a few months ago, it was a mind blowing mine of information about Symfony2, tools that support symfony2 applications like composer and behat and general practises around using the framework itself. I was pretty impressed by the fact how Drupal8 and Symfony2 work together, and get the famous CRM event better. It definitely proves that PHP as the language, environment and surrounding community matures. The talks in fact were only the background of the opportunities that I had today – meeting in person and being able to talk to people like Fabien Potencier (creator of Symfony2), Jordi Boggiano (the man behind composer), Konstantin Kudryashov (the Behat man) and Marcello Duarte (the PHPSpec man). The conference venue was all buzzing – people talking and exchanging experiences.

I have seen 6 talks in total:

Every talk was unique and touched on different subject, but I have to admit Konstantin and Marcello just blown my mind off with they approach to testing with BDD. I still can’t get all the things together and they gave me plenty of things to think about. The next thing I will do will be to replicate what they have done with Behat and PHPSpec2 during their presentation.  I do not agree with what they have said about unit testing, and I still believe that unit testing has it’s own place in the development processes, just needs to be used right and for the right purpose. For me the BBD way they have shown us it’s a different approach to testing only complementing xUnit. Anyway have a look at their slides and at Behat and PHPSpec2 if you haven’t yet. PHPSpec2 is still pre,pre,pre,pre,pre,pre,pre,[pre,] alpha, but looks really promising and I can see myself including it in my toolset.

Finally, I’d like to thank everybody who attended my talk Effective Code Reviews and asked all the questions afterwards. It’s been a pleasure to be able to meet you all and exchange all the experiences around code review process. Thanks to everybody who left the feedback on joind.in, and if you have seen my talk and not yet gave me the feedback please do so. It’s very important for me to know what did you like and what not, so I can improve it in the future.

Enough writing, I am too excited about Behat and PHPSpec2 so I am gonna download it and play with it for another hour, and I hope to meet you all again in a year time at the the next Symfony Live! London Conference!

Speaking at Symfony Live London

Symfony Live London It’s quite late now, but there are still some tickets and time to get on board and come to London to participate at Symfony Live London conference this Friday (14th Sept 2012). The lineup is pretty impressive, including well known speakers like Fabien PotencierJordi BoggianoRowan MerewoodRichard Miller and Konstantin Kudryashov. The more honoured I feel to be able to speak amongst them. I will be talking about code reviews and will share my experiences “straight from the field”. I count on you to challenge me and discuss all the things about development processes, specifically around code quality.

See you in London!

Speaking at Forum PHP Paris 2012

I am getting very excited about next week. I will be speaking in Paris at Forum PHP Paris 2012 conference about software metrics. So if you are interested in software metrics or you want to learn how to read them and use them to produce better software grab a ticket and come to Paris next week. I am looking forward to meet some old friends and also to make some new friendships. Hopefully my inability to speak French will not stop me ;)

PHP54Compatibility 0.2 released

Last night I have merged in a few pull requests adding additional sniffs to the existing collection and packaged it all. List of all supported checks is in the README file. Check the repository on github to see it. Big thanks go to Laura Beth, Nathaniel McHugh and Ben Selby for all the contributions. I have created a pear package and published it on my github pear channel so you can easily install it from there. No more alpha release either, so even easier to install.

$> pear channel-discover proofek.github.com/pear
$> pear install proofek/PHP54Compatibility

There are 2 dependencies at the moment: PHP 5.4 and PHP_CodeSniffer 1.3+. A few people asked my to downgrade the minimum PHP version to 5.3. The reason why I want PHP 5.4 is I want to use 5.4 tokaniser to make sure  code is parsed the way it will be parsed on the target PHP 5.4 platform. If you want to run pre-checks, before you actually upgrade to 5.4, use -f flag to perform forced installation:

$> pear install -f proofek/PHP54Compatibility

But I would still encourage you to upgrade first to get the best results.

PHP 5.4 Compatibility Coding Standard for PHP_CodeSniffer

So with PHP 5.3 upgrade underway (and PHP 5.4 out of the door now!) I thought it’s time to prepare for PHP 5.4 and make sure we’re compatible. So by looking at Wim Godden’s PHP53Compatibility code sniffs I have created a base for PHP 5.4 sniffs that we want to use to make sure we’re compatible. It’s a base, so it’s not finished/perfect in any sense so I will be relying heavily on PHP Community to help me out with it. I will obviously be working on it hard, but any pull requests are greatly appreciated.

So where is it then?! It’s on github! I have also set up my personal github PEAR channel and will release a first alpha package so you can install it easily.

Right, back to work!

PHPUnit assertion gotcha

A recent discovery. If you found yourself upgrading from PHPUnit 3.5.x to PHPUnit 3.6.x be aware of a subtle change. assertEquals() and I guess all related assertions will behave a bit differently after the upgrade. When you assert 2 values and one of them is an object with defined __toString() function PHPUnit 3.5.x will fail the test, while PHPUnit 3.6 will happily pass it (by I am guessing converting the object to string). See an example on gisthub for more details:

$ phpunit testCaseTest.php
PHPUnit 3.5.15 by Sebastian Bergmann.
F.
Time: 1 second, Memory: 4.50Mb
There was 1 failure:
1) TestCase::testAssertStringWithObject
Failed asserting that
myClass Object
(
)
matches expected .
/private/tmp/Testing/testCaseTest.php:16
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
$ phpunit testCaseTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
..
Time: 1 second, Memory: 5.50Mb
OK (2 tests, 2 assertions)

PHP Benelux 2012 – 4 days of awesomeness

PHP BeneluxI was extremely pleased that I was invited to talk at the annual PHP Benelux conference in Antwerp. It was my first time in Antwerp, but I have heard about this conference so much before. And I must tell, all of it was true.

The arrival

But starting from the beginning, which was the landing of PHPNWAir plane (as named by Michelangelo van Dam) with a representation of 7 people from British PHP NorthWest Community (including 3 speakers – Rowan Merewood, Jeremy Coates and me) in Brussels Charleoi. And yes, I do admit it was a poor choice of mine to decide to fly Ryanair from Manchester to this airport. Connection is bad, it’s about an hour drive from Antwerp, and if not the help from PHP Benelux crew (namely @DragonBe and @Martin1982) we would be in trouble. So I am sorry once again guys, I won’t do it again! :D

Socials

From then on I will only guess what heppened next, as I have problems recalling what happened on what day. Speakers dinner was an amazing opportunity to meet the conference crew and speak to the other speakers. A rare occasion indeed. Some of them I already knew from previous conferences like PHPNW, PHPUK and 4developers, but some of them I had the pleasure of meeting for the first time, face to face. There is nothing better, then speaking to other experienced developers at a conference, truly amazing and unique experience. How often you have an opportunity to meet people like @derickr, @weierophinney, @rdohms, @davidcoallier, @dzuelke, @joshholmes, @h, @DragonBe, @skoop and @ianbarber in one place at the same time? And that’s only a short list, cause I could go and go and go naming them one after another!

The conference was executed perfectly! I couldn’t be looked after better even by my own mother! @ThijsFeryn@DragonBe@Martin1982 and the rest of the crew – it’s a massive thank you from me! Sponsors did great too – @ibuildings and @engineyard responsible for the afterconference socials helped a lot with maintaining the spirit and helping everybody enjoy the conference to its very limits! We had so much fun bowling on Friday night (and yes I won!) and watching @juokaz and @dzuelke competing – whether it was about which phone is better or who’s better at Kinect Tennis! Can’t deny the fact that beer tasting session and Belgian fries and spécialités also helped during that day.

The winter BBQ, well… it was bloody cold, but again the beers, liquors, hot food and @Orchestra_io scarf helped to survive the evening. We even managed to go for a pizza to Antwerp late night when we got hungry! Some people even managed to come back in a car’s boot!

Learning

But conference it’s not about fun, but first of all about learning. And hell yeah, there was a lot to learn! I have seen a number of talks (and shamefully missed a few ones for reasons I am not gonna mention here ;)).

I’ll start with @dzuelke‘s  “Large-Scale Data Processing with Hadoop and PHP”. David is always an inspiration for me – an excellent speaker with really good talk. This one definitely gave me a few ideas how I could use Hadoop in my daily job. Hopefully I will find some time to come up with some prototypes.

Helgi Þorbjörnsson also gave a solid talk on “PHAR, the PHP .exe format”. While I already knew a few things about phar, he definitely managed to mention some interesting things about phar I didn’t know before. I really liked the way he engaged the audience. Really good idea!

Rowan Merewood and his “Estimation or, “How to Dig you own Grave”” was another great performance. Nothing new for me there (well, we worked together for a number of years), but I always admire Rowan for his slides and the way he talks. A true masterpiece.

Matthew Weier O’Phinney’s “VIM + *nix Toolchain == PHP IDE” definitely inspired me again to come back and learn a few new things about vim. Inspirational speaker with a lot of charisma!

“PHP traits, treat or threat?” by Nick Belhomme was the talk I really wanted to see since I really didn’t have any time to look at PHP 5.4. While the talk was a bit chaotic at times for me, I still learned a lot about traits, and as of now, I know one thing – I don’t think I like them and I don’t think I will use them – it’s more of a threat for me. They seem to be too messy.

You could tell Marcel was nervous during his “The lust for knowledge and experience” talk. But still it was a good talk with very good professional slides. It was a chance for me to rethink a few things about the relations between “the master”, “apprentice” and “a junior”.

Finally, the best talk of the conference from David Coallier “Taking it to the next level”. I absolutely loved the way he did his talk and how he engaged the audience. He only made me realise how rarly we tell other how much we appriciate their work and how we all take the work of others for granted. Thanks you very much for that David!

My software metrics talk

At last I’d like to thank everybody for attending my “Magic Behind the Numbers – Software Metrics In Practice” talk. I am really glad, that more and more people are looking at improving the quality of their code and want to learn more about it. I was impressed with the fact how many people in Benelux are using tools like PHP Depend and PHP Mess Detector! Keep it up guys! Thank you very much for all the feedback. I’ll definitely use it in the future to improve my presentation and make it more interesting. You can find the slides from my talk on slideshare.

Summary

I will definitely do my best to come back to Antwerp next year, but until then I hope to see you next month in London at PHPUK 2012!

Software metrics at PHPNW 2011 Conference

PHPNW11I am really excited to be able to speak again in Manchester this October. This time I will be speaking about the ways and tools you can use to assess your code quality and its design. While software metrics help a lot and tell a lot about code, you have to remember they only should be treated as a guidance, not the goal in itself.  So be careful and don’t start coding only to make the metrics look pretty. Here is an excellent video from Alberto Savoia for these who’s gone a bit too far with software metrics.

See you in Manchester!

Spring cleaning? Hell no!

http://flic.kr/p/xSB58

It’s summer time already, but every time I hear about cleaning revolution it reminds me of spring cleaning. And I can tell you I hate spring cleaning! I don’t get the whole idea of one time movements just because you didn’t care enough to keep your things tidy all year long!

The whole thing gets triggered now and again, because another developer gets annoyed with the amount of rubbish in the error log. Through years of not caring “suddenly” it’s hard to spot major issues through the flood of notices, warnings and stricts. So Tech Debt Tuesdays, unit test Thursdays and  clean up Fridays take place. Yada yada yada. Everybody get annoyed trying to fix it and after that all gets to normal. Why?! Have Starbucks Mondays and Orange Wednesdays instead – it’s fun! Do your day to day job and keep your code tidy. Every day. Every hour. Every minute. You spot something, you fix it. Simple. And it doesn’t matter whether it’s your code or not. Whether you are going to change it or not. The fact you have seen it is good enough to make you fix it.

I can only repeat after Uncle Bob Martin:

Cleaning code is just part of the normal everyday job of a programmer. It should not be something special on the schedule.

Teams should gradually fix code quality _outside_ the schedule. The improvements should be long term an on-going. Never schedule them!

Code quality issues should _never_ be in the backlog, or on the schedule. They should never get to that point.

Go team!