Skip to content

Commit

Permalink
Test dispatcher process fixes URI
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Aug 12, 2014
1 parent 9eb5a05 commit e64f620
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Dispatcher/Dispatcher.php
Expand Up @@ -123,7 +123,7 @@ public function process() {
* Gets the name of the requested file in the current directory path, or returns
* the default index filename if the directory is requested.
*/
public function getFilename($uri, $indexFilename, $path) {
public function getFilename($uri, $indexFilename) {
$filename = basename($uri);
if(empty($filename) || substr($uri, -1) === "/") {
$filename = $indexFilename;
Expand Down
76 changes: 64 additions & 12 deletions test/Unit/Dispatcher/PageDispatcher.test.php
Expand Up @@ -12,25 +12,34 @@ class PageDispatcher_Test extends \PHPUnit_Framework_TestCase {
private $tmp;
private $pageViewDir;

private $request;
private $response;
private $apiFactory;
private $dbFactory;

public function setUp() {
$this->tmp = \Gt\Test\Helper::createTmpDir();
$this->pageViewDir = \Gt\Test\Helper::createTmpDir("/src/Page/View");

$cfg = new \Gt\Core\ConfigObj();

$request = $this->getMock("\Gt\Request\Request", null, [
$this->request = $this->getMock("\Gt\Request\Request", null, [
"/", $cfg,
]);
$response = $this->getMock("\Gt\Response\Reponse", null);
$apiFactory = $this->getMock("\Gt\Api\ApiFactory", null, [
$this->response = $this->getMock("\Gt\Response\Reponse", null);
$this->apiFactory = $this->getMock("\Gt\Api\ApiFactory", null, [
$cfg
]);
$dbFactory = $this->getMock("\Gt\Database\DatabaseFactory", null, [
$this->dbFactory = $this->getMock("\Gt\Database\DatabaseFactory", null, [
$cfg
]);

$this->dispatcher = new PageDispatcher(
$request, $response, $apiFactory, $dbFactory);
$this->request,
$this->response,
$this->apiFactory,
$this->dbFactory
);
}

public function tearDown() {
Expand Down Expand Up @@ -220,8 +229,7 @@ public function testCreateResponseContentThrowsTypeException() {
* @dataProvider data_uris
*/
public function testGetFilenameRequestedFromUri($uri) {
$path = $this->pageViewDir;
$filename = $this->dispatcher->getFilename($uri, "index", $path);
$filename = $this->dispatcher->getFilename($uri, "index");

if(substr($uri, -1) === "/") {
$this->assertEquals("index", $filename);
Expand All @@ -231,12 +239,56 @@ public function testGetFilenameRequestedFromUri($uri) {
}
}

// /**
// * @dataProvider data_uris
// */
// public function testDispatcherFixesUri($uri) {
/**
* @dataProvider data_uris
*/
public function testDispatcherProcessFixesUri($uri) {
if($uri === "/") {
// Nothing to correct when empty URI
return;
}

$uriRand = \Gt\Test\Helper::randomiseCase($uri);
$filePath = $this->pageViewDir . $uri;
$dirname = (substr($filePath, -1) === "/")
? $filePath
: dirname($filePath);
$dirname = rtrim($dirname, "/");
$filePath = rtrim($filePath, "/");

// }
if(!is_dir($dirname) ) {
mkdir($dirname, 0775, true);
}

if(is_dir($filePath)) {
$index = \Gt\Test\Helper::randomiseCase("index");
file_put_contents($filePath . "/$index.test", "dummy data ($uri)");
$uri .= "index";
}
else {
file_put_contents($filePath, "dummy data ($uri)");
}

$request = new \StdClass();
$request->forceExtension = false;
$request->indexFilename = "index";
$request->uri = $uriRand;

$this->dispatcher = new PageDispatcher(
$request,
$this->response,
$this->apiFactory,
$this->dbFactory
);


$fixedUri = $this->dispatcher->process();
$this->assertInternalType("string", $fixedUri);
$this->assertEquals(
strtok(strtolower($fixedUri), "."),
strtok(strtolower($uriRand), ".")
);
}

public function testDispatcherFlushes() {
$html = "<!doctype html><h1>Test</h1>";
Expand Down

0 comments on commit e64f620

Please sign in to comment.