File: SupportJsTest.php

package info (click to toggle)
php-laravel-framework 8.83.26%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,444 kB
  • sloc: php: 167,609; sh: 162; makefile: 46
file content (124 lines) | stat: -rw-r--r-- 3,793 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Illuminate\Tests\Support;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Js;
use JsonSerializable;
use PHPUnit\Framework\TestCase;

class SupportJsTest extends TestCase
{
    public function testScalars()
    {
        $this->assertEquals('false', (string) Js::from(false));
        $this->assertEquals('true', (string) Js::from(true));
        $this->assertEquals('1', (string) Js::from(1));
        $this->assertEquals('1.1', (string) Js::from(1.1));
        $this->assertEquals(
            "'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'",
            (string) Js::from('<div class="foo">\'quoted html\'</div>')
        );
    }

    public function testArrays()
    {
        $this->assertEquals(
            "JSON.parse('[\\u0022hello\\u0022,\\u0022world\\u0022]')",
            (string) Js::from(['hello', 'world'])
        );

        $this->assertEquals(
            "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
            (string) Js::from(['foo' => 'hello', 'bar' => 'world'])
        );
    }

    public function testObjects()
    {
        $this->assertEquals(
            "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
            (string) Js::from((object) ['foo' => 'hello', 'bar' => 'world'])
        );
    }

    public function testJsonSerializable()
    {
        // JsonSerializable should take precedence over Arrayable, so we'll
        // implement both and make sure the correct data is used.
        $data = new class() implements JsonSerializable, Arrayable
        {
            public $foo = 'not hello';

            public $bar = 'not world';

            public function jsonSerialize()
            {
                return ['foo' => 'hello', 'bar' => 'world'];
            }

            public function toArray()
            {
                return ['foo' => 'not hello', 'bar' => 'not world'];
            }
        };

        $this->assertEquals(
            "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
            (string) Js::from($data)
        );
    }

    public function testJsonable()
    {
        // Jsonable should take precedence over JsonSerializable and Arrayable, so we'll
        // implement all three and make sure the correct data is used.
        $data = new class() implements Jsonable, JsonSerializable, Arrayable
        {
            public $foo = 'not hello';

            public $bar = 'not world';

            public function toJson($options = 0)
            {
                return json_encode(['foo' => 'hello', 'bar' => 'world'], $options);
            }

            public function jsonSerialize()
            {
                return ['foo' => 'not hello', 'bar' => 'not world'];
            }

            public function toArray()
            {
                return ['foo' => 'not hello', 'bar' => 'not world'];
            }
        };

        $this->assertEquals(
            "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
            (string) Js::from($data)
        );
    }

    public function testArrayable()
    {
        $data = new class() implements Arrayable
        {
            public $foo = 'not hello';

            public $bar = 'not world';

            public function toArray()
            {
                return ['foo' => 'hello', 'bar' => 'world'];
            }
        };

        $this->assertEquals(
            "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
            (string) Js::from($data)
        );
    }
}