Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2 into cach…
Browse files Browse the repository at this point in the history
…e_interfaces
  • Loading branch information
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
travisdir=$(dirname $(readlink /proc/$$/fd/255))
travisdir=$(dirname "$0")
testdir="$travisdir/../tests"
testedcomponents=(`cat "$travisdir/tested-components"`)
result=0
Expand Down
1 change: 1 addition & 0 deletions .travis/skipped-components
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Zend/Amf
Zend/Date
Zend/Dojo
Zend/Queue
Zend/Service
Zend/Test
Expand Down
4 changes: 3 additions & 1 deletion .travis/tested-components
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ Zend/Form
Zend/GData
Zend/Http
Zend/InfoCard
Zend/InputFilter
Zend/Json
Zend/Ldap
Zend/Loader
Zend/Locale
Zend/Log
Zend/Mail
Zend/Markup
Zend/Math
Zend/Measure
Zend/Memory
Zend/Mime
Zend/Module
Zend/ModuleManager
Zend/Mvc
Zend/Navigation
Zend/OAuth
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidUriException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidUriException
extends \InvalidArgumentException
implements ExceptionInterface
class InvalidUriException extends InvalidArgumentException
{}
11 changes: 8 additions & 3 deletions src/Exception/InvalidUriPartException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

namespace Zend\Uri\Exception;

class InvalidUriPartException
extends \InvalidArgumentException
implements ExceptionInterface
/**
* @category Zend
* @package Zend_Uri
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidUriPartException extends InvalidArgumentException
{
/**
* Part-specific error codes
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidUriTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidUriTypeException
extends \InvalidArgumentException
implements ExceptionInterface
class InvalidUriTypeException extends InvalidArgumentException
{}
5 changes: 4 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class Http extends Uri
/**
* @see Uri::$validSchemes
*/
protected static $validSchemes = array('http', 'https');
protected static $validSchemes = array(
'http',
'https'
);

/**
* @see Uri::$defaultPorts
Expand Down
43 changes: 36 additions & 7 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Uri
/**
* Create a new URI object
*
* @param \Zend\Uri\Uri|string|null $uri
* @param Uri|string|null $uri
* @throws Exception\InvalidArgumentException
*/
public function __construct($uri = null)
Expand Down Expand Up @@ -183,6 +183,33 @@ public function isValid()
return true;
}

/**
* Check if the URI is a valid relative URI
*
* @return boolean
*/
public function isValidRelative()
{
if ($this->scheme || $this->host || $this->userInfo || $this->port) {
return false;
}

if ($this->path) {
// Check path-only (no host) URI
if (substr($this->path, 0, 2) == '//') {
return false;
}
return true;
}

if (! ($this->query || $this->fragment)) {
// No host, path, query or fragment - this is not a valid URI
return false;
}

return true;
}

/**
* Check if the URI is an absolute or relative URI
*
Expand Down Expand Up @@ -274,9 +301,11 @@ public function parse($uri)
public function toString()
{
if (!$this->isValid()) {
throw new Exception\InvalidUriException(
'URI is not valid and cannot be converted into a string'
);
if ($this->isAbsolute() || !$this->isValidRelative()) {
throw new Exception\InvalidUriException(
'URI is not valid and cannot be converted into a string'
);
}
}

$uri = '';
Expand Down Expand Up @@ -388,7 +417,7 @@ public function resolve($baseUri)
if (!$baseUri instanceof static) {
throw new Exception\InvalidUriTypeException(sprintf(
'Provided base URL is not an instance of "%s"',
get_class($this)
get_called_class()
));
}

Expand Down Expand Up @@ -616,7 +645,7 @@ public function setScheme($scheme)
throw new Exception\InvalidUriPartException(sprintf(
'Scheme "%s" is not valid or is not accepted by %s',
$scheme,
get_class($this)
get_called_class()
), Exception\InvalidUriPartException::INVALID_SCHEME);
}

Expand Down Expand Up @@ -663,7 +692,7 @@ public function setHost($host)
throw new Exception\InvalidUriPartException(sprintf(
'Host "%s" is not valid or is not accepted by %s',
$host,
get_class($this)
get_called_class()
), Exception\InvalidUriPartException::INVALID_HOSTNAME);
}

Expand Down
80 changes: 79 additions & 1 deletion test/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,20 @@ public function testValidUriIsValid($uriString)
$this->assertTrue($uri->isValid());
}

/**
* Test that valid relative URIs pass validation
*
* @param string $uriString
* @dataProvider validRelativeUriStringProvider
*/
public function testValidRelativeUriIsValid($uriString)
{
$uri = new Uri($uriString);
$this->assertTrue($uri->isValidRelative());
}

/**
* Test that invalid URIs pass validation
* Test that invalid URIs fail validation
*
* @param \Zend\Uri\Uri $uri
* @dataProvider invalidUriObjectProvider
Expand All @@ -396,6 +408,17 @@ public function testInvalidUriIsInvalid(Uri $uri)
$this->assertFalse($uri->isValid());
}

/**
* Test that invalid relative URIs fail validation
*
* @param \Zend\Uri\Uri $uri
* @dataProvider invalidRelativeUriObjectProvider
*/
public function testInvalidRelativeUriIsInvalid(Uri $uri)
{
$this->assertFalse($uri->isValidRelative());
}

/**
* Check that valid schemes are valid according to validateScheme()
*
Expand Down Expand Up @@ -819,6 +842,21 @@ static public function validUriStringProvider()
);
}

/**
* Data provider for valid relative URIs, not necessarily complete
*
* @return array
*/
static public function validRelativeUriStringProvider()
{
return array(
array('foo/bar?query'),
array('../relative/path'),
array('?queryOnly'),
array('#fragmentOnly'),
);
}

/**
* Valid schemes
*
Expand Down Expand Up @@ -921,6 +959,46 @@ static public function invalidUriObjectProvider()
);
}

/**
* Data provider for invalid relative URI objects
*
* @return array
*/
static public function invalidRelativeUriObjectProvider()
{
// Empty URI is not valid
$obj1 = new Uri;

// Path cannot begin with '//'
$obj2 = new Uri;
$obj2->setPath('//path');

// A object with port
$obj3 = new Uri;
$obj3->setPort(123);

// A object with userInfo
$obj4 = new Uri;
$obj4->setUserInfo('shahar:password');

// A object with scheme
$obj5 = new Uri;
$obj5->setScheme('https');

// A object with host
$obj6 = new Uri;
$obj6->setHost('example.com');

return array(
array($obj1),
array($obj2),
array($obj3),
array($obj4),
array($obj5),
array($obj6)
);
}


/**
* Data provider for valid URIs with their different parts
Expand Down

0 comments on commit 29a471c

Please sign in to comment.