<?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>woblog &#187; Test Driven Development</title>
	<atom:link href="http://www.whiteoctober.co.uk/blog/category/test-driven-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whiteoctober.co.uk/blog</link>
	<description>great stuff about the web</description>
	<lastBuildDate>Mon, 05 Dec 2011 16:53:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Symfony2 data collectors</title>
		<link>http://www.whiteoctober.co.uk/blog/2011/12/05/symfony2-data-collectors/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2011/12/05/symfony2-data-collectors/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 16:35:48 +0000</pubDate>
		<dc:creator>Rich</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Web technologies]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=792</guid>
		<description><![CDATA[I spent some time last week working on automated MailChimp integration with one of our Symfony2 sites. The project uses Behat and Mink extensively for BDD testing, so I wanted to be able to test that when users registered on our site, they were automatically added to the client&#8217;s MailChimp distribution list. Unsubscription could happen [...]]]></description>
			<content:encoded><![CDATA[<p>I spent some time last week working on automated <a href="http://www.mailchimp.com/" title="MailChimp">MailChimp</a> integration with one of our <a href="http://symfony.com" title="Symfony">Symfony2</a> sites.  The project uses <a href="http://behat.org/">Behat</a> and <a href="http://mink.behat.org/" title="Mink">Mink</a> extensively for BDD testing, so I wanted to be able to test that when users registered on our site, they were automatically added to the client&#8217;s MailChimp distribution list.  Unsubscription could happen via a checkbox in the user&#8217;s account settings and this needed to work the same way behind the scenes.</p>
<p>I opted to use one of the <a href="https://github.com/jirafe/MailChimpBundle">available MailChimp bundles</a> to aid integration and with a bit of tweaking this worked great.  The bundle comes with a stub connection which is able to collate requests sent and make them available for inspection.  I wanted to be able to utilise this neatly inside a set of tests, and potentially also make it available in the dev environment.  A bit of skimming the Symfony2 cookbook led me to the entry on <a href="http://symfony.com/doc/current/cookbook/profiler/data_collector.html">Symfony2 data collectors</a>.</p>
<p>The concept is simple &#8211; a data collector is registered and is able to, well, collect data!  For example, the number of queries during a request, or the number of emails sent.  This can then be accessed via the profiler in the relevant environment (dev, test etc).  In my case I wanted to collect and subsequently inspect all requests sent to MailChimp.</p>
<p>First step &#8211; create me a data collector:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> Jirafe\Bundle\MailChimpBundle\DataCollector<span style="color: #339933;">;</span>
&nbsp;
use Symfony\Component\HttpKernel\DataCollector\DataCollector<span style="color: #339933;">;</span>
use Symfony\Component\HttpFoundation\Request<span style="color: #339933;">;</span>
use Symfony\Component\HttpFoundation\Response<span style="color: #339933;">;</span>
use Jirafe\Bundle\MailChimpBundle\Connection\ConnectionInterface<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MailChimpDataCollector <span style="color: #000000; font-weight: bold;">extends</span> DataCollector
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>ConnectionInterface <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> collect<span style="color: #009900;">&#40;</span>Request <span style="color: #000088;">$request</span><span style="color: #339933;">,</span> Response <span style="color: #000088;">$response</span><span style="color: #339933;">,</span> \Exception <span style="color: #000088;">$exception</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'requests'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequests</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRequestCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'requests'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRequests<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'requests'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'mail_chimp'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>collect()</code> method is the interesting part here &#8211; this is called whenever data collection is required &#8211; usually once per request.  We collect the requests from the connection supplied to the constructor.</p>
<p>Next step, register this during the bundle&#8217;s <code>load()</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Add connection to data collector</span>
<span style="color: #000088;">$definition</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$container</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDefinition</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mail_chimp.data_collector'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$definition</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addArgument</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Reference<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mail_chimp.connection.%s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above uses the connection type specified in the bundle&#8217;s configuration option and previously registered as a service in the same method.  If you&#8217;re creating your own custom collector, this is just the normal parameters you&#8217;d pass in to your service when it&#8217;s instantiated.</p>
<p>Finally, I added a method and subsequent implementation to the interface and related connection classes within the bundle, so that MailChimp request objects were collected in an array, and made available to the data collector upon request. This is the <code>getRequests()</code> method listed above in the data collector; called in the <code>collect()</code> method. Quick &#8216;n&#8217; dirty here, since there would only ever be a single MailChimp request (possibly 2) made in any one request.</p>
<p>Once all this was in place, I could then test correctly.  Note that I&#8217;d rather have done the other way round (test and then develop) but since this was experimentation, I figured it was OK ;-) In my Behat scenarios, I added the <code>getSymfonyProfiler()</code> method detailed in a Behat/Mink <a href="http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html">cookbook entry</a>, and proceeded to write my step (Behat 1.x-style):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Then</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^my email address is unsubscribed from the relevant MailChimp list$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$profiler</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSymfonyProfiler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$profiler</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> \RuntimeException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Symfony2 profiler not present for tests, wrong driver?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$mcDataCollector</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$profiler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCollector</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mail_chimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$listID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">container</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mailchimp_list_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Check the requests made to make sure we have an unsubscribe</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mcDataCollector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$requests</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mcDataCollector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequests</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$req1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$requests</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listUnsubscribe&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$req1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$req1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000088;">$listID</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Fire the redirect on the client</span>
    <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDriver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">followRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The final line here is as a result of disabling redirects in a previous step since the data collector operates on a per-request basis.  If you&#8217;re subscribing eg via a form POST, then you&#8217;ll need to disable redirects prior to submitting the form, otherwise your data collector will be empty when you check it.  Disable them and then redirect after you&#8217;ve carried out your checks.</p>
<p>The implementation for the above is up on our Github MailChimpBundle fork <a href="https://github.com/whiteoctober/MailChimpBundle">here</a>; I&#8217;m not particularly fond of having to hack about the connection classes, and ideally it would be nice to layer the data collector on top without having to adjust existing code. That said, it does the job fine here, and it&#8217;s good to use even more features of Symfony2 ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2011/12/05/symfony2-data-collectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functional testing sending emails in Silex</title>
		<link>http://www.whiteoctober.co.uk/blog/2011/08/01/functional-testing-sending-emails-in-silex/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2011/08/01/functional-testing-sending-emails-in-silex/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 11:29:03 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Silex]]></category>
		<category><![CDATA[Swiftmailer]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=760</guid>
		<description><![CDATA[We are slowly starting to fall in love with Silex the Micro Framework built using Symfony2 here at White October towers. Here is a quick code sample which shows how to test emails that are sent using Silex and Swiftmailer. I would recommend using this example app to get Silex and Swiftmailer setup if you [...]]]></description>
			<content:encoded><![CDATA[<p>We are slowly starting to fall in love with <a href="http://silex-project.org/">Silex</a> the Micro Framework built using <a href="http://symfony.com/">Symfony2</a> here at White October towers.</p>
<p>Here is a quick code sample which shows how to test emails that are sent using Silex and Swiftmailer.</p>
<p>I would recommend using <a href="https://github.com/helios-ag/Silex-SwiftMailer-Email-Sending-Example">this example app</a> to get Silex and Swiftmailer setup if you haven&#8217;t got a working setup.</p>
<p>The aim in the code is to stop sending of real email in the test environment and log the messages so we can test them.</p>
<p>Here is a quick gist of the Test class, testing for number of emails sent and the first emails subject.</p>
<p><script src="https://gist.github.com/1117957.js?file=SwiftTest.php"></script></p>
<p>I have copied out the Symfony2 MessageLogger.php class from the SwiftmailerBundle, which I&#8217;ve included in the <a href="https://gist.github.com/1117957">gist</a> for your convenience. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2011/08/01/functional-testing-sending-emails-in-silex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting into Behat with Symfony2</title>
		<link>http://www.whiteoctober.co.uk/blog/2011/06/15/getting-into-behat-with-symfony2/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2011/06/15/getting-into-behat-with-symfony2/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 07:47:21 +0000</pubDate>
		<dc:creator>Rich</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Uncategorised]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=740</guid>
		<description><![CDATA[I&#8217;ve spent the past week starting a new sprint on our latest large project, and as we&#8217;re keen to get our clients as involved as possible during sprints, we decided to get down with some Behaviour-Driven Development (BDD) which allows them to contribute to writing the actual acceptance criteria for the user stories. We&#8217;re pretty [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the past week starting a new sprint on our latest large project, and as we&#8217;re keen to get our clients as involved as possible during sprints, we decided to get down with some <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behaviour-Driven Development</a> (BDD) which allows them to contribute to writing the actual acceptance criteria for the user stories.  We&#8217;re pretty big on <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven Developmen</a>t here at WO Towers but sometimes it&#8217;s necessary to strike a balance between testing every minute detail and no testing.  BDD seems to meet that balance whilst providing good code coverage with tests.  Note: here I speak of functional tests rather than unit tests.</p>
<p>The beauty of BDD is that it&#8217;s written in plain everyday language, and this can be directly translated to code snippets.  Therefore the client is happy working &#8220;in plain english&#8221;, and the developer can crack on with writing the actual code to test these parts.</p>
<p>We&#8217;ve chosen to use <a href="http://symfony.com/">Symfony2</a> for the project (more on that in a separate post) and for the BDD aspect we&#8217;ve gone with <a href="http://behat.org/">Behat</a> (coupled with <a href="https://github.com/Behat/Mink">Mink</a>); both of which have bundles available for Symfony2. Essentially, BDD boils down to <strong>Features</strong>, consisting of a number of <strong>Scenarios</strong>, which in turn consist of a number of <strong>Steps</strong>.  Here&#8217;s an example:</p>
<pre>Feature: Login to the admin area
	As an administrator
	I want to be able to log into an admin area
	So I can administer MyApp

	Scenario: Joe Bloggs logs into the admin area
	  Given I visit the admin area
	  When I am shown the login form
	  And I click login with my correct credentials
	  Then I should be on /admin/home
</pre>
<p>This could translate to code such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Given</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^I visit the admin area$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">visit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/admin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">When</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^I am shown the login form$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    assertTrue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertTrue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">And</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^I click login with my correct credentials$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fillField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;joe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fillField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bloggs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clickButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log in&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Then</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(?:|I )should be on (?P&lt;page&gt;.+)$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    assertEquals<span style="color: #009900;">&#40;</span>
        <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathTo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_URL_PATH<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrentUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_URL_PATH<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The beauty of Behat is that the step definitions are reusable, meaning that if you stick to a standard way of describing your steps, the amount of code you need to write is vastly reduced, compared with normal functional TDD.  Steps that aren&#8217;t implemented yet are given default code as per:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Given</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^I visit the admin area$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> \Behat\Behat\Exception\PendingException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>which will translate into the output shown as a step needing to be implemented.  Behat also takes note of the Pending exception being thrown and automatically skips any steps following this in the scenario, notifying you of this at the end of the test run.</p>
<p>Behat also comes with a number of standard step definitions built-in, although these are somewhat buried in the code and not in the docs :-(  So for example the following steps are part of the included ones:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">I go to <span style="color: #339933;">/</span>my<span style="color: #339933;">/</span>url<span style="color: #339933;">:</span>
&nbsp;
<span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">When</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(?:|I )go to (?P&lt;page&gt;.+)$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">visit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathTo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">I fill in <span style="color: #0000ff;">&quot;username&quot;</span> with <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #000088;">$steps</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">When</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(?:|I )fill in &quot;(?P&lt;field&gt;[^&quot;]*)&quot; with &quot;(?P&lt;value&gt;[^&quot;]*)&quot;$/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fillField</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>which translates as even less code to write. Bonus!</p>
<h2>Tips &#8216;n&#8217; tricks</h2>
<p>The Mink (browser emulator abstraction layer) setup comes with a Symfony2 driver which is great for just testing normal usage.  In our project however we&#8217;re integrating Facebook authentication using the FOS <a href="https://github.com/FriendsOfSymfony/FacebookBundle">FacebookBundle</a>, and we needed a way to test that the user was being bounced to Facebook in order to log in.  The solution for this was simple &#8211; switch to using <a href="https://github.com/fabpot/Goutte">Goutte</a> as the driver, as per the BehatBundle docs, and voila, you can test the current URL contains eg facebook.com or whatever.  The actual testing of the authentication itself I&#8217;ll write up in a separate blog post as it&#8217;s a bit epic for here.</p>
<p>The documentation mentions using a separate front controller for testing, with a separate environment configuration.  This is definitely recommended, particularly if you&#8217;re using Goutte.  If you need services to be available, for example your entity manager, pop something like this in your env.php file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$kernel</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AppKernel<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadClassCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContainer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your.service.here&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also use the <code>env.php</code> file for setting up fixtures that need to be reset for each Scenario that you create, eg pre-defined user states and so on.</p>
<h2>Caveats</h2>
<p>Unless you&#8217;re using <a href="https://github.com/Behat/SahiClient">Sahi</a>, you obviously can&#8217;t test things such as Javascript popups effectively.  I&#8217;m in two minds as to whether this should be handled by a new type of Exception which can be thrown e.g. a <code>TestInRealBrowserException()</code>.  That way it could be caught and notified to the user running the tests, or flagged as part of an automated build process for manual testing.</p>
<p>Overall, getting into BDD has been a fun and rewarding experience.  At times the lack of docs make it somewhat frustrating, but this is easily solved by a) digging into the code and b) probably me getting off my ass and submitting some docs back.  The ease of which the steps can be written though is a massive time saver when writing your tests, coupled with the reusability of the code.  Big thumbs-up from me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2011/06/15/getting-into-behat-with-symfony2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to test UTF-8 email subject lines in Symfony</title>
		<link>http://www.whiteoctober.co.uk/blog/2011/01/25/how-to-test-utf-8-email-subject-lines-in-symfony/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2011/01/25/how-to-test-utf-8-email-subject-lines-in-symfony/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 13:23:50 +0000</pubDate>
		<dc:creator>smotraghi</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=691</guid>
		<description><![CDATA[One of our clients needs to send emails with UTF-8 subject lines. Swift Mailer, which Symfony uses, makes this easy to do, but writing functional tests for it is another story.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">
<p>(crossposted from Sebastian&#8217;s <a href="http://blog.motraghi.us/2011/01/how-to-test-utf-8-email-subject-lines-in-symfony/">personal blog</a>)</p>
<p>One of our clients needs to send emails with UTF-8 subject lines. Swift Mailer, which Symfony uses, makes this easy to do, but writing functional tests for it is another story. The problem is that, if non-ASCII characters are present in the subject line, Swift Mailer encodes them as required by <a href="http://datatracker.ietf.org/doc/rfc2047/">RFC 2047</a> – for example, a string like &#8220;Transformación&#8221; turns into &#8220;=?utf-8?Q?Transformaci=C3=B3n?=&#8221;. How do we know that this is the right subject line?</p>
<p>There&#8217;s no immediately-available &#8220;convert this into a RFC 2047-compliant subject line&#8221; function, but we can get around this by making use of <code>Swift_Message-&gt;toString</code> and some choice preg functions.</p>
<p>First, create a dummy message containing the appropriate header. <code>Swift_Message-&gt;toString()</code> will contain our encoded header:</p>
<p><script src="https://gist.github.com/794680.js?file=gistfile1.php"></script> Next, we have to extract our encoded header – I&#8217;ve used <code>preg_match()</code> and a fairly simple regex.  <script src="https://gist.github.com/794680.js?file=gistfile2.php"></script></p>
<p>Finally, now that the encoded header has been extracted, we need only wrap it in <code>preg_quote()</code> (because <code>checkHeader()</code> uses regex syntax) and test away.</p>
<p><script src="https://gist.github.com/794680.js?file=gistfile3.php"></script></p>
<p>Voila – now we can be confident that our test script can deal with Unicode email subjects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2011/01/25/how-to-test-utf-8-email-subject-lines-in-symfony/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hudson and Symfony continuous integration</title>
		<link>http://www.whiteoctober.co.uk/blog/2010/02/05/hudson-and-symfony-continuous-integration/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2010/02/05/hudson-and-symfony-continuous-integration/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 15:02:34 +0000</pubDate>
		<dc:creator>Rich</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=460</guid>
		<description><![CDATA[We&#8217;ve been experimenting this week with using Hudson as our continuous integration server for our latest Symfony project.  Previous experiences with CruiseControl via phpUnderControl and the symfonyUnderControl plugin were OK-ish but we&#8217;d occasionally experience CruiseControl dying on us with no warning apart from the lack of emails after checking our code in. So we decided [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been experimenting this week with using <a title="Hudson" href="http://hudson-ci.org/">Hudson</a> as our continuous integration server for our latest <a href="http://www.symfony-project.org/">Symfony</a> project.  Previous experiences with <a title="CruiseControl" href="http://cruisecontrol.sourceforge.net/">CruiseControl</a> via <a title="phpUnderControl" href="http://phpundercontrol.org/">phpUnderControl</a> and the <a title="symfonyUnderControl" href="http://www.symfony-project.org/plugins/symfonyUnderControlPlugin">symfonyUnderControl</a> plugin were OK-ish but we&#8217;d occasionally experience CruiseControl dying on us with no warning apart from the lack of emails after checking our code in.</p>
<p>So we decided it was time to perhaps investigate alternatives.  The obvious choice for Symfony would be to use <a title="Sismo" href="http://ci.symfony-project.org/">Sismo</a>, but sadly it hasn&#8217;t been released yet&#8230; <a title="Fabien Potencier" href="http://fabien.potencier.org">Fabien</a>, any clues as to a release date? ;-) It looks at the moment as if we&#8217;re stuck with a Java-based solution, so we decided to look at Hudson as an alternative.</p>
<p>Setting up Hudson was straightforward using <a title="Hudson and Symfony by Nicolas Perriault" href="http://prendreuncafe.com/blog/post/2009/10/06/Simple-Continuous-Integration-of-a-Symfony-Project-using-Hudson">Nicolas Perriault&#8217;s blog post</a> on exactly what we were trying to achieve, linking in with our Subversion repository.  The configuration of the projects took a bit of faffing &#8211; for some reason, Hudson liked to check out the whole repository rather than just eg the trunk folder  as specified, so this screwed up some of the paths.  A bit of trial and error later and we were up and running with a build every 30mins if there are new commits, as this pretty graph shows&#8230; (using the jUnit XML output available in Symfony 1.4).  Total number of tests in blue, failures in red.</p>
<p><img class="alignleft size-medium wp-image-461" title="Hudson build trend output" src="http://www.whiteoctober.co.uk/blog/wp-content/uploads/2010/02/build-trend-300x120.png" alt="Hudson build trend output" width="300" height="120" /></p>
<p>One thing to bear in mind if you&#8217;re getting test failures randomly, or perhaps always after a certain developer checks in (and it&#8217;s not a result of broken code) is to ensure that the permissions are correct for Symfony, as SVN likes to store permissions where it can.  The fix for this, apart from committing new permissions, is to add a build step in Hudson that carries out ./symfony project:permissions (Symfony 1.3+) shortly before the build.  This seems to solve it nicely, and explained the small red peaks in the graph above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2010/02/05/hudson-and-symfony-continuous-integration/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Agility, Speed &amp; Test Driven Development</title>
		<link>http://www.whiteoctober.co.uk/blog/2009/07/30/agility-speed-test-driven-development/</link>
		<comments>http://www.whiteoctober.co.uk/blog/2009/07/30/agility-speed-test-driven-development/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 09:18:08 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.whiteoctober.co.uk/blog/?p=313</guid>
		<description><![CDATA[I&#8217;ve just been through the updates from the groups I&#8217;m connected with on LinkedIn, and I read an interesting quote about Agility and speed. If you focus on Quality, through effective refactoring and test coverage, speed will be maintained in the long term. When the code base gets quite large, speed of change can be [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been through the updates from the groups I&#8217;m connected with on LinkedIn, and I read an interesting quote about Agility and speed.</p>
<p><em>If you focus on Quality, through effective refactoring and test coverage, speed will be maintained in the long term. When the code base gets quite large, speed of change can be difficult when there is poor quality code and little test coverage to support the code base.</em></p>
<p><em>You would be fooling yourself if speed was more important.<br />
[Adrian Richte]<br />
</em><br />
This has got me thinking about Test Driven Development (TDD) all over again.</p>
<p>Writing tests upfront for projects always  seems like a massive over head, but as any project gets larger, and more functionality is added, without any tests you just wont know what effect those changes are having on the rest of the system.  To run tests and see them fail means you can deal with an unexpected corner case before a client discovers it, and you know they will.  This will save time in the long run, and will also leave you with happy clients.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whiteoctober.co.uk/blog/2009/07/30/agility-speed-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

