ConnectionTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\VarDumper\Tests\Server;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\PhpProcess;
  13. use Symfony\Component\Process\Process;
  14. use Symfony\Component\VarDumper\Cloner\VarCloner;
  15. use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
  16. use Symfony\Component\VarDumper\Server\Connection;
  17. class ConnectionTest extends TestCase
  18. {
  19. private const VAR_DUMPER_SERVER = 'tcp://127.0.0.1:9913';
  20. public function testDump()
  21. {
  22. $cloner = new VarCloner();
  23. $data = $cloner->cloneVar('foo');
  24. $connection = new Connection(self::VAR_DUMPER_SERVER, [
  25. 'foo_provider' => new class() implements ContextProviderInterface {
  26. public function getContext(): ?array
  27. {
  28. return ['foo'];
  29. }
  30. },
  31. ]);
  32. $dumped = null;
  33. $process = $this->getServerProcess();
  34. $process->start(function ($type, $buffer) use ($process, &$dumped, $connection, $data) {
  35. if (Process::ERR === $type) {
  36. $process->stop();
  37. $this->fail();
  38. } elseif ("READY\n" === $buffer) {
  39. $connection->write($data);
  40. } else {
  41. $dumped .= $buffer;
  42. }
  43. });
  44. $process->wait();
  45. $this->assertTrue($process->isSuccessful());
  46. $this->assertStringMatchesFormat(<<<'DUMP'
  47. (3) "foo"
  48. [
  49. "timestamp" => %d.%d
  50. "foo_provider" => [
  51. (3) "foo"
  52. ]
  53. ]
  54. %d
  55. DUMP
  56. , $dumped);
  57. }
  58. public function testNoServer()
  59. {
  60. $cloner = new VarCloner();
  61. $data = $cloner->cloneVar('foo');
  62. $connection = new Connection(self::VAR_DUMPER_SERVER);
  63. $start = microtime(true);
  64. $this->assertFalse($connection->write($data));
  65. $this->assertLessThan(1, microtime(true) - $start);
  66. }
  67. private function getServerProcess(): Process
  68. {
  69. $process = new PhpProcess(file_get_contents(__DIR__.'/../Fixtures/dump_server.php'), null, [
  70. 'COMPONENT_ROOT' => __DIR__.'/../../',
  71. 'VAR_DUMPER_SERVER' => self::VAR_DUMPER_SERVER,
  72. ]);
  73. $process->inheritEnvironmentVariables(true);
  74. return $process->setTimeout(9);
  75. }
  76. }