BlackjackGameProjEndTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 55
c 3
b 0
f 0
dl 0
loc 128
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCurrentHandAndNextHand() 0 12 1
A testIsGameOver() 0 11 1
A testIsHandBusted() 0 10 2
A testIsComputerHand() 0 6 1
A testGetCurrentHandAllHandsFinished() 0 18 2
A testCheckForBlackjack() 0 21 1
A testFinishGame() 0 15 2
1
<?php
2
3
namespace App\Tests\Game;
4
5
use App\GameProj\BlackjackGameProj;
6
use App\Card\DeckProj;
7
use App\Card\Card;
8
use PHPUnit\Framework\TestCase;
9
10
class BlackjackGameProjEndTest extends TestCase
11
{
12
    /**
13
     * Test isHandBusted method.
14
     */
15
    public function testIsHandBusted()
16
    {
17
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
18
        $game->dealInitialCards();
19
20
        while (!$game->isHandBusted(0)) {
21
            $game->hitHand(0);
22
        }
23
24
        $this->assertTrue($game->isHandBusted(0));
25
    }
26
27
    /**
28
     * Test isGameOver method.
29
     */
30
    public function testIsGameOver()
31
    {
32
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00);
33
        $game->dealInitialCards();
34
35
        $this->assertFalse($game->isGameOver());
36
37
        $game->standHand(0);
38
        $game->standHand(1);
39
40
        $this->assertTrue($game->isGameOver());
41
    }
42
43
    /**
44
     * Test getCurrentHand and nextHand methods.
45
     */
46
    public function testGetCurrentHandAndNextHand()
47
    {
48
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00);
49
        $game->dealInitialCards();
50
51
        $this->assertEquals(0, $game->getCurrentHand());
52
53
        $game->nextHand();
54
        $this->assertEquals(1, $game->getCurrentHand());
55
56
        $game->nextHand();
57
        $this->assertEquals(0, $game->getCurrentHand());
58
    }
59
60
    /**
61
     * Test getCurrentHand when finished.
62
     */
63
    public function testGetCurrentHandAllHandsFinished()
64
    {
65
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00);
66
        $game->dealInitialCards();
67
68
        $handCount = 0;
69
        while (($currentHand = $game->getCurrentHand()) !== null) {
70
            $this->assertIsInt($currentHand);
71
            $this->assertGreaterThanOrEqual(0, $currentHand);
72
            $this->assertLessThan(2, $currentHand);
73
74
            $game->standHand($currentHand);
75
            $game->nextHand();
76
            $handCount++;
77
        }
78
        $this->assertEquals(2, $handCount);
79
80
        $this->assertNull($game->getCurrentHand());
81
    }
82
83
    /**
84
     * Test isComputerHand method.
85
     */
86
    public function testIsComputerHand()
87
    {
88
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00, 1, 'smart');
89
90
        $this->assertFalse($game->isComputerHand(0));
91
        $this->assertTrue($game->isComputerHand(1));
92
    }
93
94
    /**
95
     * Test checking for Blackjack.
96
     */
97
    public function testCheckForBlackjack()
98
    {
99
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
100
101
        $aceStub = $this->createMock(Card::class);
102
        $aceStub->method('getBlackjackValue')->willReturn(11);
103
        $aceStub->method('getValue')->willReturn('Ace');
104
105
        $kingStub = $this->createMock(Card::class);
106
        $kingStub->method('getBlackjackValue')->willReturn(10);
107
        $kingStub->method('getValue')->willReturn('King');
108
109
        $hand = [$aceStub, $kingStub];
110
111
        $game->setPlayerHand($hand);
112
113
        $game->checkForBlackjack();
114
115
        $hands = $game->getHands();
116
117
        $this->assertEquals('blackjack', $hands[0]['status']);
118
    }
119
120
    /**
121
     * Test finishing the game and settling bets.
122
     */
123
    public function testFinishGame()
124
    {
125
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
126
127
        while (!$game->isHandBusted(0)) {
128
            $game->hitHand(0);
129
        }
130
131
        $game->finishGame();
132
133
        $hands = $game->getHands();
134
        $dealerHand = $game->getDealerHand();
135
        $this->assertNotEquals('playing', $hands[0]['status']);
136
        $this->assertGreaterThanOrEqual(2, count($dealerHand));
137
        $this->assertEquals(990.00, $game->getPlayerBank());
138
    }
139
140
}
141