This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix object validation error when input is not an array (#158)
- Loading branch information
1 parent
0e431da
commit a400e0c
Showing
3 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Raml\Tests\Types; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Raml\ApiDefinition; | ||
use Raml\Parser; | ||
use Raml\Types\TypeValidationError; | ||
|
||
class ObjectTypeTest extends TestCase | ||
{ | ||
/** | ||
* @var ApiDefinition | ||
*/ | ||
private $apiDefinition; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->apiDefinition = (new Parser())->parse(__DIR__ . '/../fixture/object_types.raml'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldCorrectlyValidateCorrectType() | ||
{ | ||
$resource = $this->apiDefinition->getResourceByUri('/actors/1'); | ||
$method = $resource->getMethod('get'); | ||
$response = $method->getResponse(200); | ||
$body = $response->getBodyByType('application/json'); | ||
$type = $body->getType(); | ||
|
||
$type->validate([ | ||
'fistName' => 'Jackie', | ||
'lastName' => 'Сhan', | ||
]); | ||
|
||
$this->assertTrue($type->isValid()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldNotCorrectlyValidateObjectType() | ||
{ | ||
$resource = $this->apiDefinition->getResourceByUri('/actors/1'); | ||
$method = $resource->getMethod('get'); | ||
$response = $method->getResponse(200); | ||
$body = $response->getBodyByType('application/json'); | ||
$type = $body->getType(); | ||
|
||
$type->validate('Jackie Сhan'); | ||
|
||
$this->assertFalse($type->isValid()); | ||
$this->assertCount(1, $type->getErrors()); | ||
$this->assertInstanceOf(TypeValidationError::class, $type->getErrors()[0]); | ||
} | ||
} |
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,23 @@ | ||
#%RAML 1.0 | ||
|
||
title: List of actors API | ||
baseUri: http://example.api.com/{version} | ||
version: v1 | ||
mediaType: application/json | ||
|
||
/actors/{id}: | ||
get: | ||
responses: | ||
200: | ||
body: | ||
application/json: | ||
type: Actor | ||
types: | ||
Actor: | ||
properties: | ||
fistName: | ||
type: string | ||
required: true | ||
lastName: | ||
type: string | ||
required: true |