Nov
15

PHPUnit Follow-up: Code Coverage with Xdebug on Mac

It took me considerable amount of time to figure out the correct file to edit in order to have Xdebug work from the command line with PHPUnit's --report flag. This flag generates HTML files that look something like the following image:

code coverage of ASRA 2.0 with Xdebug

This displays the number of lines that have been run in the process of unit testing; and in a lovely readable format (see Xdebug's normal format to see how much more useful this is). To get PHPUnit and Xdebug working, start by following the tutorial in the previous article. Then to get Xdebug running, check out this post by Felx Geisendörfer. Follow steps 1 + 2. If you are using regular-old MAMP, 3 + 4 should get you going just fine. The problem I had was I am using MAMP Pro, so I was editing my "template" .ini file thinking that was where the lines needed to go. Xdebug was loaded as a browser module, but was not loaded when I ran the coverage report in PHPUnit. So, in Pro, you have to add the config options to /Applications/MAMP/conf/php5. This is the ini file that the CLI version of PHP uses.

Here are the lines I added to get it working. Note that you may also need to disable the ZendExtensionManager and the zend_optimizer extension for it to work..in MAMP just uncheck the box. Check the compatibility section on the install page for more.

[Zend]
;zend_optimizer.optimization_level=15
;zend_extension_manager.optimizer=/Applications/MAMP/bin/php5/zend/lib/Optimizer-3.3.3
;zend_optimizer.version=3.3.3
;zend_extension=/Applications/MAMP/bin/php5/zend/lib/ZendExtensionManager.so

[xdebug]
zend_extension=/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/xdebug.so
xdebug.remote_enable = 1

If you completed the other article, PHPUnit should be using MAMP's php, which should now have Xdebug rocking and you can do something like:

# 'report' is the name of the folder the html will output to
$ phpunit --report report My_Test 
Aug
21

Ignoring Files in TextMate's TODO Plugin

Lately, as I’ve been working on a huge Zend Framework library for rapid website development and content management, as well as the second version of my ASRA framework, I’ve been very into using Soryu’s TextMate TODO plugin. Trouble is, when you have the entire Zend library in your project (or any other huge library), you’re bound to have a ton of todo’s in your list that you don’t really “care” about.

Anyway, I just learned a super easy way to speed up and make things easier to maintain. Simply open the TextMate preferences menu and go to Advanced > Shell Variables. Add a new entry called TM_TODO_IGNORE and give it a value of Zend* (or even just Zend should work). The TM_TODO_IGNORE value just specifies a regular expression for files paths to ignore within your project, so basically this just ignores everything in the Zend library. Yay! Concise to-do list!

Jul
20

Installing and Using PHPUnit with MAMP

Unit testing is becoming more and more popular in every day web development. I decided to see what all the fuss was about. So here's a quick tutorial on getting PHPUnit up and running with MAMP on OSX.

First you need to grab a PHPUnit release. You need a command line utility called pear-phpunit to run tests and also the PHP files that make up the framework. I recommend first doing an svn export somewhere to get the command line script and then doing another export to just export the framework library.

MAMP's default include path is /Applications/MAMP/bin/php5/lib/php and the Pear installation is there already so that's where you should install PHPUnit. So, let's start by grabbing the PHPUnit Framework:

$ cd /Applications/MAMP/bin/php5/lib/php
$ svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/PHPUnit
# ...
# Exported revision 4387.

You can use their Trac Browser to decide which release you want to work with. If you ever want to upgrade, you can just delete this folder and export a different release.

In the resulting folder, open PHPUnit/Util/Fileloader.php and do a find and replace for @php_bin@ with the path to your PHP command-line interpreter which for php5 in MAMP should be /Applications/MAMP/bin/php5/bin/php.

Next export or download the command line script:

svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/pear-phpunit

Open pear-phpuint and replace @php_bin@ with /Applications/MAMP/bin/php5/bin/php (same as the earlier file).

Next, copy the pear-phpunit executable to a location that exists in your unix PATH environment variable. My PATH already included a /usr/local/bin and I had installed a different version of Pear there earlier so I went ahead and moved it there (you may have to create a directory or put it in another directory in your path). At this point you should rename the file to "phpunit" as well. You can accomplish this via command line or (as I like to sometimes do) go to Finder > Go > Go To Folder... and type in /usr. This will allow you to use the OS GUI to copy the file over and rename it (providing you enter your password). Now you have to make the file executable to be able to run it:

sudo chmod +x /usr/local/bin/phpunit

So at this point you should have:

  • a) The PHPUnit framework in /Applications/MAMP/bin/php5/lib/php/PHPUnit
  • b) The phpunit executable moved, renamed and chmod'ed in /usr/local/bin/phpunit

