CardHandTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 86
rs 10
c 2
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddCardAndGetCards() 0 14 1
A testGetTotalMoreAces() 0 8 1
A testToArray() 0 7 1
A testGetTotal() 0 8 1
A testGetTotalTwoAces() 0 7 1
A testGetTotalBlackJack() 0 15 1
1
<?php
2
3
namespace App\Tests\Card;
4
5
use PHPUnit\Framework\TestCase;
6
use App\Card\CardHand;
7
use App\Card\Card;
8
9
/**
10
 * Test cases for class CardHand.
11
 */
12
class CardHandTest extends TestCase
13
{
14
    /**
15
     * Test adding cards to the hand and retrieving them.
16
     */
17
    public function testAddCardAndGetCards(): void
18
    {
19
        $cardhand = new CardHand();
20
        $card1 = new Card('Hearts', '8');
21
        $card2 = new Card('Clubs', 'Jack');
22
23
        $cardhand->addCard($card1);
24
        $cardhand->addCard($card2);
25
26
        $cards = $cardhand->getCards();
27
28
        $this->assertSame($card1, $cards[0]);
29
        $this->assertSame($card2, $cards[1]);
30
        $this->assertCount(2, $cards);
31
    }
32
33
    /**
34
     * Test converting the card hand to an array format.
35
     */
36
    public function testToArray(): void
37
    {
38
        $cardhand = new CardHand();
39
        $cardhand->addCard(new Card('Diamonds', 'Queen'));
40
        $handArray = $cardhand->toArray();
41
42
        $this->assertEquals(['value' => 'Queen', 'suit' => 'Diamonds'], $handArray[0]);
43
    }
44
45
    /**
46
     * Test the total value calculation of a card hand.
47
     */
48
    public function testGetTotal(): void
49
    {
50
        $cardhand = new CardHand();
51
        $cardhand->addCard(new Card('Diamonds', 'Queen'));
52
        $cardhand->addCard(new Card('Clubs', '5'));
53
        $cardhand->addCard(new Card('Spades', 'Ace'));
54
55
        $this->assertEquals(12 + 5 + 1, $cardhand->getTotal());
56
    }
57
58
    /**
59
     * Test total value calculation with two Aces.
60
     */
61
    public function testGetTotalTwoAces(): void
62
    {
63
        $cardhand = new CardHand();
64
        $cardhand->addCard(new Card('Heats', 'Ace'));
65
        $cardhand->addCard(new Card('Spades', 'Ace'));
66
67
        $this->assertEquals(1 + 14, $cardhand->getTotal());
68
    }
69
70
    /**
71
     * Test total value calculation with more than two Aces.
72
     */
73
    public function testGetTotalMoreAces(): void
74
    {
75
        $cardhand = new CardHand();
76
        $cardhand->addCard(new Card('Heats', 'Ace'));
77
        $cardhand->addCard(new Card('Spades', 'Ace'));
78
        $cardhand->addCard(new Card('Diamonds', 'Ace'));
79
80
        $this->assertEquals(1 + 14 + 1, $cardhand->getTotal());
81
    }
82
83
    public function testGetTotalBlackJack(): void
84
    {
85
        $hand = new CardHand();
86
87
        // Total without ace 20
88
        $hand->addCard(new Card('Hearts', '10'));
89
        $hand->addCard(new Card('Clubs', '10'));
90
91
        // Add an Ace — total would be 31, Ace becomes 1 instead
92
        $hand->addCard(new Card('Spades', 'Ace'));
93
94
        $total = $hand->getTotalBlackJack();
95
96
        // Expect Ace to be counted as 1
97
        $this->assertEquals(21, $total);
98
    }
99
}
100