Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying JSON encode flags #4

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Model/JsonStringConvertible.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public static function fromJSON(string $json): self
return static::jsonDeserialize($object);
}

public function toJSON(): string
public function toJSON(int $flags = 0): string
{
return \Safe\json_encode(static::jsonSerialize());
return \Safe\json_encode(
static::jsonSerialize(),
$flags
);
}

abstract public static function jsonDeserialize(\stdClass $object): self;
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public function __construct(Document $document)
$this->document = $document;
}

public static function jsonDeserialize(\stdClass $value): self
public static function jsonDeserialize(\stdClass $object): self
{
return new self(
Document::jsonDeserialize($value->document)
Document::jsonDeserialize($object->document)
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function testGetConcatenatedPlainTextContentOfInnerNodes(): void
{
$document = $this->loadDocumentFromFixture(__DIR__ . '/fixtures/document_with_text.json');

$this->assertEquals(
$this->assertSame(
"I'd like to introduce you to a very important person!",
$document->text
);
Expand Down
15 changes: 15 additions & 0 deletions tests/LeafTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Prezly\Slate\Tests;

use Prezly\Slate\Model\Leaf;

final class LeafTest extends TestCase
{
public function testToJSON(): void
{
$leaf = new Leaf('foo 😎');
$this->assertSame('{"object":"leaf","text":"foo 😎","marks":[]}', $leaf->toJSON(JSON_UNESCAPED_UNICODE));
$this->assertSame('{"object":"leaf","text":"foo \ud83d\ude0e","marks":[]}', $leaf->toJSON());
}
}
10 changes: 5 additions & 5 deletions tests/UnserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function testAddChildrenToDocument(): void
$this->assertInstanceOf(Block::class, $node);
}

$this->assertEquals('paragraph', $nodes[0]->type);
$this->assertEquals('blockquote', $nodes[1]->type);
$this->assertEquals('list', $nodes[2]->type);
$this->assertSame('paragraph', $nodes[0]->type);
$this->assertSame('blockquote', $nodes[1]->type);
$this->assertSame('list', $nodes[2]->type);
}

public function testNestChildren(): void
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testLoadDocumentWithLeaves(): void

foreach ($text->leaves as $i => $leaf) {
$this->assertInstanceOf(Leaf::class, $leaf);
$this->assertEquals($expected_texts[$i], $leaf->text);
$this->assertSame($expected_texts[$i], $leaf->text);
}
}

Expand All @@ -122,8 +122,8 @@ public function testSetNodeData(): void
$block->data
);

/** @var Inline $inline */
$inline = $block->nodes[0];
assert($inline instanceof Inline);
$this->assertEquals(
(object) ['name' => 'John Doe', 'id' => 1234],
$inline->data
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
final class ValueTest extends TestCase
{
/** @dataProvider fixtures */
public function testSerializeToJson(string $filePath): void
public function testSerializeToJSON(string $filePath): void
{
$json = $this->loadFixture($filePath);
$value = $this->loadContentFromFixture($filePath);
Expand All @@ -18,7 +18,7 @@ public function testSerializeToJson(string $filePath): void
)
);

$this->assertEquals(trim($json), \Safe\json_encode($value, JSON_PRETTY_PRINT));
$this->assertSame(trim($json), \Safe\json_encode($value, JSON_PRETTY_PRINT));
}

/** @return iterable<array{string}> */
Expand Down
Loading