/src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php

https://gitlab.com/agungpambudi/php-phantomjs · PHP · 568 lines · 274 code · 94 blank · 200 comment · 0 complexity · 5c434453dd0f5e62eaf894cf2c10725f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Tests\Unit\Http;
  9. use JonnyW\PhantomJs\Http\CaptureRequest;
  10. use JonnyW\PhantomJs\Http\RequestInterface;
  11. /**
  12. * PHP PhantomJs
  13. *
  14. * @author Jon Wenmoth <contact@jonnyw.me>
  15. */
  16. class CaptureRequestTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /** +++++++++++++++++++++++++++++++++++ **/
  19. /** ++++++++++++++ TESTS ++++++++++++++ **/
  20. /** +++++++++++++++++++++++++++++++++++ **/
  21. /**
  22. * Test capture type is returned by default
  23. * if no type is set.
  24. *
  25. * @access public
  26. * @return void
  27. */
  28. public function testCaptureTypeIsReturnedByDefaultIfNotTypeIsSet()
  29. {
  30. $captureRequest = $this->getCaptureRequest();
  31. $this->assertSame(RequestInterface::REQUEST_TYPE_CAPTURE, $captureRequest->getType());
  32. }
  33. /**
  34. * Test custom type can be set.
  35. *
  36. * @access public
  37. * @return void
  38. */
  39. public function testCustomTypeCanBeSet()
  40. {
  41. $requestType = 'testType';
  42. $captureRequest = $this->getCaptureRequest();
  43. $captureRequest->setType($requestType);
  44. $this->assertSame($requestType, $captureRequest->getType());
  45. }
  46. /**
  47. * Test URL can be set via constructor.
  48. *
  49. * @access public
  50. * @return void
  51. */
  52. public function testUrlCanBeSetViaConstructor()
  53. {
  54. $url = 'http://test.com';
  55. $captureRequest = $this->getCaptureRequest($url);
  56. $this->assertSame($url, $captureRequest->getUrl());
  57. }
  58. /**
  59. * Test method can be set via constructor.
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. public function testMethodCanBeSetViaConstructor()
  65. {
  66. $method = 'GET';
  67. $captureRequest = $this->getCaptureRequest(null, $method);
  68. $this->assertSame($method, $captureRequest->getMethod());
  69. }
  70. /**
  71. * Test timeout can be set via constructor.
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. public function testTimeoutCanBeSetViaConstructor()
  77. {
  78. $timeout = 100000;
  79. $captureRequest = $this->getCaptureRequest('http://test.com', 'GET', $timeout);
  80. $this->assertSame($timeout, $captureRequest->getTimeout());
  81. }
  82. /**
  83. * Test invalid method is thrown if method
  84. * is invalid.
  85. *
  86. * @access public
  87. * @return void
  88. */
  89. public function testInvalidMethodIsThrownIfMethodIsInvalid()
  90. {
  91. $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
  92. $captureRequest = $this->getCaptureRequest();
  93. $captureRequest->setMethod('INVALID_METHOD');
  94. }
  95. /**
  96. * Test rect width can be set.
  97. *
  98. * @access public
  99. * @return void
  100. */
  101. public function testRectWidthCanBeSet()
  102. {
  103. $width = 100;
  104. $height = 200;
  105. $captureRequest = $this->getCaptureRequest();
  106. $captureRequest->setCaptureDimensions($width, $height);
  107. $this->assertSame($width, $captureRequest->getRectWidth());
  108. }
  109. /**
  110. * Test rect height can be set.
  111. *
  112. * @access public
  113. * @return void
  114. */
  115. public function testRectHeightCanBeSet()
  116. {
  117. $width = 100;
  118. $height = 200;
  119. $captureRequest = $this->getCaptureRequest();
  120. $captureRequest->setCaptureDimensions($width, $height);
  121. $this->assertSame($height, $captureRequest->getRectHeight());
  122. }
  123. /**
  124. * Test rect top can be set.
  125. *
  126. * @access public
  127. * @return void
  128. */
  129. public function testRectTopCanBeSet()
  130. {
  131. $width = 100;
  132. $height = 200;
  133. $top = 50;
  134. $captureRequest = $this->getCaptureRequest();
  135. $captureRequest->setCaptureDimensions($width, $height, $top);
  136. $this->assertSame($top, $captureRequest->getRectTop());
  137. }
  138. /**
  139. * Test rect left can be set.
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. public function testRectLeftCanBeSet()
  145. {
  146. $width = 100;
  147. $height = 200;
  148. $left = 50;
  149. $captureRequest = $this->getCaptureRequest();
  150. $captureRequest->setCaptureDimensions($width, $height, 0, $left);
  151. $this->assertSame($left, $captureRequest->getRectLeft());
  152. }
  153. /**
  154. * Test invalid URL exception is thrown
  155. * if URL is invalid format.
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. public function testInvalidUrlExceptionIsThrownIfUrlIsInvalidFormat()
  161. {
  162. $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');
  163. $captureRequest = $this->getCaptureRequest();
  164. $captureRequest->setUrl('\\AnInvalidUrl');
  165. }
  166. /**
  167. * Test URL does not contain query params if
  168. * mehtod is not HEAD or GET.
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. public function testUrlDoesNotContainQueryParamsIfMethodIsNotHeadOrGet()
  174. {
  175. $url = 'http://test.com';
  176. $data = array(
  177. 'test_param1' => 'Testing1',
  178. 'test_param2' => 'Testing2'
  179. );
  180. $captureRequest = $this->getCaptureRequest();
  181. $captureRequest->setMethod('POST');
  182. $captureRequest->setUrl($url);
  183. $captureRequest->setRequestData($data);
  184. $this->assertSame($url, $captureRequest->getUrl());
  185. }
  186. /**
  187. * Test URL does contain query params if mehthod
  188. * is GET.
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. public function testUrlDoesContainQueryParamsIfMethodIsGet()
  194. {
  195. $url = 'http://test.com';
  196. $data = array(
  197. 'test_param1' => 'Testing1',
  198. 'test_param2' => 'Testing2'
  199. );
  200. $captureRequest = $this->getCaptureRequest();
  201. $captureRequest->setMethod('GET');
  202. $captureRequest->setUrl($url);
  203. $captureRequest->setRequestData($data);
  204. $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
  205. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  206. }
  207. /**
  208. * Test URL does contain query params if method
  209. * is HEAD.
  210. *
  211. * @access public
  212. * @return void
  213. */
  214. public function testUrlDoesContainQueryParamsIfMethodIsHead()
  215. {
  216. $url = 'http://test.com';
  217. $data = array(
  218. 'test_param1' => 'Testing1',
  219. 'test_param2' => 'Testing2'
  220. );
  221. $captureRequest = $this->getCaptureRequest();
  222. $captureRequest->setMethod('HEAD');
  223. $captureRequest->setUrl($url);
  224. $captureRequest->setRequestData($data);
  225. $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
  226. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  227. }
  228. /**
  229. * Test query params are appended to URL if
  230. * URL contains existng query params.
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. public function testQueryParamsAreAppendedToUrlIfUrlContainsExistingQueryParams()
  236. {
  237. $url = 'http://test.com?existing_param=Existing';
  238. $data = array(
  239. 'test_param1' => 'Testing1',
  240. 'test_param2' => 'Testing2'
  241. );
  242. $captureRequest = $this->getCaptureRequest();
  243. $captureRequest->setMethod('GET');
  244. $captureRequest->setUrl($url);
  245. $captureRequest->setRequestData($data);
  246. $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
  247. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  248. }
  249. /**
  250. * Test request contains no body if method
  251. * is GET.
  252. *
  253. * @access public
  254. * @return void
  255. */
  256. public function testRequestContainsNoBodyIfMethodIsGet()
  257. {
  258. $data = array(
  259. 'test_param1' => 'Testing1',
  260. 'test_param2' => 'Testing2'
  261. );
  262. $captureRequest = $this->getCaptureRequest();
  263. $captureRequest->setMethod('GET');
  264. $captureRequest->setRequestData($data);
  265. $this->assertSame('', $captureRequest->getBody());
  266. }
  267. /**
  268. * Test request contains no body if method
  269. * is HEAD.
  270. *
  271. * @access public
  272. * @return void
  273. */
  274. public function testRequestContainsNoBodyIfMethodIsHead()
  275. {
  276. $data = array(
  277. 'test_param1' => 'Testing1',
  278. 'test_param2' => 'Testing2'
  279. );
  280. $captureRequest = $this->getCaptureRequest();
  281. $captureRequest->setMethod('HEAD');
  282. $captureRequest->setRequestData($data);
  283. $this->assertSame('', $captureRequest->getBody());
  284. }
  285. /**
  286. * Test request contains a body if method is
  287. * not HEAD or GET.
  288. *
  289. * @access public
  290. * @return void
  291. */
  292. public function testRequestContainsABodyIfMethodIsNotHeadOrGet()
  293. {
  294. $data = array(
  295. 'test_param1' => 'Testing1',
  296. 'test_param2' => 'Testing2'
  297. );
  298. $captureRequest = $this->getCaptureRequest();
  299. $captureRequest->setMethod('POST');
  300. $captureRequest->setRequestData($data);
  301. $body = 'test_param1=Testing1&test_param2=Testing2';
  302. $this->assertSame($body, $captureRequest->getBody());
  303. }
  304. /**
  305. * Test request data can be flattened.
  306. *
  307. * @access public
  308. * @return void
  309. */
  310. public function testRequestDataCanBeFalttened()
  311. {
  312. $data = array(
  313. 'test_param1' => 'Testing1',
  314. 'test_param2' => array(
  315. 'Testing2',
  316. 'Testing3'
  317. )
  318. );
  319. $captureRequest = $this->getCaptureRequest();
  320. $captureRequest->setRequestData($data);
  321. $flatData = array(
  322. 'test_param1' => 'Testing1',
  323. 'test_param2[0]' => 'Testing2',
  324. 'test_param2[1]' => 'Testing3'
  325. );
  326. $this->assertSame($flatData, $captureRequest->getRequestData(true));
  327. }
  328. /**
  329. * Test raw request data can be accessed.
  330. *
  331. * @access public
  332. * @return void
  333. */
  334. public function testRawRequestDataCanBeAccessed()
  335. {
  336. $data = array(
  337. 'test_param1' => 'Testing1',
  338. 'test_param2' => array(
  339. 'Testing2',
  340. 'Testing3'
  341. )
  342. );
  343. $captureRequest = $this->getCaptureRequest();
  344. $captureRequest->setRequestData($data);
  345. $this->assertSame($data, $captureRequest->getRequestData(false));
  346. }
  347. /**
  348. * Test headers can be added.
  349. *
  350. * @access public
  351. * @return void
  352. */
  353. public function testHeadersCanBeAdded()
  354. {
  355. $existingHeaders = array(
  356. 'Header1' => 'Header 1'
  357. );
  358. $newHeaders = array(
  359. 'Header2' => 'Header 2',
  360. 'Header3' => 'Header 3'
  361. );
  362. $captureRequest = $this->getCaptureRequest();
  363. $captureRequest->setHeaders($existingHeaders);
  364. $captureRequest->addHeaders($newHeaders);
  365. $expectedHeaders = array_merge($existingHeaders, $newHeaders);
  366. $this->assertSame($expectedHeaders, $captureRequest->getHeaders());
  367. }
  368. /**
  369. * Test headers can be accessed in
  370. * JSON format
  371. *
  372. * @access public
  373. * @return void
  374. */
  375. public function testHeadersCanBeAccessedInJsonFormat()
  376. {
  377. $headers = array(
  378. 'Header1' => 'Header 1',
  379. 'Header2' => 'Header 2'
  380. );
  381. $captureRequest = $this->getCaptureRequest();
  382. $captureRequest->setHeaders($headers);
  383. $expectedHeaders = json_encode($headers);
  384. $this->assertSame($expectedHeaders, $captureRequest->getHeaders('json'));
  385. }
  386. /**
  387. * Test raw headers can be accessed.
  388. *
  389. * @access public
  390. * @return void
  391. */
  392. public function testRawHeadersCanBeAccessed()
  393. {
  394. $headers = array(
  395. 'Header1' => 'Header 1',
  396. 'Header2' => 'Header 2'
  397. );
  398. $captureRequest = $this->getCaptureRequest();
  399. $captureRequest->setHeaders($headers);
  400. $this->assertSame($headers, $captureRequest->getHeaders('default'));
  401. }
  402. /**
  403. * Test not writable exception is thrown if
  404. * output path is not writable.
  405. *
  406. * @access public
  407. * @return void
  408. */
  409. public function tesNotWritableExceptonIsThrownIfOutputPathIsNotWritable()
  410. {
  411. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
  412. $invalidPath = '/invalid/path';
  413. $captureRequest = $this->getCaptureRequest();
  414. $captureRequest->setOutputFile($invalidPath);
  415. }
  416. /**
  417. * Test can set output file.
  418. *
  419. * @access public
  420. * @return void
  421. */
  422. public function testCanSetOutputFile()
  423. {
  424. $outputFile = sprintf('%s/test.jpg', sys_get_temp_dir());
  425. $captureRequest = $this->getCaptureRequest();
  426. $captureRequest->setOutputFile($outputFile);
  427. $this->assertSame($outputFile, $captureRequest->getOutputFile());
  428. }
  429. /**
  430. * Test can set viewport width.
  431. *
  432. * @access public
  433. * @return void
  434. */
  435. public function testCanSetViewportWidth()
  436. {
  437. $width = 100;
  438. $height = 200;
  439. $caputreRequest = $this->getCaptureRequest();
  440. $caputreRequest->setViewportSize($width, $height);
  441. $this->assertSame($width, $caputreRequest->getViewportWidth());
  442. }
  443. /**
  444. * Test can set viewport height.
  445. *
  446. * @access public
  447. * @return void
  448. */
  449. public function testCanSetViewportHeight()
  450. {
  451. $width = 100;
  452. $height = 200;
  453. $caputreRequest = $this->getCaptureRequest();
  454. $caputreRequest->setViewportSize($width, $height);
  455. $this->assertSame($height, $caputreRequest->getViewportHeight());
  456. }
  457. /** +++++++++++++++++++++++++++++++++++ **/
  458. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  459. /** +++++++++++++++++++++++++++++++++++ **/
  460. /**
  461. * Get capture request instance.
  462. *
  463. * @access protected
  464. * @param string $url (default: null)
  465. * @param string $method (default: RequestInterface::METHOD_GET)
  466. * @param int $timeout (default: 5000)
  467. * @return \JonnyW\PhantomJs\Http\CaptureRequest
  468. */
  469. protected function getCaptureRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
  470. {
  471. $captureRequest = new CaptureRequest($url, $method, $timeout);
  472. return $captureRequest;
  473. }
  474. }