Skip to content

Commit

Permalink
refactor: Add RandomString generator class
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Dec 11, 2023
1 parent 93caeb8 commit e32b21a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomString.php
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 tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.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\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)
);
}
}

0 comments on commit e32b21a

Please sign in to comment.