ServerDumperTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Dumper;
  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\Dumper\DataDumperInterface;
  17. use Symfony\Component\VarDumper\Dumper\ServerDumper;
  18. class ServerDumperTest extends TestCase
  19. {
  20. private const VAR_DUMPER_SERVER = 'tcp://127.0.0.1:9913';
  21. public function testDumpForwardsToWrappedDumperWhenServerIsUnavailable()
  22. {
  23. $wrappedDumper = $this->getMockBuilder(DataDumperInterface::class)->getMock();
  24. $dumper = new ServerDumper(self::VAR_DUMPER_SERVER, $wrappedDumper);
  25. $cloner = new VarCloner();
  26. $data = $cloner->cloneVar('foo');
  27. $wrappedDumper->expects($this->once())->method('dump')->with($data);
  28. $dumper->dump($data);
  29. }
  30. public function testDump()
  31. {
  32. $wrappedDumper = $this->getMockBuilder(DataDumperInterface::class)->getMock();
  33. $wrappedDumper->expects($this->never())->method('dump'); // test wrapped dumper is not used
  34. $cloner = new VarCloner();
  35. $data = $cloner->cloneVar('foo');
  36. $dumper = new ServerDumper(self::VAR_DUMPER_SERVER, $wrappedDumper, [
  37. 'foo_provider' => new class() implements ContextProviderInterface {
  38. public function getContext(): ?array
  39. {
  40. return ['foo'];
  41. }
  42. },
  43. ]);
  44. $dumped = null;
  45. $process = $this->getServerProcess();
  46. $process->start(function ($type, $buffer) use ($process, &$dumped, $dumper, $data) {
  47. if (Process::ERR === $type) {
  48. $process->stop();
  49. $this->fail();
  50. } elseif ("READY\n" === $buffer) {
  51. $dumper->dump($data);
  52. } else {
  53. $dumped .= $buffer;
  54. }
  55. });
  56. $process->wait();
  57. $this->assertTrue($process->isSuccessful());
  58. $this->assertStringMatchesFormat(<<<'DUMP'
  59. (3) "foo"
  60. [
  61. "timestamp" => %d.%d
  62. "foo_provider" => [
  63. (3) "foo"
  64. ]
  65. ]
  66. %d
  67. DUMP
  68. , $dumped);
  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. $process->inheritEnvironmentVariables(true);
  77. return $process->setTimeout(9);
  78. }
  79. }