Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
[Slim4] Add integer formats support to Data Mocker (OpenAPITools#4965)
Browse files Browse the repository at this point in the history
* [Slim4] Support int32 and int64 data formats

* [Slim4] Fix data format key in object mocking

* [Slim4] Refresh samples
  • Loading branch information
ybelenko authored and wing328 committed Jan 18, 2020
1 parent ffc6969 commit 420039c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ final class OpenApiDataMocker implements IMocker
$exclusiveMinimum = false,
$exclusiveMaximum = false
) {
$dataFormat = is_string($dataFormat) ? strtolower($dataFormat) : $dataFormat;
switch ($dataFormat) {
case IMocker::DATA_FORMAT_INT32:
// -2147483647..2147483647
$minimum = is_numeric($minimum) ? max($minimum, -2147483647) : -2147483647;
$maximum = is_numeric($maximum) ? min($maximum, 2147483647) : 2147483647;
break;
case IMocker::DATA_FORMAT_INT64:
// -9223372036854775807..9223372036854775807
$minimum = is_numeric($minimum) ? max($minimum, -9223372036854775807) : -9223372036854775807;
$maximum = is_numeric($maximum) ? min($maximum, 9223372036854775807) : 9223372036854775807;
break;
default:
// do nothing, unsupported format
}

return $this->getRandomNumber($minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, 0);
}

Expand Down Expand Up @@ -365,7 +381,7 @@ final class OpenApiDataMocker implements IMocker
foreach ($properties as $propName => $propValue) {
$options = $this->extractSchemaProperties($propValue);
$dataType = $options['type'];
$dataFormat = $options['dataFormat'] ?? null;
$dataFormat = $options['format'] ?? null;
$ref = $options['$ref'] ?? null;
$data = $this->mockFromRef($ref);
$obj->$propName = ($data) ? $data : $this->mock($dataType, $dataFormat, $options);
Expand Down Expand Up @@ -525,7 +541,7 @@ final class OpenApiDataMocker implements IMocker
if ($maxDecimals > 0) {
return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $maxDecimals);
}
return mt_rand($min, $max);
return mt_rand((int) $min, (int) $max);
}
}
{{/apiInfo}}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ class OpenApiDataMockerTest extends TestCase
];
}

/**
* @covers ::mockInteger
* @dataProvider provideMockIntegerFormats
*/
public function testMockIntegerWithFormats(
$dataFormat,
$minimum,
$maximum,
$expectedMin,
$expectedMax
) {
$mocker = new OpenApiDataMocker();
$integer = $mocker->mockInteger($dataFormat, $minimum, $maximum);
$this->assertGreaterThanOrEqual($expectedMin, $integer);
$this->assertLessThanOrEqual($expectedMax, $integer);
}

public function provideMockIntegerFormats()
{
return [
[IMocker::DATA_FORMAT_INT32, -2147483648, 2147483648, -2147483647, 2147483647],
[IMocker::DATA_FORMAT_INT64, '-9223372036854775808', '9223372036854775808', -9223372036854775807, 9223372036854775807],
[IMocker::DATA_FORMAT_INT32, -10, 10, -10, 10],
[IMocker::DATA_FORMAT_INT64, -10, 10, -10, 10],
[IMocker::DATA_FORMAT_INT32, -9223372036854775807, 9223372036854775807, -2147483647, 2147483647],
[strtoupper(IMocker::DATA_FORMAT_INT32), -2147483648, 2147483648, -2147483647, 2147483647],
[strtoupper(IMocker::DATA_FORMAT_INT64), '-9223372036854775808', '9223372036854775808', -9223372036854775807, 9223372036854775807],
];
}

/**
* @dataProvider provideMockNumberCorrectArguments
* @covers ::mockNumber
Expand Down
20 changes: 18 additions & 2 deletions samples/server/petstore/php-slim4/lib/Mock/OpenApiDataMocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ public function mockInteger(
$exclusiveMinimum = false,
$exclusiveMaximum = false
) {
$dataFormat = is_string($dataFormat) ? strtolower($dataFormat) : $dataFormat;
switch ($dataFormat) {
case IMocker::DATA_FORMAT_INT32:
// -2147483647..2147483647
$minimum = is_numeric($minimum) ? max($minimum, -2147483647) : -2147483647;
$maximum = is_numeric($maximum) ? min($maximum, 2147483647) : 2147483647;
break;
case IMocker::DATA_FORMAT_INT64:
// -9223372036854775807..9223372036854775807
$minimum = is_numeric($minimum) ? max($minimum, -9223372036854775807) : -9223372036854775807;
$maximum = is_numeric($maximum) ? min($maximum, 9223372036854775807) : 9223372036854775807;
break;
default:
// do nothing, unsupported format
}

return $this->getRandomNumber($minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, 0);
}

Expand Down Expand Up @@ -357,7 +373,7 @@ public function mockObject(
foreach ($properties as $propName => $propValue) {
$options = $this->extractSchemaProperties($propValue);
$dataType = $options['type'];
$dataFormat = $options['dataFormat'] ?? null;
$dataFormat = $options['format'] ?? null;
$ref = $options['$ref'] ?? null;
$data = $this->mockFromRef($ref);
$obj->$propName = ($data) ? $data : $this->mock($dataType, $dataFormat, $options);
Expand Down Expand Up @@ -517,6 +533,6 @@ protected function getRandomNumber(
if ($maxDecimals > 0) {
return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $maxDecimals);
}
return mt_rand($min, $max);
return mt_rand((int) $min, (int) $max);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ public function provideMockIntegerInvalidArguments()
];
}

/**
* @covers ::mockInteger
* @dataProvider provideMockIntegerFormats
*/
public function testMockIntegerWithFormats(
$dataFormat,
$minimum,
$maximum,
$expectedMin,
$expectedMax
) {
$mocker = new OpenApiDataMocker();
$integer = $mocker->mockInteger($dataFormat, $minimum, $maximum);
$this->assertGreaterThanOrEqual($expectedMin, $integer);
$this->assertLessThanOrEqual($expectedMax, $integer);
}

public function provideMockIntegerFormats()
{
return [
[IMocker::DATA_FORMAT_INT32, -2147483648, 2147483648, -2147483647, 2147483647],
[IMocker::DATA_FORMAT_INT64, '-9223372036854775808', '9223372036854775808', -9223372036854775807, 9223372036854775807],
[IMocker::DATA_FORMAT_INT32, -10, 10, -10, 10],
[IMocker::DATA_FORMAT_INT64, -10, 10, -10, 10],
[IMocker::DATA_FORMAT_INT32, -9223372036854775807, 9223372036854775807, -2147483647, 2147483647],
[strtoupper(IMocker::DATA_FORMAT_INT32), -2147483648, 2147483648, -2147483647, 2147483647],
[strtoupper(IMocker::DATA_FORMAT_INT64), '-9223372036854775808', '9223372036854775808', -9223372036854775807, 9223372036854775807],
];
}

/**
* @dataProvider provideMockNumberCorrectArguments
* @covers ::mockNumber
Expand Down

0 comments on commit 420039c

Please sign in to comment.