Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add provider for fakerphp/faker #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "1.*",
"vimeo/psalm": "^4.6.2 || ^5.2"
"vimeo/psalm": "^4.6.2 || ^5.2",
"fakerphp/faker": "^1.21"
}
}
78 changes: 78 additions & 0 deletions src/Faker/FakerEnumProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace MyCLabs\Enum\Faker;

use Faker\Provider\Base;
use InvalidArgumentException;
use MyCLabs\Enum\Enum;

class FakerEnumProvider extends Base
{
/**
* A random instance of the enum you pass in.
*
* @param class-string $enum
*
* @return Enum
*/
public static function randomEnum(string $enum): Enum
{
if (!is_subclass_of($enum, Enum::class)) {
throw new InvalidArgumentException(
sprintf(
'You have to pass the FQCN of a "%s" class but you passed "%s".',
Enum::class,
$enum
)
);
}

return static::randomElement($enum::values());
}

/**
* A random value of the enum you pass in.
*
* @param class-string $enum
*
* @return string|int
*/
public static function randomEnumValue(string $enum)
{
if (!is_subclass_of($enum, Enum::class)) {
throw new InvalidArgumentException(
sprintf(
'You have to pass the FQCN of a "%s" class but you passed "%s".',
Enum::class,
$enum
)
);
}

return static::randomElement($enum::toArray());
}

/**
* A random label of the enum you pass in.
*
* @param class-string $enum
*
* @return string
*/
public static function randomEnumKey(string $enum): string
{
if (!is_subclass_of($enum, Enum::class)) {
throw new InvalidArgumentException(
sprintf(
'You have to pass the FQCN of a "%s" class but you passed "%s".',
Enum::class,
$enum
)
);
}

return static::randomElement($enum::keys());
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Faker/EnumFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace MyCLabs\Tests\Enum\Unit\Faker;

use MyCLabs\Enum\Enum;

/**
* @method static self TESTED()
* @method static self UNTESTED()
*/
class EnumFixture extends Enum
{
private const TESTED = 'tested';

private const UNTESTED = 'untested';
}
49 changes: 49 additions & 0 deletions tests/Unit/Faker/FakerEnumProviderUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MyCLabs\Tests\Enum\Unit\Faker;

use Faker\Factory;
use Faker\Generator;
use MyCLabs\Enum\Faker\FakerEnumProvider;
use PHPUnit\Framework\TestCase;

class FakerEnumProviderUnitTest extends TestCase
{
/** @var Generator */
private $faker;

public function testSuccessCanFakeEnum(): void
{
/** @var EnumFixture $value */
$value = $this->faker->randomEnum(EnumFixture::class);

self::assertContains($value->getKey(), EnumFixture::keys());
self::assertContains($value->getValue(), EnumFixture::toArray());
}

public function testSuccessCanFakeEnumValue(): void
{
/** @var EnumFixture $value */
$value = $this->faker->randomEnumValue(EnumFixture::class);

self::assertContains($value, EnumFixture::toArray());
}

public function testSuccessCanFakeEnumKey(): void
{
/** @var EnumFixture $value */
$value = $this->faker->randomEnumKey(EnumFixture::class);

self::assertContains($value, EnumFixture::keys());
}

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

$this->faker = Factory::create();
$this->faker->addProvider(FakerEnumProvider::class);
}
}