Merge branch 'test-factories'

This commit is contained in:
Oliver Davies 2019-02-14 19:08:39 +00:00
commit 0fb5afaf58
5 changed files with 136 additions and 76 deletions

View file

@ -62,6 +62,7 @@
} }
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "^1.8",
"symfony/maker-bundle": "^1.11", "symfony/maker-bundle": "^1.11",
"symfony/test-pack": "^1.0", "symfony/test-pack": "^1.0",
"symfony/var-dumper": "4.2.*" "symfony/var-dumper": "4.2.*"

52
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": "ccd0911d9e64d0a7e474b2f57a50a49a", "content-hash": "734449da136b803395044a6a830c701f",
"packages": [ "packages": [
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
@ -2011,6 +2011,56 @@
], ],
"time": "2018-05-16T17:37:13+00:00" "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", "name": "nikic/php-parser",
"version": "v4.2.0", "version": "v4.2.0",

View file

@ -5,6 +5,9 @@
"facebook/webdriver": { "facebook/webdriver": {
"version": "1.6.0" "version": "1.6.0"
}, },
"fzaninotto/faker": {
"version": "v1.8.0"
},
"guzzlehttp/guzzle": { "guzzlehttp/guzzle": {
"version": "6.3.3" "version": "6.3.3"
}, },

View 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;
}
}

View file

@ -4,6 +4,7 @@ namespace App\Tests\Service;
use App\Comment; use App\Comment;
use App\Service\Picker; use App\Service\Picker;
use App\Tests\Helpers\Factory\CommentFactory;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection; use Tightenco\Collect\Support\Collection;
@ -41,29 +42,9 @@ class PickerTest extends TestCase
/** @test */ /** @test */
public function comments_for_multiple_events_are_flattened_and_combined() 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 = [ $data = [
[$comment1, $comment2], (new CommentFactory())->setCount(2)->create(),
[$comment3], (new CommentFactory())->setCount(1)->create(),
]; ];
$comments = (new Picker()) $comments = (new Picker())
@ -77,72 +58,29 @@ class PickerTest extends TestCase
/** @test */ /** @test */
public function comments_from_event_hosts_cannot_be_picked() public function comments_from_event_hosts_cannot_be_picked()
{ {
$comments = (new CommentFactory())->setCount(3)->create();
$event = [ $event = [
'hosts' => [ 'hosts' => [
['host_name' => 'Oliver Davies'], ['host_name' => $hostName = $comments[1]->user_display_name],
], ],
]; ];
$talkTitle = 'Another awesome PHP talk'; /** @var \Tightenco\Collect\Support\Collection $userNames */
$userNames = (new Picker())
$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())
->setHosts(collect([$event])) ->setHosts(collect([$event]))
->setComments(collect($comments)) ->setComments(collect($comments))
->getComments(); ->getComments()
->map->getUserDisplayName();
$this->assertCount(2, $comments); $this->assertCount(2, $userNames);
$this->assertSame('Peter Fisher', $comments[0]->getUserDisplayName()); $this->assertFalse($userNames->contains($hostName));
$this->assertSame('Zan Baldwin', $comments[1]->getUserDisplayName());
} }
/** @test */ /** @test */
public function winners_can_be_selected() public function winners_can_be_selected()
{ {
$talkTitle = 'A super talk about PHP'; $comments = (new CommentFactory())->setCount(3)->create();
$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],
];
$picker = new Picker(); $picker = new Picker();
$picker->setComments(collect($comments)); $picker->setComments(collect($comments));