MockSocketTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Socket;
  11. use HeadlessChromium\Communication\Socket\MockSocket;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * @covers \HeadlessChromium\Communication\Socket\MockSocket
  15. */
  16. class MockSocketTest extends TestCase
  17. {
  18. public function testMockSocket(): void
  19. {
  20. $mock = new MockSocket();
  21. // not connected
  22. self::assertFalse($mock->isConnected());
  23. self::assertFalse($mock->sendData('foo'));
  24. self::assertEmpty($mock->getSentData());
  25. self::assertEmpty($mock->receiveData());
  26. // connected
  27. $mock->connect();
  28. self::assertTrue($mock->isConnected());
  29. self::assertTrue($mock->sendData('foo'));
  30. self::assertEquals(['foo'], $mock->getSentData());
  31. self::assertEquals(['foo'], $mock->getSentData()); // not empty until flush
  32. self::assertEmpty($mock->receiveData());
  33. // flush sent data
  34. $mock->flushData();
  35. self::assertEmpty($mock->getSentData());
  36. // with received data
  37. $mock->addReceivedData('bar');
  38. self::assertEquals(['bar'], $mock->receiveData());
  39. self::assertEmpty($mock->receiveData());
  40. // disconnected
  41. $mock->disconnect();
  42. self::assertFalse($mock->isConnected());
  43. }
  44. public function testReceivedDateForNextMessage(): void
  45. {
  46. $mock = new MockSocket();
  47. // connected
  48. $mock->connect();
  49. $mock->addReceivedData(\json_encode(['foo' => 'bar']), true);
  50. self::assertEmpty($mock->receiveData());
  51. $mock->sendData(\json_encode(['id' => 1]));
  52. self::assertEquals([\json_encode(['foo' => 'bar', 'id' => 1])], $mock->receiveData());
  53. }
  54. }