<commit_before><?php
/**
 * PHP.Gt (http://php.gt)
 * @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
 * @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
 */
namespace Gt\Cli;

class Gateway_Test extends \PHPUnit_Framework_TestCase {

const DUMMY_CONTENT = "!!! DUMMY CONTENT !!!";
const TEMP_PREFIX = "gt-test-phpgt-tmp";

private $uriTestArray = [
	"/test.txt",
	"/Script/test-app.js",
	"/Script/test-app.js?v=123",
	"/Script/Lib/jquery.js",
	"/Asset/Img/Cat.jpg",
];
private $tempDir;

public function setUp() {
	// Temporary directory required to serve static files from.
	$this->tempDir = $this->createTempDir();
}

public function tearDown() {
	$this->cleanup($this->tempDir);
}

public function testStaticFileRequest() {
	// Check that Gateway understands strange queryStrings.
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg"));
	$this->assertTrue(Gateway::isStaticFileRequest("/directory/image.jpg"));
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg?query=string"));
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg?query=string"));
	$this->assertTrue(Gateway::isStaticFileRequest("/t.txt?a=1/2/3/"));	
}

public function testDynamicRequest() {
	$this->assertFalse(Gateway::isStaticFileRequest("/"));
	$this->assertFalse(Gateway::isStaticFileRequest("/page"));
	$this->assertFalse(Gateway::isStaticFileRequest("/directory/page"));
	$this->assertFalse(Gateway::isStaticFileRequest("/?query=string"));
	$this->assertFalse(Gateway::isStaticFileRequest("/example?query=string"));
	// Default pathinfo function is confused by this request. strtok is now 
	// used within Gateway.
	$this->assertFalse(Gateway::isStaticFileRequest(
		"/example?query=string&file=picture.jpg"));
}

public function testGetAbsoluteFilePath() {
	// Fake a document root, so the server object thinks there is an application
	// in use.
	$docRoot = "/dev/null/test-app/www";
	$_SERVER = ["DOCUMENT_ROOT" => $docRoot];

	foreach ($this->uriTestArray as $uri) {
		// Build expected full path to check against.
		$fullPath = $docRoot . $uri;

		$this->assertEquals(
			$fullPath,
			Gateway::getAbsoluteFilePath($uri)
		);
	}

	unset($_SERVER);
}

public function testServeStaticFile() {
	$expected = "";

	foreach ($this->uriTestArray as $uri) {
		$path = $this->getTempFilePath($uri);

		$expected .= self::DUMMY_CONTENT . " (from $uri).";
		Gateway::serveStaticFile($path);
	}
	$this->expectOutputString($expected);
}

public function brokenPathList() {
	$pathList = [];
	foreach ($this->uriTestArray as $uri) {
		$path = $this->getTempFilePath($uri);
		$pathList []= ["$path/"];
		$pathList []= ["/$path/"];
		$pathList []= ["?$path"];
		$pathList []= ["$path$path"];
		$pathList []= ["\\$path"];
	}
	return $pathList;
}

/**
 * @expectedException \Gt\Response\NotFoundException
 * @dataProvider brokenPathList<commit_msg>Add data prefix to dataprovider<commit_after><?php
/**
 * PHP.Gt (http://php.gt)
 * @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
 * @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
 */
namespace Gt\Cli;

class Gateway_Test extends \PHPUnit_Framework_TestCase {

const DUMMY_CONTENT = "!!! DUMMY CONTENT !!!";
const TEMP_PREFIX = "gt-test-phpgt-tmp";

private $uriTestArray = [
	"/test.txt",
	"/Script/test-app.js",
	"/Script/test-app.js?v=123",
	"/Script/Lib/jquery.js",
	"/Asset/Img/Cat.jpg",
];
private $tempDir;

public function setUp() {
	// Temporary directory required to serve static files from.
	$this->tempDir = $this->createTempDir();
}

public function tearDown() {
	$this->cleanup($this->tempDir);
}

public function testStaticFileRequest() {
	// Check that Gateway understands strange queryStrings.
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg"));
	$this->assertTrue(Gateway::isStaticFileRequest("/directory/image.jpg"));
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg?query=string"));
	$this->assertTrue(Gateway::isStaticFileRequest("/image.jpg?query=string"));
	$this->assertTrue(Gateway::isStaticFileRequest("/t.txt?a=1/2/3/"));	
}

public function testDynamicRequest() {
	$this->assertFalse(Gateway::isStaticFileRequest("/"));
	$this->assertFalse(Gateway::isStaticFileRequest("/page"));
	$this->assertFalse(Gateway::isStaticFileRequest("/directory/page"));
	$this->assertFalse(Gateway::isStaticFileRequest("/?query=string"));
	$this->assertFalse(Gateway::isStaticFileRequest("/example?query=string"));
	// Default pathinfo function is confused by this request. strtok is now 
	// used within Gateway.
	$this->assertFalse(Gateway::isStaticFileRequest(
		"/example?query=string&file=picture.jpg"));
}

public function testGetAbsoluteFilePath() {
	// Fake a document root, so the server object thinks there is an application
	// in use.
	$docRoot = "/dev/null/test-app/www";
	$_SERVER = ["DOCUMENT_ROOT" => $docRoot];

	foreach ($this->uriTestArray as $uri) {
		// Build expected full path to check against.
		$fullPath = $docRoot . $uri;

		$this->assertEquals(
			$fullPath,
			Gateway::getAbsoluteFilePath($uri)
		);
	}

	unset($_SERVER);
}

public function testServeStaticFile() {
	$expected = "";

	foreach ($this->uriTestArray as $uri) {
		$path = $this->getTempFilePath($uri);

		$expected .= self::DUMMY_CONTENT . " (from $uri).";
		Gateway::serveStaticFile($path);
	}
	$this->expectOutputString($expected);
}

public function data_brokenPathList() {
	$pathList = [];
	foreach ($this->uriTestArray as $uri) {
		$path = $this->getTempFilePath($uri);
		$pathList []= ["$path/"];
		$pathList []= ["/$path/"];
		$pathList []= ["?$path"];
		$pathList []= ["$path$path"];
		$pathList []= ["\\$path"];
	}
	return $pathList;
}

/**
 * @expectedException \Gt\Response\NotFoundException
 * @dataProvider data_brokenPathList
{'fraction_non_alphanumeric': 0.1820689655172414, 'fraction_numerical': 0.005517241379310344, 'mean_word_length': 5.298588490770901, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661148', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1907, 'n_words': 359}