JsonTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace League\CLImate\Tests\TerminalObject\Basic;
  3. use League\CLImate\Tests\TestBase;
  4. use function json_encode;
  5. use function str_replace;
  6. class JsonTest extends TestBase
  7. {
  8. /** @test */
  9. public function it_can_output_an_object_as_json()
  10. {
  11. $json = json_encode((object) [
  12. "cell1" => "Cell 1",
  13. "cell2" => "Cell 2",
  14. "cell3" => "Cell 3",
  15. "cell4" => "Cell 4",
  16. ], \JSON_PRETTY_PRINT);
  17. $this->shouldWrite("\e[m{$json}\e[0m");
  18. $this->shouldHavePersisted();
  19. $this->cli->json((object) [
  20. "cell1" => "Cell 1",
  21. "cell2" => "Cell 2",
  22. "cell3" => "Cell 3",
  23. "cell4" => "Cell 4",
  24. ]);
  25. }
  26. /**
  27. * We do this test specifically because json escapes the tags,
  28. * we want to make sure we"re taking care of that
  29. *
  30. * @test */
  31. public function it_can_output_json_with_tags()
  32. {
  33. $json = json_encode((object) [
  34. "cell1" => "Cell 1",
  35. "cell2" => "Cell 2",
  36. "cell3" => "Cell 3",
  37. "cell4" => "Cell 4",
  38. ], \JSON_PRETTY_PRINT);
  39. $json = str_replace("Cell 4", "\e[5mCell 4\e[0m", $json);
  40. $this->shouldWrite("\e[m{$json}\e[0m");
  41. $this->shouldHavePersisted();
  42. $this->cli->json((object) [
  43. "cell1" => "Cell 1",
  44. "cell2" => "Cell 2",
  45. "cell3" => "Cell 3",
  46. "cell4" => "<blink>Cell 4</blink>",
  47. ]);
  48. }
  49. public function test_we_dont_escape_slashes()
  50. {
  51. $this->shouldWrite("\e[m{\n \"package\": \"league/climate\"\n}\e[0m");
  52. $this->shouldHavePersisted();
  53. $this->cli->json(["package" => "league/climate"]);
  54. }
  55. }