SupportJsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Contracts\Support\Jsonable;
  5. use Illuminate\Support\Js;
  6. use JsonSerializable;
  7. use PHPUnit\Framework\TestCase;
  8. class SupportJsTest extends TestCase
  9. {
  10. public function testScalars()
  11. {
  12. $this->assertEquals('false', (string) Js::from(false));
  13. $this->assertEquals('true', (string) Js::from(true));
  14. $this->assertEquals('1', (string) Js::from(1));
  15. $this->assertEquals('1.1', (string) Js::from(1.1));
  16. $this->assertEquals(
  17. "'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'",
  18. (string) Js::from('<div class="foo">\'quoted html\'</div>')
  19. );
  20. }
  21. public function testArrays()
  22. {
  23. $this->assertEquals(
  24. "JSON.parse('[\\u0022hello\\u0022,\\u0022world\\u0022]')",
  25. (string) Js::from(['hello', 'world'])
  26. );
  27. $this->assertEquals(
  28. "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
  29. (string) Js::from(['foo' => 'hello', 'bar' => 'world'])
  30. );
  31. }
  32. public function testObjects()
  33. {
  34. $this->assertEquals(
  35. "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
  36. (string) Js::from((object) ['foo' => 'hello', 'bar' => 'world'])
  37. );
  38. }
  39. public function testJsonSerializable()
  40. {
  41. // JsonSerializable should take precedence over Arrayable, so we'll
  42. // implement both and make sure the correct data is used.
  43. $data = new class() implements JsonSerializable, Arrayable
  44. {
  45. public $foo = 'not hello';
  46. public $bar = 'not world';
  47. public function jsonSerialize()
  48. {
  49. return ['foo' => 'hello', 'bar' => 'world'];
  50. }
  51. public function toArray()
  52. {
  53. return ['foo' => 'not hello', 'bar' => 'not world'];
  54. }
  55. };
  56. $this->assertEquals(
  57. "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
  58. (string) Js::from($data)
  59. );
  60. }
  61. public function testJsonable()
  62. {
  63. // Jsonable should take precedence over JsonSerializable and Arrayable, so we'll
  64. // implement all three and make sure the correct data is used.
  65. $data = new class() implements Jsonable, JsonSerializable, Arrayable
  66. {
  67. public $foo = 'not hello';
  68. public $bar = 'not world';
  69. public function toJson($options = 0)
  70. {
  71. return json_encode(['foo' => 'hello', 'bar' => 'world'], $options);
  72. }
  73. public function jsonSerialize()
  74. {
  75. return ['foo' => 'not hello', 'bar' => 'not world'];
  76. }
  77. public function toArray()
  78. {
  79. return ['foo' => 'not hello', 'bar' => 'not world'];
  80. }
  81. };
  82. $this->assertEquals(
  83. "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
  84. (string) Js::from($data)
  85. );
  86. }
  87. public function testArrayable()
  88. {
  89. $data = new class() implements Arrayable
  90. {
  91. public $foo = 'not hello';
  92. public $bar = 'not world';
  93. public function toArray()
  94. {
  95. return ['foo' => 'hello', 'bar' => 'world'];
  96. }
  97. };
  98. $this->assertEquals(
  99. "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
  100. (string) Js::from($data)
  101. );
  102. }
  103. }