-
-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: jasonvarga <[email protected]>
- Loading branch information
1 parent
c0dc744
commit 0c078d8
Showing
7 changed files
with
124 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Modifiers; | ||
|
||
use Facades\Tests\Factories\EntryFactory; | ||
use Statamic\Facades\Entry; | ||
use Statamic\Modifiers\Modify; | ||
use Tests\PreventSavingStacheItemsToDisk; | ||
use Tests\TestCase; | ||
|
||
class ToJsonTest extends TestCase | ||
{ | ||
use PreventSavingStacheItemsToDisk; | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider bourneJsonBourneProvider | ||
*/ | ||
public function it_converts_to_json($input, $expected): void | ||
{ | ||
$modified = $this->modify(value($input)); | ||
|
||
$this->assertEquals($expected, $modified); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider bourneJsonBourneProvider | ||
*/ | ||
public function it_pretty_prints($input, $expected): void | ||
{ | ||
$modified = $this->modify(value($input), ['pretty']); | ||
|
||
$this->assertEquals(json_encode(json_decode($expected, true), JSON_PRETTY_PRINT), $modified); | ||
} | ||
|
||
private function modify($value, $options = []) | ||
{ | ||
return Modify::value($value)->toJson($options)->fetch(); | ||
} | ||
|
||
public static function bourneJsonBourneProvider(): array | ||
{ | ||
return [ | ||
'empty array' => [[], '[]'], | ||
'array' => [['book' => 'All The Places You\'ll Go'], '{"book":"All The Places You\'ll Go"}'], | ||
'string' => ['foo bar baz', '"foo bar baz"'], | ||
'null' => [null, 'null'], | ||
'collection' => [collect(['book' => 'All The Places You\'ll Go']), '{"book":"All The Places You\'ll Go"}'], | ||
'collection with JsonSerializables' => [ | ||
collect([ | ||
new class implements \JsonSerializable | ||
{ | ||
public function jsonSerialize(): array | ||
{ | ||
return ['book' => 'All The Places You\'ll Go']; | ||
} | ||
}, | ||
new class implements \JsonSerializable | ||
{ | ||
public function jsonSerialize(): array | ||
{ | ||
return ['book' => 'Oh, The Places You\'ll Go']; | ||
} | ||
}, | ||
]), '[{"book":"All The Places You\'ll Go"},{"book":"Oh, The Places You\'ll Go"}]', | ||
], | ||
'JsonSerializable object' => [ | ||
new class implements \JsonSerializable | ||
{ | ||
public function jsonSerialize(): array | ||
{ | ||
return ['book' => 'All The Places You\'ll Go']; | ||
} | ||
}, '{"book":"All The Places You\'ll Go"}', | ||
], | ||
'query builder' => [ | ||
function () { | ||
EntryFactory::collection('blog')->data(['title' => 'Post One'])->create(); | ||
EntryFactory::collection('blog')->data(['title' => 'Post Two'])->create(); | ||
|
||
return Entry::query()->get(['title']); | ||
}, '[{"title":"Post One"},{"title":"Post Two"}]', | ||
], | ||
]; | ||
} | ||
} |