-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from xefi/issue-38
Issue 38 - Add some methods
- Loading branch information
Showing
10 changed files
with
249 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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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
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
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
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,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)); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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