Picking winners

This commit is contained in:
Oliver Davies 2019-02-14 00:44:48 +00:00
parent e54d57a322
commit a18d7c8238
5 changed files with 93 additions and 4 deletions

View file

@ -6,6 +6,7 @@
"ext-ctype": "*", "ext-ctype": "*",
"ext-iconv": "*", "ext-iconv": "*",
"guzzlehttp/guzzle": "^6.3", "guzzlehttp/guzzle": "^6.3",
"josephlavin/tap": "^1.0",
"symfony/cache": "4.2.*", "symfony/cache": "4.2.*",
"symfony/console": "4.2.*", "symfony/console": "4.2.*",
"symfony/dotenv": "4.2.*", "symfony/dotenv": "4.2.*",

41
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "15c99cfaa96233a5a12cf632ac267c27", "content-hash": "35f8b30095e57576a83ae4bdaa898a19",
"packages": [ "packages": [
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
@ -189,6 +189,45 @@
], ],
"time": "2018-12-04T20:46:45+00:00" "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", "name": "psr/cache",
"version": "1.0.1", "version": "1.0.1",

View file

@ -69,8 +69,11 @@ class Picker
* Set the comments for the events. * Set the comments for the events.
* *
* @param \Tightenco\Collect\Support\Collection $comments * @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 $this->comments = $comments
->flatten(1) ->flatten(1)
@ -94,4 +97,17 @@ class Picker
{ {
return $this->hosts->contains($user_display_name); 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);
}
} }

View file

@ -14,6 +14,9 @@
"guzzlehttp/psr7": { "guzzlehttp/psr7": {
"version": "1.5.2" "version": "1.5.2"
}, },
"josephlavin/tap": {
"version": "v1.0.0"
},
"nikic/php-parser": { "nikic/php-parser": {
"version": "v4.2.0" "version": "v4.2.0"
}, },

View file

@ -104,8 +104,38 @@ class PickerTest extends TestCase
} }
/** @test */ /** @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()));
});
} }
} }