@@ -1806,4 +1806,50 @@ public function test_partition(): void
18061806 {
18071807 $ this ->assertSame ([[true , true ], [false ]], arr ([true , true , false ])->partition (fn (bool $ value ) => $ value === true )->toArray ());
18081808 }
1809+
1810+ public function test_json_encode (): void
1811+ {
1812+ $ data = ['name ' => 'tempest ' , 'version ' => '1.0 ' , 'tags ' => ['php ' , 'framework ' ]];
1813+ $ result = arr ($ data )->jsonEncode ();
1814+
1815+ $ this ->assertInstanceOf (\Tempest \Support \Str \ImmutableString::class, $ result );
1816+ $ this ->assertSame ('{"name":"tempest","version":"1.0","tags":["php","framework"]} ' , $ result ->toString ());
1817+ }
1818+
1819+ public function test_json_encode_pretty (): void
1820+ {
1821+ $ data = ['name ' => 'tempest ' , 'version ' => '1.0 ' ];
1822+ $ result = arr ($ data )->jsonEncode (pretty: true );
1823+
1824+ $ this ->assertInstanceOf (\Tempest \Support \Str \ImmutableString::class, $ result );
1825+ $ this ->assertStringContainsString ("{ \n" , $ result ->toString ());
1826+ $ this ->assertStringContainsString ('"name": "tempest" ' , $ result ->toString ());
1827+ $ this ->assertStringContainsString ('"version": "1.0" ' , $ result ->toString ());
1828+ }
1829+
1830+ public function test_json_encode_mutable (): void
1831+ {
1832+ $ data = ['name ' => 'tempest ' , 'version ' => '1.0 ' ];
1833+ $ result = arr ($ data )->jsonEncode (mutable: true );
1834+
1835+ $ this ->assertInstanceOf (\Tempest \Support \Str \MutableString::class, $ result );
1836+ $ this ->assertSame ('{"name":"tempest","version":"1.0"} ' , $ result ->toString ());
1837+ }
1838+
1839+ public function test_json_encode_array (): void
1840+ {
1841+ $ data = ['php ' , 'framework ' , 'tempest ' ];
1842+ $ result = arr ($ data )->jsonEncode ();
1843+
1844+ $ this ->assertInstanceOf (\Tempest \Support \Str \ImmutableString::class, $ result );
1845+ $ this ->assertSame ('["php","framework","tempest"] ' , $ result ->toString ());
1846+ }
1847+
1848+ public function test_json_encode_empty_array (): void
1849+ {
1850+ $ result = arr ([])->jsonEncode ();
1851+
1852+ $ this ->assertInstanceOf (\Tempest \Support \Str \ImmutableString::class, $ result );
1853+ $ this ->assertSame ('[] ' , $ result ->toString ());
1854+ }
18091855}
0 commit comments