PHPUnit 3.5: Less $this Required

Sebastian Bergmann » 16 August 2010 » in New Features » 31 Comments

The feature discussed below has been removed from PHPUnit due to community feedback.

Over the years, I have gotten quite a few "complaints" from PHPUnit users that they do not like typing $this-> as often as they have to:

<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));
 
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));
 
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>

As of PHPUnit 3.5, they can write test code that requires less $this-> statements:

<?php
require_once 'PHPUnit/Framework/Assert/Functions.php';
 
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        assertEquals(0, count($stack));
 
        array_push($stack, 'foo');
        assertEquals('foo', $stack[count($stack)-1]);
        assertEquals(1, count($stack));
 
        assertEquals('foo', array_pop($stack));
        assertEquals(0, count($stack));
    }
}
?>

Here's hoping that this makes some people happy :-)

Defined tags for this entry:

PHP Summit in Düsseldorf

Sebastian Bergmann » 04 August 2010 » in Events » 0 Comments

PHP Summit

This blog posting is in German as the event it relates to is German-only.
Sorry for the inconvenience.

Der PHP SUMMIT ist eine neue und einzigartige Veranstaltung, mit dem Anspruch, in drei Tagen alle wichtigen PHP-Themen in kompakter Form zu vermitteln. Sie müssen sich dabei nur noch entscheiden, welche persönlichen Themenschwerpunkte Sie setzen möchten. Sie können aus insgesamt 18 intensiven und interaktiven Power Workshops auswählen.

Sämtliche Workshops beziehen sich auf die tägliche Projektarbeit und zeigen Ihnen den produktiven Live-Einsatz von Tools und Methoden. Hohe Interaktion mit den Teilnehmern, Live-Coding statt vorgefertigte foo/bar-Beispiele, Informationen über neueste Trends in der PHP-Entwicklung — alles gewürzt mit einer guten Prise Humor — das sind die einzigartigen Merkmale des PHP SUMMIT. Hier profitieren Sie vom geballten Wissen und der Praxiserfahrung von drei der bedeutendsten deutschsprachigen PHP-Experten.

Am Montag- und Dienstagabend finden jeweils die Speakerpanel statt. Erleben Sie wie Arne Blankerts, Sebastian Bergmann und Stefan Priebsch gemeinsam eine Code-Review-Session durchführen — auch das sollten Sie auf keinen Fall verpassen!

Melden Sie sich noch heute an, um sich Ihren Platz zu reservieren!

Defined tags for this entry:

Using HipHop for Static Analysis

Sebastian Bergmann » 27 July 2010 » in Articles » 0 Comments

HipHop for PHP, the source code transformer that turns PHP code into C++ code that can then be compiled with g++, can also be used for static code analysis to find problems in PHP source code.

sb@vmware Money % hphp -t analyze --input-dir .
running hphp...
creating temporary directory /tmp/hphp_Zz7AXg ...
parsing inputs...
parsing inputs took 0'00" (20 ms) wall time
inferring types...
inferring types took 0'00" (10 ms) wall time
saving code errors, dependency graph and stats...
all files saved in /tmp/hphp_Zz7AXg ...
running hphp took 0'00" (208 ms) wall time

The script below takes a CodeErrors.js file (which is generated by hphp and in the example above is saved to /tmp/hphp_Zz7AXg) as its input and print an XML document in Checkstyle's format (the same XML format that is also used by PHP_CodeSniffer, for instance). This XML logfile can then be used with Hudson, for instance, in a continuous integration context.

Defined tags for this entry: , ,

GTAC 2010

Sebastian Bergmann » 23 July 2010 » in Presentations » 1 Comment

I have been invited to attend the 5th Annual Google Test Automation Conference, better known as GTAC, in Hyderabad, India in October. I am very much looking forward to discuss cutting edge challenges in test automation and evaluate potential solutions, especially with this year's focus on testability.

Another thing I like about this year's GTAC is that the participants are responsible for selecting the presentations for the conference. Here is my submission:

Challenges in Unit Testing PHP Applications

According to TIOBE, PHP is the most popular programming language after C/C++ and Java. The language has made strong inroads into large-scale, business-critical Web systems. In the six years since the release of PHP 5 -- which not only kickstarted the development of PHP-based frameworks for Web development but also the development of tools for dynamic and static testing techniques -- the PHP community as a whole has developed an increasing interest in developing software that delivers the best possible quality.

When PHP developers start to write unit tests they rarely find themselves without any constraints that are imposed by prior work of less than optimal quality. It is a well-known fact that writing unit tests for legacy code is hard. In the case of PHP it can be even harder: the legacy code has not only been written without testability in mind, but it may have been written for earlier versions of PHP that encouraged practices that make the code next to impossible to unit test.

PHPUnit, the de-facto standard framework for unit-testing PHP code, has some unique features not found in other xUnit test frameworks that allow the testing of untestable code. While developers should not use these features (as they are not required when writing tests for testable code), these features ease the pain of writing tests for legacy code and thus help developers get started with unit testing before they refactor the code for testability.

This session, presented by the creator of PHPUnit, highlights the challenges developers are facing when unit testing legacy PHP code. Some of these challenges will be familiar to developers that use other programming languages such as Java but they will see a new perspective on the problem and different approaches to solve it.

Although I am hoping, of course, that my submission will be accepted by my peers, I know that GTAC will be valuable for me even if I do not get to present: the "hallway track" of GTAC 2008 was amazing.

Defined tags for this entry: ,

PHPUnit 3.5: Refactoring to Components

Sebastian Bergmann » 23 July 2010 » in Articles » 1 Comment

When you look at the list of changes for PHPUnit 3.5, you will see that many of them deal with refactoring to components. Here is an overview of these new components:

  • PHP_CodeCoverage

    The collection, processing, and rendering of code coverage information has been factored out into a separate component. A bit more information can be found here.

  • PHPUnit_MockObject

    The functionality to automatically generate an object that can act as a test double for a specified original class has been factored out into a separate component. Do not worry, getMock() and related methods will work just as they did in previous versions of PHPUnit. The refactoring, however, makes the usage of other mock object libraries (such as Mockery or Phake, for instance) easier.

  • DbUnit

    The database testing functionality that is provided by the DbUnit extension and the respective DatabaseTestCase class has been moved to a separate component. Michael Lively Jr, the author of DbUnit, is now able to make releases of the database testing extension on a release schedule that is separate from PHPUnit itself, meaning that I will be even less involved in its development than I have before.

  • PHPUnit_Selenium

    SeleniumTestCase, the Selenium RC integration for PHPUnit, has been moved to a separate component to allow a release cycle separate from PHPUnit itself.

  • File_Iterator, PHP_Timer, Text_Template

    Utility methods to collect files and report resource usage as well as a simple templating mechanism have been moved to separate components so that they can be reused by other tools such as phpcb, phpcs, phpcpd, and pdepend.

Defined tags for this entry: