Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
test: add more tests for the JsonGuard error conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
RETFU committed Nov 9, 2017
1 parent 7910dff commit 2c53c67
Show file tree
Hide file tree
Showing 31 changed files with 597 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Error
const DATA_VALIDATION_MAXITEMS = 'DATA_VALIDATION_MAXITEMS';
const DATA_VALIDATION_UNIQUEITEMS = 'DATA_VALIDATION_UNIQUEITEMS';
const DATA_VALIDATION_ONEOF = 'DATA_VALIDATION_ONEOF';
const DATA_VALIDATION_PATTERN = 'DATA_VALIDATION_PATTERN';


/**
Expand Down
8 changes: 3 additions & 5 deletions src/Validator/JsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\JsonReference\Dereferencer;
use RREST\Error;
use RREST\Exception\InvalidJSONException;
use RREST\Validator\Util\JsonGuardValidationError\Converter;
use RREST\Validator\Util\JsonGuardValidationErrorConverter\Converter;

class JsonValidator
{
Expand Down Expand Up @@ -87,10 +87,8 @@ private function getJsonFromString($value)
{
$json = json_decode($value);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidJSONException([new Error(
ucfirst(json_last_error_msg()),
Error::INVALID_JSON
)]);
$error = new Error(ucfirst(json_last_error_msg()), Error::INVALID_JSON);
throw new InvalidJSONException([$error]);
}

return $json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use League\JsonGuard\Constraint\DraftFour\Required;
use RREST\Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use League\JsonGuard\ValidationError;
use RREST\Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use League\JsonGuard\ValidationError;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down Expand Up @@ -35,11 +35,8 @@ public function getErrors()
break;
default:
$message = 'Invalid format';
break;
}
return [new Error(
$message,
Error::DATA_VALIDATION_FORMAT,
$context
)];
return [new Error($message, Error::DATA_VALIDATION_FORMAT, $context)];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand All @@ -13,11 +13,11 @@ public function getErrors()
{
$context = new \stdClass;
$context->field = $this->getFieldPath($this->validationError, '');
$context->minLength = $this->validationError->getParameter();
$context->maxLength = $this->validationError->getParameter();
$context->currentValue = $this->validationError->getData();
return [new Error(
\sprintf('The field %s must be less than %s characters long', $context->field, $context->maxLength),
Error::DATA_VALIDATION_MINLENGTH,
Error::DATA_VALIDATION_MAXLENGTH,
$context
)];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand All @@ -17,7 +17,7 @@ public function getErrors()
$context->currentValues = $this->validationError->getData();
return [new Error(
\sprintf('The field %s must match the pattern %s', $context->field, $context->pattern),
Error::DATA_VALIDATION_UNIQUEITEMS,
Error::DATA_VALIDATION_PATTERN,
$context
)];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace RREST\Validator\Util\JsonGuardValidationError;
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter;

use RREST\Error;

Expand Down
21 changes: 17 additions & 4 deletions tests/units/Validator/JsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

namespace RREST\tests\units\Validator;

require_once __DIR__.'/../boostrap.php';
require_once __DIR__ . '/../boostrap.php';

use atoum;

class JsonValidator extends atoum
{
public function testFails()
{
$jsonValidator= new JsonValidator('{}', '{}');

$this
->given($jsonValidator)
->given($this->newTestedInstance('{}', '{} '))
->boolean($this->testedInstance->fails())
->isFalse();

$schema = file_get_contents(__DIR__ . '/../../fixture/song.json');
$this
->given($this->newTestedInstance('{}', $schema))
->boolean($this->testedInstance->fails())
->isTrue();


$this
->exception(
function () {
$this->newTestedInstance('}', '}')->fails();
}
)
->isInstanceOf('\RREST\Exception\InvalidJSONException');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace RREST\tests\units\Validator\Util\JsonGuardValidationErrorConverter;

require_once __DIR__ . '/../../../boostrap.php';

use atoum;
use League\JsonGuard\Validator;
use RREST\Error;

class AnyOfConverter extends atoum
{
public function testGetErrors()
{
$json = json_decode('{"x":5}');
$schema = json_decode('{
"$schema": "http://json-schema.org/schema",
"type": "object",
"properties": {
"artist": { "type": "string" }
},
"anyOf":[
{"required": ["artist"]},
{"required": ["toto"]}
]
}');
$validator = new Validator($json, $schema);
$error = $validator->errors()[0];

$this
->given($this->newTestedInstance($error))
->array($this->testedInstance->getErrors())
->hasSize(1)
->values
->object[0]->isInstanceOf('\RREST\Error')
->string($this->testedInstance->getErrors()[0]->code)->isEqualTo(Error::DATA_VALIDATION_REQUIRED_ANYOF);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RREST\tests\units\Validator\Util\JsonGuardValidationErrorConverter;

require_once __DIR__ . '/../../../boostrap.php';

use atoum;
use League\JsonGuard\Constraint\DraftFour\Required;

class Converter extends atoum
{
public function testGetErrors()
{
$this
->given($this->newTestedInstance(
new \League\JsonGuard\ValidationError('a','b','c','d','e','f','g')
))
->array($this->testedInstance->getErrors())
->hasSize(1)
->values
->object[0]->isInstanceOf('\RREST\Error');

$this
->given($this->newTestedInstance(
new \League\JsonGuard\ValidationError('a',Required::KEYWORD,'c',['d'],'e','f','g')
))
->array($this->testedInstance->getErrors())
->hasSize(1)
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RREST\tests\units\Validator\Util\JsonGuardValidationErrorConverter;

require_once __DIR__ . '/../../../boostrap.php';

use atoum;
use League\JsonGuard\Validator;
use RREST\Error;

class EnumConverter extends atoum
{
public function testGetErrors()
{
$json = json_decode('{"artist":"c"}');
$schema = json_decode('{
"$schema": "http://json-schema.org/schema",
"type": "object",
"properties": {
"artist": { "type": "string", "enum": ["a","b"] }
},
"required": [ "artist" ]
}');
$validator = new Validator($json, $schema);
$error = $validator->errors()[0];

$this
->given($this->newTestedInstance($error))
->array($this->testedInstance->getErrors())
->hasSize(1)
->values
->object[0]->isInstanceOf('\RREST\Error')
->string($this->testedInstance->getErrors()[0]->code)->isEqualTo(Error::DATA_VALIDATION_ENUM);
}
}
Loading

0 comments on commit 2c53c67

Please sign in to comment.