From ab9980c92d3bc1612ba8e36024155adaab4a5392 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 13 Feb 2019 22:46:31 +0000 Subject: [PATCH 1/8] Inject the Picker service --- src/Command/PickWinnerCommand.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Command/PickWinnerCommand.php b/src/Command/PickWinnerCommand.php index 4777b34..8dce116 100644 --- a/src/Command/PickWinnerCommand.php +++ b/src/Command/PickWinnerCommand.php @@ -2,6 +2,7 @@ namespace App\Command; +use App\Service\Picker; use GuzzleHttp\Client; use Symfony\Component\Cache\Simple\FilesystemCache; use Symfony\Component\Console\Command\Command; @@ -19,10 +20,22 @@ class PickWinnerCommand extends Command */ private $client; - public function __construct() + /** + * @var \App\Service\Picker + */ + private $picker; + + /** + * PickWinnerCommand constructor. + * + * @param \App\Service\Picker $picker + * The Picker service. + */ + public function __construct(Picker $picker) { parent::__construct(); $this->client = new Client(); + $this->picker = $picker; } /** From 4e551b1347dd00370f00b7b110513d9331b1a296 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 13 Feb 2019 22:47:34 +0000 Subject: [PATCH 2/8] Group hosts for multiple events --- src/Service/Picker.php | 51 ++++++++++++++++++++++++++++++++++++ tests/Service/PickerTest.php | 39 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/Service/Picker.php create mode 100644 tests/Service/PickerTest.php diff --git a/src/Service/Picker.php b/src/Service/Picker.php new file mode 100644 index 0000000..c14d037 --- /dev/null +++ b/src/Service/Picker.php @@ -0,0 +1,51 @@ +hosts = collect(); + } + + /** + * Retrieve the event hosts. + * + * @return \Tightenco\Collect\Support\Collection + */ + public function getHosts(): Collection + { + return $this->hosts; + } + + /** + * Set the hosts for the retrieved events. + * + * @param \Tightenco\Collect\Support\Collection $data + * The event data. + * + * @return self + */ + public function setHosts(Collection $data): self + { + $this->hosts = $data->pluck('hosts.*.host_name') + ->flatten(1) + ->unique() + ->sort(); + + return $this; + } +} diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php new file mode 100644 index 0000000..43c13a7 --- /dev/null +++ b/tests/Service/PickerTest.php @@ -0,0 +1,39 @@ + [ + ['host_name' => 'Lee Stone'], + ['host_name' => 'Dave Liddament'], + ['host_name' => 'Kat Zien'], + ], + ], + [ + 'hosts' => [ + ['host_name' => 'Oliver Davies'], + ['host_name' => 'Lee Stone'], + ['host_name' => 'Lucia Velasco'], + ['host_name' => 'Dave Liddament'], + ], + ], + ]; + + $picker = new Picker(); + $picker->setHosts(collect($data)); + + $hosts = $picker->getHosts(); + $this->assertInstanceOf(Collection::class, $hosts); + $this->assertCount(5, $hosts); + } +} From 6b8931ef5f3329f782809f47f21c7fc51031a3aa Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 13 Feb 2019 23:28:49 +0000 Subject: [PATCH 3/8] Add some more empty tests --- tests/Service/PickerTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index 43c13a7..9c9da46 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -36,4 +36,22 @@ class PickerTest extends TestCase $this->assertInstanceOf(Collection::class, $hosts); $this->assertCount(5, $hosts); } + + /** @test */ + public function comments_for_multiple_events_are_flattened_and_combined() + { + $this->markTestIncomplete(); + } + + /** @test */ + public function comments_from_event_hosts_cannot_be_picked() + { + $this->markTestIncomplete(); + } + + /** @test */ + public function a_winner_can_be_selected() + { + $this->markTestIncomplete(); + } } From ac520c4ea53da6b3a5cb4c6cc1c0cbd50f606666 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 13 Feb 2019 23:53:54 +0000 Subject: [PATCH 4/8] Comments for multiple events are flattened and combined --- src/Service/Picker.php | 26 ++++++++++++++++++++++++++ tests/Service/PickerTest.php | 17 ++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Service/Picker.php b/src/Service/Picker.php index c14d037..8f4da20 100644 --- a/src/Service/Picker.php +++ b/src/Service/Picker.php @@ -13,14 +13,30 @@ class Picker */ private $hosts; + /** + * @var \Tightenco\Collect\Support\Collection + */ + private $comments; + /** * Picker constructor. */ public function __construct() { + $this->comments = collect(); $this->hosts = collect(); } + /** + * Retrieve the combined comments for all events. + * + * @return \Tightenco\Collect\Support\Collection + */ + public function getComments(): Collection + { + return $this->comments; + } + /** * Retrieve the event hosts. * @@ -48,4 +64,14 @@ class Picker return $this; } + + /** + * Set the comments for the events. + * + * @param \Tightenco\Collect\Support\Collection $comments + */ + public function setComments(Collection $comments) + { + $this->comments = $comments->flatten(1)->values(); + } } diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index 9c9da46..3482c4f 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -40,7 +40,22 @@ class PickerTest extends TestCase /** @test */ public function comments_for_multiple_events_are_flattened_and_combined() { - $this->markTestIncomplete(); + $data = [ + [ + ['comment' => 'Great talk!'], + ['comment' => 'Could be better.'], + ], + [ + ['comment' => 'Needs more cat pictures.'], + ], + ]; + + $picker = new Picker(); + $picker->setComments(collect($data)); + + $comments = $picker->getComments(); + $this->assertInstanceOf(Collection::class, $comments); + $this->assertCount(3, $comments); } /** @test */ From db30fd704486d8e860086316c78145567b998b95 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 00:04:07 +0000 Subject: [PATCH 5/8] Update variable names --- tests/Service/PickerTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index 3482c4f..e99b688 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -40,7 +40,7 @@ class PickerTest extends TestCase /** @test */ public function comments_for_multiple_events_are_flattened_and_combined() { - $data = [ + $comments = [ [ ['comment' => 'Great talk!'], ['comment' => 'Could be better.'], @@ -51,11 +51,11 @@ class PickerTest extends TestCase ]; $picker = new Picker(); - $picker->setComments(collect($data)); + $picker->setComments(collect($comments)); - $comments = $picker->getComments(); - $this->assertInstanceOf(Collection::class, $comments); - $this->assertCount(3, $comments); + $result = $picker->getComments(); + $this->assertInstanceOf(Collection::class, $result); + $this->assertCount(3, $result); } /** @test */ From 034e896a4c1be04a5b9613fa8cff1ea6b34f4f84 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 00:27:07 +0000 Subject: [PATCH 6/8] Comments from event hosts are excluded --- src/Service/Picker.php | 22 ++++++++++++++++- tests/Service/PickerTest.php | 47 +++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/Service/Picker.php b/src/Service/Picker.php index 8f4da20..b7b5027 100644 --- a/src/Service/Picker.php +++ b/src/Service/Picker.php @@ -72,6 +72,26 @@ class Picker */ public function setComments(Collection $comments) { - $this->comments = $comments->flatten(1)->values(); + $this->comments = $comments + ->flatten(1) + ->filter(function (array $comment) { + return !$this->isUserAnEventHost($comment['user_display_name']); + }) + ->values(); + + return $this; + } + + /** + * Determine whether a commenter is an event host. + * + * @param string $user_display_name + * The user's display name. + * + * @return bool + */ + private function isUserAnEventHost(string $user_display_name): bool + { + return $this->hosts->contains($user_display_name); } } diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index e99b688..34341e8 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -42,11 +42,20 @@ class PickerTest extends TestCase { $comments = [ [ - ['comment' => 'Great talk!'], - ['comment' => 'Could be better.'], + [ + 'comment' => 'Great talk!', + 'user_display_name' => 'Dan Ackroyd', + ], + [ + 'comment' => 'Could be better.', + 'user_display_name' => 'Lucia Velasco', + ], ], [ - ['comment' => 'Needs more cat pictures.'], + [ + 'comment' => 'Needs more cat pictures.', + 'user_display_name' => 'Rupert Jabelman', + ], ], ]; @@ -61,7 +70,37 @@ class PickerTest extends TestCase /** @test */ public function comments_from_event_hosts_cannot_be_picked() { - $this->markTestIncomplete(); + $event = [ + 'hosts' => [ + ['host_name' => 'Oliver Davies'], + ], + ]; + + $comments = [ + [ + [ + 'comment' => 'Great talk!', + 'user_display_name' => 'Peter Fisher', + ], + [ + 'comment' => 'Text on slides could be bigger.', + 'user_display_name' => 'Oliver Davies', + ], + [ + 'comment' => 'Speak slower.', + 'user_display_name' => 'Zan Baldwin', + ], + ], + ]; + + $comments = (new Picker()) + ->setHosts(collect([$event])) + ->setComments(collect($comments)) + ->getComments(); + + $this->assertCount(2, $comments); + $this->assertSame('Peter Fisher', $comments[0]['user_display_name']); + $this->assertSame('Zan Baldwin', $comments[1]['user_display_name']); } /** @test */ From e54d57a322a9377c3ed7dc62c66abdc48973b95a Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 00:30:21 +0000 Subject: [PATCH 7/8] Tidying --- tests/Service/PickerTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index 34341e8..338702c 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -29,10 +29,10 @@ class PickerTest extends TestCase ], ]; - $picker = new Picker(); - $picker->setHosts(collect($data)); + $hosts = (new Picker()) + ->setHosts(collect($data)) + ->getHosts(); - $hosts = $picker->getHosts(); $this->assertInstanceOf(Collection::class, $hosts); $this->assertCount(5, $hosts); } @@ -40,7 +40,7 @@ class PickerTest extends TestCase /** @test */ public function comments_for_multiple_events_are_flattened_and_combined() { - $comments = [ + $data = [ [ [ 'comment' => 'Great talk!', @@ -59,12 +59,12 @@ class PickerTest extends TestCase ], ]; - $picker = new Picker(); - $picker->setComments(collect($comments)); + $comments = (new Picker()) + ->setComments(collect($data)) + ->getComments(); - $result = $picker->getComments(); - $this->assertInstanceOf(Collection::class, $result); - $this->assertCount(3, $result); + $this->assertInstanceOf(Collection::class, $comments); + $this->assertCount(3, $comments); } /** @test */ From a18d7c82382335be7f052a2b6d71e66f66347008 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 00:44:48 +0000 Subject: [PATCH 8/8] Picking winners --- composer.json | 1 + composer.lock | 41 +++++++++++++++++++++++++++++++++++- src/Service/Picker.php | 18 +++++++++++++++- symfony.lock | 3 +++ tests/Service/PickerTest.php | 34 ++++++++++++++++++++++++++++-- 5 files changed, 93 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index eeeec7d..aca927c 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "ext-ctype": "*", "ext-iconv": "*", "guzzlehttp/guzzle": "^6.3", + "josephlavin/tap": "^1.0", "symfony/cache": "4.2.*", "symfony/console": "4.2.*", "symfony/dotenv": "4.2.*", diff --git a/composer.lock b/composer.lock index dc0a09e..4340eea 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": "15c99cfaa96233a5a12cf632ac267c27", + "content-hash": "35f8b30095e57576a83ae4bdaa898a19", "packages": [ { "name": "guzzlehttp/guzzle", @@ -189,6 +189,45 @@ ], "time": "2018-12-04T20:46:45+00:00" }, + { + "name": "josephlavin/tap", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/josephlavin/tap.git", + "reference": "8733d96fb9b7d7dca204dfe59ec3ff34f3c69be9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/josephlavin/tap/zipball/8733d96fb9b7d7dca204dfe59ec3ff34f3c69be9", + "reference": "8733d96fb9b7d7dca204dfe59ec3ff34f3c69be9", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "require-dev": { + "phpunit/phpunit": "~5.7" + }, + "type": "library", + "autoload": { + "files": [ + "src/tap.php", + "src/TapProxy.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Stand alone port of Laravel's tap method.", + "keywords": [ + "laravel", + "php", + "tap" + ], + "time": "2017-09-24T23:36:28+00:00" + }, { "name": "psr/cache", "version": "1.0.1", diff --git a/src/Service/Picker.php b/src/Service/Picker.php index b7b5027..888b15f 100644 --- a/src/Service/Picker.php +++ b/src/Service/Picker.php @@ -69,8 +69,11 @@ class Picker * Set the comments for the events. * * @param \Tightenco\Collect\Support\Collection $comments + * A collection of comments. + * + * @return $this */ - public function setComments(Collection $comments) + public function setComments(Collection $comments): self { $this->comments = $comments ->flatten(1) @@ -94,4 +97,17 @@ class Picker { return $this->hosts->contains($user_display_name); } + + /** + * Select and return the winners. + * + * @param int $count + * The number of winners. + * + * @return \Tightenco\Collect\Support\Collection + */ + public function getWinners(int $count): Collection + { + return $this->getComments()->random($count); + } } diff --git a/symfony.lock b/symfony.lock index 7bb8952..f4cb6b9 100644 --- a/symfony.lock +++ b/symfony.lock @@ -14,6 +14,9 @@ "guzzlehttp/psr7": { "version": "1.5.2" }, + "josephlavin/tap": { + "version": "v1.0.0" + }, "nikic/php-parser": { "version": "v4.2.0" }, diff --git a/tests/Service/PickerTest.php b/tests/Service/PickerTest.php index 338702c..50b53c7 100644 --- a/tests/Service/PickerTest.php +++ b/tests/Service/PickerTest.php @@ -104,8 +104,38 @@ class PickerTest extends TestCase } /** @test */ - public function a_winner_can_be_selected() + public function winners_can_be_selected() { - $this->markTestIncomplete(); + $comments = [ + [ + [ + 'comment' => 'Great talk!', + 'user_display_name' => 'Peter Fisher', + ], + [ + 'comment' => 'Text on slides could be bigger.', + 'user_display_name' => 'Michael Bush', + ], + [ + 'comment' => 'Speak slower.', + 'user_display_name' => 'Zan Baldwin', + ], + ], + ]; + + $picker = new Picker(); + $picker->setComments(collect($comments)); + $this->assertCount(3, $picker->getComments()); + + tap($picker->getWinners(1), function (Collection $winners) use ($picker) { + $this->assertCount(1, $winners); + $this->assertTrue($picker->getComments()->contains($winners->first())); + }); + + tap($picker->getWinners(2), function (Collection $winners) use ($picker) { + $this->assertCount(2, $winners); + $this->assertTrue($picker->getComments()->contains($winners->first())); + $this->assertTrue($picker->getComments()->contains($winners->last())); + }); } }