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

Adding a With_Faker trait #285

Merged
merged 2 commits into from
Jun 24, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/mantle/database/class-factory-service-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function register_mantle_factory() {
$this->app->singleton(
FakerGenerator::class,
function ( $app, $parameters ) {
$locale = 'en_US';
$locale = config( 'app.faker_locale', Factory::DEFAULT_LOCALE );

if ( ! isset( static::$fakers[ $locale ] ) ) {
static::$fakers[ $locale ] = Factory::create();
Expand Down
49 changes: 49 additions & 0 deletions src/mantle/testing/concerns/trait-with-faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file contains the With_Faker trait
*
* @package Mantle
*/

namespace Mantle\Testing\Concerns;

use Faker\Generator;
use Faker\Factory;

/**
* This trait sets up a faker instance for use in tests.
*/
trait With_Faker {
/**
* Faker instance.
*
* @var Generator
*/
protected Generator $faker;

/**
* Setup the Faker instance.
*/
public function with_faker_set_up() {
$this->faker = $this->make_faker();

$this->faker->unique( true );
}

/**
* Create a faker instance.
*
* @return Generator
*/
protected function make_faker(): Generator {
$locale = isset( $this->app['config'] )
? $this->app['config']->get( 'app.faker_locale', Factory::DEFAULT_LOCALE )
: Factory::DEFAULT_LOCALE;

if ( isset( $this->app ) && $this->app->bound( Generator::class ) ) {
return $this->app->make( Generator::class, [ 'locale' => $locale ] );
}

return Factory::create( $locale );
}
}
13 changes: 13 additions & 0 deletions tests/testing/test-with-faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Mantle\Testing;

use Faker\Generator;
use Mantle\Testing\Concerns\With_Faker;

class Test_With_Faker extends Framework_Test_Case {
use With_Faker;

public function test_creates_faker() {
$this->assertInstanceOf( Generator::class, $this->faker );
}
}
14 changes: 14 additions & 0 deletions tests/testkit/test-with-faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Mantle\Tests\Testkit;

use Faker\Generator;
use Mantle\Testing\Concerns\With_Faker;
use Mantle\Testkit\Test_Case;

class Test_With_Faker extends Test_Case {
use With_Faker;

public function test_creates_faker() {
$this->assertInstanceOf( Generator::class, $this->faker );
}
}