ConnectionTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HeadlessChromium\Test\Communication;
  11. use HeadlessChromium\Communication\Connection;
  12. use HeadlessChromium\Communication\Message;
  13. use HeadlessChromium\Communication\ResponseReader;
  14. use HeadlessChromium\Communication\Session;
  15. use HeadlessChromium\Communication\Socket\MockSocket;
  16. use HeadlessChromium\Exception\CommunicationException\CannotReadResponse;
  17. use HeadlessChromium\Exception\CommunicationException\InvalidResponse;
  18. use HeadlessChromium\Exception\OperationTimedOut;
  19. use PHPUnit\Framework\TestCase;
  20. /**
  21. * @covers \HeadlessChromium\Communication\Connection
  22. */
  23. class ConnectionTest extends TestCase
  24. {
  25. /**
  26. * @var MockSocket
  27. */
  28. protected $mocSocket;
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. $this->mocSocket = new MockSocket();
  33. }
  34. public function testIsStrict(): void
  35. {
  36. $connection = new Connection($this->mocSocket);
  37. self::assertTrue($connection->isStrict());
  38. $connection->setStrict(false);
  39. self::assertFalse($connection->isStrict());
  40. }
  41. public function testConnectDisconnect(): void
  42. {
  43. $connection = new Connection($this->mocSocket);
  44. $connection->connect();
  45. self::assertTrue($this->mocSocket->isConnected());
  46. $connection->disconnect();
  47. self::assertFalse($this->mocSocket->isConnected());
  48. }
  49. public function testCreateSession(): void
  50. {
  51. $connection = new Connection($this->mocSocket);
  52. $connection->connect();
  53. $this->mocSocket->addReceivedData(\json_encode(['result' => ['sessionId' => 'foo-bar']]), true);
  54. $session = $connection->createSession('baz-qux');
  55. self::assertInstanceOf(Session::class, $session);
  56. self::assertEquals('foo-bar', $session->getSessionId());
  57. self::assertEquals('baz-qux', $session->getTargetId());
  58. self::assertSame($connection, $session->getConnection());
  59. }
  60. public function testSendMessage(): void
  61. {
  62. $connection = new Connection($this->mocSocket);
  63. $connection->connect();
  64. $message = new Message('foo', ['bar' => 'baz']);
  65. $reader = $connection->sendMessage($message);
  66. self::assertInstanceOf(ResponseReader::class, $reader);
  67. self::assertSame($message, $reader->getMessage());
  68. self::assertSame($connection, $reader->getConnection());
  69. self::assertEquals(
  70. [
  71. \json_encode([
  72. 'id' => $message->getId(),
  73. 'method' => 'foo',
  74. 'params' => ['bar' => 'baz'],
  75. ]),
  76. ],
  77. $this->mocSocket->getSentData()
  78. );
  79. }
  80. /**
  81. * This test asserts that data are sent when a delay is set. It does not test that the delay works.
  82. */
  83. public function testSendMessageWorksWithDelay(): void
  84. {
  85. $connection = new Connection($this->mocSocket);
  86. $connection->connect();
  87. $connection->setConnectionDelay(1);
  88. $message = new Message('foo', ['bar' => 'baz']);
  89. $connection->sendMessage($message);
  90. $connection->sendMessage($message);
  91. self::assertEquals(
  92. [
  93. \json_encode(
  94. [
  95. 'id' => $message->getId(),
  96. 'method' => 'foo',
  97. 'params' => ['bar' => 'baz'],
  98. ]
  99. ),
  100. \json_encode(
  101. [
  102. 'id' => $message->getId(),
  103. 'method' => 'foo',
  104. 'params' => ['bar' => 'baz'],
  105. ]
  106. ),
  107. ],
  108. $this->mocSocket->getSentData()
  109. );
  110. }
  111. public function testConnectionHttpHeaders(): void
  112. {
  113. $connection = new Connection($this->mocSocket);
  114. $header = [
  115. 'header_name' => 'header_value',
  116. ];
  117. $connection->setConnectionHttpHeaders($header);
  118. self::assertSame($header, $connection->getConnectionHttpHeaders());
  119. }
  120. public function testSendMessageSync(): void
  121. {
  122. $connection = new Connection($this->mocSocket);
  123. $connection->connect();
  124. $message = new Message('foo', ['bar' => 'baz']);
  125. $this->mocSocket->addReceivedData(\json_encode(['id' => $message->getId(), 'bar' => 'foo']));
  126. $response = $connection->sendMessageSync($message, 2);
  127. self::assertSame($message, $response->getMessage());
  128. self::assertEquals(['id' => $message->getId(), 'bar' => 'foo'], $response->getData());
  129. }
  130. public function testSendMessageSyncException(): void
  131. {
  132. $connection = new Connection($this->mocSocket);
  133. $connection->connect();
  134. $message = new Message('foo', ['bar' => 'baz']);
  135. $this->expectException(OperationTimedOut::class);
  136. $connection->sendMessageSync($message, 2);
  137. }
  138. public function testReadData(): void
  139. {
  140. $connection = new Connection($this->mocSocket);
  141. $connection->connect();
  142. self::assertFalse($connection->readData());
  143. $this->mocSocket->addReceivedData(\json_encode([
  144. 'id' => 1,
  145. 'foo' => 'bar',
  146. ]));
  147. self::assertTrue($connection->readData());
  148. self::assertFalse($connection->readData());
  149. self::assertTrue($connection->hasResponseForId(1));
  150. self::assertTrue($connection->hasResponseForId(1)); // still true until read
  151. self::assertFalse($connection->hasResponseForId(2));
  152. $data = $connection->getResponseForId(1);
  153. self::assertEquals(
  154. [
  155. 'id' => 1,
  156. 'foo' => 'bar',
  157. ],
  158. $data
  159. );
  160. self::assertFalse($connection->hasResponseForId(1));
  161. }
  162. public function testExceptionInvalideJson(): void
  163. {
  164. $this->expectException(CannotReadResponse::class);
  165. $connection = new Connection($this->mocSocket);
  166. $connection->connect();
  167. // set invalid json
  168. $this->mocSocket->addReceivedData('{');
  169. $connection->readData();
  170. }
  171. public function testExceptionInvalideArrayResponse(): void
  172. {
  173. $this->expectException(CannotReadResponse::class);
  174. $connection = new Connection($this->mocSocket);
  175. $connection->connect();
  176. // set string variable instead of array
  177. $this->mocSocket->addReceivedData('"foo"');
  178. $connection->readData();
  179. }
  180. public function testInvalidResponseId(): void
  181. {
  182. $this->expectException(InvalidResponse::class);
  183. $connection = new Connection($this->mocSocket);
  184. $connection->connect();
  185. // set string variable instead of array
  186. $this->mocSocket->addReceivedData('{"message": "foo"}');
  187. $connection->readData();
  188. }
  189. }