Skip to content

Commit

Permalink
Begin test for path from uri
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Aug 11, 2014
1 parent 4a4cb4a commit 5b69e86
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Response/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NotFoundException extends \Gt\Core\Exception\GtException {

public function __construct() {
http_response_code(404);
call_user_func_array([parent, "construct"], func_get_args());
call_user_func_array([$this, "parent::" . __FUNCTION__], func_get_args());
}

}#
35 changes: 35 additions & 0 deletions test/Unit/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@

class Helper {

/**
* Creates a directory in the system's temporary directory, with an optionally
* given subdirectory, and returns the path.
*
* @param string|null $dir Name of the subdirectory to create
*
* @return string Absolute path to newly created directory
*/
public static function createTmpDir($dir = null) {
$tmp = sys_get_temp_dir() . "/gt-temp-test";
$_SERVER["DOCUMENT_ROOT"] = $tmp . "/www";

$path = $tmp;

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

if(!is_null($dir)) {
// Ensure starting slash.
if(substr($dir, 0, 1) !== "/") {
$dir = "/" . $dir;
}

$path .= $dir;

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


return $path;
}

/**
* Recursive function to empty and remove a whole directory.
*
Expand Down
6 changes: 1 addition & 5 deletions test/Unit/Core/Path.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ class Path_Test extends \PHPUnit_Framework_TestCase {
private $src;

public function setup() {
$this->tmp = sys_get_temp_dir() . "/gt-temp-path-test";
if(!is_dir($this->tmp)) {
mkdir($this->tmp, 0775, true);
}

$this->tmp = \Gt\Test\Helper::createTmpDir();
$this->root = $this->tmp . "/www";
$this->src = $this->tmp . "/src";
$_SERVER["DOCUMENT_ROOT"] = $this->tmp . "/www";
Expand Down
88 changes: 83 additions & 5 deletions test/Unit/Dispatcher/PageDispatcher.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
class PageDispatcher_Test extends \PHPUnit_Framework_TestCase {

private $dispatcher;
private $tmp;
private $pageViewDir;

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, [
"/", $cfg,
]);
$response = $this->getMock("\Gt\Response\Reponse", null);/*, [
$cfg
]);*/
$response = $this->getMock("\Gt\Response\Reponse", null);
$apiFactory = $this->getMock("\Gt\Api\ApiFactory", null, [
$cfg
]);
Expand All @@ -30,9 +33,84 @@ public function setUp() {
$request, $response, $apiFactory, $dbFactory);
}

public function tearDown() {
// \Gt\Test\Helper::cleanup($this->tmp);
}

private $uriList = [
"/",
"/index",
"/one",
"/two",
"/three-four-five",
"/directory/",
"/directory/inner-file",
"/directory/nested/double-inner-file",
];

public function data_uris() {
$return = [];

foreach ($this->uriList as $uri) {
$return []= [$uri];
$return []= [$uri . ".html"];
$return []= [$uri . ".json"];
$return []= [$uri . ".jpg"];
}

return $return;
}

public function testDispatcherCreated() {
$this->assertInstanceOf("\Gt\Dispatcher\PageDispatcher",
$this->dispatcher);
$this->assertInstanceOf("\Gt\Dispatcher\PageDispatcher", $this->dispatcher);
}

/**
* @dataProvider data_uris
*/
public function testGetPathThrowsExceptionWhenNoDirectoryExists($uri) {
$this->setExpectedException("\Gt\Response\NotFoundException");
$this->dispatcher->getPath($uri . "/does/not/exist", $fixedUri);
}

/**
* @dataProvider data_uris
*/
public function testGetPathFromUri($uri) {
$filePath = $this->pageViewDir . $uri;
if(!is_dir(dirname($filePath)) ) {
mkdir(dirname($filePath), 0775, true);
}

var_dump(is_dir($filePath));

if(is_dir($filePath)) {
file_put_contents($filePath . "/index.test", "dummy data");
$uri .= "index";
}
else {
var_dump($filePath);
file_put_contents($filePath, "dummy data");
}

$path = $this->dispatcher->getPath($uri, $fixedUri);
$this->assertInternalType("string", $path);
}

// public function testGetPathFixesUri() {

// }

// public function testLoadSourceFromPath() {

// }

// public function testCreateResponseContentFromHtml() {

// }

// public function testGetFilenameRequestedFromUri() {
// // Or index filename if none set.
// }

}#

0 comments on commit 5b69e86

Please sign in to comment.