scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests;
4  
5 use GuzzleHttp\UriTemplate;
6  
7 /**
8 * @covers GuzzleHttp\UriTemplate
9 */
10 class UriTemplateTest extends \PHPUnit_Framework_TestCase
11 {
12 /**
13 * @return array
14 */
15 public function templateProvider()
16 {
17 $params = array(
18 'var' => 'value',
19 'hello' => 'Hello World!',
20 'empty' => '',
21 'path' => '/foo/bar',
22 'x' => '1024',
23 'y' => '768',
24 'null' => null,
25 'list' => array('red', 'green', 'blue'),
26 'keys' => array(
27 "semi" => ';',
28 "dot" => '.',
29 "comma" => ','
30 ),
31 'empty_keys' => array(),
32 );
33  
34 return array_map(function ($t) use ($params) {
35 $t[] = $params;
36 return $t;
37 }, array(
38 array('foo', 'foo'),
39 array('{var}', 'value'),
40 array('{hello}', 'Hello%20World%21'),
41 array('{+var}', 'value'),
42 array('{+hello}', 'Hello%20World!'),
43 array('{+path}/here', '/foo/bar/here'),
44 array('here?ref={+path}', 'here?ref=/foo/bar'),
45 array('X{#var}', 'X#value'),
46 array('X{#hello}', 'X#Hello%20World!'),
47 array('map?{x,y}', 'map?1024,768'),
48 array('{x,hello,y}', '1024,Hello%20World%21,768'),
49 array('{+x,hello,y}', '1024,Hello%20World!,768'),
50 array('{+path,x}/here', '/foo/bar,1024/here'),
51 array('{#x,hello,y}', '#1024,Hello%20World!,768'),
52 array('{#path,x}/here', '#/foo/bar,1024/here'),
53 array('X{.var}', 'X.value'),
54 array('X{.x,y}', 'X.1024.768'),
55 array('{/var}', '/value'),
56 array('{/var,x}/here', '/value/1024/here'),
57 array('{;x,y}', ';x=1024;y=768'),
58 array('{;x,y,empty}', ';x=1024;y=768;empty'),
59 array('{?x,y}', '?x=1024&y=768'),
60 array('{?x,y,empty}', '?x=1024&y=768&empty='),
61 array('?fixed=yes{&x}', '?fixed=yes&x=1024'),
62 array('{&x,y,empty}', '&x=1024&y=768&empty='),
63 array('{var:3}', 'val'),
64 array('{var:30}', 'value'),
65 array('{list}', 'red,green,blue'),
66 array('{list*}', 'red,green,blue'),
67 array('{keys}', 'semi,%3B,dot,.,comma,%2C'),
68 array('{keys*}', 'semi=%3B,dot=.,comma=%2C'),
69 array('{+path:6}/here', '/foo/b/here'),
70 array('{+list}', 'red,green,blue'),
71 array('{+list*}', 'red,green,blue'),
72 array('{+keys}', 'semi,;,dot,.,comma,,'),
73 array('{+keys*}', 'semi=;,dot=.,comma=,'),
74 array('{#path:6}/here', '#/foo/b/here'),
75 array('{#list}', '#red,green,blue'),
76 array('{#list*}', '#red,green,blue'),
77 array('{#keys}', '#semi,;,dot,.,comma,,'),
78 array('{#keys*}', '#semi=;,dot=.,comma=,'),
79 array('X{.var:3}', 'X.val'),
80 array('X{.list}', 'X.red,green,blue'),
81 array('X{.list*}', 'X.red.green.blue'),
82 array('X{.keys}', 'X.semi,%3B,dot,.,comma,%2C'),
83 array('X{.keys*}', 'X.semi=%3B.dot=..comma=%2C'),
84 array('{/var:1,var}', '/v/value'),
85 array('{/list}', '/red,green,blue'),
86 array('{/list*}', '/red/green/blue'),
87 array('{/list*,path:4}', '/red/green/blue/%2Ffoo'),
88 array('{/keys}', '/semi,%3B,dot,.,comma,%2C'),
89 array('{/keys*}', '/semi=%3B/dot=./comma=%2C'),
90 array('{;hello:5}', ';hello=Hello'),
91 array('{;list}', ';list=red,green,blue'),
92 array('{;list*}', ';list=red;list=green;list=blue'),
93 array('{;keys}', ';keys=semi,%3B,dot,.,comma,%2C'),
94 array('{;keys*}', ';semi=%3B;dot=.;comma=%2C'),
95 array('{?var:3}', '?var=val'),
96 array('{?list}', '?list=red,green,blue'),
97 array('{?list*}', '?list=red&list=green&list=blue'),
98 array('{?keys}', '?keys=semi,%3B,dot,.,comma,%2C'),
99 array('{?keys*}', '?semi=%3B&dot=.&comma=%2C'),
100 array('{&var:3}', '&var=val'),
101 array('{&list}', '&list=red,green,blue'),
102 array('{&list*}', '&list=red&list=green&list=blue'),
103 array('{&keys}', '&keys=semi,%3B,dot,.,comma,%2C'),
104 array('{&keys*}', '&semi=%3B&dot=.&comma=%2C'),
105 array('{.null}', ''),
106 array('{.null,var}', '.value'),
107 array('X{.empty_keys*}', 'X'),
108 array('X{.empty_keys}', 'X'),
109 // Test that missing expansions are skipped
110 array('test{&missing*}', 'test'),
111 // Test that multiple expansions can be set
112 array('http://{var}/{var:2}{?keys*}', 'http://value/va?semi=%3B&dot=.&comma=%2C'),
113 // Test more complex query string stuff
114 array('http://www.test.com{+path}{?var,keys*}', 'http://www.test.com/foo/bar?var=value&semi=%3B&dot=.&comma=%2C')
115 ));
116 }
117  
118 /**
119 * @dataProvider templateProvider
120 */
121 public function testExpandsUriTemplates($template, $expansion, $params)
122 {
123 $uri = new UriTemplate($template);
124 $this->assertEquals($expansion, $uri->expand($template, $params));
125 }
126  
127 public function expressionProvider()
128 {
129 return array(
130 array(
131 '{+var*}', array(
132 'operator' => '+',
133 'values' => array(
134 array('value' => 'var', 'modifier' => '*')
135 )
136 ),
137 ),
138 array(
139 '{?keys,var,val}', array(
140 'operator' => '?',
141 'values' => array(
142 array('value' => 'keys', 'modifier' => ''),
143 array('value' => 'var', 'modifier' => ''),
144 array('value' => 'val', 'modifier' => '')
145 )
146 ),
147 ),
148 array(
149 '{+x,hello,y}', array(
150 'operator' => '+',
151 'values' => array(
152 array('value' => 'x', 'modifier' => ''),
153 array('value' => 'hello', 'modifier' => ''),
154 array('value' => 'y', 'modifier' => '')
155 )
156 )
157 )
158 );
159 }
160  
161 /**
162 * @dataProvider expressionProvider
163 */
164 public function testParsesExpressions($exp, $data)
165 {
166 $template = new UriTemplate($exp);
167  
168 // Access the config object
169 $class = new \ReflectionClass($template);
170 $method = $class->getMethod('parseExpression');
171 $method->setAccessible(true);
172  
173 $exp = substr($exp, 1, -1);
174 $this->assertEquals($data, $method->invokeArgs($template, array($exp)));
175 }
176  
177 /**
178 * @ticket https://github.com/guzzle/guzzle/issues/90
179 */
180 public function testAllowsNestedArrayExpansion()
181 {
182 $template = new UriTemplate();
183  
184 $result = $template->expand('http://example.com{+path}{/segments}{?query,data*,foo*}', array(
185 'path' => '/foo/bar',
186 'segments' => array('one', 'two'),
187 'query' => 'test',
188 'data' => array(
189 'more' => array('fun', 'ice cream')
190 ),
191 'foo' => array(
192 'baz' => array(
193 'bar' => 'fizz',
194 'test' => 'buzz'
195 ),
196 'bam' => 'boo'
197 )
198 ));
199  
200 $this->assertEquals('http://example.com/foo/bar/one,two?query=test&more%5B0%5D=fun&more%5B1%5D=ice%20cream&baz%5Bbar%5D=fizz&baz%5Btest%5D=buzz&bam=boo', $result);
201 }
202 }