Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #13

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions library/Barberry/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ class Application
*/
private $resources;

public function __construct(Config $config, Filter\FilterInterface $filter = null)
/**
* @var RequestSource
*/
private $requestSource;

public function __construct(Config $config, Filter\FilterInterface $filter = null, RequestSource $requestSource = null)
{
$this->resources = new Resources($config, $filter);
if (is_null($requestSource)) {
$requestSource = new RequestSource();
}
$this->requestSource = $requestSource;

$this->resources = new Resources($config, $requestSource, $filter);
}

/**
Expand All @@ -22,7 +32,7 @@ public function run()
$controller = new Controller($this->resources->request(), $this->resources->storage(), new Direction\Factory());

try {
$response = $controller->{$_SERVER['REQUEST_METHOD']}();
$response = $controller->{$this->requestSource->_SERVER['REQUEST_METHOD']}();
$this->invokeCache($response);

return $response;
Expand All @@ -41,9 +51,9 @@ public function run()

private function invokeCache(Response $response)
{
if('GET' == strtoupper($_SERVER['REQUEST_METHOD'])) {
if('GET' == strtoupper($this->requestSource->_SERVER['REQUEST_METHOD'])) {
$this->resources->cache()->save($response->body, $this->resources->request());
} elseif('DELETE' == strtoupper($_SERVER['REQUEST_METHOD'])) {
} elseif('DELETE' == strtoupper($this->requestSource->_SERVER['REQUEST_METHOD'])) {
$this->resources->cache()->invalidate($this->resources->request());
}
}
Expand Down
7 changes: 4 additions & 3 deletions library/Barberry/PostedFile/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public function unshift($offset, \Barberry\PostedFile $value) {
}
}

protected function readTempFile($filepath) {
if (is_uploaded_file($filepath)) {
protected function readTempFile($filepath, $trusted) {
if (is_uploaded_file($filepath) || $trusted === true) {
return file_get_contents($filepath);
}
return null;
Expand All @@ -147,8 +147,9 @@ private function getPostedFile($key) {

$spec = $this->specsIterator[$key];
if (is_array($spec)) {
$trusted = (array_key_exists('trusted', $spec) && $spec['trusted'] === true)?:false;
$this->specsIterator[$key] = new \Barberry\PostedFile(
$this->readTempFile($spec['tmp_name']),
$this->readTempFile($spec['tmp_name'], $trusted),
$spec['name']
);
}
Expand Down
55 changes: 55 additions & 0 deletions library/Barberry/RequestSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Barberry;

class RequestSource
{
/**
* @var array
*/
private $_SERVER;

/**
* @var array
*/

private $_POST;

/**
* @var array
*/
private $_FILES;

public function __construct(array $propertiesToOverride = array())
{
$this->setDefaultValues();

foreach ($propertiesToOverride as $key => $value) {
if (property_exists($this, $key) && (gettype($value) === gettype($this->$key))) {
$this->$key = $value;
}
}

}

public function __get($property) {
if (property_exists($this, $property)) {
return $this->{$property};
}
trigger_error('Undefined property via __get(): ' . $property, E_USER_NOTICE);
return null;
}

private function setDefaultValues()
{
$this->_SERVER = array_merge(
array(
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/',
),
$_SERVER
);
$this->_FILES = $_FILES;
$this->_POST = $_POST;
}

}
15 changes: 12 additions & 3 deletions library/Barberry/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class Resources
*/
private $filter;

/**
* @var RequestSource
*/
private $requestSource;


/**
* @param Config $config
* @param Filter\FilterInterface $filter
*/
public function __construct(Config $config, Filter\FilterInterface $filter = null)
public function __construct(Config $config, RequestSource $requestSource, Filter\FilterInterface $filter = null)
{
$this->config = $config;
$this->filter = $filter;
$this->requestSource = $requestSource;
}

