ClientTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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://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://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://jonnyw.kiwi/tests/test-default.php');
  167. $client->send($request, $response);
  168. $this->assertContains('PHANTOMJS_DEFAULT_TEST', $response->getContent());
  169. }
  170. /**
  171. * Test response contains console error if a
  172. * javascript error exists on the page.
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. public function testResponseContainsConsoleErrorIfAJavascriptErrorExistsOnThePage()
  178. {
  179. $client = $this->getClient();
  180. $request = $client->getMessageFactory()->createRequest();
  181. $response = $client->getMessageFactory()->createResponse();
  182. $request->setMethod('GET');
  183. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  184. $client->send($request, $response);
  185. $console = $response->getConsole();
  186. $this->assertCount(1, $console);
  187. $this->assertContains('ReferenceError: Can\'t find variable: invalid', $console[0]['message']);
  188. }
  189. /**
  190. * Test response contains console trace if a
  191. * javascript error exists on the page.
  192. *
  193. * @access public
  194. * @return void
  195. */
  196. public function testResponseContainsConsoleTraceIfAJavascriptErrorExistsOnThePage()
  197. {
  198. $client = $this->getClient();
  199. $request = $client->getMessageFactory()->createRequest();
  200. $response = $client->getMessageFactory()->createResponse();
  201. $request->setMethod('GET');
  202. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  203. $client->send($request, $response);
  204. $console = $response->getConsole();
  205. $this->assertCount(1, $console[0]['trace']);
  206. }
  207. /**
  208. * Test response contains headers.
  209. *
  210. * @access public
  211. * @return void
  212. */
  213. public function testResponseContainsHeaders()
  214. {
  215. $client = $this->getClient();
  216. $request = $client->getMessageFactory()->createRequest();
  217. $response = $client->getMessageFactory()->createResponse();
  218. $request->setMethod('GET');
  219. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  220. $client->send($request, $response);
  221. $this->assertNotEmpty($response->getHeaders());
  222. }
  223. /**
  224. * Test redirect URL is set in response
  225. * if request is redirected.
  226. *
  227. * @access public
  228. * @return void
  229. */
  230. public function testRedirectUrlIsSetInResponseIfRequestIsRedirected()
  231. {
  232. $client = $this->getClient();
  233. $request = $client->getMessageFactory()->createRequest();
  234. $response = $client->getMessageFactory()->createResponse();
  235. $request->setMethod('GET');
  236. $request->setUrl('https://jigsaw.w3.org/HTTP/300/302.html');
  237. $client->send($request, $response);
  238. $this->assertNotEmpty($response->getRedirectUrl());
  239. }
  240. /**
  241. * Test POST request sends request data.
  242. *
  243. * @access public
  244. * @return void
  245. */
  246. public function testPostRequestSendsRequestData()
  247. {
  248. $client = $this->getClient();
  249. $request = $client->getMessageFactory()->createRequest();
  250. $response = $client->getMessageFactory()->createResponse();
  251. $request->setMethod('POST');
  252. $request->setUrl('http://jonnyw.kiwi/tests/test-post.php');
  253. $request->setRequestData(array(
  254. 'test1' => 'http://test.com',
  255. 'test2' => 'A string with an \' ) / # some other invalid [ characters.'
  256. ));
  257. $client->send($request, $response);
  258. $this->assertContains(sprintf('<li>test1=%s</li>', 'http://test.com'), $response->getContent());
  259. $this->assertContains(sprintf('<li>test2=%s</li>', 'A string with an \' ) / # some other invalid [ characters.'), $response->getContent());
  260. }
  261. /**
  262. * Test capture request saves file to
  263. * to local disk.
  264. *
  265. * @access public
  266. * @return void
  267. */
  268. public function testCaptureRequestSavesFileToLocalDisk()
  269. {
  270. $this->filename = 'test.jpg';
  271. $file = ($this->directory . '/' . $this->filename);
  272. $client = $this->getClient();
  273. $request = $client->getMessageFactory()->createCaptureRequest();
  274. $response = $client->getMessageFactory()->createResponse();
  275. $request->setMethod('GET');
  276. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  277. $request->setOutputFile($file);
  278. $client->send($request, $response);
  279. $this->assertTrue(file_exists($file));
  280. }
  281. /**
  282. * Test capture request saves file to
  283. * disk with correct capture dimensions.
  284. *
  285. * @access public
  286. * @return void
  287. */
  288. public function testCaptureRequestSavesFileToDiskWithCorrectCaptureDimensions()
  289. {
  290. $this->filename = 'test.jpg';
  291. $file = ($this->directory . '/' . $this->filename);
  292. $width = 200;
  293. $height = 400;
  294. $client = $this->getClient();
  295. $request = $client->getMessageFactory()->createCaptureRequest();
  296. $response = $client->getMessageFactory()->createResponse();
  297. $request->setMethod('GET');
  298. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  299. $request->setOutputFile($file);
  300. $request->setCaptureDimensions($width, $height);
  301. $client->send($request, $response);
  302. $imageInfo = getimagesize($file);
  303. $this->assertEquals($width, $imageInfo[0]);
  304. $this->assertEquals($height, $imageInfo[1]);
  305. }
  306. /**
  307. * Test PDF request saves pdf to
  308. * to local disk.
  309. *
  310. * @access public
  311. * @return void
  312. */
  313. public function testPdfRequestSavesPdfToLocalDisk()
  314. {
  315. $this->filename = 'test.pdf';
  316. $file = ($this->directory . '/' . $this->filename);
  317. $client = $this->getClient();
  318. $request = $client->getMessageFactory()->createPdfRequest();
  319. $response = $client->getMessageFactory()->createResponse();
  320. $request->setMethod('GET');
  321. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  322. $request->setOutputFile($file);
  323. $client->send($request, $response);
  324. $this->assertTrue(file_exists($file));
  325. }
  326. /**
  327. * Test PDF request saves file to
  328. * disk with correct paper size.
  329. *
  330. * @access public
  331. * @return void
  332. */
  333. public function testPdfRequestSavesFileToDiskWithCorrectPaperSize()
  334. {
  335. $this->filename = 'test.pdf';
  336. $file = ($this->directory . '/' . $this->filename);
  337. $width = 20;
  338. $height = 30;
  339. $client = $this->getClient();
  340. $request = $client->getMessageFactory()->createPdfRequest();
  341. $response = $client->getMessageFactory()->createResponse();
  342. $request->setMethod('GET');
  343. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  344. $request->setOutputFile($file);
  345. $request->setPaperSize(sprintf('%scm', $width), sprintf('%scm', $height));
  346. $request->setMargin('0cm');
  347. $client->send($request, $response);
  348. $pdf = \ZendPdf\PdfDocument::load($file);
  349. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  350. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  351. $this->assertEquals($width, $pdfWidth);
  352. $this->assertEquals($height, $pdfHeight);
  353. }
  354. /**
  355. * Test PDF request saves file to
  356. * disk with correct format size.
  357. *
  358. * @access public
  359. * @return void
  360. */
  361. public function testPdfRequestSavesFileToDiskWithCorrectFormatSize()
  362. {
  363. $this->filename = 'test.pdf';
  364. $file = ($this->directory . '/' . $this->filename);
  365. $client = $this->getClient();
  366. $request = $client->getMessageFactory()->createPdfRequest();
  367. $response = $client->getMessageFactory()->createResponse();
  368. $request->setMethod('GET');
  369. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  370. $request->setOutputFile($file);
  371. $request->setFormat('A4');
  372. $request->setMargin('0cm');
  373. $client->send($request, $response);
  374. $pdf = \ZendPdf\PdfDocument::load($file);
  375. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  376. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  377. $this->assertEquals(21, $pdfWidth);
  378. $this->assertEquals(30, $pdfHeight);
  379. }
  380. /**
  381. * Test PDF request saves file to
  382. * disk with correct orientation.
  383. *
  384. * @access public
  385. * @return void
  386. */
  387. public function testPdfRequestSavesFileToDiskWithCorrectOrientation()
  388. {
  389. $this->filename = 'test.pdf';
  390. $file = ($this->directory . '/' . $this->filename);
  391. $client = $this->getClient();
  392. $request = $client->getMessageFactory()->createPdfRequest();
  393. $response = $client->getMessageFactory()->createResponse();
  394. $request->setMethod('GET');
  395. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  396. $request->setOutputFile($file);
  397. $request->setFormat('A4');
  398. $request->setOrientation('landscape');
  399. $request->setMargin('0cm');
  400. $client->send($request, $response);
  401. $pdf = \ZendPdf\PdfDocument::load($file);
  402. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  403. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  404. $this->assertEquals(30, $pdfWidth);
  405. $this->assertEquals(21, $pdfHeight);
  406. }
  407. /**
  408. * Test can set repeating header
  409. * for PDF request
  410. *
  411. * @access public
  412. * @return void
  413. */
  414. public function testCanSetRepeatingHeaderForPDFRequest()
  415. {
  416. $this->filename = 'test.pdf';
  417. $file = ($this->directory . '/' . $this->filename);
  418. $client = $this->getClient();
  419. $request = $client->getMessageFactory()->createPdfRequest();
  420. $response = $client->getMessageFactory()->createResponse();
  421. $request->setMethod('GET');
  422. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  423. $request->setOutputFile($file);
  424. $request->setFormat('A4');
  425. $request->setOrientation('landscape');
  426. $request->setMargin('0cm');
  427. $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
  428. $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
  429. $client->send($request, $response);
  430. $parser = new \Smalot\PdfParser\Parser();
  431. $pdf = $parser->parseFile($file);
  432. $text = str_replace(' ', '', $pdf->getText());
  433. $this->assertContains('Header', $text);
  434. }
  435. /**
  436. * Test can set repeating footer
  437. * for PDF request
  438. *
  439. * @access public
  440. * @return void
  441. */
  442. public function testCanSetRepeatingFooterForPDFRequest()
  443. {
  444. $this->filename = 'test.pdf';
  445. $file = ($this->directory . '/' . $this->filename);
  446. $client = $this->getClient();
  447. $request = $client->getMessageFactory()->createPdfRequest();
  448. $response = $client->getMessageFactory()->createResponse();
  449. $request->setMethod('GET');
  450. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  451. $request->setOutputFile($file);
  452. $request->setFormat('A4');
  453. $request->setOrientation('landscape');
  454. $request->setMargin('0cm');
  455. $request->setRepeatingHeader('<h1>Header <span style="float:right">%pageNum% / %pageTotal%</span></h1>', '2cm');
  456. $request->setRepeatingFooter('<footer>Footer <span style="float:right">%pageNum% / %pageTotal%</span></footer>', '2cm');
  457. $client->send($request, $response);
  458. $parser = new \Smalot\PdfParser\Parser();
  459. $pdf = $parser->parseFile($file);
  460. $text = str_replace(' ', '', $pdf->getText());
  461. $this->assertContains('Footer', $text);
  462. }
  463. /**
  464. * Test set viewport size sets
  465. * size of viewport in default
  466. * request.
  467. *
  468. * @access public
  469. * @return void
  470. */
  471. public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
  472. {
  473. $width = 100;
  474. $height = 200;
  475. $client = $this->getClient();
  476. $request = $client->getMessageFactory()->createRequest();
  477. $response = $client->getMessageFactory()->createResponse();
  478. $request->setMethod('GET');
  479. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  480. $request->setViewportsize($width, $height);
  481. $client->send($request, $response);
  482. $logs = explode("\n", $client->getLog());
  483. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  484. $this->assertTrue(($startIndex !== false));
  485. }
  486. /**
  487. * Test set viewport size sets
  488. * size of viewport in capture
  489. * request.
  490. *
  491. * @access public
  492. * @return void
  493. */
  494. public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
  495. {
  496. $width = 100;
  497. $height = 200;
  498. $client = $this->getClient();
  499. $request = $client->getMessageFactory()->createCaptureRequest();
  500. $response = $client->getMessageFactory()->createResponse();
  501. $request->setMethod('GET');
  502. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  503. $request->setViewportsize($width, $height);
  504. $client->send($request, $response);
  505. $logs = explode("\n", $client->getLog());
  506. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  507. $this->assertTrue(($startIndex !== false));
  508. }
  509. /**
  510. * Test delay logs start time
  511. * in client for default request.
  512. *
  513. * @access public
  514. * @return void
  515. */
  516. public function testDelayLogsStartTimeInClientForDefaultRequest()
  517. {
  518. $delay = 1;
  519. $client = $this->getClient();
  520. $request = $client->getMessageFactory()->createRequest();
  521. $response = $client->getMessageFactory()->createResponse();
  522. $request->setMethod('GET');
  523. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  524. $request->setDelay($delay);
  525. $client->send($request, $response);
  526. $logs = explode("\n", $client->getLog());
  527. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  528. $this->assertTrue(($startIndex !== false));
  529. }
  530. /**
  531. * Test delay logs end time
  532. * in client for default request.
  533. *
  534. * @access public
  535. * @return void
  536. */
  537. public function testDelayLogsEndTimeInClientForDefaultRequest()
  538. {
  539. $delay = 1;
  540. $client = $this->getClient();
  541. $request = $client->getMessageFactory()->createRequest();
  542. $response = $client->getMessageFactory()->createResponse();
  543. $request->setMethod('GET');
  544. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  545. $request->setDelay($delay);
  546. $client->send($request, $response);
  547. $logs = explode("\n", $client->getLog());
  548. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  549. $this->assertTrue(($endIndex !== false));
  550. }
  551. /**
  552. * Test delay delays page render for
  553. * specified time for default request.
  554. *
  555. * @access public
  556. * @return void
  557. */
  558. public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
  559. {
  560. $delay = 1;
  561. $client = $this->getClient();
  562. $request = $client->getMessageFactory()->createRequest();
  563. $response = $client->getMessageFactory()->createResponse();
  564. $request->setMethod('GET');
  565. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  566. $request->setDelay($delay);
  567. $client->send($request, $response);
  568. $logs = explode("\\n", $client->getLog());
  569. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  570. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  571. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  572. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  573. $this->assertSame(($startTime+$delay), $endTime);
  574. }
  575. /**
  576. * Test delay logs start time
  577. * in client for capture request.
  578. *
  579. * @access public
  580. * @return void
  581. */
  582. public function testDelayLogsStartTimeInClientForCaptureRequest()
  583. {
  584. $delay = 1;
  585. $client = $this->getClient();
  586. $request = $client->getMessageFactory()->createCaptureRequest();
  587. $response = $client->getMessageFactory()->createResponse();
  588. $request->setMethod('GET');
  589. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  590. $request->setDelay($delay);
  591. $client->send($request, $response);
  592. $logs = explode("\\n", $client->getLog());
  593. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  594. $this->assertTrue(($startIndex !== false));
  595. }
  596. /**
  597. * Test delay logs end time
  598. * in client for capture request.
  599. *
  600. * @access public
  601. * @return void
  602. */
  603. public function testDelayLogsEndTimeInClientForCaptureRequest()
  604. {
  605. $delay = 1;
  606. $client = $this->getClient();
  607. $request = $client->getMessageFactory()->createCaptureRequest();
  608. $response = $client->getMessageFactory()->createResponse();
  609. $request->setMethod('GET');
  610. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  611. $request->setDelay($delay);
  612. $client->send($request, $response);
  613. $logs = explode("\\n", $client->getLog());
  614. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  615. $this->assertTrue(($endIndex !== false));
  616. }
  617. /**
  618. * Test delay delays page render for
  619. * specified time for capture request.
  620. *
  621. * @access public
  622. * @return void
  623. */
  624. public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
  625. {
  626. $delay = 1;
  627. $client = $this->getClient();
  628. $request = $client->getMessageFactory()->createCaptureRequest();
  629. $response = $client->getMessageFactory()->createResponse();
  630. $request->setMethod('GET');
  631. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  632. $request->setDelay($delay);
  633. $client->send($request, $response);
  634. $logs = explode("\\n", $client->getLog());
  635. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  636. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  637. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  638. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  639. $this->assertSame(($startTime+$delay), $endTime);
  640. }
  641. /**
  642. * Test debug logs debug info to
  643. * client log.
  644. *
  645. * @access public
  646. * @return void
  647. */
  648. public function testDebugLogsDebugInfoToClientLog()
  649. {
  650. $client = $this->getClient();
  651. $client->getEngine()->debug(true);
  652. $request = $client->getMessageFactory()->createRequest();
  653. $response = $client->getMessageFactory()->createResponse();
  654. $request->setMethod('GET');
  655. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  656. $client->send($request, $response);
  657. $this->assertContains('[DEBUG]', $client->getLog());
  658. }
  659. /**
  660. * Test test can set page
  661. * background color
  662. *
  663. * @access public
  664. * @return void
  665. */
  666. public function testCanSetPageBackgroundColor()
  667. {
  668. $this->filename = 'test.jpg';
  669. $file = ($this->directory . '/' . $this->filename);
  670. $client = $this->getClient();
  671. $request = $client->getMessageFactory()->createCaptureRequest();
  672. $response = $client->getMessageFactory()->createResponse();
  673. $request->setMethod('GET');
  674. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  675. $request->setBodyStyles(array('backgroundColor' => 'red'));
  676. $request->setOutputFile($file);
  677. $client->send($request, $response);
  678. $this->assertContains('body style="background-color: red;"', $response->getContent());
  679. }
  680. /** +++++++++++++++++++++++++++++++++++ **/
  681. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  682. /** +++++++++++++++++++++++++++++++++++ **/
  683. /**
  684. * Get client instance.
  685. *
  686. * @return \JonnyW\PhantomJs\Client
  687. */
  688. protected function getClient()
  689. {
  690. $serviceContainer = ServiceContainer::getInstance();
  691. $client = new Client(
  692. $serviceContainer->get('engine'),
  693. $serviceContainer->get('procedure_loader'),
  694. $serviceContainer->get('procedure_compiler'),
  695. $serviceContainer->get('message_factory')
  696. );
  697. return $client;
  698. }
  699. /** +++++++++++++++++++++++++++++++++++ **/
  700. /** ++++++++++++ UTILITIES ++++++++++++ **/
  701. /** +++++++++++++++++++++++++++++++++++ **/
  702. /**
  703. * Set up test environment.
  704. *
  705. * @access public
  706. * @return void
  707. */
  708. public function setUp()
  709. {
  710. $this->filename = 'test.proc';
  711. $this->directory = sys_get_temp_dir();
  712. if (!is_writable($this->directory)) {
  713. throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
  714. }
  715. }
  716. /**
  717. * Tear down test environment.
  718. *
  719. * @access public
  720. * @return void
  721. */
  722. public function tearDown()
  723. {
  724. $filename = $this->getFilename();
  725. if (file_exists($filename)) {
  726. unlink($filename);
  727. }
  728. }
  729. /**
  730. * Get test filename.
  731. *
  732. * @access public
  733. * @return string
  734. */
  735. public function getFilename()
  736. {
  737. return sprintf('%1$s/%2$s', $this->directory, $this->filename);
  738. }
  739. /**
  740. * Write procedure body to file.
  741. *
  742. * @access public
  743. * @param string $data
  744. * @return string
  745. */
  746. public function writeProcedure($procedure)
  747. {
  748. $filename = $this->getFilename();
  749. file_put_contents($filename, $procedure);
  750. return $filename;
  751. }
  752. /**
  753. * Get log entry index.
  754. *
  755. * @access public
  756. * @param array $logs
  757. * @param string $search
  758. * @return int|false
  759. */
  760. public function getLogEntryIndex(array $logs, $search)
  761. {
  762. foreach ($logs as $index => $log) {
  763. $pos = stripos($log, $search);
  764. if ($pos !== false) {
  765. return $index;
  766. }
  767. }
  768. return false;
  769. }
  770. }