SessionTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Response;
  14. use HeadlessChromium\Communication\ResponseReader;
  15. use HeadlessChromium\Communication\Session;
  16. use HeadlessChromium\Communication\Socket\MockSocket;
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * @covers \HeadlessChromium\Communication\Session
  20. */
  21. class SessionTest extends TestCase
  22. {
  23. /**
  24. * @var MockSocket
  25. */
  26. protected $mockSocket;
  27. public function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->mockSocket = new MockSocket();
  31. }
  32. public function testSession(): void
  33. {
  34. $connection = new Connection($this->mockSocket);
  35. $session = new Session('foo', 'bar', $connection);
  36. self::assertEquals('foo', $session->getTargetId());
  37. self::assertEquals('bar', $session->getSessionId());
  38. self::assertSame($connection, $session->getConnection());
  39. }
  40. public function testSendMessage(): void
  41. {
  42. $connection = new Connection($this->mockSocket);
  43. $connection->connect();
  44. $session = new Session('foo', 'bar', $connection);
  45. $message = new Message('baz', ['qux' => 'quux']);
  46. $responseReader = $session->sendMessage($message);
  47. self::assertInstanceOf(ResponseReader::class, $responseReader);
  48. self::assertEquals(
  49. [
  50. \json_encode([
  51. 'id' => $message->getId(),
  52. 'method' => 'baz',
  53. 'params' => ['qux' => 'quux'],
  54. 'sessionId' => 'bar',
  55. ]),
  56. ],
  57. $this->mockSocket->getSentData()
  58. );
  59. }
  60. public function testSendMessageSync(): void
  61. {
  62. $connection = new Connection($this->mockSocket);
  63. $connection->connect();
  64. $session = new Session('foo', 'bar', $connection);
  65. $message = new Message('baz', ['qux' => 'quux']);
  66. $this->mockSocket->addReceivedData(\json_encode(['corge' => 'grault']), true);
  67. $this->mockSocket->addReceivedData(\json_encode(['id' => $message->getId(), 'garply' => 'thud']));
  68. $response = $session->sendMessageSync($message);
  69. self::assertInstanceOf(Response::class, $response);
  70. self::assertEquals(
  71. [
  72. \json_encode([
  73. 'id' => $message->getId(),
  74. 'method' => 'baz',
  75. 'params' => ['qux' => 'quux'],
  76. 'sessionId' => 'bar',
  77. ]),
  78. ],
  79. $this->mockSocket->getSentData()
  80. );
  81. }
  82. }