<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>teranex weblog - PHP</title><link href="https://budts.be/" rel="alternate"></link><link href="https://budts.be/feeds/php.atom.xml" rel="self"></link><id>https://budts.be/</id><updated>2013-02-03T00:38:00+01:00</updated><entry><title>Vim: checking PHP code (and Python and...)</title><link href="https://budts.be/weblog/2013/02/vim-checking-php-code-and-python-and" rel="alternate"></link><published>2013-02-03T00:32:00+01:00</published><updated>2013-02-03T00:38:00+01:00</updated><author><name>Jeroen Budts</name></author><id>tag:budts.be,2013-02-03:/weblog/2013/02/vim-checking-php-code-and-python-and</id><summary type="html">&lt;p&gt;Do you hate it as much as I do when you are writing some PHP (or Python or whatever) code in your favorite editor, hit 'save', reload the page in the browser in the hopes that you will see the new most awesome feature you ever wrote, but instead you are greeted by an unfriendly PARSE_ERROR_YOU_FORGOT_A_CLOSING_BRACKET_YOU_STUPID-error? Right, it feels like you just built a rocket, but upon launch you discover you didn't foresee a doorknob and can't get in anymore...&lt;/p&gt;

&lt;p&gt;Luckily, if you are using Vim there is a nice plugin to quickly catch these errors (and more). The plugin I'm talking about is &lt;a href="https://github.com/scrooloose/syntastic"&gt;Syntastic&lt;/a&gt;. The plugin is built as a generic framework to run and process the result of a syntax checker. The syntax checkers themselves are specific for each language. Most of the syntax checkers start an external syntax checker or linter and return the output to Syntastic, telling it how to parse it.&lt;/p&gt;

&lt;p&gt;By default Syntastic will automatically run the correct checker every time you save the …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Do you hate it as much as I do when you are writing some PHP (or Python or whatever) code in your favorite editor, hit 'save', reload the page in the browser in the hopes that you will see the new most awesome feature you ever wrote, but instead you are greeted by an unfriendly PARSE_ERROR_YOU_FORGOT_A_CLOSING_BRACKET_YOU_STUPID-error? Right, it feels like you just built a rocket, but upon launch you discover you didn't foresee a doorknob and can't get in anymore...&lt;/p&gt;

&lt;p&gt;Luckily, if you are using Vim there is a nice plugin to quickly catch these errors (and more). The plugin I'm talking about is &lt;a href="https://github.com/scrooloose/syntastic"&gt;Syntastic&lt;/a&gt;. The plugin is built as a generic framework to run and process the result of a syntax checker. The syntax checkers themselves are specific for each language. Most of the syntax checkers start an external syntax checker or linter and return the output to Syntastic, telling it how to parse it.&lt;/p&gt;

&lt;p&gt;By default Syntastic will automatically run the correct checker every time you save the file. When Syntastic finds some errors or warnings in your code, it can show signs on the offending lines and open a window at the bottom with an overview of the problems (depending on your Syntastic configuration). Using the window with the errors (a Vim location list) you can quickly jump to the lines with errors and warnings. In my configuration Syntastic will automatically open the location list whenever there is a problem and will also close the Location list once the problem is fixed.&lt;/p&gt;

&lt;p&gt;Syntastic has support for over 50 languages, including PHP, Python, Ruby, CSS, Puppet, and so on. For a lot of languages, Syntastic comes with support for some alternative or supplementary syntax checkers. For PHP, Syntastic will first use the PHP built-in linter (&lt;code&gt;`php -l`&lt;/code&gt;). If you also have PHP_CodeSniffer and/or PHP Mess Detector installed it will also run these tools on your file. For Python it depends on which tools you have installed. If you have only Python, Syntastic will use that and notify you of any syntax errors. Personally I like to use &lt;a href="http://pypi.python.org/pypi/flake8"&gt;flake8&lt;/a&gt; as it checks both your style against PEP8 rules and also checks your code with pyflakes.&lt;/p&gt;

