Skip to content

Commit 2c1cde2

Browse files
committed
Fix broken tests
Many tests become useless with PHP strict type hinting. InvalidArgumentException should be thrown less often.
1 parent 0b96538 commit 2c1cde2

File tree

8 files changed

+39
-186
lines changed

8 files changed

+39
-186
lines changed

src/Mock/BaseModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct()
8787
*/
8888
public static function getOpenApiSchema(): array
8989
{
90-
return json_decode(static::MODEL_SCHEMA);
90+
return json_decode(static::MODEL_SCHEMA, true);
9191
}
9292

9393
/**

src/Mock/OpenApiDataMocker.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function mockInteger(
124124
// do nothing, unsupported format
125125
}
126126

127-
return $this->getRandomNumber($minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, 0);
127+
return (int) $this->getRandomNumber($minimum, $maximum, $exclusiveMinimum, $exclusiveMaximum, 0);
128128
}
129129

130130
/**
@@ -208,19 +208,15 @@ public function mockString(
208208
}
209209

210210
if ($minLength !== 0 && $minLength !== null) {
211-
if (is_int($minLength) === false) {
212-
throw new InvalidArgumentException('"minLength" must be an integer');
213-
} elseif ($minLength < 0) {
211+
if ($minLength < 0) {
214212
throw new InvalidArgumentException('"minLength" must be greater than, or equal to, 0');
215213
}
216214
} else {
217215
$minLength = 0;
218216
}
219217

220218
if ($maxLength !== null) {
221-
if (is_int($maxLength) === false) {
222-
throw new InvalidArgumentException('"maxLength" must be an integer');
223-
} elseif ($maxLength < 0) {
219+
if ($maxLength < 0) {
224220
throw new InvalidArgumentException('"maxLength" must be greater than, or equal to, 0');
225221
}
226222
} else {
@@ -250,7 +246,7 @@ public function mockString(
250246
case IMocker::DATA_FORMAT_DATE:
251247
case IMocker::DATA_FORMAT_DATE_TIME:
252248
// min unix timestamp is 0 and max is 2147483647 for 32bit systems which equals 2038-01-19 03:14:07
253-
$date = DateTime::createFromFormat('U', mt_rand(0, 2147483647));
249+
$date = DateTime::createFromFormat('U', (string) mt_rand(0, 2147483647));
254250
$str = ($dataFormat === IMocker::DATA_FORMAT_DATE) ? $date->format('Y-m-d') : $date->format('Y-m-d\TH:i:sP');
255251

256252
// truncate or pad datestring to fit minLength and maxLength
@@ -334,24 +330,20 @@ public function mockArray(
334330
$minSize = 0;
335331
$maxSize = \PHP_INT_MAX;
336332

337-
if (
338-
(is_array($items) === false && is_object($items) === false)
339-
|| (is_array($items) && array_key_exists('type', $items) === false)
340-
|| (is_object($items) && isset($items->type) === false)
341-
) {
342-
new InvalidArgumentException('"items" must be object or assoc array with "type" key');
333+
if ($items && array_key_exists('type', $items) === false) {
334+
new InvalidArgumentException('"items" must assoc array with "type" key');
343335
}
344336

345337
if ($minItems !== null) {
346-
if (is_integer($minItems) === false || $minItems < 0) {
347-
throw new InvalidArgumentException('"mitItems" must be an integer. This integer must be greater than, or equal to, 0');
338+
if ($minItems < 0) {
339+
throw new InvalidArgumentException('"mitItems" must be an integer greater than, or equal to, 0');
348340
}
349341
$minSize = $minItems;
350342
}
351343

352344
if ($maxItems !== null) {
353-
if (is_integer($maxItems) === false || $maxItems < 0) {
354-
throw new InvalidArgumentException('"maxItems" must be an integer. This integer must be greater than, or equal to, 0.');
345+
if ($maxItems < 0) {
346+
throw new InvalidArgumentException('"maxItems" must be an integer greater than, or equal to, 0.');
355347
}
356348
if ($maxItems < $minItems) {
357349
throw new InvalidArgumentException('"maxItems" value cannot be less than "minItems"');
@@ -399,25 +391,21 @@ public function mockObject(
399391
): object {
400392
$obj = new StdClass();
401393

402-
if (is_object($properties) === false && is_array($properties) === false) {
403-
throw new InvalidArgumentException('The value of "properties" must be an array or object');
404-
}
405-
406394
foreach ($properties as $propName => $propValue) {
407395
if (is_object($propValue) === false && is_array($propValue) === false) {
408396
throw new InvalidArgumentException('Each value of "properties" must be an array or object');
409397
}
410398
}
411399

412400
if ($minProperties !== null) {
413-
if (is_integer($minProperties) === false || $minProperties < 0) {
414-
throw new InvalidArgumentException('"minProperties" must be an integer. This integer must be greater than, or equal to, 0');
401+
if ($minProperties < 0) {
402+
throw new InvalidArgumentException('"minProperties" must be integer greater than, or equal to, 0');
415403
}
416404
}
417405

418406
if ($maxProperties !== null) {
419-
if (is_integer($maxProperties) === false || $maxProperties < 0) {
420-
throw new InvalidArgumentException('"maxProperties" must be an integer. This integer must be greater than, or equal to, 0.');
407+
if ($maxProperties < 0) {
408+
throw new InvalidArgumentException('"maxProperties" must be an integer greater than, or equal to, 0.');
421409
}
422410
if ($maxProperties < $minProperties) {
423411
throw new InvalidArgumentException('"maxProperties" value cannot be less than "minProperties"');
@@ -431,11 +419,8 @@ public function mockObject(
431419
}
432420

433421
if ($required !== null) {
434-
if (
435-
is_array($required) === false
436-
|| count($required) > count(array_unique($required))
437-
) {
438-
throw new InvalidArgumentException('The value of "required" must be an array. Elements of this array must be unique.');
422+
if (count($required) > count(array_unique($required))) {
423+
throw new InvalidArgumentException('The value of "required" must be an array of unique elements.');
439424
}
440425
foreach ($required as $requiredPropName) {
441426
if (is_string($requiredPropName) === false) {
@@ -471,7 +456,7 @@ public function mockFromSchema(array $schema)
471456
if (array_key_exists('$ref', $props) && !empty($props['$ref'])) {
472457
return $this->mockFromRef($props['$ref']);
473458
} elseif ($props['type'] === null) {
474-
throw new InvalidArgumentException('"schema" must be object or assoc array with "type" property');
459+
throw new InvalidArgumentException('"schema" must be assoc array with "type" property');
475460
}
476461
return $this->mock($props['type'], $props['format'], $props);
477462
}
@@ -541,10 +526,8 @@ private function extractSchemaProperties(array $val): array
541526
'$ref',
542527
] as $propName
543528
) {
544-
if (is_array($val) && array_key_exists($propName, $val)) {
529+
if ($val && array_key_exists($propName, $val)) {
545530
$props[$propName] = $val[$propName];
546-
} elseif (is_object($val) && isset($val->$propName)) {
547-
$props[$propName] = $val->$propName;
548531
}
549532
}
550533
return $props;
@@ -581,18 +564,14 @@ protected function getRandomNumber(
581564
}
582565

583566
if ($exclusiveMinimum !== false) {
584-
if (is_bool($exclusiveMinimum) === false) {
585-
throw new InvalidArgumentException('"exclusiveMinimum" must be a boolean');
586-
} elseif ($minimum === null) {
567+
if ($minimum === null) {
587568
throw new InvalidArgumentException('If "exclusiveMinimum" is present, "minimum" must also be present');
588569
}
589570
$min += 1;
590571
}
591572

592573
if ($exclusiveMaximum !== false) {
593-
if (is_bool($exclusiveMaximum) === false) {
594-
throw new InvalidArgumentException('"exclusiveMaximum" must be a boolean');
595-
} elseif ($maximum === null) {
574+
if ($maximum === null) {
596575
throw new InvalidArgumentException('If "exclusiveMaximum" is present, "maximum" must also be present');
597576
}
598577
$max -= 1;
@@ -605,6 +584,10 @@ protected function getRandomNumber(
605584
if ($maxDecimals > 0) {
606585
return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $maxDecimals);
607586
}
587+
if ($min >= \PHP_INT_MAX || $min <= \PHP_INT_MIN || $max >= \PHP_INT_MAX || $max <= \PHP_INT_MIN) {
588+
// mt_rand accepts only integers
589+
return round($min + mt_rand() / mt_getrandmax() * ($max - $min));
590+
}
608591
return mt_rand((int) $min, (int) $max);
609592
}
610593

@@ -617,10 +600,6 @@ protected function getRandomNumber(
617600
*/
618601
public function setModelsNamespace(?string $namespace = null): void
619602
{
620-
if ($namespace !== null && !is_string($namespace)) {
621-
throw new InvalidArgumentException('"namespace" must be a string or null');
622-
}
623-
624603
$this->modelsNamespace = $namespace;
625604
}
626605

