diff --git a/.travis.yml b/.travis.yml index d29ba72..7da156c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ php: - '7.0' - '7.1' - '7.2' + - '7.3' - 'nightly' matrix: diff --git a/composer.json b/composer.json index 23b4873..9d81168 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,9 @@ "psr-4": {"ExceptionalJSON\\": "src/"}, "files": ["src/functions.php"] }, + "autoload-dev": { + "psr-4": {"ExceptionalJSON\\Tests\\": "tests/"} + }, "require": { "php": ">=7.0" }, diff --git a/phpunit.xml b/phpunit.xml index d425b8a..f9d9b93 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,6 +3,7 @@ diff --git a/tests/DecodeTest.php b/tests/DecodeTest.php index b139fa9..fd708a8 100644 --- a/tests/DecodeTest.php +++ b/tests/DecodeTest.php @@ -1,6 +1,11 @@ "foo bar baz", "array" => [1,2,3], "bigint" => 12345678901234567890]; @@ -58,8 +63,9 @@ 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()); } } @@ -67,8 +73,9 @@ 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()); } } @@ -76,8 +83,9 @@ 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()); } } @@ -85,8 +93,9 @@ 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()); } } @@ -94,8 +103,9 @@ 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()); } } @@ -103,8 +113,9 @@ 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()); } } } diff --git a/tests/EncodeTest.php b/tests/EncodeTest.php new file mode 100644 index 0000000..096167e --- /dev/null +++ b/tests/EncodeTest.php @@ -0,0 +1,39 @@ + "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()); + } + } +}