ClientTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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\Integration;
  9. use JonnyW\PhantomJs\Test\TestCase;
  10. use JonnyW\PhantomJs\Client;
  11. use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;
  12. /**
  13. * PHP PhantomJs
  14. *
  15. * @author Jon Wenmoth <contact@jonnyw.me>
  16. */
  17. class ClientTest extends TestCase
  18. {
  19. /**
  20. * Test filename
  21. *
  22. * @var string
  23. * @access protected
  24. */
  25. protected $filename;
  26. /**
  27. * Test directory
  28. *
  29. * @var string
  30. * @access protected
  31. */
  32. protected $directory;
  33. /** +++++++++++++++++++++++++++++++++++ **/
  34. /** ++++++++++++++ TESTS ++++++++++++++ **/
  35. /** +++++++++++++++++++++++++++++++++++ **/
  36. /**
  37. * Test additional procedures can be loaded
  38. * through chain loader.
  39. *
  40. * @access public
  41. * @return void
  42. */
  43. public function testAdditionalProceduresCanBeLoadedThroughChainLoader()
  44. {
  45. $content = 'TEST_PROCEDURE';
  46. $procedure = <<<EOF
  47. console.log(JSON.stringify({"content": "$content"}, undefined, 4));
  48. phantom.exit(1);
  49. EOF;
  50. $this->writeProcedure($procedure);
  51. $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
  52. $procedureLoader = $procedureLoaderFactory->createProcedureLoader($this->directory);
  53. $client = $this->getClient();
  54. $client->setProcedure('test');
  55. $client->getProcedureLoader()->addLoader($procedureLoader);
  56. $request = $client->getMessageFactory()->createRequest();
  57. $response = $client->getMessageFactory()->createResponse();
  58. $client->send($request, $response);
  59. $this->assertSame($content, $response->getContent());
  60. }
  61. /**
  62. * Test additional procedures can be loaded
  63. * through chain loader if procedures
  64. * contain comments
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. public function testAdditionalProceduresCanBeLoadedThroughChainLoaderIfProceduresContainComments()
  70. {
  71. $content = 'TEST_PROCEDURE';
  72. $procedure = <<<EOF
  73. console.log(JSON.stringify({"content": "$content"}, undefined, 4));
  74. phantom.exit(1);
  75. var test = function () {
  76. // Test comment
  77. console.log('test');
  78. };
  79. EOF;
  80. $this->writeProcedure($procedure);
  81. $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
  82. $procedureLoader = $procedureLoaderFactory->createProcedureLoader($this->directory);
  83. $client = $this->getClient();
  84. $client->setProcedure('test');
  85. $client->getProcedureLoader()->addLoader($procedureLoader);
  86. $request = $client->getMessageFactory()->createRequest();
  87. $response = $client->getMessageFactory()->createResponse();
  88. $client->send($request, $response);
  89. $this->assertSame($content, $response->getContent());
  90. }
  91. /**
  92. * Test syntax exception is thrown if request
  93. * procedure contains syntax error.
  94. *
  95. * @access public
  96. * @return void
  97. */
  98. public function testSyntaxExceptionIsThrownIfRequestProcedureContainsSyntaxError()
  99. {
  100. $this->setExpectedException('\JonnyW\PhantomJs\Exception\SyntaxException');
  101. $content = 'TEST_PROCEDURE';
  102. $procedure = <<<EOF
  103. console.log(;
  104. EOF;
  105. $this->writeProcedure($procedure);
  106. $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
  107. $procedureLoader = $procedureLoaderFactory->createProcedureLoader($this->directory);
  108. $client = $this->getClient();
  109. $client->setProcedure('test');
  110. $client->getProcedureLoader()->addLoader($procedureLoader);
  111. $request = $client->getMessageFactory()->createRequest();
  112. $response = $client->getMessageFactory()->createResponse();
  113. $client->send($request, $response);
  114. }
  115. /**
  116. * Test response contains 200 status code if page
  117. * is successfully loaded.
  118. *
  119. * @access public
  120. * @return void
  121. */
  122. public function testResponseContains200StatusCodeIfPageIsSuccessfullyLoaded()
  123. {
  124. $client = $this->getClient();
  125. $request = $client->getMessageFactory()->createRequest();
  126. $response = $client->getMessageFactory()->createResponse();
  127. $request->setMethod('GET');
  128. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  129. $client->send($request, $response);
  130. $this->assertEquals(200, $response->getStatus());
  131. }
  132. /**
  133. * Test response contains 200 status code if
  134. * request URL contains reserved characters.
  135. *
  136. * @access public
  137. * @return void
  138. */
  139. public function testResponseContains200StatusCodeIfRequestUrlContainsReservedCharacters()
  140. {
  141. $client = $this->getClient();
  142. $request = $client->getMessageFactory()->createRequest();
  143. $response = $client->getMessageFactory()->createResponse();
  144. $request->setMethod('GET');
  145. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  146. $request->setRequestData(array(
  147. 'test1' => 'http://test.com',
  148. 'test2' => 'A string with an \' ) / # some other invalid [ characters.'
  149. ));
  150. $client->send($request, $response);
  151. $this->assertEquals(200, $response->getStatus());
  152. }
  153. /**
  154. * Test response contains valid body if page is
  155. * successfully loaded.
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. public function testResponseContainsValidBodyIfPageIsSuccessfullyLoaded()
  161. {
  162. $client = $this->getClient();
  163. $request = $client->getMessageFactory()->createRequest();
  164. $response = $client->getMessageFactory()->createResponse();
  165. $request->setMethod('GET');
  166. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  167. $client->send($request, $response);
  168. $this->assertContains('PHANTOMJS_DEFAULT_TEST', $response->getContent());
  169. }
  170. /**
  171. * Test can set user agent in settings
  172. *
  173. * @access public
  174. * @return void
  175. */
  176. public function testCanSetUserAgentInSettings()
  177. {
  178. $client = $this->getClient();
  179. $request = $client->getMessageFactory()->createRequest();
  180. $response = $client->getMessageFactory()->createResponse();
  181. $request->setMethod('GET');
  182. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  183. $request->addSetting('userAgent', 'PhantomJS TEST');
  184. $client->send($request, $response);
  185. $this->assertContains('userAgent=PhantomJS TEST', $response->getContent());
  186. }
  187. /**
  188. * Test can add cookies to request
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. public function testCanAddCookiesToRequest()
  194. {
  195. $client = $this->getClient();
  196. $request = $client->getMessageFactory()->createRequest();
  197. $response = $client->getMessageFactory()->createResponse();
  198. $request->setMethod('GET');
  199. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  200. $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi');
  201. $client->send($request, $response);
  202. $this->assertContains('cookie_test_cookie=TESTING_COOKIES', $response->getContent());
  203. }
  204. /**
  205. * Test can load cookies from
  206. * persistent cookie file
  207. *
  208. * @access public
  209. * @return void
  210. */
  211. public function testCanLoadCookiesFromPersistentCookieFile()
  212. {
  213. $this->filename = 'cookies.txt';
  214. $file = ($this->directory . '/' . $this->filename);
  215. $client = $this->getClient();
  216. $client->getEngine()->addOption('--cookies-file=' . $file);
  217. $request = $client->getMessageFactory()->createRequest();
  218. $response = $client->getMessageFactory()->createResponse();
  219. $expireAt = strtotime('16-Nov-2020 00:00:00');
  220. $request->setMethod('GET');
  221. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  222. $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
  223. $client->send($request, $response);
  224. $this->assertContains('test_cookie=TESTING_COOKIES; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
  225. }
  226. /**
  227. * Test can delete cookie from
  228. * persistent cookie file
  229. *
  230. * @access public
  231. * @return void
  232. */
  233. public function testCanDeleteCookieFromPersistentCookieFile()
  234. {
  235. $this->filename = 'cookies.txt';
  236. $file = ($this->directory . '/' . $this->filename);
  237. $client = $this->getClient();
  238. $client->getEngine()->addOption('--cookies-file=' . $file);
  239. $request = $client->getMessageFactory()->createRequest();
  240. $response = $client->getMessageFactory()->createResponse();
  241. $expireAt = strtotime('16-Nov-2020 00:00:00');
  242. $request->setMethod('GET');
  243. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  244. $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
  245. $client->send($request, $response);
  246. $request = $client->getMessageFactory()->createRequest();
  247. $request->setMethod('GET');
  248. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  249. $request->deleteCookie('test_cookie');
  250. $client->send($request, $response);
  251. $this->assertNotContains('test_cookie=TESTING_COOKIES; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
  252. }
  253. /**
  254. * Test can delete all cookies from
  255. * persistent cookie file
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. public function testCanDeleteAllCookiesFromPersistentCookieFile()
  261. {
  262. $this->filename = 'cookies.txt';
  263. $file = ($this->directory . '/' . $this->filename);
  264. $client = $this->getClient();
  265. $client->getEngine()->addOption('--cookies-file=' . $file);
  266. $request = $client->getMessageFactory()->createRequest();
  267. $response = $client->getMessageFactory()->createResponse();
  268. $expireAt = strtotime('16-Nov-2020 00:00:00');
  269. $request->setMethod('GET');
  270. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  271. $request->addCookie('test_cookie_1', 'TESTING_COOKIES_1', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
  272. $request->addCookie('test_cookie_2', 'TESTING_COOKIES_2', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
  273. $client->send($request, $response);
  274. $request = $client->getMessageFactory()->createRequest();
  275. $request->setMethod('GET');
  276. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  277. $request->deleteCookie('*');
  278. $client->send($request, $response);
  279. $this->assertNotContains('test_cookie_1=TESTING_COOKIES_1; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
  280. $this->assertNotContains('test_cookie_2=TESTING_COOKIES_2; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/)', file_get_contents($file));
  281. }
  282. /**
  283. * Test can load cookies from
  284. * persistent cookie file
  285. *
  286. * @access public
  287. * @return void
  288. */
  289. public function testCookiesPresentInResponse()
  290. {
  291. $client = $this->getClient();
  292. $request = $client->getMessageFactory()->createRequest();
  293. $response = $client->getMessageFactory()->createResponse();
  294. $expireAt = strtotime('16-Nov-2020 00:00:00');
  295. $request->setMethod('GET');
  296. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  297. $request->addCookie('test_cookie', 'TESTING_COOKIES', '/', '.jonnyw.kiwi', true, false, ($expireAt * 1000));
  298. $client->send($request, $response);
  299. $cookies = $response->getCookies();
  300. $this->assertEquals(array(
  301. 'domain' => '.jonnyw.kiwi',
  302. 'expires' => 'Mon, 16 Nov 2020 00:00:00 GMT',
  303. 'expiry' => '1605484800',
  304. 'httponly' => true,
  305. 'name' => 'test_cookie',
  306. 'path' => '/',
  307. 'secure' => false,
  308. 'value' => 'TESTING_COOKIES',
  309. ), $cookies[0]);
  310. }
  311. /**
  312. * Test response contains console error if a
  313. * javascript error exists on the page.
  314. *
  315. * @access public
  316. * @return void
  317. */
  318. public function testResponseContainsConsoleErrorIfAJavascriptErrorExistsOnThePage()
  319. {
  320. $client = $this->getClient();
  321. $request = $client->getMessageFactory()->createRequest();
  322. $response = $client->getMessageFactory()->createResponse();
  323. $request->setMethod('GET');
  324. $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
  325. $client->send($request, $response);
  326. $console = $response->getConsole();
  327. $this->assertCount(1, $console);
  328. $this->assertContains('ReferenceError: Can\'t find variable: invalid', $console[0]['message']);
  329. }
  330. /**
  331. * Test response contains console trace if a
  332. * javascript error exists on the page.
  333. *
  334. * @access public
  335. * @return void
  336. */
  337. public function testResponseContainsConsoleTraceIfAJavascriptErrorExistsOnThePage()
  338. {
  339. $client = $this->getClient();
  340. $request = $client->getMessageFactory()->createRequest();
  341. $response = $client->getMessageFactory()->createResponse();
  342. $request->setMethod('GET');
  343. $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
  344. $client->send($request, $response);
  345. $console = $response->getConsole();
  346. $this->assertCount(1, $console[0]['trace']);
  347. }
  348. /**
  349. * Test response contains headers.
  350. *
  351. * @access public
  352. * @return void
  353. */
  354. public function testResponseContainsHeaders()
  355. {
  356. $client = $this->getClient();
  357. $request = $client->getMessageFactory()->createRequest();
  358. $response = $client->getMessageFactory()->createResponse();
  359. $request->setMethod('GET');
  360. $request->setUrl('http://www.jonnyw.kiwi/tests/test-console-error.php');
  361. $client->send($request, $response);
  362. $this->assertNotEmpty($response->getHeaders());
  363. }
  364. /**
  365. * Test redirect URL is set in response
  366. * if request is redirected.
  367. *
  368. * @access public
  369. * @return void
  370. */
  371. public function testRedirectUrlIsSetInResponseIfRequestIsRedirected()
  372. {
  373. $client = $this->getClient();
  374. $request = $client->getMessageFactory()->createRequest();
  375. $response = $client->getMessageFactory()->createResponse();
  376. $request->setMethod('GET');
  377. $request->setUrl('https://jigsaw.w3.org/HTTP/300/302.html');
  378. $client->send($request, $response);
  379. $this->assertNotEmpty($response->getRedirectUrl());
  380. }
  381. /**
  382. * Test POST request sends request data.
  383. *
  384. * @access public
  385. * @return void
  386. */
  387. public function testPostRequestSendsRequestData()
  388. {
  389. $client = $this->getClient();
  390. $request = $client->getMessageFactory()->createRequest();
  391. $response = $client->getMessageFactory()->createResponse();
  392. $request->setMethod('POST');
  393. $request->setUrl('http://www.jonnyw.kiwi/tests/test-post.php');
  394. $request->setRequestData(array(
  395. 'test1' => 'http://test.com',
  396. 'test2' => 'A string with an \' ) / # some other invalid [ characters.'
  397. ));
  398. $client->send($request, $response);
  399. $this->assertContains(sprintf('<li>test1=%s</li>', 'http://test.com'), $response->getContent());
  400. $this->assertContains(sprintf('<li>test2=%s</li>', 'A string with an \' ) / # some other invalid [ characters.'), $response->getContent());
  401. }
  402. /**
  403. * Test capture request saves file to
  404. * to local disk.
  405. *
  406. * @access public
  407. * @return void
  408. */
  409. public function testCaptureRequestSavesFileToLocalDisk()
  410. {
  411. $this->filename = 'test.jpg';
  412. $file = ($this->directory . '/' . $this->filename);
  413. $client = $this->getClient();
  414. $request = $client->getMessageFactory()->createCaptureRequest();
  415. $response = $client->getMessageFactory()->createResponse();
  416. $request->setMethod('GET');
  417. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  418. $request->setOutputFile($file);
  419. $client->send($request, $response);
  420. $this->assertTrue(file_exists($file));
  421. }
  422. /**
  423. * Test capture request saves file to
  424. * disk with correct capture dimensions.
  425. *
  426. * @access public
  427. * @return void
  428. */
  429. public function testCaptureRequestSavesFileToDiskWithCorrectCaptureDimensions()
  430. {
  431. $this->filename = 'test.jpg';
  432. $file = ($this->directory . '/' . $this->filename);
  433. $width = 200;
  434. $height = 400;
  435. $client = $this->getClient();
  436. $request = $client->getMessageFactory()->createCaptureRequest();
  437. $response = $client->getMessageFactory()->createResponse();
  438. $request->setMethod('GET');
  439. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  440. $request->setOutputFile($file);
  441. $request->setCaptureDimensions($width, $height);
  442. $client->send($request, $response);
  443. $imageInfo = getimagesize($file);
  444. $this->assertEquals($width, $imageInfo[0]);
  445. $this->assertEquals($height, $imageInfo[1]);
  446. }
  447. /**
  448. * Test PDF request saves pdf to
  449. * to local disk.
  450. *
  451. * @access public
  452. * @return void
  453. */
  454. public function testPdfRequestSavesPdfToLocalDisk()
  455. {
  456. $this->filename = 'test.pdf';
  457. $file = ($this->directory . '/' . $this->filename);
  458. $client = $this->getClient();
  459. $request = $client->getMessageFactory()->createPdfRequest();
  460. $response = $client->getMessageFactory()->createResponse();
  461. $request->setMethod('GET');
  462. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  463. $request->setOutputFile($file);
  464. $client->send($request, $response);
  465. $this->assertTrue(file_exists($file));
  466. }
  467. /**
  468. * Test PDF request saves file to
  469. * disk with correct paper size.
  470. *
  471. * @access public
  472. * @return void
  473. */
  474. public function testPdfRequestSavesFileToDiskWithCorrectPaperSize()
  475. {
  476. $this->filename = 'test.pdf';
  477. $file = ($this->directory . '/' . $this->filename);
  478. $width = 20;
  479. $height = 30;
  480. $client = $this->getClient();
  481. $request = $client->getMessageFactory()->createPdfRequest();
  482. $response = $client->getMessageFactory()->createResponse();
  483. $request->setMethod('GET');
  484. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  485. $request->setOutputFile($file);
  486. $request->setPaperSize(sprintf('%scm', $width), sprintf('%scm', $height));
  487. $request->setMargin('0cm');
  488. $client->send($request, $response);
  489. $pdf = \ZendPdf\PdfDocument::load($file);
  490. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  491. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  492. $this->assertEquals($width, $pdfWidth);
  493. $this->assertEquals($height, $pdfHeight);
  494. }
  495. /**
  496. * Test PDF request saves file to
  497. * disk with correct format size.
  498. *
  499. * @access public
  500. * @return void
  501. */
  502. public function testPdfRequestSavesFileToDiskWithCorrectFormatSize()
  503. {
  504. $this->filename = 'test.pdf';
  505. $file = ($this->directory . '/' . $this->filename);
  506. $client = $this->getClient();
  507. $request = $client->getMessageFactory()->createPdfRequest();
  508. $response = $client->getMessageFactory()->createResponse();
  509. $request->setMethod('GET');
  510. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  511. $request->setOutputFile($file);
  512. $request->setFormat('A4');
  513. $request->setMargin('0cm');
  514. $client->send($request, $response);
  515. $pdf = \ZendPdf\PdfDocument::load($file);
  516. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  517. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  518. $this->assertEquals(21, $pdfWidth);
  519. $this->assertEquals(30, $pdfHeight);
  520. }
  521. /**
  522. * Test PDF request saves file to
  523. * disk with correct orientation.
  524. *
  525. * @access public
  526. * @return void
  527. */
  528. public function testPdfRequestSavesFileToDiskWithCorrectOrientation()
  529. {
  530. $this->filename = 'test.pdf';
  531. $file = ($this->directory . '/' . $this->filename);
  532. $client = $this->getClient();
  533. $request = $client->getMessageFactory()->createPdfRequest();
  534. $response = $client->getMessageFactory()->createResponse();
  535. $request->setMethod('GET');
  536. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  537. $request->setOutputFile($file);
  538. $request->setFormat('A4');
  539. $request->setOrientation('landscape');
  540. $request->setMargin('0cm');
  541. $client->send($request, $response);
  542. $pdf = \ZendPdf\PdfDocument::load($file);
  543. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  544. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  545. $this->assertEquals(30, $pdfWidth);
  546. $this->assertEquals(21, $pdfHeight);
  547. }
  548. /**
  549. * Test can set repeating header
  550. * for PDF request
  551. *
  552. * @access public
  553. * @return void
  554. */
  555. public function testCanSetRepeatingHeaderForPDFRequest()
  556. {
  557. $this->filename = 'test.pdf';
  558. $file = ($this->directory . '/' . $this->filename);
  559. $client = $this->getClient();
  560. $request = $client->getMessageFactory()->createPdfRequest();
  561. $response = $client->getMessageFactory()->createResponse();
  562. $request->setMethod('GET');
  563. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  564. $request->setOutputFile($file);
  565. $request->setFormat('A4');
  566. $request->setOrientation('landscape');
  567. $request->setMargin('0cm');
  568. $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
  569. $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
  570. $client->send($request, $response);
  571. $parser = new \Smalot\PdfParser\Parser();
  572. $pdf = $parser->parseFile($file);
  573. $text = str_replace(' ', '', $pdf->getText());
  574. $this->assertContains('Header', $text);
  575. }
  576. /**
  577. * Test can set repeating footer
  578. * for PDF request
  579. *
  580. * @access public
  581. * @return void
  582. */
  583. public function testCanSetRepeatingFooterForPDFRequest()
  584. {
  585. $this->filename = 'test.pdf';
  586. $file = ($this->directory . '/' . $this->filename);
  587. $client = $this->getClient();
  588. $request = $client->getMessageFactory()->createPdfRequest();
  589. $response = $client->getMessageFactory()->createResponse();
  590. $request->setMethod('GET');
  591. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  592. $request->setOutputFile($file);
  593. $request->setFormat('A4');
  594. $request->setOrientation('landscape');
  595. $request->setMargin('0cm');
  596. $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
  597. $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
  598. $client->send($request, $response);
  599. $parser = new \Smalot\PdfParser\Parser();
  600. $pdf = $parser->parseFile($file);
  601. $text = str_replace(' ', '', $pdf->getText());
  602. $this->assertContains('Footer', $text);
  603. }
  604. /**
  605. * Test set viewport size sets
  606. * size of viewport in default
  607. * request.
  608. *
  609. * @access public
  610. * @return void
  611. */
  612. public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
  613. {
  614. $width = 100;
  615. $height = 200;
  616. $client = $this->getClient();
  617. $request = $client->getMessageFactory()->createRequest();
  618. $response = $client->getMessageFactory()->createResponse();
  619. $request->setMethod('GET');
  620. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  621. $request->setViewportsize($width, $height);
  622. $client->send($request, $response);
  623. $logs = explode("\n", $client->getLog());
  624. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  625. $this->assertTrue(($startIndex !== false));
  626. }
  627. /**
  628. * Test set viewport size sets
  629. * size of viewport in capture
  630. * request.
  631. *
  632. * @access public
  633. * @return void
  634. */
  635. public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
  636. {
  637. $width = 100;
  638. $height = 200;
  639. $client = $this->getClient();
  640. $request = $client->getMessageFactory()->createCaptureRequest();
  641. $response = $client->getMessageFactory()->createResponse();
  642. $request->setMethod('GET');
  643. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  644. $request->setViewportsize($width, $height);
  645. $client->send($request, $response);
  646. $logs = explode("\n", $client->getLog());
  647. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  648. $this->assertTrue(($startIndex !== false));
  649. }
  650. /**
  651. * Test delay logs start time
  652. * in client for default request.
  653. *
  654. * @access public
  655. * @return void
  656. */
  657. public function testDelayLogsStartTimeInClientForDefaultRequest()
  658. {
  659. $delay = 1;
  660. $client = $this->getClient();
  661. $request = $client->getMessageFactory()->createRequest();
  662. $response = $client->getMessageFactory()->createResponse();
  663. $request->setMethod('GET');
  664. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  665. $request->setDelay($delay);
  666. $client->send($request, $response);
  667. $logs = explode("\n", $client->getLog());
  668. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  669. $this->assertTrue(($startIndex !== false));
  670. }
  671. /**
  672. * Test delay logs end time
  673. * in client for default request.
  674. *
  675. * @access public
  676. * @return void
  677. */
  678. public function testDelayLogsEndTimeInClientForDefaultRequest()
  679. {
  680. $delay = 1;
  681. $client = $this->getClient();
  682. $request = $client->getMessageFactory()->createRequest();
  683. $response = $client->getMessageFactory()->createResponse();
  684. $request->setMethod('GET');
  685. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  686. $request->setDelay($delay);
  687. $client->send($request, $response);
  688. $logs = explode("\n", $client->getLog());
  689. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  690. $this->assertTrue(($endIndex !== false));
  691. }
  692. /**
  693. * Test delay delays page render for
  694. * specified time for default request.
  695. *
  696. * @access public
  697. * @return void
  698. */
  699. public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
  700. {
  701. $delay = 1;
  702. $client = $this->getClient();
  703. $request = $client->getMessageFactory()->createRequest();
  704. $response = $client->getMessageFactory()->createResponse();
  705. $request->setMethod('GET');
  706. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  707. $request->setDelay($delay);
  708. $client->send($request, $response);
  709. $logs = explode("\\n", $client->getLog());
  710. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  711. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  712. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  713. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  714. $this->assertSame(($startTime+$delay), $endTime);
  715. }
  716. /**
  717. * Test delay logs start time
  718. * in client for capture request.
  719. *
  720. * @access public
  721. * @return void
  722. */
  723. public function testDelayLogsStartTimeInClientForCaptureRequest()
  724. {
  725. $delay = 1;
  726. $client = $this->getClient();
  727. $request = $client->getMessageFactory()->createCaptureRequest();
  728. $response = $client->getMessageFactory()->createResponse();
  729. $request->setMethod('GET');
  730. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  731. $request->setDelay($delay);
  732. $client->send($request, $response);
  733. $logs = explode("\\n", $client->getLog());
  734. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  735. $this->assertTrue(($startIndex !== false));
  736. }
  737. /**
  738. * Test delay logs end time
  739. * in client for capture request.
  740. *
  741. * @access public
  742. * @return void
  743. */
  744. public function testDelayLogsEndTimeInClientForCaptureRequest()
  745. {
  746. $delay = 1;
  747. $client = $this->getClient();
  748. $request = $client->getMessageFactory()->createCaptureRequest();
  749. $response = $client->getMessageFactory()->createResponse();
  750. $request->setMethod('GET');
  751. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  752. $request->setDelay($delay);
  753. $client->send($request, $response);
  754. $logs = explode("\\n", $client->getLog());
  755. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  756. $this->assertTrue(($endIndex !== false));
  757. }
  758. /**
  759. * Test delay delays page render for
  760. * specified time for capture request.
  761. *
  762. * @access public
  763. * @return void
  764. */
  765. public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
  766. {
  767. $delay = 1;
  768. $client = $this->getClient();
  769. $request = $client->getMessageFactory()->createCaptureRequest();
  770. $response = $client->getMessageFactory()->createResponse();
  771. $request->setMethod('GET');
  772. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  773. $request->setDelay($delay);
  774. $client->send($request, $response);
  775. $logs = explode("\\n", $client->getLog());
  776. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  777. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  778. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  779. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  780. $this->assertSame(($startTime+$delay), $endTime);
  781. }
  782. /**
  783. * Test lazy request returns content after
  784. * all resources are loaded
  785. *
  786. * @access public
  787. * @return void
  788. */
  789. public function testLazyRequestReturnsResourcesAfterAllResourcesAreLoaded()
  790. {
  791. $client = $this->getClient();
  792. $client->isLazy();
  793. $request = $client->getMessageFactory()->createRequest();
  794. $response = $client->getMessageFactory()->createResponse();
  795. $request->setMethod('GET');
  796. $request->setUrl('http://www.jonnyw.kiwi/tests/test-lazy.php');
  797. $request->setTimeout(5000);
  798. $client->send($request, $response);
  799. $this->assertContains('<p id="content">loaded</p>', $response->getContent());
  800. }
  801. /**
  802. * Test content is returned for lazy request
  803. * if timeout is reached before resource is
  804. * loaded
  805. *
  806. * @access public
  807. * @return void
  808. */
  809. public function testContentIsReturnedForLazyRequestIfTimeoutIsReachedBeforeResourceIsLoaded()
  810. {
  811. $client = $this->getClient();
  812. $client->isLazy();
  813. $request = $client->getMessageFactory()->createRequest();
  814. $response = $client->getMessageFactory()->createResponse();
  815. $request->setMethod('GET');
  816. $request->setUrl('http://www.jonnyw.kiwi/tests/test-lazy.php');
  817. $request->setTimeout(1000);
  818. $client->send($request, $response);
  819. $this->assertContains('<p id="content"></p>', $response->getContent());
  820. }
  821. /**
  822. * Test debug logs debug info to
  823. * client log.
  824. *
  825. * @access public
  826. * @return void
  827. */
  828. public function testDebugLogsDebugInfoToClientLog()
  829. {
  830. $client = $this->getClient();
  831. $client->getEngine()->debug(true);
  832. $request = $client->getMessageFactory()->createRequest();
  833. $response = $client->getMessageFactory()->createResponse();
  834. $request->setMethod('GET');
  835. $request->setUrl('http://www.jonnyw.kiwi/tests/test-default.php');
  836. $client->send($request, $response);
  837. $this->assertContains('[DEBUG]', $client->getLog());
  838. }
  839. /**
  840. * Test test can set page
  841. * background color
  842. *
  843. * @access public
  844. * @return void
  845. */
  846. public function testCanSetPageBackgroundColor()
  847. {
  848. $this->filename = 'test.jpg';
  849. $file = ($this->directory . '/' . $this->filename);
  850. $client = $this->getClient();
  851. $request = $client->getMessageFactory()->createCaptureRequest();
  852. $response = $client->getMessageFactory()->createResponse();
  853. $request->setMethod('GET');
  854. $request->setUrl('http://www.jonnyw.kiwi/tests/test-capture.php');
  855. $request->setBodyStyles(array('backgroundColor' => 'red'));
  856. $request->setOutputFile($file);
  857. $client->send($request, $response);
  858. $this->assertContains('body style="background-color: red;"', $response->getContent());
  859. }
  860. /** +++++++++++++++++++++++++++++++++++ **/
  861. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  862. /** +++++++++++++++++++++++++++++++++++ **/
  863. /**
  864. * Get client instance.
  865. *
  866. * @return \JonnyW\PhantomJs\Client
  867. */
  868. protected function getClient()
  869. {
  870. $serviceContainer = ServiceContainer::getInstance();
  871. $client = new Client(
  872. $serviceContainer->get('engine'),
  873. $serviceContainer->get('procedure_loader'),
  874. $serviceContainer->get('procedure_compiler'),
  875. $serviceContainer->get('message_factory')
  876. );
  877. return $client;
  878. }
  879. /** +++++++++++++++++++++++++++++++++++ **/
  880. /** ++++++++++++ UTILITIES ++++++++++++ **/
  881. /** +++++++++++++++++++++++++++++++++++ **/
  882. /**
  883. * Set up test environment.
  884. *
  885. * @access public
  886. * @return void
  887. */
  888. public function setUp()
  889. {
  890. $this->filename = 'test.proc';
  891. $this->directory = sys_get_temp_dir();
  892. if (!is_writable($this->directory)) {
  893. throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
  894. }
  895. }
  896. /**
  897. * Tear down test environment.
  898. *
  899. * @access public
  900. * @return void
  901. */
  902. public function tearDown()
  903. {
  904. $filename = $this->getFilename();
  905. if (file_exists($filename)) {
  906. unlink($filename);
  907. }
  908. }
  909. /**
  910. * Get test filename.
  911. *
  912. * @access public
  913. * @return string
  914. */
  915. public function getFilename()
  916. {
  917. return sprintf('%1$s/%2$s', $this->directory, $this->filename);
  918. }
  919. /**
  920. * Write procedure body to file.
  921. *
  922. * @access public
  923. * @param string $data
  924. * @return string
  925. */
  926. public function writeProcedure($procedure)
  927. {
  928. $filename = $this->getFilename();
  929. file_put_contents($filename, $procedure);
  930. return $filename;
  931. }
  932. /**
  933. * Get log entry index.
  934. *
  935. * @access public
  936. * @param array $logs
  937. * @param string $search
  938. * @return int|false
  939. */
  940. public function getLogEntryIndex(array $logs, $search)
  941. {
  942. foreach ($logs as $index => $log) {
  943. $pos = stripos($log, $search);
  944. if ($pos !== false) {
  945. return $index;
  946. }
  947. }
  948. return false;
  949. }
  950. }