Merge branch 'test-factories'
This commit is contained in:
commit
0fb5afaf58
|
@ -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.*"
|
||||
|
|
52
composer.lock
generated
52
composer.lock
generated
|
@ -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",
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
"facebook/webdriver": {
|
||||
"version": "1.6.0"
|
||||
},
|
||||
"fzaninotto/faker": {
|
||||
"version": "v1.8.0"
|
||||
},
|
||||
"guzzlehttp/guzzle": {
|
||||
"version": "6.3.3"
|
||||
},
|
||||
|
|
68
tests/Helpers/Factory/CommentFactory.php
Normal file
68
tests/Helpers/Factory/CommentFactory.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Helpers\Factory;
|
||||
|
||||
use App\Comment;
|
||||
use Faker\Factory;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class CommentFactory
|
||||
{
|
||||
/**
|
||||
* @var \Faker\Factory
|
||||
*/
|
||||
private $faker;
|
||||
|
||||
/**
|
||||
* The number of comments to create.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $commentCount = 0;
|
||||
|
||||
/**
|
||||
* CommentFactory constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
|
|
Loading…
Reference in a new issue