Close and re-open tickets from tests: Fogbugz PHPUnit TicketListener

When working with unit testing in PHP, you might already be using my favorite PHP unit testing tool PHPUnit. If you are, perhaps you’ve heard of the TicketListener interface. No? Then read on.

Raphael Stolt closes GitHub issues from his PHPUnit tests. Which is cool. He inspired me.

I wrote some code. You can now do the same from FogBugz. (FogBugz is a commercial project management system that includes bug tracking.)

To install this, you can now just head over to the PHPUnit PEAR channel:

pear install phpunit/PHPUnit_TicketListener_Fogbugz

Steps to do it

Have a nice failing test that can re-open your ticket.


< ?
class ExampleClassTest extends PHPUnit_Framework_TestCase
{
    /**
     * @ticket 2
     * @test
     */
    function example() {
        $this->assertTrue(FALSE);
    }

}

Please note the annotation documentation block above the test method: it denotes that the method is a test, and that the issue with ID 2 should be re-opened if this test should ever fail.

Then, set up a continuous integration-specific PHPUnit configuration file (oh, why? Well, this extra checking against the bugs system takes time, and we don’t want to burden our test suite with that):


<phpunit>
  <listeners>
    <listener class="PHPUnit_Extensions_TicketListener_Fogbugz"
              file="../olleolleolle-phpunit/PHPUnit/Extensions/TicketListener/Fogbugz.php">
      <arguments>
        <string>username@xstream.dk</string>
        <string>secretpassword</string>
        <string>http://fogbugz.example.org/api.php</string>
        <boolean>false</boolean>
        <boolean>true</boolean>
        <string>Fixed</string>
        <string>Started</string>
      </arguments>
    </listener>
  </listeners>
</phpunit>

Run the PHPUnit test (or, more realistically, your whole test suite) with the specific configuration, like so:

phpunit -ddisplay_errors=1 --configuration fogbugzlistener.xml ExampleClassTest

Thank you, MacPorts: How to add OpenSSL support to your PHP

Many interesting web APIs require SSL, for instance Google’s GData API. This adds OpenSSL support to your MacPorts-installed PHP, so that you can use the “ssl” transport layer:

sudo port install php5-openssl

Done.

Thanks, jmr_mp in the MacPorts IRC channel, for telling me about the existence of this short and neat patch-applying port, so that I could spare you, dear reader, from an introductory tutorial on how to edit Portfiles that don’t add the bells and whistles that you want. I had one written, but the above is saner. There’s always another hood underneath the hood. You don’t have to fix everything. Some things should just be fixed by the package system.

Here’s that Portfile:

$ port cat php5-openssl
# $Id: Portfile 69619 2010-07-11 03:00:23Z ryandesign@macports.org $

PortSystem              1.0
PortGroup               php5extension 1.0

php5extension.setup     openssl 5.3.2 bundled
revision                1
categories-append       devel security
platforms               darwin
maintainers             ryandesign

description             a PHP interface to OpenSSL signature-generation \
                        and -verification and data-encryption and \
                        -decryption functions

long_description        ${description}

checksums               md5     46f500816125202c48a458d0133254a4 \
                        sha1    79ea4ee3da3a7542d1e348ac963a5b38bcbb4b6b \
                        rmd160  60a8aac0d51511ecaf8dcad9d31bdf072c0c99cf

depends_lib-append      port:openssl

post-extract {
    move ${build.dir}/config0.m4 ${build.dir}/config.m4
}

configure.args-append   --with-openssl=${prefix}

platform macosx {
    configure.args-append --with-kerberos=/usr
}

use_parallel_build      yes

CakePHP: grab only the data you need

When dealing with an abstraction that outputs dynamic SQL “behind your back”, it’s often easy to forget that some of the queries are… too greedy and grab unneeded data. Here is how to ask for less data, using CakePHP.

Today, I listed only name and id fields of a Model, and to restrict the findAll() to those things:

`$data = $this->Thing->findAll( aa("Thing.user_id", $user_id), a("id", "name"), null, null, null, -1 );`

Breaking down the call, we have:

