Skip to content
Open
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ php:
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- 'nightly'

matrix:
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"psr-4": {"ExceptionalJSON\\": "src/"},
"files": ["src/functions.php"]
},
"autoload-dev": {
"psr-4": {"ExceptionalJSON\\Tests\\": "tests/"}
},
"require": {
"php": ">=7.0"
},
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- https://phpunit.de/manual/5.3/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
>
<testsuites>
Expand Down
25 changes: 18 additions & 7 deletions tests/DecodeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php declare(strict_types=1);

final class DecodeTest extends \PHPUnit\Framework\TestCase
namespace ExceptionalJSON\Tests;

use ExceptionalJSON\DecodeErrorException;
use PHPUnit\Framework\TestCase;

final class DecodeTest extends TestCase
{
const VALID_ENCODED = '{"string":"foo bar baz","array":[1,2,3],"bigint":12345678901234567890}';
const DECODED = ["string" => "foo bar baz", "array" => [1,2,3], "bigint" => 12345678901234567890];
Expand Down Expand Up @@ -58,53 +63,59 @@ public function testDecodeValidJsonInvalidDepth()
{
try {
\ExceptionalJSON\decode(self::VALID_ENCODED, false, 2);
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_DEPTH, $e->getCode());
$this->assertSame(self::VALID_ENCODED, $e->getJSON());
}
}

public function testDecodeInvalidJsonStateMismatch()
{
try {
\ExceptionalJSON\decode('{"foo":"bar"]');
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_STATE_MISMATCH, $e->getCode());
$this->assertSame('{"foo":"bar"]', $e->getJSON());
}
}

public function testDecodeInvalidJsonControlChar()
{
try {
\ExceptionalJSON\decode('{"foo"' . "\x03" . ':"bar"}');
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_CTRL_CHAR, $e->getCode());
$this->assertSame('{"foo"' . "\x03" . ':"bar"}', $e->getJSON());
}
}

public function testDecodeInvalidJsonSyntax()
{
try {
\ExceptionalJSON\decode('{"foo" => "bar"}');
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_SYNTAX, $e->getCode());
$this->assertSame('{"foo" => "bar"}', $e->getJSON());
}
}

public function testDecodeInvalidUtf8()
{
try {
\ExceptionalJSON\decode('{"foo' . "\x80\x80" . '":"bar"}');
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_UTF8, $e->getCode());
$this->assertSame('{"foo' . "\x80\x80" . '":"bar"}', $e->getJSON());
}
}

public function testDecodeInvalidPropertyName()
{
try {
\ExceptionalJSON\decode('{"\u0000foo":"bar"}');
} catch (\ExceptionalJSON\DecodeErrorException $e) {
} catch (DecodeErrorException $e) {
$this->assertSame(\JSON_ERROR_INVALID_PROPERTY_NAME, $e->getCode());
$this->assertSame('{"\u0000foo":"bar"}', $e->getJSON());
}
}
}
39 changes: 39 additions & 0 deletions tests/EncodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace ExceptionalJSON\Tests;

use ExceptionalJSON\EncodeErrorException;
use PHPUnit\Framework\TestCase;

final class EncodeTest extends TestCase
{
const VALID_ENCODED = '{"string":"foo bar baz","array":[1,2,3],"bigint":"12345678901234567890"}';
const ASSOC_ARRAY = ["string" => "foo bar baz", "array" => [1,2,3], "bigint" => "12345678901234567890"];
const BIGINT_AS_STRING = "12345678901234567890";

public function testEncodeAssocArray()
{
$this->assertSame(
self::VALID_ENCODED,
\ExceptionalJSON\encode(self::ASSOC_ARRAY)
);
}

public function testEncodeBigIntegerString()
{
$this->assertSame(
'"' . self::BIGINT_AS_STRING . '"',
\ExceptionalJSON\encode(self::BIGINT_AS_STRING)
);
}

public function testEncodeNonUtf8String()
{
try {
\ExceptionalJSON\encode("\xB1\x31");
} catch (EncodeErrorException $e) {
$this->assertSame(5, $e->getCode());
$this->assertSame("\xB1\x31", $e->getValue());
}
}
}