If everything worked correctly, you should be able to type "phpunit" anywhere and see the "useage" information. I used the following link to set this up, so refer to that if you have any other troubles.

Here's my first couple tests for testing the Json export class in the in-progress Asra 2.0. Though I would not see myself doing this for "normal" websites, I can see how useful it is in application development. I already found a missing required class and a possible bug in just creating this simple test case.

<?php

/**
* test setup ~ /tests/Tests.php
*/
require_once dirname(__FILE__) . '/../../../Tests.php';

/**
* @see Asra.Export
*/
require_once 'Asra/Export/Data.php';
require_once 'Asra/Export/Format/Json.php';

/**
* @see PHPUnit_Framework
*/
require_once 'PHPUnit/Framework.php';

class Asra_Export_Format_JsonTest extends PHPUnit_Framework_TestCase
{
    /**
     * assert that export returns json and is valid json
     */
    public function testExportData()
    {
        $array = array("hello, world!", "hello, again!");
        $data = new Asra_Export_Data($array);
        $format = new Asra_Export_Format_Json($data);
        $this->assertNotNull($format->export());
        $this->assertNotNull(json_decode($format->export()));
    }

    /**
     * assert that passing a non-array to format throws an exception
     */
    public function testDataNotArray()
    {
        //$this->setExpectedException('Asra_Export_Exception');
        $data = "This is not an array!";
        try
        {
            $format = new Asra_Export_Format_Json($data);
        }
        catch (Asra_Export_Exception $e)
        {
            return;
        }
        $this->fail("An expected exception has not been raised.");
    }
}
Jun
06

@see TextMate Snippet

I've been doing a ton of work with the newest Zend Framework version 1.8.2 this weekend. I've found that typing out all the requires for each class can be a pain, so I made a quick TextMate snippet to make adding @see directives for phpdoc at the same time as the require_once directive. Here it is; simple but useful:

/**
 * @see $1
 */
require_once "${1/_///g}.php";$0

Add this as a "Tab Trigger" for the word "see" then just type "see" and hit tab then type your class name. It will auto fill the comment and the path to the pear-conventionally-named .php file. Got any other useful TextMate snippets? Please do share!

Jan
01

Custom Key Bindings in XCode for TextMate Users

I’ve recently been getting doing some development in XCode and Interface Builder (learning the iPhone SDK). I found XCode a real pleasure to work with (especially its unique form of “Code Sense”), but I did miss some of TextMate’s default text shortcuts, specifically “delete line” and “duplicate line.” There are some Key Binding options in the Preferences but nothing like those.

I asked a question on StackOverflow, a great new wiki/forum/help site for programmers and was pointed to the Cocoa docs and some useful posts where I figured out how to put together these simple key bindings. Results follow.

Create the file ~/Library/KeyBindings/PBKeyBinding.dict if it doesn’t exist and add the following to the file:

{
    "^$K" = (
        "selectLine:",
        "cut:"
    );
    "^$D" = (
        "selectLine:",
        "copy:",
        "moveToEndOfLine:",
        "insertNewline:",
        "paste:"
    );
}

The above snippet creates a key binding for “^$K” (Control-Shift-K – aka TextMate’s “delete line”) and “^$D” (Control-Shift-D – AKA TextMate’s “duplicate line”). You can see there’s loads of different macro’s you can add to this file to make unique key binding snippets. Note that they won’t take effect until you restart XCode.

prev 1 2 3 4 5 next

Products

Goodies

T1EOS

Content Management System T1EOS: Content Management System

Our customized content management framework T1EOS manages articles, blogs, categories, events, tagging, images & galleries, a Facebook Connect integrated commenting system, and more — all out-of-the-box.

github

ASRA

A Simple Restful API

ASRA is a lightweight package that assists in the rapid development of simple APIs for exporting data for Flash, Flex or other applications.

Download

Plum Dumb

A Typeoneerror TextMate Theme Plum Dumb