-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from swagger-mock/unique-array-generator-impro…
…vement fix: issue with exceeding limits of unique array value generator
- Loading branch information
Showing
16 changed files
with
620 additions
and
251 deletions.
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
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
23 changes: 23 additions & 0 deletions
23
src/Mock/Generation/Value/Composite/ArrayGenerator/ArrayLength.php
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 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class ArrayLength | ||
{ | ||
/** @var int */ | ||
public $value; | ||
|
||
/** @var int */ | ||
public $minValue; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Mock/Generation/Value/Composite/ArrayGenerator/ArrayLengthGenerator.php
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,34 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
use App\Mock\Parameters\Schema\Type\Composite\ArrayType; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class ArrayLengthGenerator | ||
{ | ||
private const DEFAULT_MIN_ITEMS = 1; | ||
private const DEFAULT_MAX_ITEMS = 20; | ||
|
||
public function generateArrayLength(ArrayType $type): ArrayLength | ||
{ | ||
$minItems = $type->minItems > 0 ? $type->minItems : self::DEFAULT_MIN_ITEMS; | ||
$maxItems = $type->maxItems > 0 ? $type->maxItems : self::DEFAULT_MAX_ITEMS; | ||
|
||
$length = new ArrayLength(); | ||
$length->value = random_int($minItems, $maxItems); | ||
$length->minValue = $minItems; | ||
|
||
return $length; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Mock/Generation/Value/Composite/ArrayGenerator/ArrayRegularValueGenerator.php
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,41 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
use App\Mock\Generation\Value\ValueGeneratorInterface; | ||
use App\Mock\Parameters\Schema\Type\Composite\ArrayType; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class ArrayRegularValueGenerator | ||
{ | ||
/** @var ArrayLengthGenerator */ | ||
private $lengthGenerator; | ||
|
||
public function __construct(ArrayLengthGenerator $lengthGenerator) | ||
{ | ||
$this->lengthGenerator = $lengthGenerator; | ||
} | ||
|
||
public function generateArray(ValueGeneratorInterface $generator, ArrayType $type): array | ||
{ | ||
$length = $this->lengthGenerator->generateArrayLength($type); | ||
|
||
$values = []; | ||
|
||
for ($i = 1; $i <= $length->value; $i++) { | ||
$values[] = $generator->generateValue($type->items); | ||
} | ||
|
||
return $values; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Mock/Generation/Value/Composite/ArrayGenerator/ArrayUniqueValueGenerator.php
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,76 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
use App\Mock\Generation\Value\ValueGeneratorInterface; | ||
use App\Mock\Parameters\Schema\Type\Composite\ArrayType; | ||
use App\Mock\Parameters\Schema\Type\TypeInterface; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class ArrayUniqueValueGenerator | ||
{ | ||
private const MAX_ATTEMPTS = 100; | ||
|
||
/** @var ArrayLengthGenerator */ | ||
private $lengthGenerator; | ||
|
||
public function __construct(ArrayLengthGenerator $lengthGenerator) | ||
{ | ||
$this->lengthGenerator = $lengthGenerator; | ||
} | ||
|
||
public function generateArray(ValueGeneratorInterface $generator, ArrayType $type): array | ||
{ | ||
$length = $this->lengthGenerator->generateArrayLength($type); | ||
|
||
$values = []; | ||
$uniqueValues = []; | ||
|
||
for ($i = 1; $i <= $length->value; $i++) { | ||
[$value, $attemptsExceeded] = $this->generateUniqueValue($generator, $type->items, $uniqueValues); | ||
|
||
if ($attemptsExceeded) { | ||
if ($i > $length->minValue) { | ||
break; | ||
} | ||
|
||
throw new \RuntimeException('Cannot generate array with unique values, attempts limit exceeded'); | ||
} | ||
|
||
$values[] = $value; | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
private function generateUniqueValue(ValueGeneratorInterface $generator, TypeInterface $itemsType, array &$uniqueValues): array | ||
{ | ||
$attempts = 0; | ||
$attemptsExceeded = false; | ||
|
||
do { | ||
$value = $generator->generateValue($itemsType); | ||
$attempts++; | ||
|
||
if ($attempts > self::MAX_ATTEMPTS) { | ||
$attemptsExceeded = true; | ||
|
||
break; | ||
} | ||
} while (\in_array($value, $uniqueValues, true)); | ||
|
||
$uniqueValues[] = $value; | ||
|
||
return [$value, $attemptsExceeded]; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Mock/Generation/Value/Composite/ArrayGenerator/DelegatingArrayValueGenerator.php
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,65 @@ | ||
<?php | ||
/* | ||
* This file is part of Swagger Mock. | ||
* | ||
* (c) Igor Lazarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
use App\Mock\Generation\Value\ValueGeneratorInterface; | ||
use App\Mock\Generation\ValueGeneratorLocator; | ||
use App\Mock\Parameters\Schema\Type\Composite\ArrayType; | ||
use App\Mock\Parameters\Schema\Type\TypeInterface; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class DelegatingArrayValueGenerator implements ValueGeneratorInterface | ||
{ | ||
/** @var ValueGeneratorLocator */ | ||
private $generatorLocator; | ||
|
||
/** @var ArrayRegularValueGenerator */ | ||
private $regularValueGenerator; | ||
|
||
/** @var ArrayUniqueValueGenerator */ | ||
private $uniqueValueGenerator; | ||
|
||
public function __construct( | ||
ValueGeneratorLocator $generatorLocator, | ||
ArrayRegularValueGenerator $regularValueGenerator, | ||
ArrayUniqueValueGenerator $uniqueValueGenerator | ||
) { | ||
$this->generatorLocator = $generatorLocator; | ||
$this->regularValueGenerator = $regularValueGenerator; | ||
$this->uniqueValueGenerator = $uniqueValueGenerator; | ||
} | ||
|
||
public function generateValue(TypeInterface $type): ?array | ||
{ | ||
if ($type->isNullable() && 0 === random_int(0, 1)) { | ||
$value = null; | ||
} else { | ||
$value = $this->generateArray($type); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
private function generateArray(ArrayType $type): array | ||
{ | ||
$valueGenerator = $this->generatorLocator->getValueGenerator($type->items); | ||
|
||
if ($type->uniqueItems) { | ||
$value = $this->uniqueValueGenerator->generateArray($valueGenerator, $type); | ||
} else { | ||
$value = $this->regularValueGenerator->generateArray($valueGenerator, $type); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
97 changes: 0 additions & 97 deletions
97
src/Mock/Generation/Value/Composite/ArrayValueGenerator.php
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
GET http://localhost:8080/pets | ||
GET http://localhost:8080/persons | ||
|
||
### |
Oops, something went wrong.