From c375390cbbac34e9cc93d29c2ffacef5122adf00 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 18:57:59 +0000 Subject: [PATCH] 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));