ConnectionTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if ('True' === getenv('APPVEYOR')) {
  23. $this->markTestSkipped('Skip transient test on AppVeyor');
  24. }
  25. $cloner = new VarCloner();
  26. $data = $cloner->cloneVar('foo');
  27. $connection = new Connection(self::VAR_DUMPER_SERVER, [
  28. 'foo_provider' => new class() implements ContextProviderInterface {
  29. public function getContext(): ?array
  30. {
  31. return ['foo'];
  32. }
  33. },
  34. ]);
  35. $dumped = null;
  36. $process = $this->getServerProcess();
  37. $process->start(function ($type, $buffer) use ($process, &$dumped, $connection, $data) {
  38. if (Process::ERR === $type) {
  39. $process->stop();
  40. $this->fail();
  41. } elseif ("READY\n" === $buffer) {
  42. $connection->write($data);
  43. } else {
  44. $dumped .= $buffer;
  45. }
  46. });
  47. $process->wait();
  48. $this->assertTrue($process->isSuccessful());
  49. $this->assertStringMatchesFormat(<<<'DUMP'
  50. (3) "foo"
  51. [
  52. "timestamp" => %d.%d
  53. "foo_provider" => [
  54. (3) "foo"
  55. ]
  56. ]
  57. %d
  58. DUMP
  59. , $dumped);
  60. }
  61. public function testNoServer()
  62. {
  63. $cloner = new VarCloner();
  64. $data = $cloner->cloneVar('foo');
  65. $connection = new Connection(self::VAR_DUMPER_SERVER);
  66. $start = microtime(true);
  67. $this->assertFalse($connection->write($data));
  68. $this->assertLessThan(4, microtime(true) - $start);
  69. }
  70. private function getServerProcess(): Process
  71. {
  72. $process = new PhpProcess(file_get_contents(__DIR__.'/../Fixtures/dump_server.php'), null, [
  73. 'COMPONENT_ROOT' => __DIR__.'/../../',
  74. 'VAR_DUMPER_SERVER' => self::VAR_DUMPER_SERVER,
  75. ]);
  76. return $process->setTimeout(9);
  77. }
  78. }