Skip to content

Commit

Permalink
Merge pull request #41 from xefi/issue-38
Browse files Browse the repository at this point in the history
Issue 38 - Add some methods
  • Loading branch information
GautierDele authored Dec 18, 2024
2 parents 9f97fcc + f5a9965 commit 5334be2
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/BadParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Xefi\Faker\Exceptions;

class BadParameterException extends \RuntimeException
{
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
20 changes: 20 additions & 0 deletions src/Extensions/ArrayExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Xefi\Faker\Extensions;

use Xefi\Faker\Extensions\Traits\HasLocale;

class ArrayExtension extends Extension
{
use HasLocale;

public function randomElement(array $array): mixed
{
return $this->pickArrayRandomElement($array);
}

public function randomKey(array $array): mixed
{
return $this->pickArrayRandomKey($array);
}
}
22 changes: 22 additions & 0 deletions src/Extensions/BooleanExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Xefi\Faker\Extensions;

use Xefi\Faker\Exceptions\BadParameterException;
use Xefi\Faker\Extensions\Traits\HasLocale;

class BooleanExtension extends Extension
{
use HasLocale;

public function boolean(int $percentage = 50)
{
if ($percentage < 0 || $percentage > 100) {
throw new BadParameterException('Percentage must be between 0 and 100');
}

$randomValue = $this->randomizer->getInt(1, 100);

return $randomValue <= $percentage;
}
}
10 changes: 10 additions & 0 deletions src/Extensions/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ protected function pickArrayRandomElement(array $array): mixed
return reset($elements);
}

protected function pickArrayRandomKeys(array $array, int $elements = 1): array
{
return $this->randomizer->pickArrayKeys($array, $elements);
}

protected function pickArrayRandomKey(array $array): mixed
{
return $this->pickArrayRandomKeys($array)[0];
}

protected function formatString(string $string): string
{
while (($pos = strpos($string, '{a}')) !== false) {
Expand Down
10 changes: 10 additions & 0 deletions src/Extensions/InternetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ public function macAddress()
{
return implode(':', str_split(substr(md5($this->randomizer->getInt(0, 2147483647)), 0, 12), 2));
}

public function email()
{
$letters = $this->randomizer->getBytesFromString(
implode(range('a', 'z')),
$this->randomizer->getInt(4, 30)
);

return sprintf('%s@%s', $letters, $this->domain());
}
}
4 changes: 4 additions & 0 deletions src/FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xefi\Faker;

use Xefi\Faker\Extensions\ArrayExtension;
use Xefi\Faker\Extensions\BooleanExtension;
use Xefi\Faker\Extensions\ColorsExtension;
use Xefi\Faker\Extensions\DateTimeExtension;
use Xefi\Faker\Extensions\FinancialExtension;
Expand All @@ -27,6 +29,8 @@ public function boot(): void
ColorsExtension::class,
PersonExtension::class,
FinancialExtension::class,
BooleanExtension::class,
ArrayExtension::class,
]);
}
}
41 changes: 41 additions & 0 deletions tests/Unit/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,45 @@ public function testExtensionPickArrayRandomElement(): void
$this->assertGreaterThanOrEqual(1, $result);
$this->assertLessThanOrEqual(100, $result);
}

public function testExtensionPickArrayRandomKeys(): void
{
$elements = [
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
'e' => 5,
];

$method = (new ReflectionClass(Extension::class))->getMethod('pickArrayRandomKeys');
$result = $method->invoke(new Extension(new Randomizer()), $elements, 3);

$this->assertCount(
3,
$result
);

foreach ($result as $key) {
$this->assertArrayHasKey($key, $elements);
$this->assertIsString($key);
}
}

public function testExtensionPickArrayRandomKey(): void
{
$elements = [
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
'e' => 5,
];

$method = (new ReflectionClass(Extension::class))->getMethod('pickArrayRandomKey');
$result = $method->invoke(new Extension(new Randomizer()), $elements);

$this->assertArrayHasKey($result, $elements);
$this->assertIsString($result);
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Extensions/ArraysExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Xefi\Faker\Tests\Unit\Extensions;

final class ArraysExtensionTest extends TestCase
{
protected array $testArray = [];

protected function setUp(): void
{
parent::setUp();

$this->testArray = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
}

public function testRandomElement(): void
{
$elements = [];
for ($i = 0; $i < count($this->testArray); $i++) {
$elements[] = $this->faker->unique()->randomElement($this->testArray);
}

$this->assertEqualsCanonicalizing($elements, $this->testArray);
}

public function testRandomKey(): void
{
$elements = [];
for ($i = 0; $i < count($this->testArray); $i++) {
$elements[] = $this->faker->unique()->randomKey($this->testArray);
}

$this->assertEqualsCanonicalizing($elements, array_keys($this->testArray));
}
}
80 changes: 80 additions & 0 deletions tests/Unit/Extensions/BooleanExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Xefi\Faker\Tests\Unit\Extensions;

use Xefi\Faker\Exceptions\BadParameterException;

final class BooleanExtensionTest extends TestCase
{
public function testBoolean(): void
{
$results = [];
for ($i = 0; $i < 2; $i++) {
$results[] = $this->faker->unique()->boolean();
}

$this->assertEqualsCanonicalizing($results, [true, false]);
}

public function testBooleanAlwaysTrue(): void
{
for ($i = 0; $i < 100; $i++) {
$this->assertTrue($this->faker->boolean(100));
}
}

public function testBooleanAlwaysFalse(): void
{
for ($i = 0; $i < 100; $i++) {
$this->assertFalse($this->faker->boolean(0));
}
}

public function testBooleanWithDefaultPercentage(): void
{
$trueCount = 0;
$falseCount = 0;

for ($i = 0; $i < 1000; $i++) {
if ($this->faker->boolean()) {
$trueCount++;
} else {
$falseCount++;
}
}

// We expect 50% of "true" so we check that there is minimum 45% of each value
$this->assertGreaterThan(450, $trueCount);
$this->assertGreaterThan(450, $falseCount);
}

public function testBooleanWithPercentage(): void
{
$trueCount = 0;
$falseCount = 0;

for ($i = 0; $i < 1000; $i++) {
if ($this->faker->boolean(30)) {
$trueCount++;
} else {
$falseCount++;
}
}

// We expect 30% of "true" so we check that there is minimum 25% of "true" and 65% of "false"
$this->assertGreaterThan(250, $trueCount);
$this->assertGreaterThan(650, $falseCount);
}

public function testBooleanWithPercentageLowerThan100(): void
{
$this->expectException(BadParameterException::class);
$this->faker->boolean(-1);
}

public function testBooleanWithPercentageGreaterThan100(): void
{
$this->expectException(BadParameterException::class);
$this->faker->boolean(101);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Extensions/InternetExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ public function testMacAddress(): void
$this->assertNotFalse(filter_var($result, FILTER_VALIDATE_MAC));
}
}

public function testEmail(): void
{
$faker = new Container(false);

$results = [];

for ($i = 0; $i < 100; $i++) {
$results[] = $faker->unique()->email();
}

foreach ($results as $result) {
// Email regex according to RFC 5322 Official Standard (reference : https://emailregex.com/)
$this->assertMatchesRegularExpression('/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD', $result);
}
}
}

0 comments on commit 5334be2

Please sign in to comment.