New Friend on the TDD Block: PHPAutotest with Behat and et al

So here I will present a snapshot of what it looks like to work with PHPAutotest:

./autotest.php behat demo/Behat/features/minesweeper.feature

Then we shall see:

And the icon on the right corner notifying the current test is passing:

Should we change and save file or press ‘r’ on the terminal we would get a red non passing test:

The little gadget will wait for you to fix this on your next iteration:

credits to its authors at https://github.com/Programania/PHPAutotest.

PHPSpec Autorun-Test Notifier

This article here basically tells us to do:

sudo apt-get install libnotify-bin

This other article pushes us to install also:

sudo apt-get install inotify-tools

Now we would like to modify the basic phpunit monitor script autotest-phpunit in a way that works for phpspec command. Wow after a whole day of work we end up collaborating on a github project and the result was:

https://github.com/Programania/PHPAutotest

Now you can ejoy all the flavors! And you are welcome to participate. We are now working on a bundle version.

Symfony2: Discussion on Before and After Methods for Controllers

There is a nice article written here about implementing some hooks for controllers right before and after processing the controller. This is ideal for RESTful APIs that would like to authenticate or provide execution of tasks for requests meeting certain criteria such as certain tokens on the parameters sent with the request.

There are several proposals to the solution, and really just reading the blog post and the github entry one can see the value on these approaches. There is even use of functional programming in php in one of the proposals. That is the use of closures to turn the controller and pass it as a closure with some pre- and after-logic details. There is also another approach that meets some demands in terms of checking the implementation of certain interfaces. The truth of the matter is that all of these approaches can be combined. That is the power of good design in PHP. Learning about these ideas and approaches is just what we are here for after all.

PHP Pre-commit Hook

I am commenting the code for a pre-commit I found in php here. The idea is that we would like to embed these type of pre-commits or other type of hooks in a way that is automatized through a DevelopmentBundle as described in an earlier post here.

#!/usr/bin/php
<?php
 
// setup up variables for input output, namely an output array and resource control temporary variable
$output = array();
$rc     = 0;
// gets the sha1 for the previous to the last current commit if not the head
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);
// if resource controle is found then use HEAD else use an unknown sha1
if ($rc == 0)  $against = 'HEAD';
else           $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
// gets the list of the files that have changed
exec('git diff-index --cached --name-only '. $against, $output);
// match expression to make sure files are php type
$needle            = '/(\.php|\.module|\.install)$/';
$exit_status = 0;
// loop over the file list
foreach ($output as $file) {
        if (!preg_match($needle, $file)) {
                // only check php files
                continue;
        }
 
        $phpcs_output = array();
        $rc              = 0;
        // calls phpcs CodeSniffer on each file
	exec('phpcs '. escapeshellarg($file), $phpcs_output, $rc);
        if ($rc == 0) {
                continue;
        }
        // outputs errors or warnings
	echo implode("\n", $phpcs_output), "\n";
        // impede the commit
        $exit_status = 1;
}

Now when doing a git commit -a phpcs will be run on the file(s) with changes:

cordoval@ms:~/sites-2/$ git commit -a
 
FILE: ...ordoval/sites-2/MyClass.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
 39 | ERROR | Multi-line function call not indented correctly; expected 8
    |       | spaces but found 12
 41 | ERROR | Multi-line function call not indented correctly; expected 8
    |       | spaces but found 12
 43 | ERROR | Multi-line function call not indented correctly; expected 8
    |       | spaces but found 12
 45 | ERROR | Multi-line function call not indented correctly; expected 8
    |       | spaces but found 12
 47 | ERROR | Multi-line function call not indented correctly; expected 8
    |       | spaces but found 12
--------------------------------------------------------------------------------
 
Time: 0 seconds, Memory: 6.75Mb

After the problems are resolved git commit -a should proceed to commit the changes.