From 468b558102a82238daddf0a22f21228e530baccf Mon Sep 17 00:00:00 2001 From: Martin Soenen Date: Sat, 30 Nov 2024 12:18:48 +0100 Subject: [PATCH 1/5] :sparkles: Add boolean method --- src/Exceptions/BadParameterException.php | 11 +++ src/Extensions/BooleanExtension.php | 21 +++++ src/FakerServiceProvider.php | 2 + .../Unit/Extensions/BooleanExtensionTest.php | 81 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/Exceptions/BadParameterException.php create mode 100644 src/Extensions/BooleanExtension.php create mode 100644 tests/Unit/Extensions/BooleanExtensionTest.php diff --git a/src/Exceptions/BadParameterException.php b/src/Exceptions/BadParameterException.php new file mode 100644 index 0000000..fa50232 --- /dev/null +++ b/src/Exceptions/BadParameterException.php @@ -0,0 +1,11 @@ + 100) { + throw new BadParameterException('Percentage must be between 0 and 100'); + } + + $randomValue = $this->randomizer->getInt(1, 100); + + return $randomValue <= $percentage; + } +} \ No newline at end of file diff --git a/src/FakerServiceProvider.php b/src/FakerServiceProvider.php index c95f7e6..fe5243c 100644 --- a/src/FakerServiceProvider.php +++ b/src/FakerServiceProvider.php @@ -2,6 +2,7 @@ namespace Xefi\Faker; +use Xefi\Faker\Extensions\BooleanExtension; use Xefi\Faker\Extensions\ColorsExtension; use Xefi\Faker\Extensions\DateTimeExtension; use Xefi\Faker\Extensions\FinancialExtension; @@ -27,6 +28,7 @@ public function boot(): void ColorsExtension::class, PersonExtension::class, FinancialExtension::class, + BooleanExtension::class ]); } } diff --git a/tests/Unit/Extensions/BooleanExtensionTest.php b/tests/Unit/Extensions/BooleanExtensionTest.php new file mode 100644 index 0000000..5735aea --- /dev/null +++ b/tests/Unit/Extensions/BooleanExtensionTest.php @@ -0,0 +1,81 @@ +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); + } +} \ No newline at end of file From b2311df19481fbe0654f4fc4ba70d9fa65890d5c Mon Sep 17 00:00:00 2001 From: Martin Soenen Date: Sat, 30 Nov 2024 13:12:39 +0100 Subject: [PATCH 2/5] :sparkles: Add array methods --- src/Extensions/ArraysExtension.php | 20 +++++++++ src/Extensions/Extension.php | 10 +++++ src/FakerServiceProvider.php | 4 +- tests/Unit/ExtensionTest.php | 42 +++++++++++++++++++ tests/Unit/Extensions/ArraysExtensionTest.php | 34 +++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/Extensions/ArraysExtension.php create mode 100644 tests/Unit/Extensions/ArraysExtensionTest.php diff --git a/src/Extensions/ArraysExtension.php b/src/Extensions/ArraysExtension.php new file mode 100644 index 0000000..6fa7b9c --- /dev/null +++ b/src/Extensions/ArraysExtension.php @@ -0,0 +1,20 @@ +pickArrayRandomElement($array); + } + + public function randomKey(array $array): mixed + { + return $this->pickArrayRandomKey($array); + } +} \ No newline at end of file diff --git a/src/Extensions/Extension.php b/src/Extensions/Extension.php index 1485b70..fe03b67 100644 --- a/src/Extensions/Extension.php +++ b/src/Extensions/Extension.php @@ -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) { diff --git a/src/FakerServiceProvider.php b/src/FakerServiceProvider.php index fe5243c..72acb13 100644 --- a/src/FakerServiceProvider.php +++ b/src/FakerServiceProvider.php @@ -2,6 +2,7 @@ namespace Xefi\Faker; +use Xefi\Faker\Extensions\ArraysExtension; use Xefi\Faker\Extensions\BooleanExtension; use Xefi\Faker\Extensions\ColorsExtension; use Xefi\Faker\Extensions\DateTimeExtension; @@ -28,7 +29,8 @@ public function boot(): void ColorsExtension::class, PersonExtension::class, FinancialExtension::class, - BooleanExtension::class + BooleanExtension::class, + ArraysExtension::class, ]); } } diff --git a/tests/Unit/ExtensionTest.php b/tests/Unit/ExtensionTest.php index 1e6535f..f217ded 100644 --- a/tests/Unit/ExtensionTest.php +++ b/tests/Unit/ExtensionTest.php @@ -58,4 +58,46 @@ 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); + } + } diff --git a/tests/Unit/Extensions/ArraysExtensionTest.php b/tests/Unit/Extensions/ArraysExtensionTest.php new file mode 100644 index 0000000..5768f13 --- /dev/null +++ b/tests/Unit/Extensions/ArraysExtensionTest.php @@ -0,0 +1,34 @@ +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)); + } +} \ No newline at end of file From 48c3919cc2bfed55231b5474e8d5a6f53889d935 Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Sat, 30 Nov 2024 13:54:49 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- src/Extensions/ArraysExtension.php | 2 +- src/Extensions/BooleanExtension.php | 5 +++-- tests/Unit/ExtensionTest.php | 1 - tests/Unit/Extensions/ArraysExtensionTest.php | 5 +++-- tests/Unit/Extensions/BooleanExtensionTest.php | 7 +++---- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Extensions/ArraysExtension.php b/src/Extensions/ArraysExtension.php index 6fa7b9c..52234a7 100644 --- a/src/Extensions/ArraysExtension.php +++ b/src/Extensions/ArraysExtension.php @@ -17,4 +17,4 @@ public function randomKey(array $array): mixed { return $this->pickArrayRandomKey($array); } -} \ No newline at end of file +} diff --git a/src/Extensions/BooleanExtension.php b/src/Extensions/BooleanExtension.php index c1895cb..bed64e9 100644 --- a/src/Extensions/BooleanExtension.php +++ b/src/Extensions/BooleanExtension.php @@ -9,7 +9,8 @@ class BooleanExtension extends Extension { use HasLocale; - public function boolean(int $percentage = 50) { + public function boolean(int $percentage = 50) + { if ($percentage < 0 || $percentage > 100) { throw new BadParameterException('Percentage must be between 0 and 100'); } @@ -18,4 +19,4 @@ public function boolean(int $percentage = 50) { return $randomValue <= $percentage; } -} \ No newline at end of file +} diff --git a/tests/Unit/ExtensionTest.php b/tests/Unit/ExtensionTest.php index f217ded..3193d4e 100644 --- a/tests/Unit/ExtensionTest.php +++ b/tests/Unit/ExtensionTest.php @@ -99,5 +99,4 @@ public function testExtensionPickArrayRandomKey(): void $this->assertArrayHasKey($result, $elements); $this->assertIsString($result); } - } diff --git a/tests/Unit/Extensions/ArraysExtensionTest.php b/tests/Unit/Extensions/ArraysExtensionTest.php index 5768f13..f1c0eee 100644 --- a/tests/Unit/Extensions/ArraysExtensionTest.php +++ b/tests/Unit/Extensions/ArraysExtensionTest.php @@ -2,7 +2,8 @@ namespace Xefi\Faker\Tests\Unit\Extensions; -final class ArraysExtensionTest extends TestCase { +final class ArraysExtensionTest extends TestCase +{ protected array $testArray = []; protected function setUp(): void @@ -31,4 +32,4 @@ public function testRandomKey(): void $this->assertEqualsCanonicalizing($elements, array_keys($this->testArray)); } -} \ No newline at end of file +} diff --git a/tests/Unit/Extensions/BooleanExtensionTest.php b/tests/Unit/Extensions/BooleanExtensionTest.php index 5735aea..1f9f43e 100644 --- a/tests/Unit/Extensions/BooleanExtensionTest.php +++ b/tests/Unit/Extensions/BooleanExtensionTest.php @@ -2,11 +2,10 @@ namespace Xefi\Faker\Tests\Unit\Extensions; -use Random\Randomizer; use Xefi\Faker\Exceptions\BadParameterException; -use Xefi\Faker\Extensions\ColorsExtension; -final class BooleanExtensionTest extends TestCase { +final class BooleanExtensionTest extends TestCase +{ public function testBoolean(): void { $results = []; @@ -78,4 +77,4 @@ public function testBooleanWithPercentageGreaterThan100(): void $this->expectException(BadParameterException::class); $this->faker->boolean(101); } -} \ No newline at end of file +} From 1c083830a6cbfe92aafbaae3581719898ef43a0b Mon Sep 17 00:00:00 2001 From: Martin Soenen Date: Tue, 10 Dec 2024 21:05:30 +0100 Subject: [PATCH 4/5] :sparkles: Add email method --- src/Extensions/InternetExtension.php | 10 ++++++++++ tests/Unit/Extensions/InternetExtensionTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Extensions/InternetExtension.php b/src/Extensions/InternetExtension.php index 09bf860..4bb88e7 100644 --- a/src/Extensions/InternetExtension.php +++ b/src/Extensions/InternetExtension.php @@ -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()); + } } diff --git a/tests/Unit/Extensions/InternetExtensionTest.php b/tests/Unit/Extensions/InternetExtensionTest.php index 36c6f33..cd6e1f5 100644 --- a/tests/Unit/Extensions/InternetExtensionTest.php +++ b/tests/Unit/Extensions/InternetExtensionTest.php @@ -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); + } + } } From f5a9965b730c4f8bb42062617d25ebbf02b3faad Mon Sep 17 00:00:00 2001 From: Martin Soenen Date: Sun, 15 Dec 2024 17:35:28 +0100 Subject: [PATCH 5/5] Rename ArraysExtension to ArrayExtension --- src/Extensions/{ArraysExtension.php => ArrayExtension.php} | 2 +- src/FakerServiceProvider.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/Extensions/{ArraysExtension.php => ArrayExtension.php} (89%) diff --git a/src/Extensions/ArraysExtension.php b/src/Extensions/ArrayExtension.php similarity index 89% rename from src/Extensions/ArraysExtension.php rename to src/Extensions/ArrayExtension.php index 52234a7..8435949 100644 --- a/src/Extensions/ArraysExtension.php +++ b/src/Extensions/ArrayExtension.php @@ -4,7 +4,7 @@ use Xefi\Faker\Extensions\Traits\HasLocale; -class ArraysExtension extends Extension +class ArrayExtension extends Extension { use HasLocale; diff --git a/src/FakerServiceProvider.php b/src/FakerServiceProvider.php index 72acb13..26bc037 100644 --- a/src/FakerServiceProvider.php +++ b/src/FakerServiceProvider.php @@ -2,7 +2,7 @@ namespace Xefi\Faker; -use Xefi\Faker\Extensions\ArraysExtension; +use Xefi\Faker\Extensions\ArrayExtension; use Xefi\Faker\Extensions\BooleanExtension; use Xefi\Faker\Extensions\ColorsExtension; use Xefi\Faker\Extensions\DateTimeExtension; @@ -30,7 +30,7 @@ public function boot(): void PersonExtension::class, FinancialExtension::class, BooleanExtension::class, - ArraysExtension::class, + ArrayExtension::class, ]); } }