aa() is a CakePHP convenience function to make an associative array. Odd parameters are keys, even params values. The parameters here creates a WHERE clause that matches the current user’s Things.

a() does the same for regular arrays. The params list the two fields we need returned.

A couple of null settings, and then the crucial -1, which turns off the Model associations completely.

Grab less data!

Update: here is how to use unbindModel. First take a look at the Thing model definition, courtesy of Mladen Mihajlovic:


 array(
      ‘className’ => ‘AnotherThing’
    ),
    ‘SomeOtherThing’ => array(
      ‘className’ => ‘SomeOtherThing’
    ),
    ‘YetAnotherThing’ => array(
      ‘className’ => ‘YetAnotherThing’
    )
  );
}
?>

Now, in our controller, before calling findAll, we remove two of the associated Models:


$this->Thing->unbindModel( array( "hasMany" => array( "AnotherThing", "SomeOtherThing" ) ) );

[tags]cakephp[/tags]

Calling an XML-RPC method (PHP, Ruby)

Calling web services. Great fun when it works. Thanks to XML-RPC’s simplicity, it’s quite easy to cobble together a working client. Here is me using the Technorati “ping” service, which tells them about the Copenhagen Ruby Brigade’s weblog being updated. And then, prints the result.

My PHP take on this looks klutzy. But the request gets encoded smartly. cURL takes a lot parameters to get running. I write the appropriate headers myself, there.

Simple XML-RPC in PHP

In Ruby’s standard library, there is a library to do these things. Exciting.

Even simpler XML-RPC in Ruby

[tags]technorati,xmlrpc,ruby,php,curl[/tags]

List of local geek communities (work-in-progress)

I talked with a local PHP developer today, who’d seen my blog, and we talked some CakePHP, frameworks, servers, the general state of scripting languages, and so forth. And then we came to the subject of local communities, where developers meet up and talk shop.

And, as a Copenhagener, I don’t know very much about that. There are these two that I know of:

Is there a PHP club? A Perl club? What other communities for developers have meetings in the Copenhagen area? Are you a member? Are these communities active? Please tell about it here.

CakePHP news: Data Bindings v0.1 Example

You might know that I am involved in the [CakePHP framework](http://www.cakephp.org/) project, and I’m developing web sites with it now.

On the IRC channel today, I was alerted to the existence of the Bindings sister project. Now they have an example up, for all to see. Data Bindings v0.1 Example is a piece of very easy interaction between your PHP and the HTML the user manipulates.

Take a look at the “color box” example – yummy.

Cake PHP framework in alpha

Release announcement for “cake_0.10.0.1217_alpha”, which is more stable than any other release.

Most of all, this release is about making the developer experience more cohesive, and “orthogonal”. Method calls and return values look more or less the same everywhere, and when you grok how Cake hangs together, it becomes easier to make web apps.

I am becoming a bit more expressive with it.

I looked at Perl yesterday, using my Linux laptop. Without perldoc or man pages, it’s difficult broaching a new programming language. When there’s no editor with emacs keybindings, it gets harder. Vi is hard for me. Playing Nethack’s been making it easier lately. I know how to navigate, and the modefulness is becoming less of an enigma. But still, it ain’t like emacs yet. I’m used to jed, a lightweight editor with emacs bindings. (It’s a Debian package, so just apt-get it. That’s what I should do, when I get that laptop online again.)

Now, see you. Been nice writing for you.

Update: just installed the Windows binary for jed. It’s cute, and it’s a lot faster to work with than a terminal window on a remote computer. I don’t know yet if I’ll use it locally.

PEAR news: Validate_DK validates Danish numbers

Rejoice! A PEAR class for Danish numbers is now in alpha: Docs For Class Validate_DK are sparse, but you can see the four methods:

* carReg
* phoneNumber
* postalCode
* ssn

I like this. One person, in this case Jesper Veggerby Hansen, can make a significant contribution to the Open Source world. [An article](http://asay.blogspot.com/2005/09/analyst-nature-and-size-of-open-source.html) I read yesterday told the real story: most projects involve few minds. If you are able to add something (a fine bug report or a piece of documentation), you are golden.

Cheers to Jesper for this micro-project.