Skip to content

Commit

Permalink
Update plugin to new Validator Interface
Browse files Browse the repository at this point in the history
With the new plugin structure from Xulieta 1.0 we can write just the
Validator so we don't have to handle file parsers details.

Co-Authored-by: Jefersson Nathan <malukenho.dev@gmail.com>
Signed-off-by: Alexandre Eher <alexandre@eher.com.br>
  • Loading branch information
EHER and malukenho committed Nov 18, 2020
1 parent 5e491d1 commit 738a5bf
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .xulieta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xulieta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/codelicia/xulieta/xulieta.xsd">
<plugin>Codelicia\XulietaJson\Plugin</plugin>
<plugin>Codelicia\XulietaJson\Plugin</plugin>
<validator>Codelicia\XulietaJson\JsonValidator</validator>
<validator>Codelicia\XulietaJson\JsonValidator</validator>
</xulieta>
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"php": ">=7.4 || ~8.0"
},
"require-dev": {
"codelicia/xulieta": "^0.1.3",
"doctrine/coding-standard": "^8.2.0"
"codelicia/xulieta": "1.0.x-dev as 1.0.0",
"doctrine/coding-standard": "^8.2.0",
"phpunit/phpunit": "^9.4"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutResourceUsageDuringSmallTests="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
bootstrap="./vendor/autoload.php"
verbose="true"
colors="true"
>
<testsuites>
<testsuite name="functional">
<directory suffix=".phpt">./tests/functional</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
45 changes: 45 additions & 0 deletions src/JsonValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Codelicia\XulietaJson;

use Codelicia\Xulieta\Validator\Validator;
use Codelicia\Xulieta\ValueObject\SampleCode;
use Codelicia\Xulieta\ValueObject\Violation;
use LogicException;
use Throwable;

use function json_decode;

use const JSON_THROW_ON_ERROR;

final class JsonValidator implements Validator
{
public function supports(SampleCode $sampleCode): bool
{
return $sampleCode->language() === 'json';
}

public function hasViolation(SampleCode $sampleCode): bool
{
try {
json_decode($sampleCode->code(), true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $ex) {
return true;
}

return false;
}

public function getViolation(SampleCode $sampleCode): Violation
{
try {
json_decode($sampleCode->code(), true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $ex) {
return new Violation($sampleCode, $ex->getMessage(), 0);
}

throw new LogicException();
}
}
48 changes: 0 additions & 48 deletions src/Plugin.php

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions tests/assets/valid-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# File containing json with good syntax

This should be fine

```json
{
"good": "",
"so": "good"
}
```
11 changes: 11 additions & 0 deletions tests/functional/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../../vendor/autoload.php';

return static function (string $params) : void {
$basePath = dirname(__DIR__, 2);

system("php $basePath/vendor/bin/xulieta check:erromeu $params");
};
22 changes: 22 additions & 0 deletions tests/functional/invalid-json.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Report error when invalid json is detected
--FILE--
<?php

$checkRunner = require __DIR__ . '/init.php';

$checkRunner('tests/assets/invalid-json.md');

--EXPECTF--
Finding documentation files on tests/assets/invalid-json.md

--> tests/assets/invalid-json.md
1 | {
2 | "nosso": "",
3 | "erro": "
4 | }
| |
= note: Control character error, possibly incorrectly encoded


Operation failed!
14 changes: 14 additions & 0 deletions tests/functional/valid-json.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Report success with a valid json
--FILE--
<?php

$checkRunner = require __DIR__ . '/init.php';

$checkRunner('tests/assets/valid-json.md');

--EXPECTF--
Finding documentation files on tests/assets/valid-json.md


Everything is OK!

0 comments on commit 738a5bf

Please sign in to comment.