Skip to content
Prev Previous commit
Next Next commit
[Slim4] Add ref support to mockFromSchema method
  • Loading branch information
ybelenko committed Jan 8, 2020
commit 110dc9674fee66f1d164bd01114f7fb9e421da67
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ final class OpenApiDataMocker implements IMocker
public function mockFromSchema($schema)
{
$props = $this->extractSchemaProperties($schema);
if ($props['type'] === null) {
if (array_key_exists('$ref', $props) && !empty($props['$ref'])) {
return $this->mockFromRef($props['$ref']);
} elseif ($props['type'] === null) {
throw new InvalidArgumentException('"schema" must be object or assoc array with "type" property');
}
return $this->mock($props['type'], $props['format'], $props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ class OpenApiDataMockerTest extends TestCase
],
IsType::TYPE_OBJECT,
],
'referenced class' => [
['$ref' => '#/components/schemas/CatRefTestClass'],
IsType::TYPE_OBJECT,
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ public function mockObject(
public function mockFromSchema($schema)
{
$props = $this->extractSchemaProperties($schema);
if ($props['type'] === null) {
if (array_key_exists('$ref', $props) && !empty($props['$ref'])) {
return $this->mockFromRef($props['$ref']);
} elseif ($props['type'] === null) {
throw new InvalidArgumentException('"schema" must be object or assoc array with "type" property');
}
return $this->mock($props['type'], $props['format'], $props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ public function provideMockFromSchemaCorrectArguments()
],
IsType::TYPE_OBJECT,
],
'referenced class' => [
['$ref' => '#/components/schemas/CatRefTestClass'],
IsType::TYPE_OBJECT,
],
];
}

Expand Down