PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/solarium/solarium/tests/Solarium/Tests/Core/Client/RequestTest.php

https://gitlab.com/Blueprint-Marketing/solr-power
PHP | 534 lines | 416 code | 86 blank | 32 comment | 0 complexity | 7c73f17158855568ad45004420b90462 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this listof conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation are
  28. * those of the authors and should not be interpreted as representing official
  29. * policies, either expressed or implied, of the copyright holder.
  30. */
  31. namespace Solarium\Tests\Core\Client;
  32. use Solarium\Core\Client\Request;
  33. class RequestTest extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * @var Request
  37. */
  38. protected $request;
  39. public function setup()
  40. {
  41. $this->request = new Request;
  42. }
  43. public function testConfigMode()
  44. {
  45. $options = array(
  46. 'method' => Request::METHOD_POST,
  47. 'handler' => 'myHandler',
  48. 'param' => array(
  49. 'param1' => 1,
  50. 'param2' => 'test',
  51. ),
  52. 'rawdata' => 'raw post data here',
  53. 'header' => array(
  54. 'myHeader1' => 'X-myHeader1: value1',
  55. 'myHeader2' => 'X-myHeader2: value2',
  56. ),
  57. 'authentication' => array(
  58. 'username' => 'testuser',
  59. 'password' => 'testpass',
  60. ),
  61. 'file' => __FILE__,
  62. );
  63. $this->request->setOptions($options);
  64. $this->assertEquals(
  65. $options['method'],
  66. $this->request->getMethod()
  67. );
  68. $this->assertEquals(
  69. $options['handler'],
  70. $this->request->getHandler()
  71. );
  72. $this->assertEquals(
  73. $options['rawdata'],
  74. $this->request->getRawData()
  75. );
  76. $this->assertEquals(
  77. $options['param'],
  78. $this->request->getParams()
  79. );
  80. $this->assertEquals(
  81. array(
  82. $options['header']['myHeader1'],
  83. $options['header']['myHeader2']
  84. ),
  85. $this->request->getHeaders()
  86. );
  87. $this->assertEquals(
  88. array(
  89. 'username' => $options['authentication']['username'],
  90. 'password' => $options['authentication']['password'],
  91. ),
  92. $this->request->getAuthentication()
  93. );
  94. $this->assertEquals(
  95. $options['file'],
  96. $this->request->getFileUpload()
  97. );
  98. }
  99. public function testGetDefaultMethod()
  100. {
  101. $this->assertEquals(
  102. Request::METHOD_GET,
  103. $this->request->getMethod()
  104. );
  105. }
  106. public function testSetAndGetMethod()
  107. {
  108. $this->request->setMethod(Request::METHOD_POST);
  109. $this->assertEquals(
  110. Request::METHOD_POST,
  111. $this->request->getMethod()
  112. );
  113. }
  114. public function testSetAndGetHandler()
  115. {
  116. $this->request->setHandler('myhandler');
  117. $this->assertEquals(
  118. 'myhandler',
  119. $this->request->getHandler()
  120. );
  121. }
  122. public function testSetAndGetParams()
  123. {
  124. $params = array(
  125. 'param1' => 1,
  126. 'param2' => 2,
  127. );
  128. $this->request->setParams($params);
  129. $this->assertEquals(
  130. $params,
  131. $this->request->getParams()
  132. );
  133. }
  134. public function testSetAndGetParam()
  135. {
  136. $params = array(
  137. 'param1' => 1,
  138. 'param2' => 2,
  139. );
  140. $this->request->setParams($params);
  141. $this->assertEquals(
  142. 2,
  143. $this->request->getParam('param2')
  144. );
  145. }
  146. public function testGetInvalidParam()
  147. {
  148. $this->assertEquals(
  149. null,
  150. $this->request->getParam('invalidname')
  151. );
  152. }
  153. public function testAddParam()
  154. {
  155. $params = array(
  156. 'param1' => 1,
  157. 'param2' => 2,
  158. );
  159. $this->request->setParams($params);
  160. $this->request->addParam('param3', 3);
  161. $params['param3'] = 3;
  162. $this->assertEquals(
  163. $params,
  164. $this->request->getParams()
  165. );
  166. }
  167. public function testAddParamBoolean()
  168. {
  169. $params = array(
  170. 'param1' => true,
  171. 'param2' => false,
  172. );
  173. $this->request->addParams($params);
  174. $this->assertEquals(
  175. array(
  176. 'param1' => 'true',
  177. 'param2' => 'false',
  178. ),
  179. $this->request->getParams()
  180. );
  181. }
  182. public function testAddParamMultivalue()
  183. {
  184. $params = array(
  185. 'param1' => 1,
  186. );
  187. $this->request->setParams($params);
  188. $this->request->addParam('param2', 2);
  189. $this->request->addParam('param2', 3);
  190. $params['param2'] = array(2, 3);
  191. $this->assertEquals(
  192. $params,
  193. $this->request->getParams()
  194. );
  195. }
  196. public function testAddParamNoValue()
  197. {
  198. $params = array(
  199. 'param1' => 1,
  200. 'param2' => 2,
  201. 'param3' => 3,
  202. );
  203. $this->request->setParams($params);
  204. $this->request->addParam('param2', ''); // this should add an empty value to param2
  205. $this->request->addParam('param3', '', true); // this should overwrite param2 with an empty value
  206. $this->request->addParam('param4', ''); // this should add an empty param (for instance "q=" in dismax)
  207. $this->request->addParam('param5', null); // this param should be ignored
  208. $this->assertEquals(
  209. array(
  210. 'param1' => 1,
  211. 'param2' => array(2, ''),
  212. 'param3' => '',
  213. 'param4' => '',
  214. ),
  215. $this->request->getParams()
  216. );
  217. }
  218. public function testAddParamOverwrite()
  219. {
  220. $params = array(
  221. 'param1' => 1,
  222. );
  223. $this->request->setParams($params);
  224. $this->request->addParam('param1', 2, true);
  225. $this->assertEquals(
  226. array('param1' => 2),
  227. $this->request->getParams()
  228. );
  229. }
  230. public function testAddParams()
  231. {
  232. $params = array(
  233. 'param1' => 1,
  234. );
  235. $extraParams = array(
  236. 'param1' => 2,
  237. 'param2' => 3,
  238. );
  239. $this->request->setParams($params);
  240. $this->request->addParams($extraParams);
  241. $this->assertEquals(
  242. array(
  243. 'param1' => array(1, 2),
  244. 'param2' => 3,
  245. ),
  246. $this->request->getParams()
  247. );
  248. }
  249. public function testAddParamsOverwrite()
  250. {
  251. $params = array(
  252. 'param1' => 1,
  253. );
  254. $extraParams = array(
  255. 'param1' => 2,
  256. 'param2' => 3,
  257. );
  258. $this->request->setParams($params);
  259. $this->request->addParams($extraParams, true);
  260. $this->assertEquals(
  261. array(
  262. 'param1' => 2,
  263. 'param2' => 3,
  264. ),
  265. $this->request->getParams()
  266. );
  267. }
  268. public function testRemoveParam()
  269. {
  270. $params = array(
  271. 'param1' => 1,
  272. 'param2' => 2,
  273. );
  274. $this->request->setParams($params);
  275. $this->request->removeParam('param2');
  276. $this->assertEquals(
  277. array('param1' => 1),
  278. $this->request->getParams()
  279. );
  280. }
  281. public function testClearParams()
  282. {
  283. $params = array(
  284. 'param1' => 1,
  285. 'param2' => 2,
  286. );
  287. $this->request->setParams($params);
  288. $this->request->clearParams();
  289. $this->assertEquals(
  290. array(),
  291. $this->request->getParams()
  292. );
  293. }
  294. public function testGetAndSetRawData()
  295. {
  296. $data = '1234567890';
  297. $this->request->setRawData($data);
  298. $this->assertEquals(
  299. $data,
  300. $this->request->getRawData()
  301. );
  302. }
  303. public function testSetAndGetHeaders()
  304. {
  305. $headers = array(
  306. 'User-Agent: My Agent',
  307. 'Cache-Control: no-cache'
  308. );
  309. $this->request->setHeaders($headers);
  310. $this->assertEquals(
  311. $headers,
  312. $this->request->getHeaders()
  313. );
  314. }
  315. public function testAddHeader()
  316. {
  317. $headers = array(
  318. 'User-Agent: My Agent',
  319. );
  320. $this->request->setHeaders($headers);
  321. $this->request->addHeader('Cache-Control: no-cache');
  322. $headers[] = 'Cache-Control: no-cache';
  323. $this->assertEquals(
  324. $headers,
  325. $this->request->getHeaders()
  326. );
  327. }
  328. public function testAddHeaders()
  329. {
  330. $headers = array(
  331. 'User-Agent: My Agent',
  332. );
  333. $extraHeaders = array(
  334. 'Cache-Control: no-cache',
  335. 'X-custom: 123',
  336. );
  337. $this->request->setHeaders($headers);
  338. $this->request->addHeaders($extraHeaders);
  339. $this->assertEquals(
  340. array_merge($headers, $extraHeaders),
  341. $this->request->getHeaders()
  342. );
  343. }
  344. public function testClearHeaders()
  345. {
  346. $headers = array(
  347. 'User-Agent: My Agent',
  348. 'Cache-Control: no-cache'
  349. );
  350. $this->request->setHeaders($headers);
  351. $this->assertEquals(
  352. $headers,
  353. $this->request->getHeaders()
  354. );
  355. $this->request->clearHeaders();
  356. $this->assertEquals(
  357. array(),
  358. $this->request->getHeaders()
  359. );
  360. }
  361. public function testGetUri()
  362. {
  363. $this->assertEquals(
  364. '?',
  365. $this->request->getUri()
  366. );
  367. }
  368. public function testGetUriWithHandlerAndParams()
  369. {
  370. $params = array(
  371. 'param1' => 1,
  372. 'param2' => array(2, 3),
  373. );
  374. $this->request->setHandler('myHandler');
  375. $this->request->addParams($params);
  376. $this->assertEquals(
  377. 'myHandler?param1=1&param2=2&param2=3',
  378. $this->request->getUri()
  379. );
  380. }
  381. public function testToString()
  382. {
  383. $options = array(
  384. 'method' => Request::METHOD_POST,
  385. 'handler' => '/myHandler',
  386. 'param' => array(
  387. 'param1' => 1,
  388. 'param2' => 'test content',
  389. ),
  390. 'rawdata' => 'post data',
  391. 'header' => array(
  392. 'myHeader1' => 'X-myHeader1: value1',
  393. 'myHeader2' => 'X-myHeader2: value2',
  394. ),
  395. 'authentication' => array(
  396. 'username' => 'testuser',
  397. 'password' => 'testpass',
  398. ),
  399. 'file' => __FILE__,
  400. );
  401. $this->request->setOptions($options);
  402. $request = <<<EOF
  403. Solarium\Core\Client\Request::__toString
  404. method: POST
  405. header: Array
  406. (
  407. [0] => X-myHeader1: value1
  408. [1] => X-myHeader2: value2
  409. )
  410. authentication: Array
  411. (
  412. [username] => testuser
  413. [password] => testpass
  414. )
  415. resource: /myHandler?param1=1&param2=test+content
  416. resource urldecoded: /myHandler?param1=1&param2=test content
  417. raw data: post data
  418. EOF;
  419. $request .= PHP_EOL.'file upload: '.__FILE__.PHP_EOL;
  420. $this->assertEquals($request, (string) $this->request);
  421. }
  422. public function testGetAndSetAuthentication()
  423. {
  424. $user = 'someone';
  425. $pass = 'S0M3p455';
  426. $this->request->setAuthentication($user, $pass);
  427. $this->assertEquals(
  428. array(
  429. 'username' => $user,
  430. 'password' => $pass,
  431. ),
  432. $this->request->getAuthentication()
  433. );
  434. }
  435. public function testSetAndGetFileUpload()
  436. {
  437. $this->request->setFileUpload(__FILE__);
  438. $this->assertEquals(
  439. __FILE__,
  440. $this->request->getFileUpload()
  441. );
  442. }
  443. public function testSetAndGetFileUploadWithInvalidFile()
  444. {
  445. $this->setExpectedException('Solarium\Exception\RuntimeException');
  446. $this->request->setFileUpload('invalid-filename.dummy');
  447. }
  448. }