ClientTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 syntax exception is thrown if request
  63. * procedure contains syntax error.
  64. *
  65. * @access public
  66. * @return void
  67. */
  68. public function testSyntaxExceptionIsThrownIfRequestProcedureContainsSyntaxError()
  69. {
  70. $this->setExpectedException('\JonnyW\PhantomJs\Exception\SyntaxException');
  71. $content = 'TEST_PROCEDURE';
  72. $procedure = <<<EOF
  73. console.log(;
  74. EOF;
  75. $this->writeProcedure($procedure);
  76. $procedureLoaderFactory = $this->getContainer()->get('procedure_loader_factory');
  77. $procedureLoader = $procedureLoaderFactory->createProcedureLoader($this->directory);
  78. $client = $this->getClient();
  79. $client->setProcedure('test');
  80. $client->getProcedureLoader()->addLoader($procedureLoader);
  81. $request = $client->getMessageFactory()->createRequest();
  82. $response = $client->getMessageFactory()->createResponse();
  83. $client->send($request, $response);
  84. }
  85. /**
  86. * Test response contains 200 status code if page
  87. * is successfully loaded.
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. public function testResponseContains200StatusCodeIfPageIsSuccessfullyLoaded()
  93. {
  94. $client = $this->getClient();
  95. $request = $client->getMessageFactory()->createRequest();
  96. $response = $client->getMessageFactory()->createResponse();
  97. $request->setMethod('GET');
  98. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  99. $client->send($request, $response);
  100. $this->assertEquals(200, $response->getStatus());
  101. }
  102. /**
  103. * Test response contains 200 status code if
  104. * request URL contains reserved characters.
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. public function testResponseContains200StatusCodeIfRequestUrlContainsReservedCharacters()
  110. {
  111. $client = $this->getClient();
  112. $request = $client->getMessageFactory()->createRequest();
  113. $response = $client->getMessageFactory()->createResponse();
  114. $request->setMethod('GET');
  115. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  116. $request->setRequestData(array(
  117. 'test1' => 'http://test.com',
  118. 'test2' => 'A string with an \' ) / # some other invalid [ characters.'
  119. ));
  120. $client->send($request, $response);
  121. $this->assertEquals(200, $response->getStatus());
  122. }
  123. /**
  124. * Test response contains valid body if page is
  125. * successfully loaded.
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. public function testResponseContainsValidBodyIfPageIsSuccessfullyLoaded()
  131. {
  132. $client = $this->getClient();
  133. $request = $client->getMessageFactory()->createRequest();
  134. $response = $client->getMessageFactory()->createResponse();
  135. $request->setMethod('GET');
  136. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  137. $client->send($request, $response);
  138. $this->assertContains('PHANTOMJS_DEFAULT_TEST', $response->getContent());
  139. }
  140. /**
  141. * Test response contains console error if a
  142. * javascript error exists on the page.
  143. *
  144. * @access public
  145. * @return void
  146. */
  147. public function testResponseContainsConsoleErrorIfAJavascriptErrorExistsOnThePage()
  148. {
  149. $client = $this->getClient();
  150. $request = $client->getMessageFactory()->createRequest();
  151. $response = $client->getMessageFactory()->createResponse();
  152. $request->setMethod('GET');
  153. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  154. $client->send($request, $response);
  155. $console = $response->getConsole();
  156. $this->assertCount(1, $console);
  157. $this->assertContains('ReferenceError: Can\'t find variable: invalid', $console[0]['message']);
  158. }
  159. /**
  160. * Test response contains console trace if a
  161. * javascript error exists on the page.
  162. *
  163. * @access public
  164. * @return void
  165. */
  166. public function testResponseContainsConsoleTraceIfAJavascriptErrorExistsOnThePage()
  167. {
  168. $client = $this->getClient();
  169. $request = $client->getMessageFactory()->createRequest();
  170. $response = $client->getMessageFactory()->createResponse();
  171. $request->setMethod('GET');
  172. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  173. $client->send($request, $response);
  174. $console = $response->getConsole();
  175. $this->assertCount(1, $console[0]['trace']);
  176. }
  177. /**
  178. * Test response contains headers.
  179. *
  180. * @access public
  181. * @return void
  182. */
  183. public function testResponseContainsHeaders()
  184. {
  185. $client = $this->getClient();
  186. $request = $client->getMessageFactory()->createRequest();
  187. $response = $client->getMessageFactory()->createResponse();
  188. $request->setMethod('GET');
  189. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  190. $client->send($request, $response);
  191. $this->assertNotEmpty($response->getHeaders());
  192. }
  193. /**
  194. * Test redirect URL is set in response
  195. * if request is redirected.
  196. *
  197. * @access public
  198. * @return void
  199. */
  200. public function testRedirectUrlIsSetInResponseIfRequestIsRedirected()
  201. {
  202. $client = $this->getClient();
  203. $request = $client->getMessageFactory()->createRequest();
  204. $response = $client->getMessageFactory()->createResponse();
  205. $request->setMethod('GET');
  206. $request->setUrl('https://jigsaw.w3.org/HTTP/300/302.html');
  207. $client->send($request, $response);
  208. $this->assertNotEmpty($response->getRedirectUrl());
  209. }
  210. /**
  211. * Test POST request sends request data.
  212. *
  213. * @access public
  214. * @return void
  215. */
  216. public function testPostRequestSendsRequestData()
  217. {
  218. $client = $this->getClient();
  219. $request = $client->getMessageFactory()->createRequest();
  220. $response = $client->getMessageFactory()->createResponse();
  221. $request->setMethod('POST');
  222. $request->setUrl('http://jonnyw.kiwi/tests/test-post.php');
  223. $request->setRequestData(array(
  224. 'test1' => 'http://test.com',
  225. 'test2' => 'A string with an \' ) / # some other invalid [ characters.'
  226. ));
  227. $client->send($request, $response);
  228. $this->assertContains(sprintf('<li>test1=%s</li>', 'http://test.com'), $response->getContent());
  229. $this->assertContains(sprintf('<li>test2=%s</li>', 'A string with an \' ) / # some other invalid [ characters.'), $response->getContent());
  230. }
  231. /**
  232. * Test capture request saves file to
  233. * to local disk.
  234. *
  235. * @access public
  236. * @return void
  237. */
  238. public function testCaptureRequestSavesFileToLocalDisk()
  239. {
  240. $this->filename = 'test.jpg';
  241. $file = ($this->directory . '/' . $this->filename);
  242. $client = $this->getClient();
  243. $request = $client->getMessageFactory()->createCaptureRequest();
  244. $response = $client->getMessageFactory()->createResponse();
  245. $request->setMethod('GET');
  246. $request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
  247. $request->setOutputFile($file);
  248. $client->send($request, $response);
  249. $this->assertTrue(file_exists($file));
  250. }
  251. /**
  252. * Test capture request saves file to
  253. * disk with correct capture dimensions.
  254. *
  255. * @access public
  256. * @return void
  257. */
  258. public function testCaptureRequestSavesFileToDiskWithCorrectCaptureDimensions()
  259. {
  260. $this->filename = 'test.jpg';
  261. $file = ($this->directory . '/' . $this->filename);
  262. $width = 200;
  263. $height = 400;
  264. $client = $this->getClient();
  265. $request = $client->getMessageFactory()->createCaptureRequest();
  266. $response = $client->getMessageFactory()->createResponse();
  267. $request->setMethod('GET');
  268. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  269. $request->setOutputFile($file);
  270. $request->setCaptureDimensions($width, $height);
  271. $client->send($request, $response);
  272. $imageInfo = getimagesize($file);
  273. $this->assertEquals($width, $imageInfo[0]);
  274. $this->assertEquals($height, $imageInfo[1]);
  275. }
  276. /**
  277. * Test PDF request saves pdf to
  278. * to local disk.
  279. *
  280. * @access public
  281. * @return void
  282. */
  283. public function testPdfRequestSavesPdfToLocalDisk()
  284. {
  285. $this->filename = 'test.pdf';
  286. $file = ($this->directory . '/' . $this->filename);
  287. $client = $this->getClient();
  288. $request = $client->getMessageFactory()->createPdfRequest();
  289. $response = $client->getMessageFactory()->createResponse();
  290. $request->setMethod('GET');
  291. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  292. $request->setOutputFile($file);
  293. $client->send($request, $response);
  294. $this->assertTrue(file_exists($file));
  295. }
  296. /**
  297. * Test capture request saves file to
  298. * disk with correct paper size.
  299. *
  300. * @access public
  301. * @return void
  302. */
  303. public function testPdfRequestSavesFileToDiskWithCorrectPaperSize()
  304. {
  305. $this->filename = 'test.pdf';
  306. $file = ($this->directory . '/' . $this->filename);
  307. $width = 20;
  308. $height = 30;
  309. $client = $this->getClient();
  310. $request = $client->getMessageFactory()->createPdfRequest();
  311. $response = $client->getMessageFactory()->createResponse();
  312. $request->setMethod('GET');
  313. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  314. $request->setOutputFile($file);
  315. $request->setPaperSize(sprintf('%scm', $width), sprintf('%scm', $height));
  316. $request->setMargin('0cm');
  317. $client->send($request, $response);
  318. $pdf = \ZendPdf\PdfDocument::load($file);
  319. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  320. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  321. $this->assertEquals($width, $pdfWidth);
  322. $this->assertEquals($height, $pdfHeight);
  323. }
  324. /**
  325. * Test capture request saves file to
  326. * disk with correct format size.
  327. *
  328. * @access public
  329. * @return void
  330. */
  331. public function testPdfRequestSavesFileToDiskWithCorrectFormatSize()
  332. {
  333. $this->filename = 'test.pdf';
  334. $file = ($this->directory . '/' . $this->filename);
  335. $client = $this->getClient();
  336. $request = $client->getMessageFactory()->createPdfRequest();
  337. $response = $client->getMessageFactory()->createResponse();
  338. $request->setMethod('GET');
  339. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  340. $request->setOutputFile($file);
  341. $request->setFormat('A4');
  342. $request->setMargin('0cm');
  343. $client->send($request, $response);
  344. $pdf = \ZendPdf\PdfDocument::load($file);
  345. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  346. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  347. $this->assertEquals(21, $pdfWidth);
  348. $this->assertEquals(30, $pdfHeight);
  349. }
  350. /**
  351. * Test capture request saves file to
  352. * disk with correct orientation.
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. public function testPdfRequestSavesFileToDiskWithCorrectOrientation()
  358. {
  359. $this->filename = 'test.pdf';
  360. $file = ($this->directory . '/' . $this->filename);
  361. $client = $this->getClient();
  362. $request = $client->getMessageFactory()->createPdfRequest();
  363. $response = $client->getMessageFactory()->createResponse();
  364. $request->setMethod('GET');
  365. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  366. $request->setOutputFile($file);
  367. $request->setFormat('A4');
  368. $request->setOrientation('landscape');
  369. $request->setMargin('0cm');
  370. $client->send($request, $response);
  371. $pdf = \ZendPdf\PdfDocument::load($file);
  372. $pdfWidth = round(($pdf->pages[0]->getWidth() * 0.0352777778));
  373. $pdfHeight = round(($pdf->pages[0]->getHeight() * 0.0352777778));
  374. $this->assertEquals(30, $pdfWidth);
  375. $this->assertEquals(21, $pdfHeight);
  376. }
  377. /**
  378. * Test set viewport size sets
  379. * size of viewport in default
  380. * request.
  381. *
  382. * @access public
  383. * @return void
  384. */
  385. public function testSetViewportSizeSetsSizeOfViewportInDefaultRequest()
  386. {
  387. $width = 100;
  388. $height = 200;
  389. $client = $this->getClient();
  390. $request = $client->getMessageFactory()->createRequest();
  391. $response = $client->getMessageFactory()->createResponse();
  392. $request->setMethod('GET');
  393. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  394. $request->setViewportsize($width, $height);
  395. $client->send($request, $response);
  396. $logs = explode("\n", $client->getLog());
  397. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  398. $this->assertTrue(($startIndex !== false));
  399. }
  400. /**
  401. * Test set viewport size sets
  402. * size of viewport in capture
  403. * request.
  404. *
  405. * @access public
  406. * @return void
  407. */
  408. public function testSetViewportSizeSetsSizeOfViewportInCaptureRequest()
  409. {
  410. $width = 100;
  411. $height = 200;
  412. $client = $this->getClient();
  413. $request = $client->getMessageFactory()->createCaptureRequest();
  414. $response = $client->getMessageFactory()->createResponse();
  415. $request->setMethod('GET');
  416. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  417. $request->setViewportsize($width, $height);
  418. $client->send($request, $response);
  419. $logs = explode("\n", $client->getLog());
  420. $startIndex = $this->getLogEntryIndex($logs, 'Set viewport size ~ width: 100 height: 200');
  421. $this->assertTrue(($startIndex !== false));
  422. }
  423. /**
  424. * Test delay logs start time
  425. * in client for default request.
  426. *
  427. * @access public
  428. * @return void
  429. */
  430. public function testDelayLogsStartTimeInClientForDefaultRequest()
  431. {
  432. $delay = 1;
  433. $client = $this->getClient();
  434. $request = $client->getMessageFactory()->createRequest();
  435. $response = $client->getMessageFactory()->createResponse();
  436. $request->setMethod('GET');
  437. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  438. $request->setDelay($delay);
  439. $client->send($request, $response);
  440. $logs = explode("\n", $client->getLog());
  441. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  442. $this->assertTrue(($startIndex !== false));
  443. }
  444. /**
  445. * Test delay logs end time
  446. * in client for default request.
  447. *
  448. * @access public
  449. * @return void
  450. */
  451. public function testDelayLogsEndTimeInClientForDefaultRequest()
  452. {
  453. $delay = 1;
  454. $client = $this->getClient();
  455. $request = $client->getMessageFactory()->createRequest();
  456. $response = $client->getMessageFactory()->createResponse();
  457. $request->setMethod('GET');
  458. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  459. $request->setDelay($delay);
  460. $client->send($request, $response);
  461. $logs = explode("\n", $client->getLog());
  462. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  463. $this->assertTrue(($endIndex !== false));
  464. }
  465. /**
  466. * Test delay delays page render for
  467. * specified time for default request.
  468. *
  469. * @access public
  470. * @return void
  471. */
  472. public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
  473. {
  474. $delay = 1;
  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->setDelay($delay);
  481. $client->send($request, $response);
  482. $logs = explode("\\n", $client->getLog());
  483. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  484. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  485. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  486. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  487. $this->assertSame(($startTime+$delay), $endTime);
  488. }
  489. /**
  490. * Test delay logs start time
  491. * in client for capture request.
  492. *
  493. * @access public
  494. * @return void
  495. */
  496. public function testDelayLogsStartTimeInClientForCaptureRequest()
  497. {
  498. $delay = 1;
  499. $client = $this->getClient();
  500. $request = $client->getMessageFactory()->createCaptureRequest();
  501. $response = $client->getMessageFactory()->createResponse();
  502. $request->setMethod('GET');
  503. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  504. $request->setDelay($delay);
  505. $client->send($request, $response);
  506. $logs = explode("\\n", $client->getLog());
  507. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  508. $this->assertTrue(($startIndex !== false));
  509. }
  510. /**
  511. * Test delay logs end time
  512. * in client for capture request.
  513. *
  514. * @access public
  515. * @return void
  516. */
  517. public function testDelayLogsEndTimeInClientForCaptureRequest()
  518. {
  519. $delay = 1;
  520. $client = $this->getClient();
  521. $request = $client->getMessageFactory()->createCaptureRequest();
  522. $response = $client->getMessageFactory()->createResponse();
  523. $request->setMethod('GET');
  524. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  525. $request->setDelay($delay);
  526. $client->send($request, $response);
  527. $logs = explode("\\n", $client->getLog());
  528. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  529. $this->assertTrue(($endIndex !== false));
  530. }
  531. /**
  532. * Test delay delays page render for
  533. * specified time for capture request.
  534. *
  535. * @access public
  536. * @return void
  537. */
  538. public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
  539. {
  540. $delay = 1;
  541. $client = $this->getClient();
  542. $request = $client->getMessageFactory()->createCaptureRequest();
  543. $response = $client->getMessageFactory()->createResponse();
  544. $request->setMethod('GET');
  545. $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
  546. $request->setDelay($delay);
  547. $client->send($request, $response);
  548. $logs = explode("\\n", $client->getLog());
  549. $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
  550. $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
  551. $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
  552. $endTime = strtotime(substr($logs[$endIndex], 0 , 19));
  553. $this->assertSame(($startTime+$delay), $endTime);
  554. }
  555. /**
  556. * Test debug logs debug info to
  557. * client log.
  558. *
  559. * @access public
  560. * @return void
  561. */
  562. public function testDebugLogsDebugInfoToClientLog()
  563. {
  564. $client = $this->getClient();
  565. $client->getEngine()->debug(true);
  566. $request = $client->getMessageFactory()->createRequest();
  567. $response = $client->getMessageFactory()->createResponse();
  568. $request->setMethod('GET');
  569. $request->setUrl('http://jonnyw.kiwi/tests/test-default.php');
  570. $client->send($request, $response);
  571. $this->assertContains('[DEBUG]', $client->getLog());
  572. }
  573. /** +++++++++++++++++++++++++++++++++++ **/
  574. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  575. /** +++++++++++++++++++++++++++++++++++ **/
  576. /**
  577. * Get client instance.
  578. *
  579. * @return \JonnyW\PhantomJs\Client
  580. */
  581. protected function getClient()
  582. {
  583. $serviceContainer = ServiceContainer::getInstance();
  584. $client = new Client(
  585. $serviceContainer->get('engine'),
  586. $serviceContainer->get('procedure_loader'),
  587. $serviceContainer->get('procedure_compiler'),
  588. $serviceContainer->get('message_factory')
  589. );
  590. return $client;
  591. }
  592. /** +++++++++++++++++++++++++++++++++++ **/
  593. /** ++++++++++++ UTILITIES ++++++++++++ **/
  594. /** +++++++++++++++++++++++++++++++++++ **/
  595. /**
  596. * Set up test environment.
  597. *
  598. * @access public
  599. * @return void
  600. */
  601. public function setUp()
  602. {
  603. $this->filename = 'test.proc';
  604. $this->directory = sys_get_temp_dir();
  605. if (!is_writable($this->directory)) {
  606. throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
  607. }
  608. }
  609. /**
  610. * Tear down test environment.
  611. *
  612. * @access public
  613. * @return void
  614. */
  615. public function tearDown()
  616. {
  617. $filename = $this->getFilename();
  618. if (file_exists($filename)) {
  619. unlink($filename);
  620. }
  621. }
  622. /**
  623. * Get test filename.
  624. *
  625. * @access public
  626. * @return string
  627. */
  628. public function getFilename()
  629. {
  630. return sprintf('%1$s/%2$s', $this->directory, $this->filename);
  631. }
  632. /**
  633. * Write procedure body to file.
  634. *
  635. * @access public
  636. * @param string $data
  637. * @return string
  638. */
  639. public function writeProcedure($procedure)
  640. {
  641. $filename = $this->getFilename();
  642. file_put_contents($filename, $procedure);
  643. return $filename;
  644. }
  645. /**
  646. * Get log entry index.
  647. *
  648. * @access public
  649. * @param array $logs
  650. * @param string $search
  651. * @return int|false
  652. */
  653. public function getLogEntryIndex(array $logs, $search)
  654. {
  655. foreach ($logs as $index => $log) {
  656. $pos = stripos($log, $search);
  657. if ($pos !== false) {
  658. return $index;
  659. }
  660. }
  661. return false;
  662. }
  663. }