<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Hogue&#039;s Blog</title>
	<atom:link href="http://erichogue.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://erichogue.ca</link>
	<description>Small programming blog</description>
	<lastBuildDate>Wed, 13 Feb 2013 02:30:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mocking PDO in PHPUnit</title>
		<link>http://erichogue.ca/2013/02/best-practices/mocking-pdo-in-phpunit/</link>
		<comments>http://erichogue.ca/2013/02/best-practices/mocking-pdo-in-phpunit/#comments</comments>
		<pubDate>Wed, 13 Feb 2013 02:30:10 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[PDO]]></category>
		<category><![CDATA[PHPUnit]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=1105</guid>
		<description><![CDATA[The subject of mocking a PDO object in PHPUnit has come around a few times lately. It cannot be done like normal classes because a PDO object cannot be serialized. $pdo = $this->getMockBuilder('PDO') ->disableOriginalConstructor() ->getMock(); This will work on another class, but with PDO you will get this error: PDOException: You cannot serialize or unserialize [...]]]></description>
				<content:encoded><![CDATA[<p>The subject of mocking a PDO object in PHPUnit has come around a few times lately. It cannot be done like normal classes because a PDO object cannot be serialized. </p>
<pre>
$pdo = $this->getMockBuilder('PDO')
    ->disableOriginalConstructor()
    ->getMock();
</pre>
<p>This will work on another class, but with PDO you will get this error: </p>
<pre>
PDOException: You cannot serialize or unserialize PDO instances
</pre>
<h2>The solution</h2>
<p>My solution for this problem is to create a class that derive from PDO. This class has only an empty constructor. Then you can mock this class, making sure that you don&#8217;t disable the original constructor. This way the mocked object can be passed to the code to test even if this code does type hinting. </p>
<pre>
class PDOMock extends \PDO {
    public function __construct() {}
}

class PDOTest extends \PHPUnit_Framework_TestCase {
    public function setup() {
        $pdo = $this->getMockBuilder('PDOMock')
            ->getMock();
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2013/02/best-practices/mocking-pdo-in-phpunit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Conferences</title>
		<link>http://erichogue.ca/2012/10/training/conferences/</link>
		<comments>http://erichogue.ca/2012/10/training/conferences/#comments</comments>
		<pubDate>Sat, 20 Oct 2012 00:19:40 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Confoo]]></category>
		<category><![CDATA[speaking]]></category>
		<category><![CDATA[True North PHP]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=1057</guid>
		<description><![CDATA[In the next few months, I will be a 2 conferences. True North PHP True North PHP is a new conference that will be held in the Toronto area. It will be on November 2nd and 3rd. I&#8217;m very excited as I was chosen to speak. I will give an introduction to Continuous Integration. If [...]]]></description>
				<content:encoded><![CDATA[<p>In the next few months, I will be a 2 conferences. </p>
<h2>True North PHP</h2>
<p><a href="http://truenorthphp.ca/" title="True North PHP">True North PHP</a> is a new conference that will be held in the Toronto area. It will be on November 2nd and 3rd. I&#8217;m very excited as I was chosen to speak. I will give an introduction to Continuous Integration. If you&#8217;re curious about CI, come see me. </p>
<p>They managed to get an amazing line up of <a href="http://truenorthphp.ca/speakers.php" title="True North PHP Speakers">speakers</a>. And at $200, <a href="http://truenorthphp.ca/tickets.php" title="Buy True North PHP Tickets">the tickets</a> are really affordable. </p>
<h2>Confoo</h2>
<p><a href="http://confoo.ca" title="Confoo">Confoo</a> will have its fourth edition from February 27 to March 1st of 2013. Confoo is always great and the next edition promise to be as good as the previous ones. I will not speak there, but I will attend it. </p>
<p>If you&#8217;re in the Montreal region, <a href="http://confoo.ca/en/register" title="Buy Confoo tickets">buy your ticket</a>, you won&#8217;t regret it.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2012/10/training/conferences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Continuous Testing in PHP with Guard</title>
		<link>http://erichogue.ca/2012/09/php/continuous-testing-in-php-with-guard/</link>
		<comments>http://erichogue.ca/2012/09/php/continuous-testing-in-php-with-guard/#comments</comments>
		<pubDate>Tue, 04 Sep 2012 01:43:17 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=949</guid>
		<description><![CDATA[A few months ago, I wrote about continuous testing. When I wrote that post, I was using watchr to run my tests. A few weeks ago, I started using Guard instead of watchr and I wouldn&#8217;t go back. Reasons to Change One of the problems I had with watchr, is that it did not see [...]]]></description>
				<content:encoded><![CDATA[<p>A few months ago, I wrote about <a href="http://erichogue.ca/2012/04/best-practices/continuous-testing/" title="Continuous Testing">continuous testing</a>. When I wrote that post, I was using <a href="https://github.com/mynyml/watchr" title="watchr">watchr</a> to run my tests. A few weeks ago, I started using <a href="https://github.com/guard/guard" title="Guard">Guard</a> instead of watchr and I wouldn&#8217;t go back. </p>
<h2>Reasons to Change</h2>
<p>One of the problems I had with watchr, is that it did not see new files. Every time I added a test file, I had to switch window and restart watchr. Then I would add the class to test and have to do the same thing. It&#8217;s not a big deal, but I&#8217;m lazy. And the main reason to use a tool like this is to have the test runs automatically. </p>
<p>Another concerns, is that watchr is not maintained anymore. The latest commit on GitHub was done over a year ago. And the last time there where any real activity is 2 years ago. There is a couple pull requests waiting, but some of them have been there for a year. </p>
<p>And lastly, Guard comes built in with some functionality that required code in watchr, and others are part of additional Guards.</p>
<h2>Guard</h2>
<p>Like watchr, Guard watch your file system and trigger some actions when files are modified. It&#8217;s also build in Ruby. There are many plugins that will simplify frequent tasks. One of those is <a href="https://github.com/Maher4Ever/guard-phpunit" title="guard=phpunit">guard-phpunit</a> that runs <a href="https://github.com/sebastianbergmann/phpunit/" title="PHPUnit">PHPUnit</a> when PHP files are changed. Guard runs only the needed tests when you change a file. It will run all the tests only when you ask for it, or after a failing test finally succeed. It also has a very good notification system.</p>
<p>To install Guard, you need to have ruby already installed. On Ubuntu, you can install it like this</p>
<pre>
sudo apt-get install ruby1.9
</pre>
<p>Once you have Ruby installed, you just need to run </p>
<pre>
gem install guard guard-phpunit
</pre>
<p>to install Guard with the PHPUnit plugin. If you don&#8217;t use RVM to manage your Ruby versions, you will need to run gem install with sudo. </p>
<p>That&#8217;s all there is to it. You are now ready to use Guard. </p>
<h2>The Guardfile</h2>
<p>The Guardfile is where you tell Guard which files to watch, and what it should do when a file is modified. You can generate one with all the plugins installed by running </p>
<pre>
guard init
</pre>
<p>If you want to have a file only for one plugin, or add the code for another plugin, just run</p>
<pre>
guard init phpunit
</pre>
<p>You can also generate the Guardfile manually, it&#8217;s a simple Ruby file. My Guard file for PHPUnit looks like this:</p>
<pre>
guard 'phpunit', :cli => '--colors', :tests_path => 'tests', 
        :keep_failed => true, :all_after_pass => true do
  watch(%r{^tests/.+Test\.php$})
  watch(%r{^src/(.+)\.php$}) { |m| "tests/#{m[1]}Test.php" }
end
</pre>
<p>The first line, tells guard to use the PHPUnit plugin. This plugin will execute PHPUnit and check if the tests pass or not. We give it a few options. First we tell guard to pass the colors argument to PHPUnit. keep_failed is to make sure that when a test case fails, guard will run it on every save until it passes. all_after_pass, tells Guard to run all the tests once a failing tests succeed. This way you are sure you didn&#8217;t break anything in the process of getting the test to pass. </p>
<p>This file has two calls to watch, the first one makes Guard watch the tests folder and run PHPUnit on any file modified in it. </p>
<p>The second one watches the src folder. When a file is modified in this folder, it makes Guard run the associated file in the tests folder. For this to work, I just have to keep the structure of my src and tests folders identical. </p>
<h2>Running Guard</h2>
<p>Running Guard is done by simply issuing the &#8216;guard&#8217; command in your project folder. It will look for the Guard file in the current directory. If there are none, it will look in your home directory for a file called &#8216;.Guardfile&#8217;. </p>
<p>In addition to the &#8216;.Guardfile&#8217;, you can use a file called &#8216;.guard.rb&#8217;, also in your home directory. .Guardfile will be use if you don&#8217;t have a Guardfile in the current directory. .guard.rb is appended to your Guardfile. This is useful for configurations that you don&#8217;t want to share between developers on the same project. Notifications preferences are a good example of what can go in .guard.rb. </p>
<p>One interesting parameter to Guard is -c or &#8211;clear. If you run it with this parameter, Guard will clear the terminal before running the tests. </p>
<div id="attachment_1014" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2012/09/Guard.jpeg"><img src="http://erichogue.ca/wp-content/uploads/2012/09/Guard-300x161.jpeg" alt="Running Guard" title="Running Guard" width="300" height="161" class="size-medium wp-image-1014" /></a><p class="wp-caption-text">Running Guard</p></div>
<h2>Notifications</h2>
<p>Guard has a comprehensive notification system. It uses Libnotify on Linux, Growl on Mac and Notifu on Windows. To use it on Linux, make sure you have libnotify-bin and the libnotify gem installed. </p>
<pre>
sudo apt-get install libnotify-bin
gem install libnotify
</pre>
<p>You can then add this line to your Guardfile</p>
<pre>
notification :libnotify
</pre>
<div id="attachment_1022" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2012/09/Notifications.jpeg"><img src="http://erichogue.ca/wp-content/uploads/2012/09/Notifications-300x91.jpeg" alt="Notifications" title="Notifications" width="300" height="91" class="size-medium wp-image-1022" /></a><p class="wp-caption-text">Notifications</p></div>
<p>If you want to turn them off, change the notification value to :off.</p>
<pre>
notification :off
</pre>
<h2>Other Plugins</h2>
<p>There are a lot of <a href="https://github.com/guard/guard/wiki/List-of-available-Guards" title="Guard plugins">plugins for Guard</a>. Here&#8217;s a small list of the ones I found interesting:</p>
<ul>
<li><a href="https://github.com/guard/guard-coffeescript" title="guard-coffeescript">guard-coffeescript</a> &#8211; Compile your CoffeeScript</li>
<li><a href="https://github.com/guard/guard-jasmine" title="guard-jasmine">guard-jasmine</a> &#8211; Run Jasmine tests</li>
<li><a href="https://github.com/guard/guard-less" title="guard-less">guard-less</a> &#8211; Compile less into css</li>
<li><a href="https://github.com/guard/guard-sass" title="guard-sass">guard-sass</a> &#8211; Complie sass into css</li>
<li><a href="https://github.com/guard/guard-puppet" title="guard-puppet">guard-puppet</a> &#8211; Run Puppet</li>
<li><a href="https://github.com/pmcjury/guard-remote-sync" title="guard-remote-sync">guard-remote-sync</a> &#8211; rsync your files when you change them</li>
<li><a href="https://github.com/guard/guard-shell" title="guard-shell">guard-shell</a> &#8211; Run any shell command</li>
<li><a href="https://github.com/guard/guard-livereload" title="guard-livereload">guard-livereload</a> &#8211; Reload your browser when you change your views</li>
</ul>
<h2>Inline Guard</h2>
<p>When there are no plugin to perform a task you need, you can use guard-shell. Another alternative is to create an inline guard. Just add a class that extends Guard to your Guardfile. In the class you need to at a minimum override the method run_on_change. Here&#8217;s what I did for running <a href="http://behat.org/" title="Behat">Behat</a>.</p>
<pre>
module ::Guard
    class Behat < Guard
        def start 
            run_all
        end

        def run_all
            puts 'Run all Behat tests'
            puts `behat`
        end

        def run_on_change(paths)
            paths.each do |file|
                puts `behat #{file}`
            end
        end
    end
end

guard 'behat' do
    watch %r{^tests/integrationTests/.+\.feature$}
end
</pre>
<p>This runs Behat every time a .feature file is changed.</p>
<p>I'm working on a plugin for Behat. There is not much yet, it just runs Behat on file changes. It won't parse the results or display notifications yet. I still have a lot of work, but it can be useful as it is now. It's on <a href="https://github.com/EricHogue/guard-behat" title="guard-behat">my GitHub</a>.</p>
<h2>Give It a Try</h2>
<p>Continuous testing has been a great addition to my toolbox. It took me some time to find the correct tools for me, but now, Guard is making my life easier. If you do <a href="http://erichogue.ca/2011/06/php/test-driven-development-in-php/" title="Test Driven Development">Test Driven Development (TDD)</a>, you should try it. </p>
<p>If you have some better tools, or improvements I can make to the way I use Guard, please let me know in a comment. </p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2012/09/php/continuous-testing-in-php-with-guard/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP Resources</title>
		<link>http://erichogue.ca/2012/08/php/php-resources/</link>
		<comments>http://erichogue.ca/2012/08/php/php-resources/#comments</comments>
		<pubDate>Sun, 19 Aug 2012 18:47:21 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[blogs]]></category>
		<category><![CDATA[podcasts]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=943</guid>
		<description><![CDATA[A co-worker recently asked me how I stay current with new development in the PHP world. I started writing a document for him, then I though it might be a good idea to put this in a blog post. So here are a few of the resources I use. Most of them are about PHP, [...]]]></description>
				<content:encoded><![CDATA[<p>A co-worker recently asked me how I stay current with new development in the PHP world. I started writing a document for him, then I though it might be a good idea to put this in a blog post. So here are a few of the resources I use. Most of them are about PHP, because that is what I was asked about, but I think it&#8217;s important to look other areas also. So I will throw in a few links that are not strictly about PHP.</p>
<p>I use Google Reader to subscribe to a lot of blogs, probably too many. I also use twitter to find out what&#8217;s happening. I follow people who are very active in the PHP, agile and software craftsmanship communities. When I see something interesting, I add it to <a href="http://www.instapaper.com/" title="Instapaper">Instapaper</a>, so I can read it when I have time. </p>
<h2>Aggregators</h2>
<p>Aggregators are great, by following a few, you can quickly get a good view of the community. </p>
<p>The main one for PHP is without any doubt PHPDeveloper. This aggregator is maintained by <a href="https://twitter.com/enygma" title="Chris Cornutt">Chris Cornutt</a>. You can check the <a href="http://www.phpdeveloper.org/" title="PHPDeveloper">website</a> or the <a href="https://twitter.com/phpdeveloper" title="PHPDeveloper on Twitter">Twitter feed</a>, but don&#8217;t miss this one. Everyday, you will get links to 4 or 5 of the best articles published in the PHP world. You will also get weekly reminder of the best posts of the week and of what was popular a year ago. </p>
<p>Here&#8217;s a few other aggregator:</p>
<ul>
<li><a href="http://devzone.zend.com" title="Zend Developer Zone">Zend Developer Zone</a></li>
<li><a href="http://css.dzone.com/" title="DZone Web Builder Zone">DZone Web Builder Zone</a></li>
<li><a href="https://twitter.com/phpquickfix" title="PHP Quick Fix">PHP Quick Fix</a></li>
<li><a href="https://twitter.com/websecquickfix" title="WebSec Quick Fix">WebSec Quick Fix</a></li>
<li><a href="https://twitter.com/phpizer" title="PHP Web Developer">PHP Web Developer</a></li>
</ul>
<h2>Blogs</h2>
<p>Those are some of the PHP blogs I follow:</p>
<ul>
<li><a href="http://blog.calevans.com/" title="Postcards From My Life">Cal Evans blog</a></li>
<li><a href="http://akrabat.com/" title="Rob Allen's DevNotes">Rob Allen&#8217;s DevNotes</a></li>
<li><a href="http://www.naramore.net/blog/" title="Elizabeth Naramore's blog">Elizabeth Naramore&#8217;s blog</a></li>
<li><a href="http://www.lornajane.net/blog" title="Lorna Jane's blog">Lorna Jane&#8217;s blog</a></li>
<li><a href="http://mwop.net/blog.html" title="phly, boy, phly: matthew weier o'phinney">Matthew Weier O&#8217;Phinney project lead of Zend Framework</a></li>
<li><a href="http://sebastian-bergmann.de/blog/" title="Sebastian Bergmann">Sebastian Bergmann the creator of PHPUnit</a></li>
<li><a href="http://fabien.potencier.org/" title="Fabien Potencier">Fabien Potencier founder Symfony</a></li>
<li><a href="http://blog.8thlight.com/" title="8th Light's blog">8th Light&#8217;s blog, not PHP but interesting</a></li>
</ul>
<h2>Twitter</h2>
<ul>
<li><a href="https://twitter.com/iliaa" title="Ilia Alshanetsky">Ilia Alshanetsky</a></li>
<li><a href="https://twitter.com/daycamp4devs" title="Day Camp 4 Developers">Day Camp 4 Developers</a></li>
<li><a href="https://twitter.com/derickr" title="Derick Rethans">Derick Rethans</a></li>
<li><a href="https://twitter.com/CalEvans" title="Cal Evans">Cal Evans</a></li>
<li><a href="https://twitter.com/rasmus" title="Rasmus Lerdorf">Rasmus Lerdorf</a></li>
<li><a href="https://twitter.com/pragprog" title="PragmaticProgrammers">PragmaticProgrammers</a></li>
<li><a href="https://twitter.com/AskAManager" title="Alison Green">Alison Green</a> great career advices</li>
<li><a href="https://twitter.com/enygma" title="Chris Cornutt">Chris Cornutt</a></li>
<li><a href="https://twitter.com/giorgiosironi" title="Giorgio Sironi">Giorgio Sironi</a></li>
<li><a href="https://twitter.com/grmpyprogrammer" title="Chris Hartjes">Chris Hartjes</a></li>
<li><a href="https://twitter.com/#!/search/?q=%23linktuesday" title="#linktuesday">#linktuesday</a></li>
<li><a href="https://twitter.com/unclebobmartin" title="Uncle Bob Martin">Uncle Bob Martin</a></li>
</ul>
<h2>Podcasts And Screencasts</h2>
<ul>
<li><a href="http://voicesoftheelephpant.com/" title="Voices of the ElePHPant">Voices of the ElePHPant</a> Interviews with the PHP community</li>
<li><a href="http://devhell.info/" title="/dev/hell">/dev/hell The Development Hell Podcast</a></li>
<li><a href="http://www.cleancoders.com/" title="Clean Coders">Clean Coders</a></li>
</ul>
<h2>User Groups</h2>
<p>I attempt a few user groups here in Montreal. I go to <a href="http://www.phpquebec.org/" title="PHP Québec">PHP Québec</a> every months and to <a href="http://js-montreal.org/" title="JS-Montreal">JS-Montreal</a>, <a href="http://www.montrealonrails.com/" title="Montreal.rb">Montreal.rb</a> and <a href="https://www.owasp.org/index.php/Montreal" title="OWASP Montreal">OWASP Montreal</a> every time I can. You probably have some user groups in you region. Check them out. They are great for learning and making contacts with other developers.</p>
<h2>Conferences</h2>
<p>Conferences are more expensive, but they are really worth it. Here in Montreal, we have <a href="http://confoo.ca/" title="ConFoo">ConFoo</a> that is amazing. There is also <a href="http://at2012.agiletour.org/en/montreal.html" title="Agile Tour">Agile Tour</a> that is coming in November. This year, I will also be at the first edition of <a href="http://truenorthphp.ca/" title="True North PHP">True North PHP</a> in Toronto. </p>
<p>Conferences are a good way to be introduce to many technologies. You get back home with lots of new ideas. And a lot of things to research. </p>
<h2>PHP Mentoring</h2>
<p><a href="http://phpmentoring.org/" title="PHP Mentoring">PHP Mentoring</a> is a new project that connects mentors with apprentices. I joined the program as soon as I heard about it and I&#8217;m very happy I did. I found two apprentices and a mentor in a few days. This is a great way to improve and learn a lot. </p>
<p>If you haven&#8217;t looked at it, I suggest you go read the <a href="http://phpmentoring.org/guidelines.html" title="PHP Mentoring Guidelines and Rules">guidelines and rules</a> and put your name on the <a href="https://github.com/phpmentoring/phpmentoring.github.com/wiki/Mentors-and-Apprentices" title="PHP Mentoring Mentors and Apprentices">signup page</a> on GitHub. It does not matter if you do it as a mentor, an apprentice or both. I&#8217;m sure it will help you a lot. </p>
<p>When your name is there, look for someone who matches what you are looking for in the names already there and contact him. I see a lot of names there, and not enough matches. </p>
<p>Those are a few of the resources I check to stay current. There are a lot more in my feed reader and in the list of people I follow on Twitter. But I think those are the most informative when it comes to PHP development. </p>
<p>If you think I missed some good ones, please leave a comment. I&#8217;m always happy to find more sources of great content.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2012/08/php/php-resources/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Continuous Testing in PHP</title>
		<link>http://erichogue.ca/2012/04/best-practices/continuous-testing/</link>
		<comments>http://erichogue.ca/2012/04/best-practices/continuous-testing/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 12:30:01 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=869</guid>
		<description><![CDATA[Continuous testing is a way to automate the execution of your tests while you work. This makes the feedback loop very short. As soon as you save a file, the tests are run and you know right away if anything fails. Autotest I discovered continuous testing over a year ago when I watched a video [...]]]></description>
				<content:encoded><![CDATA[<p>Continuous testing is a way to automate the execution of your tests while you work. This makes the feedback loop very short. As soon as you save a file, the tests are run and you know right away if anything fails. </p>
<h2>Autotest</h2>
<p>I discovered continuous testing over a year ago when I watched a video where <a href="http://coreyhaines.com/" title="Corey Haines">Corey Haines</a> performed a kata in front of a crowd. He was doing it in Ruby and using <a href="http://www.zenspider.com/ZSS/Products/ZenTest/" title="Autotest">Autotest</a> to run his test suite. I tough it was awesome that he immediately saw the result of his changes without having to do anything. </p>
<p>Back then I had used <a href="http://erichogue.ca/2011/05/php/php-tool-integration-phpsrc/" title="PHP Tool Integration (PHPsrc)">PHPsrc</a>. It&#8217;s a great tool, but I didn&#8217;t like that I had to take my hand off the keyboard and click on a button to run my tests. So I was mainly running them from the command line. </p>
<h2>AutoPHPUnit</h2>
<p>When I saw Autotest, I was immediately sold and I looked for something similar in PHP. Sadly I didn&#8217;t found anything like it. So after a while I wrote something for myself. I called it <a href="https://github.com/EricHogue/AutoPHPUnit" title="AutoPHPUnit">AutoPHPUnit</a>. It uses libnotify to watch the file system. Every time a file is changed, it runs PHPUnit. You can also specify a configuration file for PHPUnit. </p>
<p>It worked well for me, I really loved using it. But it was not very flexible. </p>
<h2>Watchr</h2>
<p>Then I read the book <a href="http://www.amazon.com/gp/product/1934356700/ref=as_li_ss_tl?ie=UTF8&#038;tag=erhosbl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1934356700">Continuous Testing: with Ruby, Rails, and JavaScript</a> (affiliate link) that I won at the <a href="http://erichogue.ca/2011/05/training/code-retreat-quebec/" title="Code Retreat Quebec">Code Retreat in Quebec city</a>. </p>
<p>In the book, the authors use <a href="https://github.com/mynyml/watchr" title="watchr">watchr</a> to monitor the file system and perform any actions when a file changes. Watchr can be used to monitor any kind of file, so it&#8217;s more flexible than my solution. And you tell it what to do with a Ruby block so it can react to changes any way you want.</p>
<p>To install it, you need to have Ruby already installed on your machine. Then you use RubyGems to install it. Simply run &#8220;gem install watchr&#8221; and you will have watchr on your machine. If you are not using RVM to manage your versions of Ruby, you might need to use sudo to install watchr. </p>
<p>You can then create a simple Ruby script. Calling the function watch() with a regexp for the files you want to watch and the block you want to execute when a file changes. Then, you run watchr passing it the script file as a parameter. </p>
<p>Here&#8217;s a simple script to run PHPUnit: </p>
<p><code><br />
watch ('.*\.php$') {|phpFile| system("phpunit -c phpunit.xml")}<br />
</code></p>
<p>I watch every file that ends with &#8216;.php&#8217;. When watchr detects a change, it simply runs PHPUnit for me. </p>
<div id="attachment_876" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2012/04/watchr_result.jpeg"><img src="http://erichogue.ca/wp-content/uploads/2012/04/watchr_result-300x75.jpg" alt="watchr running" title="watchr running" width="300" height="75" class="size-medium wp-image-876" /></a><p class="wp-caption-text">watchr running</p></div>
<p></p>
<h2>Notifications</h2>
<p>Using watchr like this is great if you have enough screen space to show a terminal beside your code. But if you can&#8217;t have a terminal on the screen at the same time as your editor, you can use notify-send on Ubuntu to get a pop up in the notification section. </p>
<pre>
<code>
watch ('.*\.php$') {|phpFile| run_php_unit(phpFile)} 

def run_php_unit(modified_file)
    system('clear')
    if (system("phpunit -c phpunit.xml")) 
        system("notify-send 'All test passed'")
    else
        system("notify-send 'Test failed'")
    end
end
</code>
</pre>
<p>You can add any logic you want. In the Continuous Testing book they go further. You can have it display notifications only when the tests fails and when they pass again for the first time. They also count the numbers of successful runs and display notifications every 5 consecutive successful run. </p>
<div id="attachment_884" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2012/04/notification.jpeg"><img src="http://erichogue.ca/wp-content/uploads/2012/04/notification-300x110.jpg" alt="watchr notification" title="watchr notification" width="300" height="110" class="size-medium wp-image-884" /></a><p class="wp-caption-text">watchr notification</p></div>
<p></p>
<h2>Beyond PHP</h2>
<p>The main advantage of watchr is that it is not limited to only testing Ruby or PHP. Since you are using a block of Ruby code, you can do almost anything. At work, we are starting a little Node.js project, and I plan on using it to run the Vows tests. </p>
<p>Here&#8217;s a few other things you can do with it</p>
<ul>
<li>Compile CoffeeScript</li>
<li>Run PHP Code Sniffer</li>
<li>Run lint tests</li>
<li>Minify your JavaScript and CSS</li>
</ul>
<p>Basically, anything you can automate can be ran every time a file of a certain type is changed. I&#8217;ve seen only one drawback to watchr, it does not see new files. So when you add files, you need to restart it if you want them to be monitored.</p>
<h3>Update 2012-09-04</h3>
<p>I posted a follow up about <a href="http://erichogue.ca/2012/09/php/continuous-testing-in-php-with-guard/" title="Continuous Testing in PHP with Guard">Continuous Testing</a>. I now use Guard to run my tests automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2012/04/best-practices/continuous-testing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Clean Coder</title>
		<link>http://erichogue.ca/2011/07/books/clean-coder/</link>
		<comments>http://erichogue.ca/2011/07/books/clean-coder/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 01:46:37 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=783</guid>
		<description><![CDATA[Yesterday I finished reading The Clean Coder: A Code of Conduct for Professional Programmers (affiliate link) by Robert C. Martin. In this book, Uncle Bob expose his view of what it is to be a professional programmer. The author have been programming for 40 years and he have very strong opinion about the subject of [...]]]></description>
				<content:encoded><![CDATA[<p>Yesterday I finished reading <a href="http://www.amazon.com/gp/product/0137081073/ref=as_li_ss_tl?ie=UTF8&#038;tag=erhosbl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0137081073">The Clean Coder: A Code of Conduct for Professional Programmers</a> (affiliate link) by <a href="http://www.objectmentor.com/omTeam/martin_r.html" title="Robert C. Martin">Robert C. Martin</a>. In this book, Uncle Bob expose his view of what it is to be a professional programmer. The author have been programming for 40 years and he have very strong opinion about the subject of professionalism in programming. </p>
<h2>The Book</h2>
<p>In the first chapter he defines professionalism in our career. A lot of it turns around taking responsibility for our actions. He also compare our profession to being a doctor by saying that we should <a href="http://en.wikipedia.org/wiki/First,_do_no_harm" title="Primum non nocere">Do no harm</a>. We should strive not to harm the function and the structure of our programs. He then describes the means by which we should ensure that we don&#8217;t hurt our code. Those means are the subject of the rest of the book. </p>
<p>The following two chapters are about how we answer requests. The importance of saying no, and of delivering when we say yes. We should never accept to commit to something we can&#8217;t deliver. But when we commit to something, we need to make sure we deliver it. Uncle Bob publish a great story taken from the <a href="http://raptureinvenice.com/?p=63" title="Is Good Code Impossible?">blog of John Blanco</a> that is really worth reading. It&#8217;s a great example of how bad it can get when we fail to say no. John also has a great recipe for manipulating developers. Tell them the the app is simple, add features by faulting them for not seeing their necessity and push the deadline to get them to do more. Reading it made me realize that I have seen it in action many times. And I had fell for this trick myself. </p>
<p>He then follow about the actual act of coding. How we should prepare to code. The importance of being rested and free of worries. He takes a stand against &#8220;the zone&#8221; talks about how we should be more forgiving of interruptions. He also speaks about the importance of helping others and being helped by others. </p>
<p>The next chapter is about <a title="test driven development" href="http://en.wikipedia.org/wiki/Test-driven_development">Test driven development</a>. This is something I try to practice more and more. I already wrote <a href="http://erichogue.ca/2011/06/php/test-driven-development-in-php/" title="Test Driven Development in PHP">a post about it</a>. </p>
<p>He then move on practicing. He describes <a href="http://codekata.pragprog.com/" title="Code Kata">Code Kata</a> and <a href="http://codingdojo.org/" title="Coding Dojo">coding dojo</a>. He also talks about how to get more experience by contributing to open source projects. </p>
<p>The next two chapters are about testing. He speaks about acceptance tests. How they should define the requirements and make the definition of done pretty clear. These tests should always be automated and should run pretty quickly. He then describes the testing strategies. The most important thing to remember, is that QA should not find anything. He list the different kinds of tests and their goals. </p>
<p>In chapter 9 and 10, the author speaks about time. How to manage it and how to estimate projects. He mentions meetings, how they can be important, but also useless. We should try to be in the important one, and use <a href="http://en.wikipedia.org/wiki/Open_Space_Technology#Law_of_Two_Feet" title="Law of Two Feet">the Law of Two Feet</a> when we end up in the others. He also mention <a href="http://www.pomodorotechnique.com/" title="The Pomodoro Technique">The Pomodoro Technique</a> and avoiding getting caught in blind alleys and messes. Then he mention a few techniques for estimating tasks. </p>
<p>Chapter 11 is about pressure. How to avoid it, and how to mitigate it. Developers are often under a lot of pressure. Some of it we cause to ourselves by not saying no when we should. Some of it is caused by the importance of our work for our employers. Learning how to handle this pressure is important. </p>
<p>The following two chapters are about working with others. In chapter 12, the author speaks about collaboration. How important it is for us to collaborate with other developers, but also with other peoples. Then chapter 13 is about teams and projects. He describes the importance of having a good team that works well together. </p>
<p>Chapter 12 is about mentoring, apprenticeship and craftsmanship. He describes how valuable mentoring can be. A mentor can help bring a junior developer up to speed, but it also can help the mentor get a better understanding of what he has to teach. The author then describes an apprenticeship model where developers would start as an apprentice before moving to being a journeyman and finally a master. </p>
<p>The books ends with an appendix on the tools the author uses. He mentions source control, testing tools, editors, issue tracking and continuous integration servers. </p>
<h2>My Take On All Of This</h2>
<p>The Clean Coder is a very good book. Uncle Bob have a very strong opinion on how software should be developed. I don&#8217;t agree with everything he says, but in general he makes good points. </p>
<p>One of the interesting things about the book is that it is full of stories taken from the career of the author. He show us how to become better developers by showing us some of his failures. Seeing how and why he failed can help us avoid some of these mistakes. </p>
<p>I wish I had this book earlier in my career, but I&#8217;m not sure I will have understand it&#8217;s importance. It his a great handbook on improving as developers. And it&#8217;s a great reminder of how far I still need to go if I want to consider myself a professional developer. </p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2011/07/books/clean-coder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Test Driven Development in PHP</title>
		<link>http://erichogue.ca/2011/06/php/test-driven-development-in-php/</link>
		<comments>http://erichogue.ca/2011/06/php/test-driven-development-in-php/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 12:16:20 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=727</guid>
		<description><![CDATA[Test driven development (TDD) is at the core of the Agile Methodology and Extreme Programming. This practice has been known for a while and a lot have been written on it. However, I still meet developers that don&#8217;t know what it is. I understand that many employers won&#8217;t let their employees write tests, but we [...]]]></description>
				<content:encoded><![CDATA[<p><a title="test driven development" href="http://en.wikipedia.org/wiki/Test-driven_development">Test driven development</a> (TDD) is at the core of the <a title="Agile software development" href="http://en.wikipedia.org/wiki/Agile_software_development">Agile Methodology</a> and <a title="Extreme Programming" href="http://en.wikipedia.org/wiki/Extreme_Programming">Extreme Programming</a>. This practice has been known for a while and a lot have been written on it. However, I still meet developers that don&#8217;t know what it is. I understand that many employers won&#8217;t let their employees write tests, but we should at least know about the best practices of our industry. </p>
<p>In this post I will describe TDD as I understand it. I will also talk about the tools that are available in PHP.</p>
<h2>What Is TDD</h2>
<p>In TDD, developers write a failing test before writing any production code. The expected behavior of the code to write is defined this way. It is then easy to know when we have reached this goal. This produce a very small feedback loop. It also push the developer to write code that is very loosely coupled to the rest of the system. This code is easier to change, and it can be reused outside without having to bring half of the current system.</p>
<p>One of the greatest advantage TDD comes with maintenance. You can modify your existing code without the fear of breaking anything else. Because the code is loosely coupled, the risk of side effects is practically non existent. If the behavior of the application change in any way, your tests should warn you. You should also always write a failing test before fixing a bug. This way if the bug gets reintroduce, you will know right away.</p>
<h2>How To Practice TDD</h2>
<p>Practicing TDD can be resumed with the following mantra: red/green/refactor. Red is a failing test. You write a test for the simpler thing you can achieve. Then you write the code to make that test pass and go to green. You should write as little code as possible to get the test passing. Commit any crime you need, the only important thing is to get back to green. Then in the refactor phase, you fix the code you wrote. Remove any duplication, make sure the code is readable, use descriptive names&#8230; Just make sure your tests are still passing while you refactor. And then, you go back and write another failing test.</p>
<p><a title="Robert C. Martin" href="http://www.objectmentor.com/omTeam/martin_r.html">Robert C. Martin</a> (Uncle Bob) wrote <a title="The Three Rules Of TDD" href="http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd">The Three Rules Of TDD</a>. It is a great read, but here are the rules:</p>
<blockquote><p>1. You are not allowed to write any production code unless it is to make a failing unit test pass.<br />
2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.<br />
3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.</p></blockquote>
<p>You can see the very short feedback loop in those rules. There is also a strong emphasis on writing as little code as possible. Just write what you need and nothing more. This goes with the <a title="You ain't gonna need it" href="http://en.wikipedia.org/wiki/You_ain't_gonna_need_it">YAGNI (You ain&#8217;t gonna need it)</a> approach.</p>
<h2>TDD in PHP</h2>
<p>PHP has two main tools for unit testing. <a title="Simple Test" href="http://www.simpletest.org/">Simple Test</a> and <a title="PHPUnit" href="http://www.phpunit.de/manual/current/en/index.html">PHPUnit</a>. I always used PHPUnit for unit testing in PHP. I love the tool, it <a title="PHP Tool Integration (PHPsrc)" href="http://erichogue.ca/2011/05/09/php-tool-integration-phpsrc/">integrate with Eclipse</a> and it can generate code coverage reports. Apparently Simple Test is pretty good also, but I have never tried it. You can pick any unit testing framework you like, just start writing test.</p>
<p>To write unit tests with PHPUnit, you simply create a file with a name ending by &#8216;Test.php&#8217;. If you want to test a class named <a title="Kata Two -- Karate Chop" href="http://codekata.pragprog.com/2007/01/kata_two_karate.html">KarateChop</a>, you create the file &#8216;KarateChopTest.php&#8217;. In this file you create a class KarateChopTest that extends &#8216;PHPUnit_Framework_TestCase&#8217;.</p>
<p>You then create your testing methods inside this class. The testing methods must be public and their names must start with test. I just found out that you can use the @test annotation in the docblock instead of prefixing your method name with test. All your test will be run in isolation. An instance of the test class will be created for each test method. Just be careful with global data. Global variable and static properties can make you code very hard to test.</p>
<p>If your methods needs some code to prepare the test, you can create a setup() method. It will be call before every test. You can also add a teardown() method to do any clean up after the test.</p>
<p>Your test methods should be small. They should test for only one behavior, and every test should end with an assertion that verify that the system under test (SUT) behaved has expected. Don&#8217;t forget to write the test before the actual code. This way you can see the test fail first, and when it will pass, you will know that the system act as intended.</p>
<p>To verify the results of your test, PHPUnit has a <a title="PHPUnit Assertions" href="http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions">wide range of assertions</a>. They go from the simple assertTrue to more elaborates ones like assertEqualXMLStructure and assertGreaterThanOrEqual. I counted 36 assertions types in the documentation. It even have an assertThat method to write tests in the <a title="Behavior Driven Development" href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behavior Driven Development</a> style.</p>
<p>All your tests could simply use assertTrue to verify a condition, but the intent of your code is more obvious with the verbose assertions. Those 2 lines are the same:<br />
<code><br />
$this-&gt;assertStringStartsWith($prefix, $string);<br />
$this-&gt;assertTrue(0 === strpos($string, $prefix));<br />
</code><br />
But the first one says what it is I&#8217;m testing.</p>
<p>To run your tests, all you need to do is run phpunit, passing it the file with the tests as a parameter. If you pass it a folder, PHPUnit will run the test in every files with a name ending by &#8216;Test.php&#8217; in that folder and any sub folders.</p>
<h3>Bootstrapping</h3>
<p>Sometimes, you will need to have some code run before all the test cases. You might need to alter the require path, add an autoloader or set some environment variables. PHPUnit allow us to pass it a bootstrap file. This file will be run before your tests to prepare your testing environment.<br />
<code><br />
phpunit --bootstrap Tests/testBootstrap.php .<br />
</code></p>
<h2>Testing Databases</h2>
<p>One of the common issue when doing TDD, or writing unit tests in general is how should we test code that interact with the database. The short answer to that, is don&#8217;t. Your database should be tested during your integration tests, not in unit tests. You should try to keep your data access layer isolated from the rest of the code. That will help you testing, but also make changing the way you store your data easier.</p>
<p>However, we work in the real world and we sometimes have to test code that access a database. To make it easier, you should not create the connection, but require it in the object constructor or as a parameter to the method that use it. If you can&#8217;t do this, consider creating a setter that allow your testing code to inject a different connection.</p>
<p>Make sure you use PDO connections and try to stick to standard SQL. This way in your tests, you can create a <a title="SQLite" href="http://www.sqlite.org/">SQLite</a> database and pass it to your tests. Make sure that you create your database in memory, not in a file. This way each test will have his own database and they will stay isolated. Creating a database like this can become very cumbersome if you use a lot of tables, or need to populate them with a lot of fake data.</p>
<p>If you need to connect to a real database to run your tests, you can pass your code a different configuration so at least it does not connect to your production server. You can also add an entry to your hosts file so when you try to connect to production, you end up in the test database. This can be dangerous though, someone might end up running your tests without the entry in the hosts file and alter your production database.</p>
<p>Connecting to a real database cause problems with the isolation of the tests. Changes made by a test can alter the result of another test. Testing against a database can also be very slow. So you should do this only if you have no other choices.</p>
<h2><a title="PHPsrc" href="http://www.phpsrc.org/">PHPsrc</a></h2>
<p>I already wrote about PHPsrc in <a title="PHP Tool Integration (PHPsrc)" href="http://erichogue.ca/2011/05/php/php-tool-integration-phpsrc/">a previous post</a>. This tool will allow you to run your tests from inside Eclipse.</p>
<div id="attachment_751" class="wp-caption alignnone" style="width: 280px"><a href="http://erichogue.ca/wp-content/uploads/2011/06/PHPsrcPHPUnitConfig.png"><img class="size-medium wp-image-751" title="PHPsrc PHPUnit Configuration" src="http://erichogue.ca/wp-content/uploads/2011/06/PHPsrcPHPUnitConfig-270x300.png" alt="PHPsrc PHPUnit Configuration" width="270" height="300" /></a><p class="wp-caption-text">PHPsrc PHPUnit Configuration</p></div>
<p>The PHPUnit integration will allow you to easily run the tests. It can also jump between the tests and the class being tested. You can give it a bootstrap file in the configuration. Either in the global settings, or by project. It will underline failing tests as errors, and it can also show code that is not covered by your tests.</p>
<h2>Where To Start</h2>
<p>Beginning TDD is not an easy task. At first it will slow you down. You might appear to lose some productivity, but you should catch up pretty fast. Especially when you will maintain your code. You should then be able to make changes while being confident that you didn&#8217;t break anything.</p>
<p>If you want to be good at anything, you need to practice. There are many resources available to practice TDD. You can perform some <a title="Code Kata" href="http://codekata.pragprog.com/">Code Kata</a>, attempt a <a title="Code Retreat" href="http://coderetreat.com/">Code Retreat</a>, find a <a title="Coding Dojo" href="http://www.codingdojo.org/">Coding Dojo</a> or try <a title="CyberDojo" href="http://www.cyber-dojo.com/">CyberDojo</a>. All these are excellent, just go and practice.</p>
<p>If you need any more information about TDD, I would recommend reading <a href="http://www.amazon.com/gp/product/0321146530/ref=as_li_ss_tl?ie=UTF8&#038;tag=erhosbl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321146530">Test Driven Development: By Example</a> (affiliate link) by <a href="http://www.threeriversinstitute.org/blog/" title="Kent Beck">Kent Beck</a>. It starts with an in-depth explanation of TDD and finishes by implementing an xUnit framework in TDD. </p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2011/06/php/test-driven-development-in-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Code Retreat Quebec</title>
		<link>http://erichogue.ca/2011/05/training/code-retreat-quebec/</link>
		<comments>http://erichogue.ca/2011/05/training/code-retreat-quebec/#comments</comments>
		<pubDate>Thu, 26 May 2011 02:19:39 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[events]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=691</guid>
		<description><![CDATA[Last Saturday I had the privilege of attempting a code retreat in Quebec City. I have been hoping for this for a while, so I registered as soon as I found out about it. When I learned that Corey Haines and J. B. Rainsberger where going to be there I was really thrilled. The Setup [...]]]></description>
				<content:encoded><![CDATA[<p>Last Saturday I had the privilege of attempting <a href="http://coderetreatquebec.wordpress.com/" title="Code Retreat Quebec">a code retreat in Quebec City</a>. I have been hoping for this for a while, so I registered as soon as I found out about it. When I learned that <a href="http://coreyhaines.com/" title="Corey Haines">Corey Haines</a> and <a href="http://www.jbrains.ca/" title="J. B. Rainsberger">J. B. Rainsberger</a> where going to be there I was really thrilled. </p>
<h2>The Setup</h2>
<p>The event took place at the <a href="http://www.germaindominion.com/en/home" title="Hotel le Germain-Dominion">Germain-Dominion Hotel</a>. It is located in the old port district of Quebec City, close to the <a href="http://www.pc.gc.ca/lhn-nhs/qc/fortifications/index.aspx" title="Fortifications of Québec National Historic Site of Canada">fortifications</a>. </p>
<p>The retreat started at 8 AM with a breakfast and finished at 5 PM after 6 sessions of coding. With had 2 coffee breaks and a nice diner. The room was nice, with plenty of plugs for the laptop and a good WiFi connection. The organizers did a great job setting this up. Everything was ready for a full day of learning. </p>
<h2>How The Day Went</h2>
<p>After the breakfast, the actual retreat started by an introduction from Corey. He describes the goals of the retreat, how it got started and the problem we where going to work on. If you are interested in his introduction, you can look at the <a href="http://programmingtour.blogspot.com/2011/01/on-goals-of-coderetreat.html" title="On the goals of Coderetreat">introduction he gave in Cleveland in January</a>.<br />
<iframe src="http://player.vimeo.com/video/18955165?title=0&amp;byline=0&amp;portrait=0" width="400" height="225" frameborder="0"></iframe>
<p><a href="http://vimeo.com/18955165">Cleveland Code Retreat Introduction</a> from <a href="http://vimeo.com/coreyhaines">Corey Haines</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>After that, Corey explained that we where going to focus on the <a href="http://www.jbrains.ca/permalink/the-four-elements-of-simple-design" title="The Four Elements of Simple Design">The Four Elements of Simple Design</a>. They are in order of importance: </p>
<ul>
<li>Passes its tests</li>
<li>Minimizes duplication</li>
<li>Maximizes clarity</li>
<li>Has fewer elements</li>
</ul>
<p>Then we started the first session. Each sessions lasted 45 minutes. During this time we had to do <a href="http://en.wikipedia.org/wiki/Pair_programming" title="Pair programming">Pair programming</a> to work on the problem. Every time we tried to produce the best code we could. And at the end of the session we had to delete the code. Then Corey did a brief recap of the session and gave some tips to help us move forward. Then we had to find another partner for the next session. </p>
<p>At the end of the day, we took pictures, then we formed a circle and everyone had to answer 3 questions. What we learned, what surprised us the most and what we will change at work. </p>
<h2>My Impressions</h2>
<p>The first thing I notice is that there where people programming in many different languages. We where a few PHP developers, but not all of us had a testing environment ready. I was happy to show some people how to do TDD in PHP with <a href="https://github.com/sebastianbergmann/phpunit/" title="PHPUnit">PHPUnit</a>. </p>
<p>During the day, I paired with six different developers. We covered Java, C#, PHP and Python. I got to work with good developers. The pairing is probably the greatest thing about the retreat. During each session we got to exchange ideas with someone else. Every one brought a different way to attack the problem. This is a great chance to try to code differently. Especially in a setup where we can take the time to do it correctly, with no pressure to deliver. Pairing is also a great way to transfer knowledge. While working in pair, you can learn things such as language features, better design, IDE features and keyboard shortcuts. All this just by looking at the other developer. </p>
<p>Practicing <a href="http://en.wikipedia.org/wiki/Test-driven_development" title="Test-driven development">Test-driven development</a> during an entire day reminded me of how much I love it. Even in an event like this one, not every one I paired with was doing TDD. Some started by writing some code before adding a test to verify it. But when I was doing TDD, it really felt like the correct thing to do. I know I still need a lot of practice, but when I did TDD, programming was just more fun. </p>
<h2>What I Got Out Of It</h2>
<p>I think the greatest lesson I got from the code retreat is a new perspective on programming. Now I will try to look at my projects for another angle. Corey and J. B. made me realize that even if we are all against the waterfall approach, we still tend to over-think our design before we start coding. On the first 3 sessions, I started with a Cell class. It seemed like it was a good starting point because it will be needed for the game. But it was a dead end. </p>
<p>Corey told us that when we are ready to start coding something, we should ask why? Why are we coding this? Then take the answer and ask why again. After a few time we will reach a point where we cannot ask it anymore, this is where we should start. </p>
<p>With the Cell class example, I was starting there because I needed to know if a cell was alive or not. I needed to know this because I needed to check if a cell needed to be killed or not. I needed this because&#8230; In the end, what I needed is a board to play the game of life. Starting with the board seemed counter intuitive to me, but when I tried it everything fell into place.</p>
<p>I also got to see some Python. This is a language I barely know. Pairing with a Python developer was really fun. I think I could really like this language. I especially liked the list comprehension. This is something I really wish we had in PHP. </p>
<h2>What Now?</h2>
<p>The code retreat was fun, I got to experiment with practices that are not accepted where I work. It is now up to me to try and change that. I already used TDD on some project. But I really need to get back to doing it as much as I can. If it does not slow me down, and I end up producing better code, nobody can complain about it. It would also be great if I could get the other members of the team to try it. I gave a little presentation about it at work over a year ago. Sadly as far as I know, no one else tried it. </p>
<p>Pair programming is harder to sell. I still think it&#8217;s an amazing way to transfer knowledge. It can help bring up to speed a new developer. Pairing a junior developer with someone with more experience can help both of them. The junior will get an access to the knowledge and techniques of an experienced developers. And the experienced one should consolidate is knowledge by explaining it. And he may also learn something from the other developer. </p>
<p>I also have to thanks the organizers, the sponsors and the facilitator. They made this amazing event possible. For only 30$, I got to spend a day coding with better developers than me. I hope it made me a better programmer, and I am looking forward to the next event. </p>
<h2>Update</h2>
<p><a href="http://karlmetivier.wordpress.com/" title="Karl Metivier">Karl Metivier</a> has posted <a href="http://karlmetivier.wordpress.com/2011/08/31/introduction-un-code-retreat-par-corey-haines/" title="Introduction à un Code Retreat par Corey Haines">the videos of the introduction</a> that <a href="http://coreyhaines.com/" title="Corey Haines">Corey Haines</a> did at the Quebec code retreat. </p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2011/05/training/code-retreat-quebec/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Tool Integration (PHPsrc)</title>
		<link>http://erichogue.ca/2011/05/php/php-tool-integration-phpsrc/</link>
		<comments>http://erichogue.ca/2011/05/php/php-tool-integration-phpsrc/#comments</comments>
		<pubDate>Mon, 09 May 2011 16:22:33 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=557</guid>
		<description><![CDATA[In a previous post, I talked about Continuous Integration. If your Continuous Integration server runs on every commits, it will help you keep your code quality high. It will also make integration a non issue. However, when I make a mistake, produce sub-optimal code or if I write code that does not respect our coding [...]]]></description>
				<content:encoded><![CDATA[<p>In a <a href="http://erichogue.ca/2011/05/php/continuous-integration-in-php/" title="Continuous Integration In PHP">previous post</a>, I talked about <a href="http://en.wikipedia.org/wiki/Continuous_integration" title="Continuous Integration">Continuous Integration</a>. If your Continuous Integration server runs on every commits, it will help you keep your code quality high. It will also make integration a non issue. </p>
<p>However, when I make a mistake, produce sub-optimal code or if I write code that does not respect our coding standards I want to know before I commit. I would rather fix my errors before they break the build. I can run all the tools that the server runs. But running them on my machine and analyzing the results every time I save will be painful. Fortunately, there is an easy solution.</p>
<h3>PHPsrc</h3>
<p><a href="http://www.phpsrc.org/" title="PHPsrc">PHPsrc</a> is a plugin that allow you to run PHP_CodeSniffer, PHPUnit, PHP Depend and PHP Copy/Paste Detector directly in Eclipse. The site also says that more tools should come. As you work, you will see any transgression you make. That will save you from breaking the build, but it also makes it easier to fix the problem. After all, you just wrote the faulty lines of code. </p>
<p>You install it like any other Eclipse plugin. Go to the Help menu and click on &#8220;Install new software&#8230;&#8221; In the Install dialog, click on &#8220;Add&#8230;&#8221;, choose a name for the repository and enter &#8220;http://www.phpsrc.org/eclipse/pti/&#8221; in the location. After you click on OK, Eclipse will detect &#8220;PHP Tool integration. Select it and click on Next twice. Accept the condition and click on Finish. After the installation is completed, you will have to restart Eclipse. </p>
<p>After the installation, in the preferences you will have a new &#8220;PHP Tools&#8221; menu. Make sure that every tools have an PHP executable and a PEAR library selected. For PHP CodeSniffer, you will also need to pick a standard. <div id="attachment_543" class="wp-caption alignnone" style="width: 308px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcConfig.png"><img src="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcConfig-298x300.png" alt="PHPsrc Configuration" title="PHPsrc Configuration" width="298" height="300" class="size-medium wp-image-543" /></a><p class="wp-caption-text">PHPsrc Configuration</p></div></p>
<p>Your Eclipse should now have new buttons in the toolbar and a new &#8220;PHP Tools&#8221; sub menu in the PHP Explorer.<br />
<div id="attachment_545" class="wp-caption alignnone" style="width: 258px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcToolbar.png"><img src="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcToolbar.png" alt="PHPsrc Toolbar" title="PHPsrc Toolbar" width="248" height="27" class="size-full wp-image-545" /></a><p class="wp-caption-text">PHPsrc Toolbar</p></div></p>
<div id="attachment_546" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcMenu.png"><img src="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcMenu-300x289.png" alt="PHPsrc Menu" title="PHPsrc Menu" width="300" height="289" class="size-medium wp-image-546" /></a><p class="wp-caption-text">PHPsrc Menu</p></div>
<p>PHPsrc will now check your code every time you save. Errors and warnings will be added to the Problems tab and the Type will be the tool that found the problem. The lines that causes problems will also be underlined like a syntax error. The copy paste detector will send the results to the console. </p>
<p>Here&#8217;s what Eclipse looks like with PHPsrc:<br />
<div id="attachment_548" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcResults.png"><img src="http://erichogue.ca/wp-content/uploads/2011/04/PHPsrcResults-300x182.png" alt="PHPsrc Results" title="PHPsrc Results" width="300" height="182" class="size-medium wp-image-548" /></a><p class="wp-caption-text">PHPsrc Results</p></div></p>
<p>If you develop PHP code in eclipse, you should definitely give PHPsrc a try. If you use a continuous integration server, being warned about potential issues before you commit to your source control repository can save you from becoming the one who broke the build. </p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2011/05/php/php-tool-integration-phpsrc/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Continuous Integration In PHP</title>
		<link>http://erichogue.ca/2011/05/php/continuous-integration-in-php/</link>
		<comments>http://erichogue.ca/2011/05/php/continuous-integration-in-php/#comments</comments>
		<pubDate>Wed, 04 May 2011 01:43:25 +0000</pubDate>
		<dc:creator>Eric Hogue</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Jenkins]]></category>
		<category><![CDATA[PHPUnit]]></category>

		<guid isPermaLink="false">http://erichogue.ca/?p=334</guid>
		<description><![CDATA[According to Wikipedia, continuous integration implements continuous processes of applying quality control — small pieces of effort, applied frequently. In simple terms, you verify that your project meets the quality standards frequently. This way, you can catch any deviation early. Doing the integration in small increments makes it easier. Implementing a continuous integration server can [...]]]></description>
				<content:encoded><![CDATA[<p>According to <a title="Wikipedia" href="http://en.wikipedia.org/wiki/Continuous_integration">Wikipedia</a>, continuous integration</p>
<blockquote><p>implements continuous processes of applying quality control — small pieces of effort, applied frequently.</p></blockquote>
<p>In simple terms, you verify that your project meets the quality standards frequently. This way, you can catch any deviation early. Doing the integration in small increments makes it easier. Implementing a continuous integration server can look difficult when you&#8217;ve never done it. In this post I will try to document how to set it up. I take for granted that you already have a LAMP server configured.</p>
<h2>The Tools</h2>
<p>PHP has many tools that can be used to analyse your code. There are tools for running unit tests, detecting copy/paste, checking the coding standards and more. We have to thank <a title="Sebastian Bergmann" href="http://sebastian-bergmann.de/">Sebastian Bergmann</a> for this because he wrote most of those tools. I&#8217;ll cover some of them here, but there are more.</p>
<h3>PHPUnit</h3>
<p><a title="PHPUnit" href="http://www.phpunit.de/manual/3.6/en/index.html">PHPUnit</a> is the PHP version of the xUnit framework. It allows running automated tests on code. To install it, use those commands:<br />
<code><br />
sudo apt-get install php5-curl php-pear php5-dev<br />
sudo pear upgrade pear</p>
<p>sudo pear channel-discover pear.phpunit.de<br />
sudo pear channel-discover components.ez.no<br />
sudo pear channel-discover pear.symfony-project.com</p>
<p>sudo pear install phpunit/PHPUnit<br />
</code></p>
<p>After you installed PHPUnit, you can run it with this command:<br />
<code><br />
phpunit .<br />
</code><br />
It will look for test cases in the current folder and sub folders.</p>
<p>You can also use PHPUnit to generate a code coverage report.<br />
<code><br />
phpunit  --coverage-html ../CodeCoverage .<br />
</code><br />
This will generate an HTML report of the code that is covered by the tests. The files will be located in the path you passed it. In this case, a CodeCoverage folder located in the parent folder. The reports will look like this:</p>
<div id="attachment_495" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/CoverageIndex.png"><img class="size-medium wp-image-495" title="Coverage index page" src="http://erichogue.ca/wp-content/uploads/2011/04/CoverageIndex-300x133.png" alt="Coverage index page" width="300" height="133" /></a><p class="wp-caption-text">Coverage index page</p></div>
<p>If you click on a file, you will get the coverage by function and the content of the file. The code will be highlighted to show what is covered and what is not.</p>
<div id="attachment_497" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/CoverageFile.png"><img class="size-medium wp-image-497" title="Coverage Of a File" src="http://erichogue.ca/wp-content/uploads/2011/04/CoverageFile-300x137.png" alt="Coverage Of a File" width="300" height="137" /></a><p class="wp-caption-text">Coverage of a file</p></div>
<div id="attachment_500" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/CoverageNotCovered.png"><img class="size-medium wp-image-500" title="Code not covered" src="http://erichogue.ca/wp-content/uploads/2011/04/CoverageNotCovered-300x86.png" alt="Code not covered" width="300" height="86" /></a><p class="wp-caption-text">Code not covered</p></div>
<h3>PHP CodeSniffer</h3>
<p><a title="PHP Code Sniffer" href="http://pear.php.net/package/PHP_CodeSniffer/redirected">PHP Code Sniffer</a> analyse you code and detect violations to coding standards. It comes bundle with a few standards like the Zend and PEAR standards. But you can write your own standard by expanding an existing standard.</p>
<p>It&#8217;s a simple PEAR install. Than you can run it by calling phpcs with the standard to use and the path to check.<br />
<code><br />
sudo pear install PHP_CodeSniffer<br />
phpcs --standard=Zend .<br />
</code><br />
You will get a result like this:</p>
<div id="attachment_503" class="wp-caption alignnone" style="width: 280px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/phpcs.png"><img class="size-medium wp-image-503" title="CodeSniffer results" src="http://erichogue.ca/wp-content/uploads/2011/04/phpcs-270x300.png" alt="CodeSniffer results" width="270" height="300" /></a><p class="wp-caption-text">CodeSniffer results</p></div>
<p>If you don&#8217;t want to specify the standard to use on every call, you can set a default with:<br />
<code><br />
phpcs --config-set default_standard Zend<br />
</code></p>
<h3>PHP Depend</h3>
<p><a title="PHP Depend" href="http://pdepend.org/">PHP Depend</a> is a tool that will perform a static analysis of you code base. It generates software metrics that can be used to evaluate the complexity and quality of your code. Install it with PEAR:<br />
<code><br />
sudo pear channel-discover pear.pdepend.org<br />
sudo pear install pdepend/PHP_Depend-beta<br />
</code><br />
To execute it, you call pdepend and pass it the type of reports you want it to generate. Execute it without parameters to view a brief description of the possible parameters.<br />
<code><br />
pdepend --jdepend-xml=../jdepend.xml --jdepend-chart=../dependencies.svg --overview-pyramid=../overview-pyramid.svg .<br />
</code><br />
This will generate an XML with the packages dependencies, a diagram of the dependencies and the overview pyramid of the project.</p>
<div id="attachment_596" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/05/dependencies.png"><img class="size-medium wp-image-596" title="Abstraction Instability Chart" src="http://erichogue.ca/wp-content/uploads/2011/05/dependencies-300x192.png" alt="Abstraction Instability Chart" width="300" height="192" /></a><p class="wp-caption-text">Abstraction Instability Chart</p></div>
<p><a title="The Abstraction Instability Chart" href="http://www.objectmentor.com/resources/articles/oodmetrc.pdf">The Abstraction Instability Chart</a> was invented by <a title="Robert C. Martin" href="http://www.objectmentor.com/omTeam/martin_r.html">Robert C. Martin</a> (uncle Bob) as a mean of measuring the instability and abstraction of the packages of a project. You should try to keep your packages as close as possible to the green line. An abstract package should have more classes depending on it, so it should be very stable. Concrete packages can be unstable, because nothing should directly depends on them if you code against abstractions. Having said that, I am not sure how it breaks my code into packages. I would guess it uses folders. </p>
<div id="attachment_597" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/05/overview-pyramid.png"><img class="size-medium wp-image-597" title="Overview Pyramid" src="http://erichogue.ca/wp-content/uploads/2011/05/overview-pyramid-300x192.png" alt="Overview Pyramid" width="300" height="192" /></a><p class="wp-caption-text">Overview Pyramid</p></div>
<p>The <a title="Overview Pyramid" href="http://pdepend.org/documentation/handbook/reports/overview-pyramid.html">Overview Pyramid</a> contains the metrics extracted by PHP Depend. The PHP Depend documentation has a great explanation of the pyramid, but I&#8217;ll try to give a brief overview.</p>
<p>The top part displays the metrics about inheritance. They are Average Number of Derived Classes (ANDC) and  Average Hierarchy Height (AHH). ANDC tells you how many of your classes are derived from other classes. AHH is a measure of how deep your hierarchy is.</p>
<p>The bottom right part shows the coupling metrics. NOM represents the numbers of methods in your project. CALLS is the number of methods calls. FANOUT counts the types that are references by your classes. It counts only references that are not part of the same class hierarchy.</p>
<p>The bottom left part of the pyramid contains the metrics that are used the most. NOP is the number of packages, NOC the number of classes, NOM the number of methods, LOC the lines of code and CYCLO is the <a title="cyclomatic complexity" href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">cyclomatic complexity</a>.</p>
<p>The numbers in the middle represents the actual count and the numbers on either sides are averages of the metrics. They represent the value of the row under it divided by the value of the current row. So in my graph, I have a total of 168 lines of code and 26 methods, for an average of 6.462 lines per method.</p>
<h3>PHP Mess Detector</h3>
<p><a title="PHPMD" href="http://phpmd.org/">PHPMD</a> is used to detect problems in your code. It uses the same metrics as PHP Depends to give you feedback on your code. It can detect possible bugs and common issues. Install it with PEAR.<br />
<code><br />
sudo pear channel-discover pear.phpmd.org<br />
sudo pear channel-discover pear.pdepend.org<br />
sudo pear install --alldeps phpmd/PHP_PMD<br />
</code></p>
<p>To execute PHPMD you need to give it the files to parse, a format for the output and the rule sets to use.<br />
<code><br />
phpmd . html codesize,unusedcode,naming,design --reportfile ../messdetector.html --exclude Tests/<br />
</code><br />
This will run it on the current folder and generate html. It will use all 4 rule sets install with PHPMD. The reportfile option tells PHPMD to send the output to the specified file instead of stdout and the exclude options tells it to ignore files in the Tests folder. It generates something like this:</p>
<div id="attachment_511" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/phpmd.png"><img class="size-medium wp-image-511" title="PHP Mess Detector Output" src="http://erichogue.ca/wp-content/uploads/2011/04/phpmd-300x94.png" alt="PHP Mess Detector Output" width="300" height="94" /></a><p class="wp-caption-text">PHP Mess Detector Output</p></div>
<h3>PHP Copy/Paste Detector</h3>
<p>As the name implies, <a title="PHPCPD" href="https://github.com/sebastianbergmann/phpcpd">PHPCPD</a> detects when code has been copied over in your code. This gives you great candidates for refactoring your code.</p>
<p>Install it with PEAR. If you already installed PHPUnit, you should not need the two channel-discover commands.<br />
<code><br />
sudo pear channel-discover pear.phpunit.de<br />
sudo pear channel-discover components.ez.no</code></p>
<p><code> </code></p>
<p><code>sudo pear install phpunit/phpcpd<br />
</code><br />
When it&#8217;s installed you can just run phpcpd by giving it the path to check. It will returns something like this:</p>
<div id="attachment_519" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/phpcpd.png"><img class="size-medium wp-image-519" title="PHPCPD Output" src="http://erichogue.ca/wp-content/uploads/2011/04/phpcpd-300x122.png" alt="PHPCPD Output" width="300" height="122" /></a><p class="wp-caption-text">PHPCPD Output</p></div>
<h3>PHP Dead Code Detector</h3>
<p><a title="PHPDCD" href="https://github.com/sebastianbergmann/phpdcd">PHPDCD</a> scan you project and detect functions that are not called. This might not look like a big deal, but dead code can really slow down the maintenance of a project. Developers have to scan and understand this code, and they might end up fixing bugs in code that is never called. Delete this code, you can get it back from your source control if you need it.<br />
<code><br />
sudo pear channel-discover pear.phpunit.de<br />
sudo pear channel-discover components.ez.no</code></p>
<p><code> </code></p>
<p><code>sudo pear install phpunit/phpdcd-beta<br />
</code><br />
Run it by calling phpdcd with the path to scan. I make it ignore my tests. If not, it will flag them all because they are not called anywhere in the code, phpunit runs them.</p>
<p><div id="attachment_521" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/phpdcd.png"><img class="size-medium wp-image-521" title="PHPDCD Output" src="http://erichogue.ca/wp-content/uploads/2011/04/phpdcd-300x88.png" alt="PHPDCD Output" width="300" height="88" /></a><p class="wp-caption-text">PHPDCD Output</p></div><br />
&nbsp;</p>
<h2>Putting It All Together With Jenkins</h2>
<p>All those tools are very good, but having to call them one by one is painful. <a title="Jenkins" href="http://jenkins-ci.org/">Jenkins</a> is the continuous integration server that will manage continuously running all of those tools. It will be a central point of access for all the reports and send notifications when a build failed.</p>
<h3>Building Your Project</h3>
<p>Jenkins can build a project in many ways. It can use a Windows batch file, execute a shell script, Ant or Maven build files.</p>
<p>For my project I used Ant. To install Ant, make sure you have the Java JDK installed, then you can install it with apt.<br />
<code><br />
sudo apt-get install default-jdk<br />
sudo apt-get install ant<br />
</code><br />
Then, you need to create your build file. The Ant site has a great <a title="Ant build file introduction" href="http://ant.apache.org/manual/using.html">introduction</a> on creating build files. Here&#8217;s what my build file looks like:</p>
<pre><code>
&lt;project name="Test" default="build" basedir="."&gt;
	&lt;property name="output" location="${basedir}/buildOutput/"/&gt;

	&lt;target name="init"&gt;
		&lt;mkdir dir="${output}"/&gt;
		&lt;mkdir dir="${output}/phpcs/"/&gt;
		&lt;mkdir dir="${output}/pdepend/"/&gt;
	&lt;/target&gt;

	&lt;target name="build" depends="init, test, phpcs, phpmd, phpcpd, pdepend"&gt;
	&lt;/target&gt;

	&lt;target name="test"&gt;
		&lt;exec executable="phpunit" failonerror="true"&gt;
			&lt;arg line="--coverage-clover ${output}/CodeCoverage/clover.xml 
				--coverage-html ${output}/CodeCoverage/ 
				."/&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

	&lt;target name="phpcs"&gt;
		&lt;exec executable="phpcs"&gt; 
			&lt;arg line="--report=checkstyle
              			--report-file=${output}/phpcs/checkstyle.xml
              			--standard=Zend
              			${basedir}" /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

	&lt;target name="phpmd"&gt;
		&lt;exec executable="phpmd"&gt;
			&lt;arg line="
				 . xml codesize,unusedcode,naming,design --reportfile ${output}/messdetector.xml --exclude Tests/
			" /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

	&lt;target name="phpcpd"&gt;
		&lt;exec executable="phpcpd"&gt;
			&lt;arg line="
				 --log-pmd ${output}/phpcpd.xml .
			" /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

	&lt;target name="pdepend"&gt;
		&lt;exec executable="pdepend"&gt;
			&lt;arg line="
				--jdepend-xml=${output}/pdepend/jdepend.xml 
				--jdepend-chart=${output}/pdepend/dependencies.svg 
				--overview-pyramid=${output}/pdepend/overview-pyramid.svg 
				--ignore=Tests/ 
				.
			" /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;
&lt;/project&gt;
</code></pre>
<p>It defines a default target that make sure the needed folders for the output are there, then it calls PHPUnit, PHP Code Sniffer, PHP Mess detector, PHP Copy Paste Detector and PHP Depend.</p>
<h3>Installing Jenkins</h3>
<p>Installing it it pretty easy. There are package for many OS. For Ubuntu, just follow the instructions from <a href="http://pkg.jenkins-ci.org/debian/" title="Jenkins installation">the Jenkins site</a>. Download the key and add it to apt.<br />
<code><br />
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -<br />
</code><br />
Add this line to /etc/apt/sources.list<br />
<code><br />
deb http://pkg.jenkins-ci.org/debian binary/<br />
</code><br />
Update your apt index and install Jenkins<br />
<code><br />
sudo apt-get update<br />
sudo apt-get install jenkins<br />
</code><br />
This will install Jenkins, set it as a daemon and start it. After, you can navigate to http://localhost:8080 to access your Jenkins installation.</p>
<div id="attachment_565" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/04/JenkinsHome.png"><img class="size-medium wp-image-565" title="Jenkins Home Page" src="http://erichogue.ca/wp-content/uploads/2011/04/JenkinsHome-300x184.png" alt="Jenkins Home Page" width="300" height="184" /></a><p class="wp-caption-text">Jenkins Home Page</p></div>
<h3>Configuring Jenkins</h3>
<p>Jenkins has many configuration options. I won&#8217;t go through all of them, but I&#8217;ll try to cover those I thinks are important. First click on &#8220;Manage Jenkins&#8221; then &#8220;Configure System&#8221;. Go through the settings there and configure Jenkins according to your needs. You should probably enable the security and pick how your users will log in. You should also configure the SMTP servers so Jenkins can send you emails.</p>
<p>After, look at the plugins and enable those you need. I use Git for my source control, so I enabled the Git plugin. All I needed to do was select the plugin, click on the Install button at the bottom of the list and click on the restart button when it was done.</p>
<p>You will also need a bunch of plugins to manage the data produced by the tools. <a title="Clover" href="https://wiki.jenkins-ci.org/display/JENKINS/Clover+Plugin">Clover</a> will display a code coverage graph from with the data built by PHPUnit. <a title="Checkstyle" href="https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin">Checkstyle</a> to display the results of PHP Code Sniffer. <a title="PMD" href="https://wiki.jenkins-ci.org/display/JENKINS/PMD+Plugin">PMD</a> for the PHP Mess Detector results. <a title="DRY">DRY</a> to show duplicated code. And finally <a title="JDepend" href="https://wiki.jenkins-ci.org/display/JENKINS/JDepend+Plugin">JDepend</a> to show the metrics produces by PHP Depend.</p>
<h3>Creating You Project</h3>
<p>This is where you will configure Jenkins to build your project. Click on the &#8220;New Job&#8221; link. Give your job a name, select the free style project and click on OK. You will be taken the Configure page of the project. There are many options available and they all have a description if you click on the question mark icon.</p>
<p>First thing I did is a trick I got from Sebastian Bergmann&#8217;s <a title="Template for Jenkins Jobs for PHP Projects" href="http://jenkins-php.org/">Template for Jenkins Jobs for PHP Projects</a>. I added an embed tags with links to the svg files created by PHP Depend. Since they are not showed by the plugins and they contains valuable information, it&#8217;s nice to see them on my project page.</p>
<p>Next, I needed to configure the source control repository. I gave Jenkins the path to my Git repository on the same machine. I left the branch blank so it check for changes in all the branches and build them. I entered the URL to my Gitweb interface so my builds can contains links to the repository and the commit that triggered the build. </p>
<p>Git requires that you set a user name and an email. The advanced section of the Git repository setup have fields for them, but they are not saved correctly, so you will have to do it manually.<br />
<code><br />
su - jenkins<br />
git config --global user.name "Jenkins"<br />
git config --global user.email "jenkins@yourdomain.com"<br />
</code><br />
This problem should be fixed in the next version.</p>
<p>You needs to tell Jenkins when to build your project. I decided to have Jenkins poll Git every minutes and launch a build every time something changed. I your project has many commits and takes time to build, you might want to make interval between checks longer.</p>
<p>In the build section, I just told Jenkins to invoke Ant. Since I want it to launch the default action in my build file, I did not need to give it a target.</p>
<h4>Post Build Actions</h4>
<p>This is where most of the configuration for the project is needed. All the plugins we installed for displaying the results of the build need to be configured. Make sure, you select all the plugins you have installed.</p>
<p>Many plugins will requires a results file. This is the files produce by the tests tools. Enter the relative path the results files so Jenkins can read them. There are also &#8220;Run always&#8221; check-boxes. By default, some plugins don&#8217;t run on failed build. When &#8220;Run always&#8221; is checked, the plugin will run no matter the status of the build.</p>
<p>Some plugins also requires some thresholds. Those are the values that tell Jenkins when to consider a build unstable, or failed. Configure them to values that make sense for your project. For the code coverage, set the conditionals to 0%, because they are not reported.</p>
<p>At the bottom of the project settings, you can also configure Jenkins to send emails on failed build. It will ask you for a list of addresses where to send the email and you can make it send another email to those who break the build.</p>
<div id="attachment_626" class="wp-caption alignnone" style="width: 126px"><a href="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsProjectSettings.png"><img class="size-medium wp-image-626" title="Jenkins Project Settings" src="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsProjectSettings-116x300.png" alt="Jenkins Project Settings" width="116" height="300" /></a><p class="wp-caption-text">Jenkins Project Settings</p></div>
<p>&nbsp;</p>
<h2>The End Result</h2>
<p>Keep Jenkins running for a while. Work on your project and make a few commits. After some time, you will have a project page that looks like this:</p>
<div id="attachment_634" class="wp-caption alignnone" style="width: 182px"><a href="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsProjectPage.png"><img class="size-medium wp-image-634" title="Jenkins Project Page" src="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsProjectPage-172x300.png" alt="Jenkins Project Page" width="172" height="300" /></a><p class="wp-caption-text">Jenkins Project Page</p></div>
<p>If you click on a build in the Build History, you will get a page like this one:</p>
<div id="attachment_636" class="wp-caption alignnone" style="width: 310px"><a href="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsBuildResult.png"><img class="size-medium wp-image-636" title="Jenkins Build Results" src="http://erichogue.ca/wp-content/uploads/2011/05/JenkinsBuildResult-300x182.png" alt="Jenkins Build Results" width="300" height="182" /></a><p class="wp-caption-text">Jenkins Build Results</p></div>
<p>If you have a team that commit early and often, Jenkins will provide some invaluable information. You can use it to look for part of your code where you need improvements. You can always know if your new developer is not following the team coding standards. And you can pinpoint area that might be too complicated and should be refactored. With a good test suite in place that automatically runs on every commit, you can detect regressions as soon as they are committed.</p>
<h2>Wrapping It Up</h2>
<p>The subject of Continuous Integration is broad and this is already a long post. And there is still much I need to learn about it. Creating the build script and configuring the Jenkins project was a lot of work. It was fun to do and I learned a lot, but doing it manually for every project would be daunting. I already mentioned it earlier, there is a great <a title="Template for Jenkins Jobs for PHP Projects" href="http://jenkins-php.org/">template job for Jenkins</a>. It has all the instruction for installing it, a build script and the Jenkins project.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichogue.ca/2011/05/php/continuous-integration-in-php/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching using apc
Object Caching 1034/1156 objects using apc

 Served from: erichogue.ca @ 2013-05-20 12:11:11 by W3 Total Cache -->