Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 2c22a6c

Browse files
committed
Add Asserts::assertDefinitionSchema (#18)
1 parent 2309e65 commit 2c22a6c

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/PhpUnit/Asserts.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,19 @@ final protected static function assertResponseBody(Spec $spec, $template, $metho
297297
Assert::assertEmpty(json_decode($body), $msg);
298298
}
299299
}
300+
301+
/**
302+
* @param Spec $spec
303+
* @param string $class
304+
* @param mixed $actual
305+
* @param string $msg
306+
*/
307+
final protected static function assertDefinitionSchema(Spec $spec, $class, $actual, $msg = '')
308+
{
309+
Assert::assertThat(
310+
$actual,
311+
new JsonSchemaConstraint($spec->getDefinition($class)),
312+
$msg
313+
);
314+
}
300315
}

tests/PhpUnit/AssertsTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public function validateValidResponse($template, $method, Response $response)
8989
$this->assertResponse($this->spec, $template, $method, $response);
9090
}
9191

92+
/**
93+
* @test
94+
* @dataProvider provideValidObjects
95+
*
96+
* @param string $class
97+
* @param object $object
98+
*/
99+
public function validateValidDefinition($class, $object)
100+
{
101+
$this->assertDefinitionSchema($this->spec, $class, $object);
102+
}
103+
92104
/**
93105
* @return array
94106
*/
@@ -174,4 +186,27 @@ public function provideValidResponses()
174186
],
175187
];
176188
}
189+
190+
/**
191+
* @return array
192+
*/
193+
public function provideValidObjects()
194+
{
195+
return [
196+
[
197+
'Post',
198+
$this->createObject(
199+
[
200+
'id' => 'foo',
201+
'title' => 'Hello world!',
202+
'body' => 'Hello world!',
203+
'author' => [
204+
'name' => 'John Dou',
205+
'email' => 'john.dou@mail.com',
206+
],
207+
]
208+
),
209+
],
210+
];
211+
}
177212
}

0 commit comments

Comments
 (0)