RSS
 

Archive for July, 2012

CakePHP and PHPUnit

21 Jul

Earlier I wrote about some PHPUnit tips around Cake2. At that time the old SimpleTest testing framework had just been replaced by PHPUnit. So everything around this new framework was fairly new – especially to be.

I manually installed/downloaded the test suite files since I didnt want to install any PEAR stuff – on windows this is pretty annoying anyway. So is there a quick way to always be up to date with PHPUnit? Yes, there is. Since it is now pretty stable I want to introduce the PHPUnit plugin, which I developed together with Hyra.

Installing PHPUnit – the easy way

It never was easier. All you need for it is the PHPUnit plugin. Just open your console in your app directory and type

Phpunit.Phpunit install

Select your destination (APP or ROOT vendors) and done. This will download and extract all the necessary files, and put them in your specified Vendor folder.

Make sure you got CakePlugin::loadAll() – or specifically CakePlugin::load(‘Phpunit’) in your bootstrap! Otherwise the plugin will not be available. You can now use PHPUnit through the CLI or your favourite browser right away.

See the documentation of the plugin for details. Also note the convenience tasks (‘packages’, ‘info’) to check for updates and update the plugin shell if necessary. It also displays the so far unused pear packages.

Autoload

If you have it installed in your ROOT vendors and get some include warnings while baking put this at the top of the VENDORS/PHPUnit/Autoload.php file:

set_include_path(get_include_path().PATH_SEPARATOR.dirname(dirname(__FILE__)));

This way the vendors folder itself is also an include path and those warnings will go away.

Further setup and usage

For this I can just refer you to the official documentation.

Appendix

In my case just now, info printed the following:

# PHPUnit 3.6.11
        OK (v3.6.11)
# File Iterator 1.3.1
        OK (v1.3.1)
# Text Template 1.1.1
        OK (v1.1.1)
# PHP CodeCoverage 1.1.3
        OK (v1.1.3)
# PHP Timer 1.0.2
        OK (v1.0.2)
# PHPUnit MockObject 1.1.1
        OK (v1.1.1)
# PHP TokenStream 1.1.3
        OK (v1.1.3)
# DbUnit 1.1.2
        OK (v1.1.2)
# PHPUnit Story 1.0.0
        OK (v1.0.0)
# PHPUnit Selenium 1.2.7
        OK (v1.2.7)
# PHPUnit TicketListener GitHub 1.0.0
        OK (v1.0.0)
---------------------------------------------------------------
Unused pear packages:
# FinderFacade (v1.0.1)
# Object_Freezer (v1.0.0)
# PHPUnit_SkeletonGenerator (v1.1.0)
# PHPUnit_TestListener_DBUS (v1.0.0)
# PHPUnit_TestListener_XHProf (v1.0.0)
# PHPUnit_TicketListener_Fogbugz (v1.0.0)
# PHPUnit_TicketListener_GoogleCode (v1.0.0)
# PHPUnit_TicketListener_Trac (v1.0.0)
# PHP_CodeBrowser (v1.0.2)
# PHP_Invoker (v1.1.0)
# bytekit (v1.1.2)
# hphpa (v1.2.2)
# phpcov (v1.0.0)
# phpcpd (v1.3.5)
# phpdcd (v0.9.3)
# phploc (v1.6.4)
# ppw (v1.0.4)
# test_helpers (v1.1.0)
 
3 Comments

Posted in CakePHP

 

Cakephp and Googl Url Shortener

14 Jul

This can come in handy if you send out tweats, sms or other texts which should stay as short as possible and could contain urls. But it can also be used for html emails to track visitor clicks (using the googl analytics overview for those urls). There are many use cases where such a shortening service might be useful. The googl one is fast and reliable – and for now still without costs.

The source file can be found in my Tools plugin: GooglLib.php

Setup

If you have an API key (which is recommended for the use with analytics and high traffic pages which generate a lot of short urls) define it in your configs:

Configure::write('Googl.key', 'YOURKEY');

Usage

App::uses('GooglLib', 'Tools.Lib');
$this->Googl = new GooglLib();
 
$longUrl = 'http://www.spiegel.de'; 
$result = $this->Googl->getShort($longUrl);
$shortUrl = $result['id'];

Since this is a lib you can use it anywhere in your application: In the view (inside a helper maybe) or in the model prior to saving or as a behavior for on demand replacing etc.

If you need to get the original url from a short url:

$result = $this->Googl->getLong($shortUrl);
$longUrl = $result['longUrl'];

Other methods

You can use the statisticsUrl method to get an url to the analytics backend for this specific short url.

The getHistory would need oAuth/oacurl to work and is not yet implemented. It would return the analytics data from the above link as an array.

 
No Comments

Posted in CakePHP