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 faker() helper #41130

Closed
wants to merge 4 commits into from
Closed
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
60 changes: 60 additions & 0 deletions src/Illuminate/Support/Faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Illuminate\Support;

use Exception;
use Faker\Factory;
use Faker\Generator;

class Faker extends Factory
{
public Generator $faker;

public function __construct()
{
$this->faker = self::create();
}

/**
* Check the given method name.
* Here we can choose to use our own method,
* Or call a faker method.
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return match ($name) {
'image', 'alt' => $this->faker->$name(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't here be $this->$name() instead?

Otherwise it is equal to the default arm

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigopedra I think you are correcrt, code could be simplified to:

    public function __call(string $name, array $arguments)
    {
        return $this->faker->$name();
    }

Then it would still use the custom image and alt method, and pass the other calls to faker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure… I missed that the magic method will only be called for missing methods… might be the late hours from yesterday :)

default => $this->faker->$name(),
};
}

/**
* Returns a random picture from Lorem Picsum.
*
* @return string
*/
public function image(): string
{
try {
$rand = random_int(1, 100);
} catch (Exception) {
$rand = 1;
}

return "https://picsum.photos/200/300?random=$rand";
}

/**
* Return a faker sentence to use for our random img.
*
* @return string
*/
public function alt(): string
{
return $this->faker->sentence();
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,27 @@ function with($value, callable $callback = null)
return is_null($callback) ? $value : $callback($value);
}
}

if (! function_exists('faker')) {
/**
* Called without a parameter gives a Faker instance.
* Called with an integer parameter gives a collection of Faker instances.
*
* @param int|null $count
* @return \Illuminate\Support\Collection|\Illuminate\Support\Faker
*/
function faker(int $count = null): \Illuminate\Support\Collection|\Illuminate\Support\Faker
{
if (! $count) {
return new \Illuminate\Support\Faker();
}

$collection = collect([]);

for ($i = 0; $i < 10; $i++) {
$collection->push(new \Illuminate\Support\Faker());
}

return $collection;
}
}
52 changes: 52 additions & 0 deletions tests/Support/SupportFakerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illuminate\Tests\Support;

use Faker\Factory;
use Illuminate\Support\Faker;
use PHPUnit\Framework\TestCase;

class SupportFakerTest extends TestCase
{
public function testIsAChildOfFakerFactory()
{
$faker = faker();

$this->assertInstanceOf(Factory::class, $faker);
}

public function testAFakerHelperIsGloballyAvailable()
{
$faker = faker();

$this->assertInstanceOf(Faker::class, $faker);
}

public function testCanAcceptACount()
{
$faker = faker(10);

$this->assertCount(10, $faker);
}

public function testCollectionContainsFakerInstances()
{
$faker = faker(10);

$this->assertInstanceOf(Faker::class, $faker->first());
}

public function testCanReturnARandomName()
{
$name = faker()->name();

$this->assertIsString($name);
}

public function testCanReturnARandomNameWhenCreatedAsACollection()
{
$faker = faker(10);

$this->assertIsString($faker->first()->name());
}
}