PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Behat/SahiClient/ConnectionTest.php

https://github.com/Behat/SahiClient
PHP | 191 lines | 124 code | 41 blank | 26 comment | 3 complexity | ee8289d0f104edde207977cf73b911f8 MD5 | raw file
  1. <?php
  2. namespace Test\Behat\SahiClient;
  3. use Buzz\Browser;
  4. use Buzz\Message;
  5. use Buzz\Listener;
  6. use Behat\SahiClient\Connection;
  7. class ConnectionTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var Browser
  11. */
  12. private $browser;
  13. public function setUp()
  14. {
  15. $this->browser = new Browser(new ClientQueue());
  16. $this->browser->setListener(new Listener\HistoryListener(new ExtendedJournal()));
  17. }
  18. public function testExecuteCommand()
  19. {
  20. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  21. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'all works fine'));
  22. $response = $con->executeCommand('setSpeed', array('speed' => 2000, 'milli' => 'true'));
  23. $request = $this->browser->getListener()->getJournal()->getLastRequest();
  24. $this->assertSame($this->browser, $con->getBrowser());
  25. $this->assertEquals('all works fine', $response);
  26. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_setSpeed', $request->getUrl());
  27. $this->assertEquals('speed=2000&milli=true&sahisid='.$sid, $request->getContent());
  28. }
  29. public function testExecuteStep()
  30. {
  31. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  32. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'true'));
  33. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  34. $con->executeStep('_sahi._clearLastAlert()');
  35. $this->assertEquals(2, count($this->browser->getListener()->getJournal()));
  36. $request = $this->browser->getListener()->getJournal()->getFirst()->getRequest();
  37. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_setStep', $request->getUrl());
  38. $this->assertContains('step=' . urlencode('_sahi._clearLastAlert()'), $request->getContent());
  39. }
  40. /**
  41. * @expectedException \Behat\SahiClient\Exception\ConnectionException
  42. */
  43. public function testExecuteStepFail()
  44. {
  45. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  46. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'error: incorrect'));
  47. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  48. $con->executeStep('_sahi._clearLastAlert()');
  49. }
  50. public function testExecuteJavascript()
  51. {
  52. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  53. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', '25'));
  54. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'true'));
  55. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  56. $this->assertEquals(25, $con->evaluateJavascript('_sahi._lastConfirm()'));
  57. $this->assertEquals(3, count($this->browser->getListener()->getJournal()));
  58. $request = $this->browser->getListener()->getJournal()->getFirst()->getRequest();
  59. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_setStep', $request->getUrl());
  60. $this->assertContains('step=' . urlencode('_sahi.setServerVarPlain('), $request->getContent());
  61. $this->assertContains(urlencode('_sahi._lastConfirm()'), $request->getContent());
  62. $request = $this->browser->getListener()->getJournal()->getLastRequest();
  63. $response = $this->browser->getListener()->getJournal()->getLastResponse();
  64. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_getVariable', $request->getUrl());
  65. $this->assertContains('key=___lastValue___', $request->getContent());
  66. $this->assertEquals('25', $response->getContent());
  67. }
  68. public function testLongExecuteJavascript()
  69. {
  70. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  71. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', '22'));
  72. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'true'));
  73. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'false'));
  74. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'false'));
  75. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  76. $this->assertEquals(22, $con->evaluateJavascript('_sahi._lastConfirm()'));
  77. $this->assertEquals(5, count($this->browser->getListener()->getJournal()));
  78. $request = $this->browser->getListener()->getJournal()->getFirst()->getRequest();
  79. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_setStep', $request->getUrl());
  80. $this->assertContains('step=' . urlencode('_sahi.setServerVarPlain('), $request->getContent());
  81. $this->assertContains(urlencode('_sahi._lastConfirm()'), $request->getContent());
  82. $request = $this->browser->getListener()->getJournal()->getLastRequest();
  83. $response = $this->browser->getListener()->getJournal()->getLastResponse();
  84. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_getVariable', $request->getUrl());
  85. $this->assertContains('key=___lastValue___', $request->getContent());
  86. $this->assertEquals('22', $response->getContent());
  87. }
  88. /**
  89. * @expectedException \Behat\SahiClient\Exception\ConnectionException
  90. */
  91. public function tesExecuteJavascriptError()
  92. {
  93. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  94. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'error: incorrect'));
  95. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  96. $con->executeJavascript('_sahi._lastConfirm()');
  97. }
  98. public function testExecuteJavascriptNull()
  99. {
  100. $con = $this->createConnection($sid = uniqid(), $this->browser, true);
  101. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'null'));
  102. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'true'));
  103. $this->browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  104. $this->assertNull($con->evaluateJavascript('_sahi._lastConfirm()'));
  105. $this->assertEquals(3, count($this->browser->getListener()->getJournal()));
  106. $request = $this->browser->getListener()->getJournal()->getLastRequest();
  107. $response = $this->browser->getListener()->getJournal()->getLastResponse();
  108. $this->assertEquals('http://localhost:9999/_s_/dyn/Driver_getVariable', $request->getUrl());
  109. $this->assertContains('key=___lastValue___', $request->getContent());
  110. $this->assertEquals('null', $response->getContent());
  111. }
  112. /**
  113. * Create new Response.
  114. *
  115. * @param string $status response status description
  116. * @param string $content content
  117. *
  118. * @return Message\Response
  119. */
  120. protected function createResponse($status, $content = null)
  121. {
  122. $response = new Message\Response();
  123. $response->addHeader($status);
  124. if (null !== $content) {
  125. $response->setContent($content);
  126. }
  127. return $response;
  128. }
  129. /**
  130. * Create Sahi API Connection with custom SID.
  131. *
  132. * @param string $sid sahi id
  133. * @param Browser $browser
  134. * @param boolean $correct add correct responses to browser Queue for browser creation
  135. *
  136. * @return Connection
  137. */
  138. protected function createConnection($sid, Browser $browser, $correct = false)
  139. {
  140. if ($correct) {
  141. $browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK', 'true'));
  142. $browser->getClient()->sendToQueue($this->createResponse('1.0 200 OK'));
  143. }
  144. $connection = new Connection($sid, 'localhost', 9999, $browser);
  145. if ($correct) {
  146. $browser->getListener()->getJournal()->clear();
  147. }
  148. return $connection;
  149. }
  150. }