Skip to content

Commit

Permalink
#7 Sanitizing sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Letournel committed Jan 9, 2014
1 parent 86693b0 commit 34d3005
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/Grale/WebDav/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ protected function resolveUrl($uri)
*
* @param HttpRequest $request The request
*
* @throws Exception\NoSuchResourceException
* @throws Exception\HttpException
* @return HttpResponse Returns the server response
* @throws HttpException If an HTTP error is returned
*/
protected function doRequest(HttpRequest $request)
{
Expand Down
23 changes: 13 additions & 10 deletions src/Grale/WebDav/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,26 @@ public static function factory(BadResponseException $cause)

if ($response->isClientError()) {
$class = __NAMESPACE__ . '\\ClientFailureException';
} elseif ($response->isServerError()) {
} else {
$class = __NAMESPACE__ . '\\ServerFailureException';
}

$statusCode = $response->getStatusCode();
$httpMethod = $request->getMethod();
$exception = new $class($response->getReasonPhrase(), null, $cause);

$e = new $class($response->getReasonPhrase(), null, $cause);
$e->setStatusCode($statusCode);
$e->setResponse($response);
$e->setRequest($request);
if ($exception instanceof HttpException) {
$statusCode = $response->getStatusCode();
$httpMethod = $request->getMethod();

if (isset(self::$descriptionMapping[$httpMethod][$statusCode])) {
$e->setDescription(self::$descriptionMapping[$httpMethod][$statusCode]);
$exception->setStatusCode($statusCode);
$exception->setResponse($response);
$exception->setRequest($request);

if (isset(self::$descriptionMapping[$httpMethod][$statusCode])) {
$exception->setDescription(self::$descriptionMapping[$httpMethod][$statusCode]);
}
}

return $e;
return $exception;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Grale/WebDav/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public function getDepth()
* Accepted values are zero or {@link Header\DepthHeader::INFINITY}
*
* @param int $depth The depth of lock
*
* @throws \InvalidArgumentException
*/
public function setDepth($depth)
{
Expand Down Expand Up @@ -203,8 +205,11 @@ public function setTimeout($timeout)
}

/**
* @return self
* @param \DOMElement $element
*
* @throws \InvalidArgumentException
* @return self
*
* @todo define exception message
*/
public static function fromXml(\DOMElement $element)
Expand Down Expand Up @@ -251,6 +256,7 @@ public static function fromXml(\DOMElement $element)
* @param Client $client
* @param string $xml
*
* @throws \RuntimeException
* @return self
*
* @todo
Expand Down
7 changes: 5 additions & 2 deletions src/Grale/WebDav/MultiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getDescription()
}

/**
* @return Iterator Returns an iterator to be used in loop functions
* @return \Iterator Returns an iterator to be used in loop functions
*/
public function getIterator()
{
Expand All @@ -85,8 +85,9 @@ public function count()

/**
* @param Client $client
* @param string $xml The multi-status response as an XML string
* @param string $xml The multi-status response as an XML string
*
* @throws \RuntimeException
* @return self Returns the parsed multi-status response as an object
*
* @todo
Expand Down Expand Up @@ -158,7 +159,9 @@ public static function parse(Client $client, $xml)

/**
* @param string $statusLine
* @throws \Exception
* @return int
* @todo define exception here
*/
protected static function parseHttpStatus($statusLine)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Grale/WebDav/Property/SupportedLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public function getValue()
*/
public function isLockable($type = null, $scope = null)
{
$result = false;

if ($type === null) {
$result = count($this->capabilities) > 0;
} elseif ($scope === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Grale/WebDav/PropertySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getNames()
/**
* Returns an iterator over all properties in this set.
*
* @return Iterator An iterator over {@link PropertyInterface}
* @return \Iterator An iterator over {@link PropertyInterface}
*/
public function getIterator()
{
Expand Down
1 change: 1 addition & 0 deletions src/Grale/WebDav/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Resource implements LockableInterface
/**
* @param string $href
* @param PropertySet|array
* @throws \InvalidArgumentException
*/
public function __construct($href, $properties = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Grale/WebDav/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Response
protected $status;

/**
* @var array A list of resource properties, grouped by HTTP status code
* @var PropertySet[] A list of resource properties, grouped by HTTP status code
*/
protected $properties = array();

Expand Down
24 changes: 6 additions & 18 deletions src/Grale/WebDav/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class StreamWrapper
protected $stream;

/**
* @var Iterator An iterator used to iterate the <tt>response</tt> elements of a WebDAV multi-status response
* @var \Iterator An iterator used to iterate the <tt>response</tt> elements of a WebDAV multi-status response
*/
protected $iterator;

Expand Down Expand Up @@ -166,7 +166,7 @@ public static function getDefaultClient()
*/
public function stream_open($path, $mode, $options, &$openedPath)
{
$result = false;
$url = null;

try {
// We don't care about text-mode translation and binary-mode flags
Expand Down Expand Up @@ -334,8 +334,6 @@ public function stream_seek($offset, $whence = SEEK_SET)
*/
public function stream_lock($operation)
{
$result = false;

try {
// We don't care about LOCK_NB
$operation = $operation & ~LOCK_NB;
Expand Down Expand Up @@ -410,8 +408,6 @@ public function stream_close()
*/
public function rename($old, $new)
{
$result = false;

try {
// Retrieve the context options
$this->setClientConfig();
Expand Down Expand Up @@ -441,8 +437,6 @@ public function rename($old, $new)
*/
public function unlink($path)
{
$result = false;

try {
// Retrieve the context options
$this->setClientConfig();
Expand Down Expand Up @@ -477,8 +471,6 @@ public function unlink($path)
*/
public function mkdir($path, $mode, $options)
{
$result = false;

try {
if ($options & STREAM_MKDIR_RECURSIVE) {
throw new \RuntimeException('WebDAV stream wrapper does not allow to create directories recursively');
Expand Down Expand Up @@ -516,8 +508,6 @@ public function mkdir($path, $mode, $options)
*/
public function rmdir($path, $options)
{
$result = false;

try {
// Retrieve the context options
$this->setClientConfig();
Expand Down Expand Up @@ -591,7 +581,7 @@ public function dir_readdir()
$result = $resource->getFilename();

// Cache the resource statistics for quick url_stat lookups
$url = (string)$this->openedPath->combine($resource->getHref());
$url = (string)Url::factory($this->openedPath)->combine($resource->getHref());
self::$statCache[$url] = $resource->getStat();

$this->iterator->next();
Expand Down Expand Up @@ -764,8 +754,6 @@ protected function lock($scope)
*/
protected function releaseLock()
{
$result = false;

try {
$result = self::$client->releaseLock($this->openedPath, $this->locktoken);
$this->locktoken = null;
Expand Down Expand Up @@ -806,7 +794,7 @@ protected function openAppendMode($url)

} catch (NoSuchResourceException $exception) {
// The resource does not exist, so use a simple write stream
return $this->openWriteOnly($path);
return $this->openWriteOnly($url);
}

return true;
Expand Down Expand Up @@ -869,10 +857,10 @@ protected function clearStatCache($path = null)
* Trigger an error
*
* @param \Exception|string $error Error to trigger
* @param int $quiet If set to true, then no error or exception occurs
* @param bool|int $quiet If set to true, then no error or exception occurs
*
* @throws Exception\StreamException
* @return bool Returns false
* @throws StreamException if throw_exceptions is true
*/
protected function triggerError($error, $quiet = false)
{
Expand Down
8 changes: 5 additions & 3 deletions tests/Grale/WebDav/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Grale\WebDav;

use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\RequestFactory;
use Guzzle\Http\Exception\BadResponseException;
Expand Down Expand Up @@ -257,7 +258,8 @@ public function testMoveLockedCollection()
$this->assertContains('Destination: http://www.foo.bar/othercontainer/', $request);
$this->assertContains(
'If: (<opaquelocktoken:fe184f2e-6eec-41d0-c765-01adc56e6bb4>)' .
' (<opaquelocktoken:e454f3f3-acdc-452a-56c7-00a5c91e4b77>)', $request
' (<opaquelocktoken:e454f3f3-acdc-452a-56c7-00a5c91e4b77>)',
$request
);
}

Expand Down Expand Up @@ -468,7 +470,6 @@ public function testSimpleLockRequest()
'timeout' => 4100000000
));

$status = $client->getLastResponseStatus();
$request = $client->getLastRequest();

$this->assertInstanceOf('Grale\\WebDav\\Lock', $lock);
Expand Down Expand Up @@ -506,7 +507,7 @@ public function testRefreshingWriteLock()
'opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4',
4100000000
);
$status = $client->getLastResponseStatus();

$request = $client->getLastRequest();

$this->assertInstanceOf('Grale\\WebDav\\Lock', $result);
Expand Down Expand Up @@ -592,6 +593,7 @@ public function testUnlock()
* @param string $name
* @param bool $asString
*
* @throws \RuntimeException
* @return Request|Response
*/
protected function getFixture($name, $asString = false)
Expand Down
5 changes: 4 additions & 1 deletion tests/Grale/WebDav/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
class LockTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Lock
*/
protected $lock;

public function setUp()
Expand Down Expand Up @@ -113,7 +116,7 @@ public function testInvalidLockScope()
public function testFromInvalidXml()
{
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" encoding="utf-8"?><element/>');
$dom->loadXML('<?xml version="1.0" encoding="utf-8"?><html></html>');
Lock::fromXml($dom->documentElement);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Grale/WebDav/Property/LockDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class LockDiscoveryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var LockDiscovery
*/
protected $property;

public function setUp()
Expand Down
8 changes: 7 additions & 1 deletion tests/Grale/WebDav/PropertySetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class PropertySetTest extends \PHPUnit_Framework_TestCase
'D:getcontenttype'
);

/**
* @var PropertySet
*/
protected $props;

public function setUp()
Expand Down Expand Up @@ -58,7 +61,10 @@ public function testOffsetExists()
public function testOffsetUnset()
{
unset($this->props['R:bigbox']);
$this->assertFalse(isset($this->props['R:bigbox']), 'Unable to assert that R:bigbox property has been deleted!');
$this->assertFalse(
isset($this->props['R:bigbox']),
'Unable to assert that R:bigbox property has been deleted!'
);
}

/** Testing Countable **/
Expand Down
16 changes: 15 additions & 1 deletion tests/Grale/WebDav/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@
*/
class ResourceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Grale\WebDav\Resource
*/
protected $collection;

/**
* @var \Grale\WebDav\Resource
*/
protected $resource;

/**
* @var Lock
*/
protected $lock;

public function setUp()
Expand Down Expand Up @@ -160,7 +171,10 @@ public function testHasLockToken()

public function testGetLock()
{
$this->assertEquals($this->lock, $this->resource->getLock('opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4'));
$this->assertEquals(
$this->lock,
$this->resource->getLock('opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4')
);
}

public function testGetNoLocks()
Expand Down
Loading

0 comments on commit 34d3005

Please sign in to comment.