joindin-winner-picker-new/tests/Service/PickerTest.php

106 lines
3.2 KiB
PHP
Raw Normal View History

2019-02-13 22:47:34 +00:00
<?php
namespace App\Tests\Service;
2019-02-14 08:11:09 +00:00
use App\Comment;
2019-02-13 22:47:34 +00:00
use App\Service\Picker;
2019-02-14 18:57:59 +00:00
use App\Tests\Helpers\Factory\CommentFactory;
2019-02-13 22:47:34 +00:00
use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection;
class PickerTest extends TestCase
{
/** @test */
public function hosts_for_multiple_events_are_grouped_and_unique()
{
$data = [
[
'hosts' => [
['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'],
],
],
];
2019-02-14 00:30:21 +00:00
$hosts = (new Picker())
->setHosts(collect($data))
->getHosts();
2019-02-13 22:47:34 +00:00
$this->assertInstanceOf(Collection::class, $hosts);
$this->assertCount(5, $hosts);
}
2019-02-13 23:28:49 +00:00
/** @test */
public function comments_for_multiple_events_are_flattened_and_combined()
{
2019-02-14 00:30:21 +00:00
$data = [
2019-02-14 18:57:59 +00:00
(new CommentFactory())->setCount(2)->create(),
(new CommentFactory())->setCount(1)->create(),
];
2019-02-14 00:30:21 +00:00
$comments = (new Picker())
->setComments(collect($data))
->getComments();
2019-02-14 00:30:21 +00:00
$this->assertInstanceOf(Collection::class, $comments);
$this->assertCount(3, $comments);
2019-02-13 23:28:49 +00:00
}
/** @test */
public function comments_from_event_hosts_cannot_be_picked()
{
2019-02-14 18:57:59 +00:00
$comments = (new CommentFactory())->setCount(3)->create();
2019-02-14 00:27:07 +00:00
$event = [
'hosts' => [
2019-02-14 18:57:59 +00:00
['host_name' => $hostName = $comments[1]->user_display_name],
2019-02-14 00:27:07 +00:00
],
];
2019-02-14 18:57:59 +00:00
/** @var \Tightenco\Collect\Support\Collection $userNames */
$userNames = (new Picker())
2019-02-14 00:27:07 +00:00
->setHosts(collect([$event]))
->setComments(collect($comments))
2019-02-14 18:57:59 +00:00
->getComments()
->map->getUserDisplayName();
2019-02-14 00:27:07 +00:00
2019-02-14 18:57:59 +00:00
$this->assertCount(2, $userNames);
$this->assertFalse($userNames->contains($hostName));
2019-02-13 23:28:49 +00:00
}
/** @test */
2019-02-14 00:44:48 +00:00
public function winners_can_be_selected()
2019-02-13 23:28:49 +00:00
{
2019-02-14 18:57:59 +00:00
$comments = (new CommentFactory())->setCount(3)->create();
2019-02-14 00:44:48 +00:00
$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);
2019-02-14 08:11:09 +00:00
$this->assertInstanceOf(Comment::class, $winners->first());
2019-02-14 00:44:48 +00:00
$this->assertTrue($picker->getComments()->contains($winners->first()));
});
tap($picker->getWinners(2), function (Collection $winners) use ($picker) {
$this->assertCount(2, $winners);
2019-02-14 08:11:09 +00:00
$this->assertInstanceOf(Comment::class, $winners->first());
2019-02-14 00:44:48 +00:00
$this->assertTrue($picker->getComments()->contains($winners->first()));
2019-02-14 08:11:09 +00:00
$this->assertInstanceOf(Comment::class, $winners->last());
2019-02-14 00:44:48 +00:00
$this->assertTrue($picker->getComments()->contains($winners->last()));
});
2019-02-13 23:28:49 +00:00
}
2019-02-13 22:47:34 +00:00
}