forked from nelmio/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes nelmio#825
- Loading branch information
Showing
16 changed files
with
341 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"\\stdClass": { | ||
"dummy{1..2}": {} | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"Nelmio\\Alice\\support\\models\\User": { | ||
"user0": { | ||
"fullname": "John Doe" | ||
} | ||
} | ||
} |
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 @@ | ||
{} |
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 @@ | ||
{ |
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,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Parser\Chainable; | ||
|
||
use Nelmio\Alice\IsAServiceTrait; | ||
use Nelmio\Alice\Parser\ChainableParserInterface; | ||
use Nelmio\Alice\Throwable\Exception\InvalidArgumentExceptionFactory; | ||
use Nelmio\Alice\Throwable\Exception\Parser\ParseExceptionFactory; | ||
use Nelmio\Alice\Throwable\Exception\Parser\UnparsableFileException; | ||
|
||
final class JsonParser implements ChainableParserInterface | ||
{ | ||
use IsAServiceTrait; | ||
|
||
private const REGEX = '/.{1,}\.json$/i'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canParse(string $file): bool | ||
{ | ||
if (false === stream_is_local($file)) { | ||
return false; | ||
} | ||
|
||
return 1 === preg_match(self::REGEX, $file); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param string $file Local JSON file | ||
* | ||
* @throws UnparsableFileException | ||
*/ | ||
public function parse(string $file): array | ||
{ | ||
if (false === file_exists($file)) { | ||
throw InvalidArgumentExceptionFactory::createForFileCouldNotBeFound($file); | ||
} | ||
|
||
try { | ||
$data = json_decode(file_get_contents($file), true); | ||
|
||
if (null === $data) { | ||
throw ParseExceptionFactory::createForInvalidJson($file); | ||
} | ||
|
||
return $data; | ||
} catch (\Exception $exception) { | ||
if ($exception instanceof UnparsableFileException) { | ||
throw $exception; | ||
} | ||
|
||
throw ParseExceptionFactory::createForUnparsableFile($file, 0, $exception); | ||
} | ||
} | ||
} |
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,154 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Parser\Chainable; | ||
|
||
use Nelmio\Alice\Parser\ChainableParserInterface; | ||
use Nelmio\Alice\Parser\FileListProviderTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
|
||
/** | ||
* @covers \Nelmio\Alice\Parser\Chainable\PhpParser | ||
*/ | ||
class JsonParserTest extends TestCase | ||
{ | ||
use FileListProviderTrait; | ||
|
||
private static $dir; | ||
|
||
/** | ||
* @var JsonParser | ||
*/ | ||
private $parser; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function setUpBeforeClass() | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
self::$dir = __DIR__.'/../../../fixtures/Parser/files/json'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function tearDownAfterClass() | ||
{ | ||
self::$dir = null; | ||
|
||
parent::tearDownAfterClass(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->parser = new JsonParser(); | ||
} | ||
|
||
public function testIsAChainableParser() | ||
{ | ||
$this->assertTrue(is_a(JsonParser::class, ChainableParserInterface::class, true)); | ||
} | ||
|
||
public function testIsNotClonable() | ||
{ | ||
$this->assertFalse((new ReflectionClass(JsonParser::class))->isCloneable()); | ||
} | ||
|
||
/** | ||
* @dataProvider provideJsonList | ||
*/ | ||
public function testCanParseJsonFiles(string $file, array $expectedParsers) | ||
{ | ||
$actual = $this->parser->canParse($file); | ||
$expected = (in_array(get_class($this->parser), $expectedParsers)); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @dataProvider providePhpList | ||
*/ | ||
public function testCanNotParsePhpFiles(string $file) | ||
{ | ||
$actual = $this->parser->canParse($file); | ||
|
||
$this->assertFalse($actual); | ||
} | ||
|
||
/** | ||
* @dataProvider provideYamlList | ||
*/ | ||
public function testCannotParseYamlFiles(string $file) | ||
{ | ||
$actual = $this->parser->canParse($file); | ||
|
||
$this->assertFalse($actual); | ||
} | ||
|
||
/** | ||
* @dataProvider provideUnsupportedList | ||
*/ | ||
public function testCannotParseUnsupportedFiles(string $file) | ||
{ | ||
$actual = $this->parser->canParse($file); | ||
|
||
$this->assertFalse($actual); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage The file "/nowhere.json" could not be found. | ||
*/ | ||
public function testThrowsAnExceptionIfFileDoesNotExist() | ||
{ | ||
$this->parser->parse('/nowhere.json'); | ||
} | ||
|
||
public function testReturnsParsedFileContent() | ||
{ | ||
$actual = $this->parser->parse(self::$dir.'/basic.json'); | ||
|
||
$this->assertSame( | ||
[ | ||
'Nelmio\Alice\support\models\User' => [ | ||
'user0' => [ | ||
'fullname' => 'John Doe', | ||
], | ||
], | ||
], | ||
$actual | ||
); | ||
} | ||
|
||
public function testParsingEmptyFileResultsInEmptySet() | ||
{ | ||
$actual = $this->parser->parse(self::$dir.'/empty.json'); | ||
|
||
$this->assertSame([], $actual); | ||
} | ||
|
||
/** | ||
* @expectedException \Nelmio\Alice\Throwable\Exception\Parser\UnparsableFileException | ||
* @expectedExceptionMessageRegExp /^The file ".+\/invalid\.json" does not contain valid JSON\.$/ | ||
*/ | ||
public function testThrowsAnExceptionIfInvalidJson() | ||
{ | ||
$this->parser->parse(self::$dir.'/invalid.json'); | ||
} | ||
} |
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
Oops, something went wrong.