Pull merge.
[yaffs-website] / vendor / jcalderonzumba / gastonjs / tests / unit / BrowserScriptTest.php
1 <?php
2 namespace Zumba\GastonJS\Tests;
3
4 use GuzzleHttp\Client;
5 use GuzzleHttp\Message\Response;
6
7 /**
8  * Class BrowserScriptTest
9  * @package Zumba\GastonJS\Tests
10  */
11 class BrowserScriptTest extends BrowserCommandsTestCase {
12
13   public function testScriptEvaluate() {
14     $webClient = new Client();
15     /** @var $response Response */
16     $response = $webClient->get($this->getTestPageBaseUrl() . "/static/fibonacci.js");
17     $script = $response->getBody()->getContents();
18     //Fibonacci(10) = 55;
19     $this->assertEquals(55, $this->browser->evaluate($script));
20   }
21
22   protected function doFormSubmit() {
23     //We have changed the form so lets get the element and see such changes
24     $formElement = $this->browser->find("xpath", '//*[@id="form_1014473"]');
25     $this->assertEquals("submit", $this->browser->trigger($formElement["page_id"], $formElement["ids"][0], "submit"));
26     //STUFF works like that, lets give time for the browser and server to load.
27     sleep(2);
28     $expectedUrl = "http://127.0.0.1:6789/check-post-request/";
29     $this->assertEquals($expectedUrl, $this->browser->currentUrl());
30     $formResponse = json_decode(strip_tags($this->browser->getSource()), true);
31     $this->assertTrue(is_array($formResponse));
32     $this->assertEquals("THIS_IS_SPARTA", $formResponse["post"]["element_1"]);
33     $this->assertEquals("1", $formResponse["post"]["element_3"]);
34   }
35
36   public function testScriptExecute() {
37     $webClient = new Client();
38     /** @var $response Response */
39     $response = $webClient->get($this->getTestPageBaseUrl() . "/test/script_execute/execute.js");
40     $script = $response->getBody()->getContents();
41     $this->visitUrl($this->getTestPageBaseUrl() . "/test/script_execute/form.html");
42     $this->assertTrue($this->browser->execute($script));
43     $this->doFormSubmit();
44   }
45
46   public function testScriptExtensions() {
47     $viewJS = sprintf("%s/Server/www/web/test/script_extensions/extension.js", __DIR__);
48     $this->visitUrl($this->getTestPageBaseUrl() . "/test/script_extensions/form.html");
49     $this->assertTrue($this->browser->extensions(array($viewJS)));
50     $this->doFormSubmit();
51   }
52 }