Skip to content

Commit

Permalink
Fix combination and tests
Browse files Browse the repository at this point in the history
GuilleGF committed Nov 6, 2016
1 parent ffe9b18 commit 9c7981f
Showing 2 changed files with 69 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/SecretSanta.php
Original file line number Diff line number Diff line change
@@ -62,9 +62,11 @@ public function play()
$result = [];
if ($this->isValidCombination()) {
foreach ($this->combination as $playerId => $secretPlayerId) {
$player = $this->players->player($playerId);
$result[] = [
'player' => $this->players->player($playerId),
'secretPlayer' => $this->players->player($secretPlayerId)
'name' => $player->name(),
'email' => $player->email(),
'secretSanta' => $this->players->player($secretPlayerId)->name()
];
}
}
@@ -87,10 +89,11 @@ private function combinePlayers()
shuffle($players);
shuffle($secretPlayers);
foreach ($players as $player) {
foreach ($secretPlayers as $secretPlayer) {
foreach ($secretPlayers as $key => $secretPlayer) {
if ($player->id() != $secretPlayer->id() && !$this->players->areExclude($player, $secretPlayer)) {
if (!in_array($secretPlayer->id(), $this->combination)) {
$combination[$player->id()] = $secretPlayer->id();
$this->combination[$player->id()] = $secretPlayer->id();
unset ($secretPlayers[$key]);
break;
}
}
62 changes: 62 additions & 0 deletions src/Tests/SecretSantaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use SecretSanta\SecretSanta;

/**
* Class SecretSantaTest
*/
class SecretSantaTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException \SecretSanta\Exceptions\SecretSantaException
*/
public function testPlayerAndCouple()
{
$secretSanta = new SecretSanta();

$secretSanta->addPlayer('Player', 'player@email.com');
$secretSanta->addCouple('Player2', 'player2@email.com', 'Couple2', 'couple2@email.com');

$secretSanta->play();
}

public function testTwoCouples()
{
$secretSanta = new SecretSanta();
$secretSanta->addCouple('Player', 'player@email.com', 'Couple', 'couple@email.com');
$secretSanta->addCouple('Player2', 'player2@email.com', 'Couple2', 'couple2@email.com');

$combination = $secretSanta->play();

$this->assertSame(4 , count($combination));
}

public function testTwoCouplesAndPlayer()
{
$secretSanta = new SecretSanta();

$secretSanta->addPlayer('Player', 'player@email.com');
$secretSanta->addCouple('Player2', 'player2@email.com', 'Couple2', 'couple2@email.com');
$secretSanta->addCouple('Player3', 'player3@email.com', 'Couple3', 'couple3@email.com');

$combination = $secretSanta->play();

$this->assertSame(5 , count($combination));
}

public function testFourCouplesAndTwoPlayers()
{
$secretSanta = new SecretSanta();

$secretSanta->addPlayer('Player', 'player@email.com');
$secretSanta->addPlayer('Player2', 'player2@email.com');
$secretSanta->addCouple('Player3', 'player3@email.com', 'Couple3', 'couple3@email.com');
$secretSanta->addCouple('Player4', 'player4@email.com', 'Couple4', 'couple4@email.com');
$secretSanta->addCouple('Player5', 'player5@email.com', 'Couple5', 'couple5@email.com');
$secretSanta->addCouple('Player6', 'player6@email.com', 'Couple6', 'couple6@email.com');

$combination = $secretSanta->play();

$this->assertSame(10 , count($combination));
}
}

0 comments on commit 9c7981f

Please sign in to comment.