-
Notifications
You must be signed in to change notification settings - Fork 92
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 #376 from tienvx/add-random-int-generator-class
refactor: Add RandomInt generator class
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Generators; | ||
|
||
use PhpPact\Consumer\Matcher\Model\GeneratorInterface; | ||
|
||
/** | ||
* Generates a random integer between the min and max values (inclusive) | ||
*/ | ||
class RandomInt implements GeneratorInterface | ||
{ | ||
public function __construct(private int $min = 0, private int $max = 10) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string, string|int> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'min' => $this->min, | ||
'max' => $this->max, | ||
'pact:generator:type' => 'RandomInt', | ||
]; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Model; | ||
|
||
use JsonSerializable; | ||
|
||
interface GeneratorInterface extends JsonSerializable | ||
{ | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.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,18 @@ | ||
<?php | ||
|
||
namespace PhpPactTest\Consumer\Matcher\Generators; | ||
|
||
use PhpPact\Consumer\Matcher\Generators\RandomInt; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RandomIntTest extends TestCase | ||
{ | ||
public function testSerialize(): void | ||
{ | ||
$int = new RandomInt(5, 15); | ||
$this->assertSame( | ||
'{"min":5,"max":15,"pact:generator:type":"RandomInt"}', | ||
json_encode($int) | ||
); | ||
} | ||
} |