CaptureRequestTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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\Message;
  9. use JonnyW\PhantomJs\Message\CaptureRequest;
  10. use JonnyW\PhantomJs\Message\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 get type returns capture request type.
  23. *
  24. * @access public
  25. * @return void
  26. */
  27. public function testGetTypeReturnsCaptureRequestType()
  28. {
  29. $captureRequest = $this->getCaptureRequest();
  30. $this->assertSame(RequestInterface::REQUEST_TYPE_CAPTURE, $captureRequest->getType());
  31. }
  32. /**
  33. * Test URL can be set via constructor.
  34. *
  35. * @access public
  36. * @return void
  37. */
  38. public function testUrlCanBeSetViaConstructor()
  39. {
  40. $url = 'http://test.com';
  41. $captureRequest = $this->getCaptureRequest($url);
  42. $this->assertSame($url, $captureRequest->getUrl());
  43. }
  44. /**
  45. * Test method can be set via constructor.
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. public function testMethodCanBeSetViaConstructor()
  51. {
  52. $method = 'GET';
  53. $captureRequest = $this->getCaptureRequest(null, $method);
  54. $this->assertSame($method, $captureRequest->getMethod());
  55. }
  56. /**
  57. * Test timeout can be set via constructor.
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. public function testTimeoutCanBeSetViaConstructor()
  63. {
  64. $timeout = 100000;
  65. $captureRequest = $this->getCaptureRequest('http://test.com', 'GET', $timeout);
  66. $this->assertSame($timeout, $captureRequest->getTimeout());
  67. }
  68. /**
  69. * Test set method throws invalid method
  70. * exception if an invalid method is set
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. public function testSetMethodThrowsInvalidMethodExceptionIfAnInvalidMethodIsSet()
  76. {
  77. $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
  78. $captureRequest = $this->getCaptureRequest();
  79. $captureRequest->setMethod('INVALID_METHOD');
  80. }
  81. /**
  82. * Test set method throws invalid method
  83. * exception if an invalid method is set
  84. * via constructor
  85. *
  86. * @access public
  87. * @return void
  88. */
  89. public function testSetMethodThrowsInvalidMethodExceptionIfAnInvalidMethodIsSetViaConstructor()
  90. {
  91. $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidMethodException');
  92. $this->getCaptureRequest('http://test.com', 'INVALID_METHOD');
  93. }
  94. /**
  95. * Test set capture dimensions sets
  96. * rect width.
  97. *
  98. * @access public
  99. * @return void
  100. */
  101. public function testSetCaptureDimensionsSetsRectWidth()
  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 set capture dimensions sets
  111. * rect height.
  112. *
  113. * @access public
  114. * @return void
  115. */
  116. public function testSetCaptureDimensionsSetsRectHeight()
  117. {
  118. $width = 100;
  119. $height = 200;
  120. $captureRequest = $this->getCaptureRequest();
  121. $captureRequest->setCaptureDimensions($width, $height);
  122. $this->assertSame($height, $captureRequest->getRectHeight());
  123. }
  124. /**
  125. * Test set capture dimensions sets
  126. * rect top.
  127. *
  128. * @access public
  129. * @return void
  130. */
  131. public function testSetCaptureDimensionsSetsRectTop()
  132. {
  133. $width = 100;
  134. $height = 200;
  135. $top = 50;
  136. $captureRequest = $this->getCaptureRequest();
  137. $captureRequest->setCaptureDimensions($width, $height, $top);
  138. $this->assertSame($top, $captureRequest->getRectTop());
  139. }
  140. /**
  141. * Test set capture dimensions sets
  142. * rect left.
  143. *
  144. * @access public
  145. * @return void
  146. */
  147. public function testSetCaptureDimensionsSetsRectLeft()
  148. {
  149. $width = 100;
  150. $height = 200;
  151. $left = 50;
  152. $captureRequest = $this->getCaptureRequest();
  153. $captureRequest->setCaptureDimensions($width, $height, 0, $left);
  154. $this->assertSame($left, $captureRequest->getRectLeft());
  155. }
  156. /**
  157. * Test set URL throws invalid
  158. * URL exception if an invalid
  159. * URL is set.
  160. *
  161. * @access public
  162. * @return void
  163. */
  164. public function testSetUrlThrowsInvalidUrlExceptionIfAnInvalidUrlIsSet()
  165. {
  166. $this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');
  167. $captureRequest = $this->getCaptureRequest();
  168. $captureRequest->setUrl('\\AnInvalidUrl');
  169. }
  170. /**
  171. * Test get URL returns URL without query
  172. * paramters if method is not HEAD or GET.
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. public function testGetUrlReturnsUrlWithoutQueryParametersIfMethodIsNotHeadOrGet()
  178. {
  179. $url = 'http://test.com';
  180. $data = array(
  181. 'test_param1' => 'Testing1',
  182. 'test_param2' => 'Testing2'
  183. );
  184. $captureRequest = $this->getCaptureRequest();
  185. $captureRequest->setMethod('POST');
  186. $captureRequest->setUrl($url);
  187. $captureRequest->setRequestData($data);
  188. $this->assertSame($url, $captureRequest->getUrl());
  189. }
  190. /**
  191. * Test get URL returns URL with query
  192. * parameters if method is GET.
  193. *
  194. * @access public
  195. * @return void
  196. */
  197. public function testGetUrlReturnsUrlWithQueryParametersIfMethodIsGet()
  198. {
  199. $url = 'http://test.com';
  200. $data = array(
  201. 'test_param1' => 'Testing1',
  202. 'test_param2' => 'Testing2'
  203. );
  204. $captureRequest = $this->getCaptureRequest();
  205. $captureRequest->setMethod('GET');
  206. $captureRequest->setUrl($url);
  207. $captureRequest->setRequestData($data);
  208. $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
  209. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  210. }
  211. /**
  212. * Test get URL returns URL with query
  213. * parameters if method is HEAD.
  214. *
  215. * @access public
  216. * @return void
  217. */
  218. public function testGetUrlReturnsUrlWithQueryParametersIfMethodIsHead()
  219. {
  220. $url = 'http://test.com';
  221. $data = array(
  222. 'test_param1' => 'Testing1',
  223. 'test_param2' => 'Testing2'
  224. );
  225. $captureRequest = $this->getCaptureRequest();
  226. $captureRequest->setMethod('HEAD');
  227. $captureRequest->setUrl($url);
  228. $captureRequest->setRequestData($data);
  229. $expectedUrl = $url . '?test_param1=Testing1&test_param2=Testing2';
  230. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  231. }
  232. /**
  233. * Test get URL returns URL with query
  234. * parameters if method is HEAD.
  235. *
  236. * @access public
  237. * @return void
  238. */
  239. public function testGetUrlAppendsQueryParametersIfUrlHasExistingQueryParameters()
  240. {
  241. $url = 'http://test.com?existing_param=Existing';
  242. $data = array(
  243. 'test_param1' => 'Testing1',
  244. 'test_param2' => 'Testing2'
  245. );
  246. $captureRequest = $this->getCaptureRequest();
  247. $captureRequest->setMethod('GET');
  248. $captureRequest->setUrl($url);
  249. $captureRequest->setRequestData($data);
  250. $expectedUrl = $url . '&test_param1=Testing1&test_param2=Testing2';
  251. $this->assertSame($expectedUrl, $captureRequest->getUrl());
  252. }
  253. /**
  254. * Test get body returns an empty
  255. * string if method is GET.
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. public function testGetBodyReturnsAnEmptyStringIfMethodIsGet()
  261. {
  262. $data = array(
  263. 'test_param1' => 'Testing1',
  264. 'test_param2' => 'Testing2'
  265. );
  266. $captureRequest = $this->getCaptureRequest();
  267. $captureRequest->setMethod('GET');
  268. $captureRequest->setRequestData($data);
  269. $this->assertSame('', $captureRequest->getBody());
  270. }
  271. /**
  272. * Test get body returns and empty
  273. * string if method is HEAD.
  274. *
  275. * @access public
  276. * @return void
  277. */
  278. public function testGetBodyReturnsAnEmptyStringIfMethodIsHead()
  279. {
  280. $data = array(
  281. 'test_param1' => 'Testing1',
  282. 'test_param2' => 'Testing2'
  283. );
  284. $captureRequest = $this->getCaptureRequest();
  285. $captureRequest->setMethod('HEAD');
  286. $captureRequest->setRequestData($data);
  287. $this->assertSame('', $captureRequest->getBody());
  288. }
  289. /**
  290. * Test get body returns query string if
  291. * method is not HEAD or GET.
  292. *
  293. * @access public
  294. * @return void
  295. */
  296. public function testGetBodyReturnsQueryStringIfMethodIsNotHeadOrGet()
  297. {
  298. $data = array(
  299. 'test_param1' => 'Testing1',
  300. 'test_param2' => 'Testing2'
  301. );
  302. $captureRequest = $this->getCaptureRequest();
  303. $captureRequest->setMethod('POST');
  304. $captureRequest->setRequestData($data);
  305. $body = 'test_param1=Testing1&test_param2=Testing2';
  306. $this->assertSame($body, $captureRequest->getBody());
  307. }
  308. /**
  309. * Test get request data returns flattened
  310. * request data if flatten is set to true.
  311. *
  312. * @access public
  313. * @return void
  314. */
  315. public function testGetRequestDataReturnsFlattenedRequestDataIfFlattenIsSetToTrue()
  316. {
  317. $data = array(
  318. 'test_param1' => 'Testing1',
  319. 'test_param2' => array(
  320. 'Testing2',
  321. 'Testing3'
  322. )
  323. );
  324. $captureRequest = $this->getCaptureRequest();
  325. $captureRequest->setRequestData($data);
  326. $flatData = array(
  327. 'test_param1' => 'Testing1',
  328. 'test_param2[0]' => 'Testing2',
  329. 'test_param2[1]' => 'Testing3'
  330. );
  331. $this->assertSame($flatData, $captureRequest->getRequestData(true));
  332. }
  333. /**
  334. * Test get request data returns unflattened
  335. * request data if flatten is set to false
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. public function testGetRequestDataReturnsUnflattenedRequestDataIfFlattenIsSetToFalse()
  341. {
  342. $data = array(
  343. 'test_param1' => 'Testing1',
  344. 'test_param2' => array(
  345. 'Testing2',
  346. 'Testing3'
  347. )
  348. );
  349. $captureRequest = $this->getCaptureRequest();
  350. $captureRequest->setRequestData($data);
  351. $this->assertSame($data, $captureRequest->getRequestData(false));
  352. }
  353. /**
  354. * Test add headers merge headers with
  355. * existing headers.
  356. *
  357. * @access public
  358. * @return void
  359. */
  360. public function testAddHeadersMergesHeadersWithExistingHeaders()
  361. {
  362. $existingHeaders = array(
  363. 'Header1' => 'Header 1'
  364. );
  365. $newHeaders = array(
  366. 'Header2' => 'Header 2',
  367. 'Header3' => 'Header 3'
  368. );
  369. $captureRequest = $this->getCaptureRequest();
  370. $captureRequest->setHeaders($existingHeaders);
  371. $captureRequest->addHeaders($newHeaders);
  372. $expectedHeaders = array_merge($existingHeaders, $newHeaders);
  373. $this->assertSame($expectedHeaders, $captureRequest->getHeaders());
  374. }
  375. /**
  376. * Test get headers returns JSON encoded
  377. * headers if format is set to JSON.
  378. *
  379. * @access public
  380. * @return void
  381. */
  382. public function testGetHeadersReturnsJsonEncodedHeadersIfFormatIsSetToJson()
  383. {
  384. $headers = array(
  385. 'Header1' => 'Header 1',
  386. 'Header2' => 'Header 2'
  387. );
  388. $captureRequest = $this->getCaptureRequest();
  389. $captureRequest->setHeaders($headers);
  390. $expectedHeaders = json_encode($headers);
  391. $this->assertSame($expectedHeaders, $captureRequest->getHeaders('json'));
  392. }
  393. /**
  394. * Test get headers returns headers as
  395. * array if format is not set to json
  396. *
  397. * @access public
  398. * @return void
  399. */
  400. public function testGetHeadersReturnsHeadersAsArrayIfFormatIsNotSetToJson()
  401. {
  402. $headers = array(
  403. 'Header1' => 'Header 1',
  404. 'Header2' => 'Header 2'
  405. );
  406. $captureRequest = $this->getCaptureRequest();
  407. $captureRequest->setHeaders($headers);
  408. $this->assertSame($headers, $captureRequest->getHeaders('default'));
  409. }
  410. /**
  411. * Test set capture file throws not
  412. * writable exception if file path
  413. * is not writable.
  414. *
  415. * @access public
  416. * @return void
  417. */
  418. public function testSetCaptureFileThrowsNotWriteableExceptionIfFilePathIsNotWriteable()
  419. {
  420. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
  421. $invalidPath = '/invalid/path';
  422. $captureRequest = $this->getCaptureRequest();
  423. $captureRequest->setCaptureFile($invalidPath);
  424. }
  425. /**
  426. * Test get capture file returns capture
  427. * file if capture file is set.
  428. *
  429. * @access public
  430. * @return void
  431. */
  432. public function testGetCaptureFileReturnsCaptureFileIfCaptureFileIsSet()
  433. {
  434. $captureFile = sprintf('%s/test.jpg', sys_get_temp_dir());
  435. $captureRequest = $this->getCaptureRequest();
  436. $captureRequest->setCaptureFile($captureFile);
  437. $this->assertSame($captureFile, $captureRequest->getCaptureFile());
  438. }
  439. /** +++++++++++++++++++++++++++++++++++ **/
  440. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  441. /** +++++++++++++++++++++++++++++++++++ **/
  442. /**
  443. * Get capture request instance.
  444. *
  445. * @access protected
  446. * @param string $url (default: null)
  447. * @param string $method (default: RequestInterface::METHOD_GET)
  448. * @param int $timeout (default: 5000)
  449. * @return \JonnyW\PhantomJs\Message\CaptureRequest
  450. */
  451. protected function getCaptureRequest($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
  452. {
  453. $captureRequest = new CaptureRequest($url, $method, $timeout);
  454. return $captureRequest;
  455. }
  456. }