Skip to content

Commit

Permalink
Merge pull request #376 from tienvx/add-random-int-generator-class
Browse files Browse the repository at this point in the history
refactor: Add RandomInt generator class
  • Loading branch information
tienvx committed Dec 11, 2023
2 parents d6dbaf9 + cdc9bc6 commit 93caeb8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomInt.php
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',
];
}
}
9 changes: 9 additions & 0 deletions src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php
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 tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.php
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)
);
}
}

0 comments on commit 93caeb8

Please sign in to comment.