Permalink
Browse files

Added UriTest

Signed-off-by: Darren Hill <dchill42@gmail.com>
  • Loading branch information...
1 parent 6ec1875 commit fbbe02b16cc816cec6c252e2be1ac15f09d0b1f7 @dchill42 dchill42 committed Mar 5, 2014
Showing with 1,132 additions and 18 deletions.
  1. +23 −18 system/core/Uri.php
  2. +121 −0 tests/Mocks/core/Uri.php
  3. +988 −0 tests/Xylophone/core/UriTest.php
View
@@ -54,10 +54,10 @@ class Uri
public $rsegments = array();
/** @var string Permitted URI characters */
- protected $perm_char = '';
+ public $perm_char = '';
/** @var bool Whether query strings are enabled */
- protected $query_str = false;
+ public $query_str = false;
/**
* Constructor
@@ -275,7 +275,7 @@ protected function parseRequestUri()
// Ensure that where the URI is required to be in the query string (Nginx)
// a correct URI is found, and fix the QUERY_STRING var and $_GET array.
- if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) {
+ if (trim($uri, '/') === '' && $query[0] == '/') {
$query = explode('?', $query, 2);
$uri = rawurldecode($query[0]);
$_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
@@ -305,15 +305,17 @@ protected function parseRequestUri()
*/
protected function parseQueryString()
{
+ // Get query string, which may be empty or FALSE
$uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
- if (trim($uri, '/') === '') {
+ if (trim($uri, '/') == '') {
return '';
}
- if (strncmp($uri, '/', 1) === 0) {
- $uri = explode('?', $uri, 2);
- $_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
- $uri = rawurldecode($uri[0]);
+ // Break out path from query args where combined (Nginx)
+ if ($uri[0] == '/') {
+ $query = explode('?', $uri, 2);
+ $uri = rawurldecode($query[0]);
+ $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
}
parse_str($_SERVER['QUERY_STRING'], $_GET);
@@ -346,13 +348,10 @@ protected function parseArgv()
*/
protected function removeRelativeDirectory($uri)
{
+ // Remove empty and '..' segments
$uris = array();
- $tok = strtok($uri, '/');
- while ($tok !== false) {
- if ((!empty($tok) || $tok === '0') && $tok !== '..') {
- $uris[] = $tok;
- }
- $tok = strtok('/');
+ foreach (explode('/', $uri) as $seg) {
+ $seg != '' && $seg != '..' && $uris[] = $seg;
}
return implode('/', $uris);
}
@@ -430,18 +429,18 @@ public function ruriToAssoc($start = 3, $default = array())
* @used-by URI::uriToAssoc()
* @used-by URI::ruriToAssoc()
*
- * @param int $start Starting segment index (default: 3)
+ * @param int $start Starting segment index
* @param array $default Default values
* @param array $segments Reference to segment array
* @return array Associative array of segments
*/
- protected function toAssoc($start = 3, $default = array(), &$segments)
+ protected function toAssoc($start, $default, &$segments)
{
// Index must be numeric and within segment count
if (!is_numeric($start)) {
return $default;
}
- if ($start >= $count($segments)) {
+ if ($start >= count($segments)) {
return count($default) ? array_fill_keys($default, null) : array();
}
@@ -533,8 +532,14 @@ public function slashRsegment($n, $where = 'trailing')
* @param array $segments Reference to segment array
* @return string Segment with slash
*/
- protected function addSlash($n, $where = 'trailing', &$segments)
+ protected function addSlash($n, $where, &$segments)
{
+ // Make sure segment exists
+ if (!isset($segments[$n])) {
+ return null;
+ }
+
+ // Return segment with leading and/or trailing slash
$leading = ($where === 'trailing') ? '' : '/';
$trailing = ($where === 'leading') ? '' : '/';
return $leading.$segments[$n].$trailing;
@@ -0,0 +1,121 @@
+<?php
+/**
+ * Xylophone
+ *
+ * An open source HMVC application development framework for PHP 5.3 or newer
+ * Derived from CodeIgniter, Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
+ *
+ * NOTICE OF LICENSE
+ *
+ * Licensed under the Open Software License version 3.0
+ *
+ * This source file is subject to the Open Software License (OSL 3.0) that is
+ * bundled with this package in the files license.txt / license.rst. It is
+ * also available through the world wide web at this URL:
+ * http://opensource.org/licenses/OSL-3.0
+ * If you did not receive a copy of the license and are unable to obtain it
+ * through the world wide web, please send an email to licensing@xylophone.io
+ * so we can send you a copy immediately.
+ *
+ * @package Xylophone
+ * @author Xylophone Dev Team, EllisLab Dev Team
+ * @copyright Copyright (c) 2013, Xylophone Team (http://xylophone.io/)
+ * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
+ * @link http://xylophone.io
+ * @since Version 1.0
+ * @filesource
+ */
+namespace Mocks\core;
+
+class Uri extends \Xylophone\core\Uri
+{
+ /**
+ * Set URI String (made public)
+ *
+ * @param string $str URI string to set
+ * @return void
+ */
+ public function setUriString($str)
+ {
+ parent::setUriString($str);
+ }
+
+ /**
+ * Explode a URI into segments (made public)
+ *
+ * @param string $str URI string
+ * @return array Segment array
+ */
+ public function explodeSegments($str)
+ {
+ return parent::explodeSegments($str);
+ }
+
+ /**
+ * Parse REQUEST_URI (made public)
+ *
+ * @return string
+ */
+ public function parseRequestUri()
+ {
+ return parent::parseRequestUri();
+ }
+
+ /**
+ * Parse QUERY_STRING (made public)
+ *
+ * @return string URI string
+ */
+ public function parseQueryString()
+ {
+ return parent::parseQueryString();
+ }
+
+ /**
+ * Parse CLI arguments (made public)
+ *
+ * @return string URI string
+ */
+ public function parseArgv()
+ {
+ return parent::parseArgv();
+ }
+
+ /**
+ * Remove relative directory (../) and multi slashes (///) (made public)
+ *
+ * @param string $uri URI string
+ * @return string Cleaned string
+ */
+ public function removeRelativeDirectory($uri)
+ {
+ return parent::removeRelativeDirectory($uri);
+ }
+
+ /**
+ * Internal URI-to-assoc (made public)
+ *
+ * @param int $start Starting segment index
+ * @param array $default Default values
+ * @param array $segments Reference to segment array
+ * @return array Associative array of segments
+ */
+ public function toAssoc($start, $default, &$segments)
+ {
+ return parent::toAssoc($start, $default, $segments);
+ }
+
+ /**
+ * Internal Slash segment (made public)
+ *
+ * @param int $n Segment index
+ * @param string $where Where to add the slash ('leading', 'trailing' or 'both')
+ * @param array $segments Reference to segment array
+ * @return string Segment with slash
+ */
+ public function addSlash($n, $where, &$segments)
+ {
+ return parent::addSlash($n, $where, $segments);
+ }
+}
+
Oops, something went wrong. Retry.

0 comments on commit fbbe02b

Please sign in to comment.