Skip to content
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
48 changes: 48 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: run-tests

on: [ push, pull_request ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2 ]
dependency-version: [ prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: xdebug

- name: Install dependencies
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Lint
run: composer lint

- name: Execute tests
run: composer test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
fail_ci_if_error: false # optional (default = false)
verbose: true # optional (default = false)
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ composer.lock
coverage.xml
clover.xml
*.local.*
.phpunit.result.cache
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
}
},
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5"
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0"
},
"config": {
"optimize-autoloader": true,
Expand All @@ -36,6 +37,7 @@
"scripts": {
"post-root-package-install": [
],
"lint": "for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \\;; done",
"test": "vendor/bin/phpunit --coverage-text --coverage-clover coverage.xml"
}
}
2 changes: 1 addition & 1 deletion src/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function missingValue($value)
*
* @param string $json The JSON string to fix.
*
* @throws \RuntimeExcaption When fixing fails.
* @throws \RuntimeException When fixing fails.
*
* @return string Fixed JSON. If failed with silent then original JSON.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/PadsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function padLiteral($tmpJson)

protected function padStack($tmpJson)
{
foreach (\array_reverse($this->stack, true) as $index => $token) {
foreach (\array_reverse($this->stack, true) as $token) {
if (isset($this->pairs[$token])) {
$tmpJson .= $this->pairs[$token];
}
Expand Down
41 changes: 15 additions & 26 deletions tests/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,45 @@

class FixerTest extends \PHPUnit\Framework\TestCase
{
protected $fixer;

protected function setUp()
{
$this->fixer = new Fixer;
}

/** @dataProvider theTests */
public function test($json, $expect, $msg = null)
public function test($json, $expect, $msg = '')
{
$this->assertSame($expect, $this->fixer->fix($json), $msg);
$this->assertSame($expect, (new Fixer())->fix($json), $msg);
}

public function test_invalid_literal()
{
$this->fixer->silent(true);
$fixer = (new Fixer())->silent(true);

$this->assertSame('{"a" : invalid', $this->fixer->fix('{"a" : invalid'));
$this->assertSame(' hmm ', $this->fixer->fix(' hmm '));
$this->assertSame('{"a" : invalid', $fixer->fix('{"a" : invalid'));
$this->assertSame(' hmm ', $fixer->fix(' hmm '));
}

public function test_ws()
{
$this->assertSame('{ "a" :null}', $this->fixer->missingValue(null)->fix('{ "a" :'));
$this->assertSame("\n [{}]", $this->fixer->fix("\n [{,"));
$this->assertSame('{ "a" :null}', (new Fixer())->missingValue(null)->fix('{ "a" :'));
$this->assertSame("\n [{}]", (new Fixer())->fix("\n [{,"));
}

public function test_custom_missing()
{
$this->assertSame('{"a":false}', $this->fixer->missingValue(false)->fix('{"a'));
$this->assertSame('{"a":true}', $this->fixer->missingValue('true')->fix('{"a":'));
$this->assertSame('{"a":1,"b":"missing"}', $this->fixer->missingValue('"missing"')->fix('{"a":1,"b"'));
$fixer = new Fixer();
$this->assertSame('{"a":false}', $fixer->missingValue(false)->fix('{"a'));
$this->assertSame('{"a":true}', $fixer->missingValue('true')->fix('{"a":'));
$this->assertSame('{"a":1,"b":"missing"}', $fixer->missingValue('"missing"')->fix('{"a":1,"b"'));
}

public function test_fail_silent()
{
$this->fixer->silent(true);

$this->assertSame('{"a"}', $this->fixer->fix('{"a"}'));
$this->assertSame('{"a"}', (new Fixer())->silent(true)->fix('{"a"}'));
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Could not fix JSON
*/
public function test_fail_throws()
{
$this->fixer->silent(false);
$this->expectException('\RuntimeException');
$this->expectExceptionMessage('Could not fix JSON');

$this->fixer->fix('{,"a');
(new Fixer())->silent(false)->fix('{,"a');
}

public function theTests()
Expand Down