Currently I am trying to refresh and expand my knowledge of Zend Framework 2 and I'm looking at Zend's User Guide, specifically the page on Routing and Controllers.
Seeing four almost identical test functions for asserting actions can be accessed offended my notion of best practice, so I rewrote the last four methods, adding in a fifth as a helper, like so:
private function assertActionCanBeAccessed ($action)
{
$this->routeMatch->setParam('action', $action);
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals (200, $response->getStatusCode());
}
public function testAddActionCanBeAccessed() { $this->assertActionCanBeAccessed('add'); }
public function testDeleteActionCanBeAccessed() { $this->assertActionCanBeAccessed('delete'); }
public function testEditActionCanBeAccessed() { $this->assertActionCanBeAccessed('edit'); }
public function testIndexActionCanBeAccessed() { $this->assertActionCanBeAccessed('index'); }
When I ran PHPUnit, this worked fine.
But it seems to me that this method would likely be useful for other controllers. And, besides, I just want to know how to make methods commonly available to throughout my code.
So I wrote the following class:
<?php
class ActionTestToolkit
{
public static function assertActionCanBeAccessed ($testcase, $action)
{
$testcase->routeMatch->setParam('action', $action);
$result = $testcase->controller->dispatch($testcase->request);
$response = $testcase->controller->getResponse();
$testcase->assertEquals (200, $response->getStatusCode());
}
}
?>
... and saved it to vendor/Flux/library/ActionTestToolkit
Without Zend Framework, I would have just used require_once
, but I'm finding it impossible to get the path right within this tangled web of files. And Googling the subject seems to suggest that maybe I should be doing something with the autoloader
Can someone please tell me exactly what code I should/must add to
- public/index.php
- module/Album/test/AlbumTest/Controller/AlbumControllerTest.php
- and/or any other file(s)
in order that I can replace the lines
public function testAddActionCanBeAccessed()
{ $this->assertActionCanBeAccessed('add'); }
with
public function testAddActionCanBeAccessed()
{ ActionTestToolkit::assertActionCanBeAccessed($this, 'add'); }
This has been driving me nuts all evening, so thanks in advance!