Skip to content

Commit 6a15c6e

Browse files
authored
Merge pull request #29 from xefi/feature/regex-strategy
✨ regex strategy
2 parents 4e00dc6 + bc23745 commit 6a15c6e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Diff for: src/Container/Traits/HasStrategies.php

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Xefi\Faker\Container\Traits;
44

5+
use Xefi\Faker\Strategies\RegexStrategy;
56
use Xefi\Faker\Strategies\UniqueStrategy;
67

78
trait HasStrategies
@@ -28,6 +29,20 @@ public function unique(string $seed = '')
2829
return $this;
2930
}
3031

32+
/**
33+
* Add a regex strategy.
34+
*
35+
* @param string $regex
36+
*
37+
* @return $this
38+
*/
39+
public function regex(string $regex)
40+
{
41+
$this->strategies[] = new RegexStrategy($regex);
42+
43+
return $this;
44+
}
45+
3146
/**
3247
* Determine if a generated value passes the strategies.
3348
*

Diff for: src/Strategies/RegexStrategy.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Xefi\Faker\Strategies;
4+
5+
class RegexStrategy extends Strategy
6+
{
7+
protected string $regex;
8+
9+
public function __construct(string $regex)
10+
{
11+
$this->regex = $regex;
12+
}
13+
14+
/**
15+
* Handle the given strategy.
16+
*
17+
* @param $generatedValue
18+
*
19+
* @return bool
20+
*/
21+
public function pass($generatedValue): bool
22+
{
23+
return (bool) preg_match($this->regex, $generatedValue);
24+
}
25+
}

Diff for: tests/Unit/Strategies/RegexStrategyTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Strategies;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use Xefi\Faker\Exceptions\MaximumTriesReached;
7+
use Xefi\Faker\Faker;
8+
use Xefi\Faker\Strategies\RegexStrategy;
9+
use Xefi\Faker\Tests\Unit\TestCase;
10+
11+
class RegexStrategyTest extends TestCase
12+
{
13+
public function testRegexStrategyRegistered(): void
14+
{
15+
$faker = new Faker();
16+
17+
$container = $faker->regex('//');
18+
19+
$this->assertEquals(
20+
[new RegexStrategy('//')],
21+
$container->getStrategies()
22+
);
23+
}
24+
25+
public function testUniqueSingleTest(): void
26+
{
27+
$faker = new Faker();
28+
29+
$this->assertEquals(
30+
'hello',
31+
$faker->regex('//')->returnHello()
32+
);
33+
}
34+
35+
public function testUniqueThrowExceptionOnMaxRetriesReached(): void
36+
{
37+
$faker = new Faker();
38+
39+
$this->expectException(MaximumTriesReached::class);
40+
$this->expectExceptionMessage('Maximum tries of 20000 reached without finding a value');
41+
42+
$faker->regex('/^\d$/')->returnHello();
43+
}
44+
45+
public static function regexProvider(): array
46+
{
47+
return [
48+
['/\d/', 0, 9],
49+
['/\d+/', 0, 100],
50+
['/^[1-9]\d*$/', 1, 1000],
51+
['/^-?\d+$/', -100, 100],
52+
['/^[0-9]{1,3}$/', 0, 999],
53+
['/^[1-9][0-9]{2,5}$/', 100, 999999],
54+
['/^0$/', 0, 0],
55+
];
56+
}
57+
58+
#[DataProvider('regexProvider')]
59+
public function testMultipleRegexCases(string $regex, int $min, int $max): void
60+
{
61+
$faker = new Faker();
62+
63+
$result = $faker->regex($regex)->returnNumberBetween($min, $max);
64+
65+
$this->assertMatchesRegularExpression($regex, (string) $result);
66+
}
67+
}

0 commit comments

Comments
 (0)