&lt;p&gt;One problem you can have with running extensive checks on your code is that you are overwhelmed by the number of errors and warnings. You have to tame the beast or you will drown in the errors and just ignore them. Luckily this is not verify difficult. When developing Drupal code the first step is to install the PHP_CodeSniffer ruleset for Drupal. This is part of the &lt;a href="http://drupal.org/project/coder"&gt;coder module&lt;/a&gt;. However, in my opinion this will still give you too many errors (seriously, is anyone still coding on a computer screen which only has 80 columns??). The easiest solution to this is to create your own ruleset. This is rather easy:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;First install the Drupal ruleset at the correct location in PHP_CodeSniffer. On a *nix system this can be done by symlinking: &lt;code&gt;sudo ln -s /path/to/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Create a new ruleset.xml and refer to the Drupal ruleset to import the entire ruleset.&lt;/li&gt;
  &lt;li&gt;In your own ruleset.xml you can exclude rules from the imported ruleset to disable them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Have a look at my &lt;a href="https://github.com/teranex/dotvim/blob/master/misc/phpcs-drupal-ruleset.xml"&gt;Drupal Ruleset for PHP_CodeSniffer at github&lt;/a&gt; for inspiration. For PHP Mess detector the system is pretty much the same. Here is my &lt;a href="https://github.com/teranex/dotvim/blob/master/misc/phpmd-ruleset.xml"&gt;ruleset for PHP Mess Detector at github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After you have created your personalized rulesets you have to configure Syntastic to use your own rulesets. This can be done by setting the &lt;code&gt;g:syntastic_phpcs_conf&lt;/code&gt; and &lt;code&gt;g:syntastic_phpmd_rules&lt;/code&gt; variables from your .vimrc file. You can see my Syntastic configuration in &lt;a href="https://github.com/teranex/dotvim/blob/master/vimrc#L194"&gt;my vimrc at github&lt;/a&gt;.&lt;/p&gt;</content><category term="vim"></category><category term="inuits-planet"></category><category term="drupal"></category><category term="python"></category><category term="PHP"></category><category term="lint"></category><category term="syntastic"></category><category term="syntax"></category></entry><entry><title>UnitTesting PHP</title><link href="https://budts.be/weblog/2005/06/unittesting-php" rel="alternate"></link><published>2005-06-17T15:07:00+02:00</published><updated>2010-12-18T12:31:00+01:00</updated><author><name>Jeroen Budts</name></author><id>tag:budts.be,2005-06-17:/weblog/2005/06/unittesting-php</id><summary type="html">&lt;p&gt;Once you've discovered the advantages of unittests you'll never want to write one single line of code without a test for it (well allmost ;)). You'll become "test-infected" :).&lt;/p&gt;

&lt;p&gt;A few months ago, while doing a project in .NET, I discovered these advantages. Having unittests for your code helps you to &lt;a href="http://refactoring.com"&gt;refactor&lt;/a&gt; your code and add new/modified features to your application without having to fear that you'll break your code. Ofcourse you can still break your code, but if you have good unittests you'll immediately notice that you broke something and, depending on how good your tests are, you will also know where you broke something. When you have UnitTests you'll feel a lot safer when touching your code.&lt;/p&gt;

&lt;p&gt;Today allmost every language has one or more unittesting frameworks. Most of them are based on &lt;a href="http://junit.org"&gt;JUnit&lt;/a&gt;, and are called &lt;i&gt;xUnit frameworks&lt;/i&gt;. One example is &lt;a href="http://nunit.org"&gt;NUnit&lt;/a&gt;, which is the JUnit port to .NET. Also PHP has it's unittesting frameworks, of which &lt;a href="http://pear.php.net/package/PHPUnit/"&gt;PHPUnit&lt;/a&gt; and &lt;a href="http://www.lastcraft.com/simple_test.php"&gt;SimpleTest&lt;/a&gt; are the best known.&lt;/p&gt;

&lt;!--break--&gt;

&lt;p&gt;SimpleTest is not just …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Once you've discovered the advantages of unittests you'll never want to write one single line of code without a test for it (well allmost ;)). You'll become "test-infected" :).&lt;/p&gt;

&lt;p&gt;A few months ago, while doing a project in .NET, I discovered these advantages. Having unittests for your code helps you to &lt;a href="http://refactoring.com"&gt;refactor&lt;/a&gt; your code and add new/modified features to your application without having to fear that you'll break your code. Ofcourse you can still break your code, but if you have good unittests you'll immediately notice that you broke something and, depending on how good your tests are, you will also know where you broke something. When you have UnitTests you'll feel a lot safer when touching your code.&lt;/p&gt;

&lt;p&gt;Today allmost every language has one or more unittesting frameworks. Most of them are based on &lt;a href="http://junit.org"&gt;JUnit&lt;/a&gt;, and are called &lt;i&gt;xUnit frameworks&lt;/i&gt;. One example is &lt;a href="http://nunit.org"&gt;NUnit&lt;/a&gt;, which is the JUnit port to .NET. Also PHP has it's unittesting frameworks, of which &lt;a href="http://pear.php.net/package/PHPUnit/"&gt;PHPUnit&lt;/a&gt; and &lt;a href="http://www.lastcraft.com/simple_test.php"&gt;SimpleTest&lt;/a&gt; are the best known.&lt;/p&gt;

&lt;!--break--&gt;

