JsonTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $should_be = 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" . $should_be . "\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. $should_be = json_encode((object) [
  39. "cell1" => 'Cell 1',
  40. "cell2" => 'Cell 2',
  41. "cell3" => 'Cell 3',
  42. "cell4" => 'Cell 4',
  43. ], \JSON_PRETTY_PRINT);
  44. $should_be = str_replace('Cell 4', "\e[5mCell 4\e[0m", $should_be);
  45. $this->shouldWrite("\e[m" . $should_be . "\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. }