Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Domain/Shoe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Domain;

final class Shoe
{
private array $cards = [];

public function __construct(int $decks = 1)
{
$ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'];
$suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];

for ($i = 0; $i < $decks; $i++) {
foreach ($suits as $suit) {
foreach ($ranks as $rank) {
$this->cards[] = new Card($rank, $suit);
}
}
}
}

public function count(): int
{
return count($this->cards);
}

public function draw(): Card
{
return array_shift($this->cards);
}

public function shuffle(): void
{
shuffle($this->cards);
}
}
50 changes: 50 additions & 0 deletions tests/Domain/ShoeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Tests\Domain;

use App\Domain\Card;
use App\Domain\Shoe;
use PHPUnit\Framework\TestCase;

final class ShoeTest extends TestCase
{
public function testShoeWithOneDeckHas52Cards(): void
{
$shoe = new Shoe(1);

$this->assertSame(52, $shoe->count());
}

public function testDrawReturnsACard(): void
{
$shoe = new Shoe(1);
$card = $shoe->draw();

$this->assertInstanceOf(Card::class, $card);
}

public function testDrawReducesCardCount(): void
{
$shoe = new Shoe(1);
$shoe->draw();

$this->assertSame(51, $shoe->count());
}

public function testShuffleRandomizesCards(): void
{
$shoe1 = new Shoe(1);
$shoe2 = new Shoe(1);
$shoe2->shuffle();

$cards1 = [];
$cards2 = [];

for ($i = 0; $i < 52; $i++) {
$cards1[] = $shoe1->draw()->rank();
$cards2[] = $shoe2->draw()->rank();
}

$this->assertNotEquals($cards1, $cards2);
}
}
Loading