SessionTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $this->assertEquals('foo', $session->getTargetId());
  37. $this->assertEquals('bar', $session->getSessionId());
  38. $this->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. $this->assertInstanceOf(ResponseReader::class, $responseReader);
  48. $this->assertEquals(
  49. [
  50. \json_encode([
  51. 'id' => $responseReader->getTopResponseReader()->getMessage()->getId(),
  52. 'method' => 'Target.sendMessageToTarget',
  53. 'params' => [
  54. 'message' => \json_encode([
  55. 'id' => $message->getId(),
  56. 'method' => 'baz',
  57. 'params' => ['qux' => 'quux'],
  58. ]),
  59. 'sessionId' => 'bar',
  60. ],
  61. ]),
  62. ],
  63. $this->mockSocket->getSentData()
  64. );
  65. }
  66. public function testSendMessageSync(): void
  67. {
  68. $connection = new Connection($this->mockSocket);
  69. $connection->connect();
  70. $session = new Session('foo', 'bar', $connection);
  71. $message = new Message('baz', ['qux' => 'quux']);
  72. $this->mockSocket->addReceivedData(\json_encode(['corge' => 'grault']), true);
  73. $this->mockSocket->addReceivedData(\json_encode(['id' => $message->getId(), 'garply' => 'thud']));
  74. $response = $session->sendMessageSync($message);
  75. $this->assertInstanceOf(Response::class, $response);
  76. $this->assertEquals(
  77. [
  78. \json_encode([
  79. 'id' => $message->getId() + 1,
  80. 'method' => 'Target.sendMessageToTarget',
  81. 'params' => [
  82. 'message' => \json_encode([
  83. 'id' => $message->getId(),
  84. 'method' => 'baz',
  85. 'params' => ['qux' => 'quux'],
  86. ]),
  87. 'sessionId' => 'bar',
  88. ],
  89. ]),
  90. ],
  91. $this->mockSocket->getSentData()
  92. );
  93. }
  94. }