&lt;p&gt;SimpleTest is not just an xUnit framework. Apart from the classic TestCase and assert-methods it also offers support for Mock-objects and Stubs, webpage testing to load webpages and verify them, testing forms, authentication tests and a scriptable browser.&lt;/p&gt;

&lt;p&gt;To illustrate it a bit, i'll give a, rather dumb, example. Suppose you create some &lt;code&gt;Calculator&lt;/code&gt; class, which has a method '&lt;code&gt;multiplyByTwo($number)&lt;/code&gt;':&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt; * a KISS calculator.&lt;/span&gt;
&lt;span class="sd"&gt; */&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt;   * This method takes a number and multiplies it by 2.&lt;/span&gt;
&lt;span class="sd"&gt;   * @param int $number the number&lt;/span&gt;
&lt;span class="sd"&gt;   * @return int the multiplied number&lt;/span&gt;
&lt;span class="sd"&gt;   */&lt;/span&gt;
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;multiplyByTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now you want to ensure that your &lt;code&gt;multiplyByTwo&lt;/code&gt;-method works. Therefore you write a unittest:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestCalculator&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;UnitTestCase&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

   &lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt;    * The calculator under test.&lt;/span&gt;
&lt;span class="sd"&gt;    */&lt;/span&gt;
   &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;$_calculator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt;    * This method is run before every test, and can be used to&lt;/span&gt;
&lt;span class="sd"&gt;   * prepare the environment.&lt;/span&gt;
&lt;span class="sd"&gt;   */&lt;/span&gt;
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;_calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Calculator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt;   * This method is run after every test and can be used to clean&lt;/span&gt;
&lt;span class="sd"&gt;   * the environment.&lt;/span&gt;
&lt;span class="sd"&gt;   */&lt;/span&gt;
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;teardown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// nothing to tear down curretly. An example could be to&lt;/span&gt;
    &lt;span class="c1"&gt;// close a database connection&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="sd"&gt;/**&lt;/span&gt;
&lt;span class="sd"&gt;   * Tests the multiplyByTwo method. (this is the actual test)&lt;/span&gt;
&lt;span class="sd"&gt;   */&lt;/span&gt;
  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;testMultiplyByTwo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;_calculator&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;multiplyByTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Explanation: You see three methods in the testclass:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;code&gt;setUp()&lt;/code&gt;: This method is executed before &lt;b&gt;every&lt;/b&gt; testmethod and can be used to prepare your environment. It is very likely that we will add more methods to our calculator which we will also have to test. Therefore we use the &lt;code&gt;setUp()&lt;/code&gt;-method to create an instance of Calculator, so that this is automatically done for every test in the testclass.&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;tearDown()&lt;/code&gt;: is the same as &lt;code&gt;setUp()&lt;/code&gt; but after the testmehod. This can be used to cleanup the environment.&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;testMultiplyByTwo()&lt;/code&gt;: This is the actual test. By convention it is named 'test' followed by a name which describes what the method tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What happens in &lt;code&gt;testMultiplyByTwo&lt;/code&gt; is the following: First we invoke the &lt;code&gt;multiplyByTwo&lt;/code&gt; method on the calculator instance, which was automatically created by the &lt;code&gt;setUp()&lt;/code&gt;-method, and store the result in &lt;var&gt;$result&lt;/var&gt;. Because we give &lt;var&gt;5&lt;/var&gt; as the method's parameter we know the result should be 10, if the method works as expected. This expectation is tested with the &lt;code&gt;assertEqual&lt;/code&gt; method, which takes an expected value (10) and the actual result ($result). As an optional parameter it takes a string in which you can explain your expectation, it will be printed when the test fails and can help you determine what went wrong. The assertEqual method then verifies if both values are equal. If so the test passes, if not your test failes and you know you have a bug in your code! ofcourse there are a lot more &lt;code&gt;assert&lt;/code&gt;-methods, like &lt;code&gt;assertNotNull&lt;/code&gt;, &lt;code&gt;assertTrue&lt;/code&gt;, &lt;code&gt;assertIsA&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;To run your test you can use the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TestCalculator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$test&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;HtmlReporter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If your code works (which is very likely in this example :) ), you'll get a nice green bar, telling you how many tests succeeded. However as soon as one or more tests fail you get an ugly red bar screaming for you attention and telling you how many, and more important, which, tests failed. You can try this by changing the '2' inside &lt;code&gt;multiplyByTwo&lt;/code&gt; in 3 and enjoy the red bar.&lt;/p&gt;

&lt;p&gt;I hope this example has illustrated that you can sleep on both ears if you have enough tests to cover all your code and you keep the bar green :)&lt;/p&gt;</content><category term="PHP"></category></entry></feed>