JsonTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  9. * @test
  10. * @doesNotPerformAssertions
  11. */
  12. public function it_can_output_an_object_as_json()
  13. {
  14. $json = json_encode((object) [
  15. "cell1" => "Cell 1",
  16. "cell2" => "Cell 2",
  17. "cell3" => "Cell 3",
  18. "cell4" => "Cell 4",
  19. ], \JSON_PRETTY_PRINT);
  20. $this->shouldWrite("\e[m{$json}\e[0m");
  21. $this->shouldHavePersisted();
  22. $this->cli->json((object) [
  23. "cell1" => "Cell 1",
  24. "cell2" => "Cell 2",
  25. "cell3" => "Cell 3",
  26. "cell4" => "Cell 4",
  27. ]);
  28. }
  29. /**
  30. * We do this test specifically because json escapes the tags,
  31. * we want to make sure we"re taking care of that
  32. *
  33. * @test
  34. * @doesNotPerformAssertions
  35. */
  36. public function it_can_output_json_with_tags()
  37. {
  38. $json = json_encode((object) [
  39. "cell1" => "Cell 1",
  40. "cell2" => "Cell 2",
  41. "cell3" => "Cell 3",
  42. "cell4" => "Cell 4",
  43. ], \JSON_PRETTY_PRINT);
  44. $json = str_replace("Cell 4", "\e[5mCell 4\e[0m", $json);
  45. $this->shouldWrite("\e[m{$json}\e[0m");
  46. $this->shouldHavePersisted();
  47. $this->cli->json((object) [
  48. "cell1" => "Cell 1",
  49. "cell2" => "Cell 2",
  50. "cell3" => "Cell 3",
  51. "cell4" => "<blink>Cell 4</blink>",
  52. ]);
  53. }
  54. /**
  55. * @doesNotPerformAssertions
  56. */
  57. public function test_we_dont_escape_slashes()
  58. {
  59. $this->shouldWrite("\e[m{\n \"package\": \"league/climate\"\n}\e[0m");
  60. $this->shouldHavePersisted();
  61. $this->cli->json(["package" => "league/climate"]);
  62. }
  63. }