public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
<?php
use PHPUnit_Framework_TestCase as BaseTestCase;
class TestFixer extends BaseTestCase
{
public function fix(&$target, $source, $keep = false) {
if (!$source) {
return;
}
$keys = array();
$source = preg_replace_callback(
'/
# Match at start of string or &
(?:^|(?<=&))
# Exclude cases where the period is in brackets, e.g. foo[bar.blarg]
[^=&\[]*
# Affected cases: periods and spaces
(?:\.|%20)
# Keep matching until assignment, next variable, end of string or
# start of an array
[^=&\[]*
/x',
function ($key) use (&$keys) {
$keys[] = $key = base64_encode(urldecode($key[0]));
return urlencode($key);
},
$source
);
if (!$keep) {
$target = array();
}
parse_str($source, $data);
foreach ($data as $key => $val) {
// Only unprocess encoded keys
if (!in_array($key, $keys)) {
$target[$key] = $val;
continue;
}
$key = base64_decode($key);
$target[$key] = $val;
if ($keep) {
// Keep a copy in the underscore key version
$key = preg_replace('/(\.| )/', '_', $key);
$target[$key] = $val;
}
}
}
public function dataForTestFix()
{
return array(
// Simple cases
['foo=bar', ['foo' => 'bar']],
['foo=foo.bar', ['foo' => 'foo.bar']],
['foo=foo.bar[]', ['foo' => 'foo.bar[]']],
['foo[]=bar', ['foo' => ['bar']]],
['foo.bar', ['foo.bar' => '']],
['foo.bar[]', ['foo.bar' => ['']]],
['foo%20bar', ['foo bar' => '']],
['foo%20bar[]', ['foo bar' => ['']]],
['foo[foo.bar]=bar', ['foo' => ['foo.bar' => 'bar']]],
['foo[foo%20bar]=bar', ['foo' => ['foo bar' => 'bar']]],
// Simple cases with keeping
['foo=bar', ['foo' => 'bar'], true],
['foo=foo.bar', ['foo' => 'foo.bar'], true],
['foo=foo.bar[]', ['foo' => 'foo.bar[]'], true],
['foo[]=bar', ['foo' => ['bar']], true],
['foo.bar', ['foo_bar' => '', 'foo.bar' => ''], true],
['foo.bar[]', ['foo_bar' => [''], 'foo.bar' => ['']], true],
['foo%20bar', ['foo_bar' => '', 'foo bar' => ''], true],
['foo%20bar[]', ['foo_bar' => [''], 'foo bar' => ['']], true],
['foo[foo.bar]=bar', ['foo' => ['foo.bar' => 'bar']], true],
['foo[foo%20bar]=bar', ['foo' => ['foo bar' => 'bar']], true],
// Duplicates if cast to "_"
['foo.bar=blarg1&foo_bar=blarg2&foo%20bar=blarg3', ['foo.bar' => 'blarg1', 'foo_bar' => 'blarg2', 'foo bar' => 'blarg3']],
['foo.bar=blarg1&foo_bar=blarg2&foo%20bar=blarg3', ['foo_bar' => 'blarg3', 'foo.bar' => 'blarg1', 'foo bar' => 'blarg3'], true],
array(
'foo%20bar=blarg&foo[bar][blarg.bar]=bar&foo.bar=blarg&foobar.blarg[]=bar&blarg.foo&blargfoo.bar[asdf]=w00t&normal=normal',
array(
'foo bar' => 'blarg',
'foo' => array(
'bar' => array(
'blarg.bar' => 'bar',
),
),
'foo.bar' => 'blarg',
'foobar.blarg' => array('bar'),
'blarg.foo' => null,
'blargfoo.bar' => array(
'asdf' => 'w00t',
),
'normal' => 'normal',
)
),
);
}
/**
* @dataProvider dataForTestFix
*/
public function testFixWorks($string, $target, $keep = false)
{
parse_str($string, $source);
$this->fix($source, $string, $keep);
$this->assertEquals($source, $target);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.