-
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.
refactor: Add RandomString generator class
- Loading branch information
Showing
2 changed files
with
44 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,26 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Generators; | ||
|
||
use PhpPact\Consumer\Matcher\Model\GeneratorInterface; | ||
|
||
/** | ||
* Generates a random alphanumeric string of the provided length | ||
*/ | ||
class RandomString implements GeneratorInterface | ||
{ | ||
public function __construct(private int $size = 10) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string, string|int> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'size' => $this->size, | ||
'pact:generator:type' => 'RandomString', | ||
]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.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\RandomString; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RandomStringTest extends TestCase | ||
{ | ||
public function testSerialize(): void | ||
{ | ||
$string = new RandomString(11); | ||
$this->assertSame( | ||
'{"size":11,"pact:generator:type":"RandomString"}', | ||
json_encode($string) | ||
); | ||
} | ||
} |