/**
Expand Down Expand Up @@ -68,8 +75,10 @@ public function request()
function () use ($filter) {
$dp = new PostedDataProcessor($filter);
return new Request(
array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '/',
$dp->process($_FILES, $_POST)
array_key_exists('REQUEST_URI', $this->requestSource->_SERVER)
? $this->requestSource->_SERVER['REQUEST_URI']
: '/',
$dp->process($this->requestSource->_FILES, $this->requestSource->_POST)
);
}
);
Expand Down
8 changes: 8 additions & 0 deletions library/Barberry/Test/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public static function odsSpreadsheet() {
public static function pdfDocument() {
return file_get_contents(__DIR__ . '/data/sample.pdf');
}

public static function gif1x1Path() {
return __DIR__ . '/data/1x1.gif';
}

public static function pdfDocumentPath() {
return __DIR__ . '/data/sample.pdf';
}
}
2 changes: 0 additions & 2 deletions test/integration/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase

public function testCanRun()
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$a = new Application(new Config(__DIR__));
$this->assertInstanceOf('Barberry\\Response', $a->run());
}
Expand Down
1 change: 0 additions & 1 deletion test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="true"
stopOnFailure="true"
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class ConfigTest extends \PHPUnit_Framework_TestCase {

public function testOptionsCanBeOverriden() {
public function testOptionsCanBeOverridden() {
$config = new Config(__DIR__, '/test_config.php');
$this->assertEquals('/usr/another/storage', $config->directoryStorage);
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/PostedFile/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public function testCanBeCreatedWithPostedFilesInConstructor() {
$this->assertEquals(Test\Data::gif1x1(), $collection['file']->bin);
}

public function testCanReadTrustedTempFile() {
$collection = new Collection(array('file' => $this->goodTrustedFileInPhpFilesArray()));
$this->assertEquals(Test\Data::gif1x1(), $collection['file']->bin);
}

//--------------------------------------------------------------------------------------------------

private static function goodFileInPhpFilesArray() {
Expand Down Expand Up @@ -134,6 +139,16 @@ private static function badFileInPhpFilesArray() {
);
}

private static function goodTrustedFileInPhpFilesArray() {
return array(
'size' => 43,
'tmp_name' => Test\Data::gif1x1Path(),
'error' => UPLOAD_ERR_OK,
'name' => 'Name of a file.txt',
'trusted' => true,
);
}

private function partiallyMockedEmptyCollection(array $specs = null) {
if ($specs === null) {
$specs = array('file' => self::goodFileInPhpFilesArray());
Expand Down
73 changes: 73 additions & 0 deletions test/unit/RequestSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Barberry;

class RequestSourceTest extends \PHPUnit_Framework_TestCase {

public function testHaveServerProperty() {
$requestSource = new RequestSource();
$this->assertEquals(true, property_exists($requestSource, '_SERVER'));
}

public function testServerPropertyHasArrayType() {
$requestSource = new RequestSource();
$this->assertInternalType('array', $requestSource->_SERVER);
}

public function testHavePostProperty() {
$requestSource = new RequestSource();
$this->assertEquals(true, property_exists($requestSource, '_POST'));
}

public function testPostPropertyHasArrayType() {
$requestSource = new RequestSource();
$this->assertInternalType('array', $requestSource->_POST);
}

public function testHaveFilesProperty() {
$requestSource = new RequestSource();
$this->assertEquals(true, property_exists($requestSource, '_FILES'));
}

public function testFilesPropertyHasArrayType() {
$requestSource = new RequestSource();
$this->assertInternalType('array', $requestSource->_FILES);
}

public function testPropertiesOverriddenOnlyIfHaveSameAsDefaultType() {
$requestSource = new RequestSource(array(
'_SERVER' => null,
));
$this->assertInternalType(gettype($_SERVER), $requestSource->_SERVER);
}

public function testHaveProperServerKeys() {
$requestSource = new RequestSource();
$haveAllKeys = array_reduce(
$this->serverKeys(),
function($result, $propertyKey) use ($requestSource) {
$result = $result && array_key_exists($propertyKey, $requestSource->_SERVER);
return $result;
},
true
);

$this->assertEquals(true, $haveAllKeys);
}

public function testPropertiesCanBeOverridden() {
$requestSource = new RequestSource(array(
'_SERVER' => array(
'REQUEST_URI' => '/some/url'
),
));
$this->assertEquals('/some/url', $requestSource->_SERVER['REQUEST_URI']);
}

protected function serverKeys() {
return array(
'REQUEST_METHOD',
'REQUEST_URI',
);
}

}
2 changes: 1 addition & 1 deletion test/unit/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function testPassesDataFilterToPostedDataProcessor() {
//--------------------------------------------------------------------------------------------------

private static function r(Filter\FilterInterface $filter = null) {
return new Resources(new Config(__DIR__), $filter);
return new Resources(new Config(__DIR__), new RequestSource(), $filter);
}
}