From d95ae48026dbd8409bd618df48c358e1dca702ed Mon Sep 17 00:00:00 2001 From: Johan Cleve Date: Sun, 20 Feb 2022 20:21:38 +0100 Subject: [PATCH 1/4] Add faker helper --- src/Illuminate/Support/Faker.php | 59 ++++++++++++++++++++++++++++++ src/Illuminate/Support/helpers.php | 24 ++++++++++++ tests/Support/SupportFakerTest.php | 51 ++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/Illuminate/Support/Faker.php create mode 100644 tests/Support/SupportFakerTest.php diff --git a/src/Illuminate/Support/Faker.php b/src/Illuminate/Support/Faker.php new file mode 100644 index 000000000000..4c7d997204e7 --- /dev/null +++ b/src/Illuminate/Support/Faker.php @@ -0,0 +1,59 @@ +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(), + 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(); + } +} diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 4e0294b9bcb0..dbcc875d1a29 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -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; + } +} diff --git a/tests/Support/SupportFakerTest.php b/tests/Support/SupportFakerTest.php new file mode 100644 index 000000000000..0adab8467dce --- /dev/null +++ b/tests/Support/SupportFakerTest.php @@ -0,0 +1,51 @@ +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()); + } +} From b53f46fdeaac8673c6c7479b27d154d6694500fb Mon Sep 17 00:00:00 2001 From: Johan Cleve Date: Sun, 20 Feb 2022 20:37:39 +0100 Subject: [PATCH 2/4] Codestyle --- src/Illuminate/Support/Faker.php | 8 ++++---- src/Illuminate/Support/helpers.php | 2 +- tests/Support/SupportFakerTest.php | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Faker.php b/src/Illuminate/Support/Faker.php index 4c7d997204e7..925632662cbd 100644 --- a/src/Illuminate/Support/Faker.php +++ b/src/Illuminate/Support/Faker.php @@ -20,8 +20,8 @@ public function __construct() * Here we can choose to use our own method, * Or call a faker method. * - * @param string $name - * @param array $arguments + * @param string $name + * @param array $arguments * @return mixed */ public function __call(string $name, array $arguments) @@ -33,7 +33,7 @@ public function __call(string $name, array $arguments) } /** - * Returns a random picture from Lorem Picsum + * Returns a random picture from Lorem Picsum. * * @return string */ @@ -49,7 +49,7 @@ public function image(): string } /** - * Return a faker sentence to use for our random img + * Return a faker sentence to use for our random img. * @return string */ public function alt(): string diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index dbcc875d1a29..efd9d41c3674 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -424,7 +424,7 @@ function with($value, callable $callback = null) * Called without a parameter gives a Faker instance. * Called with an integer parameter gives a collection of Faker instances. * - * @param int|null $count + * @param int|null $count * @return \Illuminate\Support\Collection|\Illuminate\Support\Faker */ function faker(int $count = null): \Illuminate\Support\Collection|\Illuminate\Support\Faker diff --git a/tests/Support/SupportFakerTest.php b/tests/Support/SupportFakerTest.php index 0adab8467dce..bb17f64db9d2 100644 --- a/tests/Support/SupportFakerTest.php +++ b/tests/Support/SupportFakerTest.php @@ -14,6 +14,7 @@ public function testIsAChildOfFakerFactory() $this->assertInstanceOf(Factory::class, $faker); } + public function testAFakerHelperIsGloballyAvailable() { $faker = faker(); From 007e16c21d5151e1e8cafd1200b6a68d688713ea Mon Sep 17 00:00:00 2001 From: Johan Cleve Date: Sun, 20 Feb 2022 20:39:20 +0100 Subject: [PATCH 3/4] Codestyle --- src/Illuminate/Support/Faker.php | 1 + tests/Support/SupportFakerTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Faker.php b/src/Illuminate/Support/Faker.php index 925632662cbd..27398b455725 100644 --- a/src/Illuminate/Support/Faker.php +++ b/src/Illuminate/Support/Faker.php @@ -50,6 +50,7 @@ public function image(): string /** * Return a faker sentence to use for our random img. + * * @return string */ public function alt(): string diff --git a/tests/Support/SupportFakerTest.php b/tests/Support/SupportFakerTest.php index bb17f64db9d2..949cc8f7563a 100644 --- a/tests/Support/SupportFakerTest.php +++ b/tests/Support/SupportFakerTest.php @@ -14,7 +14,7 @@ public function testIsAChildOfFakerFactory() $this->assertInstanceOf(Factory::class, $faker); } - + public function testAFakerHelperIsGloballyAvailable() { $faker = faker(); From 541f3317e763c93a8b23283046c7877910f27df9 Mon Sep 17 00:00:00 2001 From: Johan Cleve Date: Sun, 20 Feb 2022 20:40:17 +0100 Subject: [PATCH 4/4] Codestyle --- src/Illuminate/Support/Faker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Faker.php b/src/Illuminate/Support/Faker.php index 27398b455725..5528bba5f30f 100644 --- a/src/Illuminate/Support/Faker.php +++ b/src/Illuminate/Support/Faker.php @@ -50,7 +50,7 @@ public function image(): string /** * Return a faker sentence to use for our random img. - * + * * @return string */ public function alt(): string