src/Utils/ModelUtilsTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public static function getSimpleRef(string $ref): ?string
5858
* @param string|null $modelNamePrefix modelNamePrefix generator option
5959
* @param string|null $modelNameSuffix modelNameSuffix generator option
6060
*
61-
* @return string capitalized model name
61+
* @return string|null capitalized model name
6262
*/
6363
public static function toModelName(
6464
string $name,
6565
?string $modelNamePrefix = null,
6666
?string $modelNameSuffix = null
6767
): ?string {
68-
if (is_string($name) === false || empty($name)) {
68+
if (empty($name)) {
6969
return null;
7070
}
7171

@@ -93,11 +93,11 @@ public static function toModelName(
9393

9494
// add prefix and/or suffic only if name does not start wth \ (e.g. \DateTime)
9595
if (preg_match('/^\\\\.*/', $name) !== 1) {
96-
if (is_string($modelNamePrefix) && !empty($modelNamePrefix)) {
96+
if (!empty($modelNamePrefix)) {
9797
$name = $modelNamePrefix . '_' . $name;
9898
}
9999

100-
if (is_string($modelNameSuffix) && !empty($modelNameSuffix)) {
100+
if (!empty($modelNameSuffix)) {
101101
$name = $name . '_' . $modelNameSuffix;
102102
}
103103
}

src/Utils/StringUtilsTrait.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ public static function camelize(string $word, ?bool $lowercaseFirstLetter = fals
106106
*/
107107
public static function isReservedWord(string $word): bool
108108
{
109-
if (is_string($word) === false) {
110-
return false;
111-
}
112109
// __halt_compiler is ommited because class names with underscores not allowed anyway
113110
return in_array(
114111
strtolower($word),

test/Mock/Model/DogRefTestClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DogRefTestClass extends BaseModel
3434
/**
3535
* @inheritdoc Override static method.
3636
*/
37-
public static function getOpenApiSchema()
37+
public static function getOpenApiSchema(): array
3838
{
3939
// return assoc array instead of object for test purpose
4040
return json_decode(static::MODEL_SCHEMA, true);

0 commit comments

Comments
 (0)