NYCPHP Meetup

NYPHP.org

[nycphp-talk] unit testing, was Survey: Development environment....

Ajai Khattri ajai at bitblit.net
Thu May 7 11:30:26 EDT 2009


On Wed, 6 May 2009, David Mintz wrote:

> It's a little
> harder to create simulated http environments complete with cookies and
> sessions

My usual answer: it depends :-)

Some PHP frameworks, as well supporting unit tests, go a little further 
and provide functional testing which simulates almost a full browsing 
session, 

e.g. in symfony:

// Create a new test browser
$browser = new sfTestBrowser();
 
$browser->
  get('/foobar/index')->
  isStatusCode(200)->
  isRequestParameter('module', 'foobar')->
  isRequestParameter('action', 'index')->
  checkResponseElement('body', '!/This is a temporary page/');


To test for cookies:

$b->test()->is($request->getCookie('foo'), 'bar');     // Incoming cookie
$cookies = $response->getCookies();
$b->test()->is($cookies['foo'], 'foo=bar');            // Outgoing cookie


To test redirects:

$b->
    get('/foobar/edit/id/1')->
    click('go', array('name' => 'dummy'))->
    isStatusCode(200)->
    isRequestParameter('module', 'foobar')->
    isRequestParameter('action', 'update')->
 
    isRedirected()->      // Check that the response is a redirect
    followRedirect()->    // Manually follow the redirection
 
    isStatusCode(200)->
    isRequestParameter('module', 'foobar')->
    isRequestParameter('action', 'show');


To check for certain elements in the DOM:

b->get('/foobar/edit/id/1');
$dom = $b->getResponseDom();
$b->test()->is($dom->getElementsByTagName('input')->item(1)->getAttribute('type'),'text');



I dont know about the Zend Library but symfony has had this for years 
which goes a long way towards encouraging TDD.


-- 
Aj.





More information about the talk mailing list