From d384398081f68d31f95fed545dc39bc7fd6cf16d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 18:50:54 +0000 Subject: [PATCH 1/2] Require faker --- composer.json | 1 + composer.lock | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++- symfony.lock | 3 +++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c73f43b..ba2fbc9 100644 --- a/composer.json +++ b/composer.json @@ -62,6 +62,7 @@ } }, "require-dev": { + "fzaninotto/faker": "^1.8", "symfony/maker-bundle": "^1.11", "symfony/test-pack": "^1.0", "symfony/var-dumper": "4.2.*" diff --git a/composer.lock b/composer.lock index 800cf9c..9b18765 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ccd0911d9e64d0a7e474b2f57a50a49a", + "content-hash": "734449da136b803395044a6a830c701f", "packages": [ { "name": "guzzlehttp/guzzle", @@ -2011,6 +2011,56 @@ ], "time": "2018-05-16T17:37:13+00:00" }, + { + "name": "fzaninotto/faker", + "version": "v1.8.0", + "source": { + "type": "git", + "url": "https://github.com/fzaninotto/Faker.git", + "reference": "f72816b43e74063c8b10357394b6bba8cb1c10de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/f72816b43e74063c8b10357394b6bba8cb1c10de", + "reference": "f72816b43e74063c8b10357394b6bba8cb1c10de", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "ext-intl": "*", + "phpunit/phpunit": "^4.8.35 || ^5.7", + "squizlabs/php_codesniffer": "^1.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8-dev" + } + }, + "autoload": { + "psr-4": { + "Faker\\": "src/Faker/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "time": "2018-07-12T10:23:15+00:00" + }, { "name": "nikic/php-parser", "version": "v4.2.0", diff --git a/symfony.lock b/symfony.lock index f4cb6b9..c7aef90 100644 --- a/symfony.lock +++ b/symfony.lock @@ -5,6 +5,9 @@ "facebook/webdriver": { "version": "1.6.0" }, + "fzaninotto/faker": { + "version": "v1.8.0" + }, "guzzlehttp/guzzle": { "version": "6.3.3" }, From c375390cbbac34e9cc93d29c2ffacef5122adf00 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 18:57:59 +0000 Subject: [PATCH 2/2] Add and use CommentFactory in tests --- tests/Helpers/Factory/CommentFactory.php | 68 ++++++++++++++++++ tests/Service/PickerTest.php | 88 ++++-------------------- 2 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 tests/Helpers/Factory/CommentFactory.php diff --git a/tests/Helpers/Factory/CommentFactory.php b/tests/Helpers/Factory/CommentFactory.php new file mode 100644 index 0000000..9eba50c --- /dev/null +++ b/tests/Helpers/Factory/CommentFactory.php @@ -0,0 +1,68 @@ +faker = Factory::create(); + } + + /** + * Create new comments. + * + * @return \Tightenco\Collect\Support\Collection + */ + public function create(): Collection + { + $talkTitle = $this->faker->sentence; + + return tap(collect(), function (Collection $data) use ($talkTitle) { + if ($this->commentCount > 0) { + foreach (range(1, $this->commentCount) as $i) { + $comment = new \stdClass(); + $comment->talk_title = $talkTitle; + $comment->comment = $this->faker->paragraph; + $comment->uri = 'http://api.joind.in/v2.1/talk_comments/' . $this->faker->randomNumber(8); + $comment->user_display_name = $this->faker->name; + + $data->push($comment); + } + } + }); + } + + /** + * Set the number of comments to create. + * + * @param int $count + * + * @return \App\Tests\Helpers\Factory\CommentFactory + */ + public function setCount(int $count): self + { + $this->commentCount = $count; + + return $this; + } +} diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index a9b99e3..1d8df5a 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -4,6 +4,7 @@ namespace App\Tests\Service; use App\Comment; use App\Service\Picker; +use App\Tests\Helpers\Factory\CommentFactory; use PHPUnit\Framework\TestCase; use Tightenco\Collect\Support\Collection; @@ -41,29 +42,9 @@ class PickerTest extends TestCase /** @test */ public function comments_for_multiple_events_are_flattened_and_combined() { - $talkTitle = 'A cool Symfony Flex talk'; - - $comment1 = new \stdClass(); - $comment1->comment = 'Great talk!'; - $comment1->user_display_name = 'Dan Ackroyd'; - $comment1->uri = 'http://api.joind.in/v2.1/talk_comments/123'; - $comment1->talk_title = $talkTitle; - - $comment2 = new \stdClass(); - $comment2->comment = 'Could be better.'; - $comment2->user_display_name = 'Lucia Velasco'; - $comment2->uri = 'http://api.joind.in/v2.1/talk_comments/456'; - $comment2->talk_title = $talkTitle; - - $comment3 = new \stdClass(); - $comment3->comment = 'Needs more cat pictures.'; - $comment3->user_display_name = 'Rupert Jabelman'; - $comment3->uri = 'http://api.joind.in/v2.1/talk_comments/789'; - $comment3->talk_title = $talkTitle; - $data = [ - [$comment1, $comment2], - [$comment3], + (new CommentFactory())->setCount(2)->create(), + (new CommentFactory())->setCount(1)->create(), ]; $comments = (new Picker()) @@ -77,72 +58,29 @@ class PickerTest extends TestCase /** @test */ public function comments_from_event_hosts_cannot_be_picked() { + $comments = (new CommentFactory())->setCount(3)->create(); + $event = [ 'hosts' => [ - ['host_name' => 'Oliver Davies'], + ['host_name' => $hostName = $comments[1]->user_display_name], ], ]; - $talkTitle = 'Another awesome PHP talk'; - - $comment1 = new \stdClass(); - $comment1->comment = 'Great talk!'; - $comment1->user_display_name = 'Peter Fisher'; - $comment1->uri = 'http://api.joind.in/v2.1/talk_comments/123'; - $comment1->talk_title = $talkTitle; - - $comment2 = new \stdClass(); - $comment2->comment = 'Text on slides could be bigger.'; - $comment2->user_display_name = 'Oliver Davies'; - $comment2->uri = 'http://api.joind.in/v2.1/talk_comments/456'; - $comment2->talk_title = $talkTitle; - - $comment3 = new \stdClass(); - $comment3->comment = 'Speak slower.'; - $comment3->user_display_name = 'Zan Baldwin'; - $comment3->uri = 'http://api.joind.in/v2.1/talk_comments/789'; - $comment3->talk_title = $talkTitle; - - $comments = [ - [$comment1, $comment2, $comment3], - ]; - - $comments = (new Picker()) + /** @var \Tightenco\Collect\Support\Collection $userNames */ + $userNames = (new Picker()) ->setHosts(collect([$event])) ->setComments(collect($comments)) - ->getComments(); + ->getComments() + ->map->getUserDisplayName(); - $this->assertCount(2, $comments); - $this->assertSame('Peter Fisher', $comments[0]->getUserDisplayName()); - $this->assertSame('Zan Baldwin', $comments[1]->getUserDisplayName()); + $this->assertCount(2, $userNames); + $this->assertFalse($userNames->contains($hostName)); } /** @test */ public function winners_can_be_selected() { - $talkTitle = 'A super talk about PHP'; - - $comment1 = new \stdClass(); - $comment1->comment = 'Great talk!'; - $comment1->user_display_name = 'Peter Fisher'; - $comment1->uri = 'http://api.joind.in/v2.1/talk_comments/123'; - $comment1->talk_title = $talkTitle; - - $comment2 = new \stdClass(); - $comment2->comment = 'Text on slides could be bigger.'; - $comment2->user_display_name = 'Michael Bush'; - $comment2->uri = 'http://api.joind.in/v2.1/talk_comments/456'; - $comment2->talk_title = $talkTitle; - - $comment3 = new \stdClass(); - $comment3->comment = 'Speak slower.'; - $comment3->user_display_name = 'Zan Baldwin'; - $comment3->uri = 'http://api.joind.in/v2.1/talk_comments/789'; - $comment3->talk_title = $talkTitle; - - $comments = [ - [$comment1, $comment2, $comment3], - ]; + $comments = (new CommentFactory())->setCount(3)->create(); $picker = new Picker(); $picker->setComments(collect($comments));