testConstructorExceptionForInvalidNumberOfHands()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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 BlackjackGameProjStartTest extends TestCase
11
{
12
    /**
13
     * Construct object and verify that the object has the expected
14
     * properties.
15
     */
16
    public function testCreateBlackjackGameProj()
17
    {
18
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
19
        $this->assertInstanceOf(BlackjackGameProj::class, $game);
20
21
        $this->assertEquals('Player', $game->getPlayerName());
22
        $this->assertEquals(990.00, $game->getPlayerBank());
23
        $this->assertCount(1, $game->getHands());
24
    }
25
26
    /**
27
     * Test constructor exception for invalid hands number.
28
     */
29
    public function testConstructorExceptionForInvalidNumberOfHands()
30
    {
31
        $this->expectException(\InvalidArgumentException::class);
32
        new BlackjackGameProj('Player', 1000.00, 4, 10.00);
33
    }
34
35
    /**
36
     * Test dealing cards.
37
     */
38
    public function testDealCards()
39
    {
40
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
41
        $game->dealInitialCards();
42
43
        $hands = $game->getHands();
44
        $this->assertCount(2, $hands[0]['hand']);
45
46
        $dealerHand = $game->getDealerHand();
47
        $this->assertCount(1, $dealerHand);
48
    }
49
50
    /**
51
     * Test hitting player and dealer.
52
     */
53
    public function testHit()
54
    {
55
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
56
        $game->dealInitialCards();
57
58
        $game->hitHand(0);
59
        $hands = $game->getHands();
60
        $this->assertCount(3, $hands[0]['hand']);
61
62
        $game->hitDealer();
63
        $dealerHand = $game->getDealerHand();
64
        $this->assertCount(2, $dealerHand);
65
    }
66
67
    /**
68
     * Test hand value, stub Card to assure the value can be asserted.
69
     */
70
    public function testGetHandValue()
71
    {
72
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
73
74
        $stub = $this->createMock(Card::class);
75
76
        $stub->method('getBlackjackValue')
77
            ->willReturn(7, 10);
78
        $stub->method('getValue')
79
            ->willReturn('7', 'King');
80
81
        $hand = [clone $stub, clone $stub];
82
83
        $value = $game->getHandValue($hand);
84
        $this->assertEquals(17, $value);
85
    }
86
87
    /**
88
     * Test getBustProbability method for soft ace.
89
     */
90
    public function testGetBustProbabilitySoftAce()
91
    {
92
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
93
94
        $aceStub = $this->createMock(Card::class);
95
        $aceStub->method('getBlackjackValue')->willReturn(11);
96
        $aceStub->method('getValue')->willReturn('Ace');
97
98
        $sevenStub = $this->createMock(Card::class);
99
        $sevenStub->method('getBlackjackValue')->willReturn(7);
100
        $sevenStub->method('getValue')->willReturn('7');
101
102
        $hand = [$aceStub, $sevenStub];
103
104
        $game->setPlayerHand($hand);
105
106
        $bustProbability = $game->getBustProbability(0);
107
108
        $this->assertEquals(0, $bustProbability);
109
    }
110
111
    /**
112
     * Test getBustProbability method.
113
     */
114
    public function testGetBustProbabilityRest()
115
    {
116
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
117
118
        $kingStub = $this->createMock(Card::class);
119
        $kingStub->method('getBlackjackValue')->willReturn(10);
120
        $kingStub->method('getValue')->willReturn('King');
121
122
        $sevenStub = $this->createMock(Card::class);
123
        $sevenStub->method('getBlackjackValue')->willReturn(7);
124
        $sevenStub->method('getValue')->willReturn('7');
125
126
        $hand = [$kingStub, $sevenStub];
127
128
        $game->setPlayerHand($hand);
129
130
        $bustProbability = $game->getBustProbability(0);
131
        $this->assertLessThan(0.70, $bustProbability);
132
        $this->assertGreaterThan(0.68, $bustProbability);
133
    }
134
135
    /**
136
     * Test playing the computer's hand.
137
     */
138
    public function testPlayComputerHand()
139
    {
140
        $game = new BlackjackGameProj('Player', 1000.00, 2, 10.00, 1, 'smart');
141
142
        $aceStub = $this->createMock(Card::class);
143
        $aceStub->method('getBlackjackValue')->willReturn(11);
144
        $aceStub->method('getValue')->willReturn('Ace');
145
146
        $sixStub = $this->createMock(Card::class);
147
        $sixStub->method('getBlackjackValue')->willReturn(6);
148
        $sixStub->method('getValue')->willReturn('6');
149
150
        $hand = [$aceStub, $sixStub];
151
152
        $game->setPlayerHand($hand, 1);
153
154
        $game->standHand(0);
155
156
        $game->playComputerHand();
157
158
        while (!$game->isHandBusted(1)) {
159
            $game->hitHand(1);
160
        }
161
162
        $hands = $game->getHands();
163
        $this->assertNotEquals('playing', $hands[1]['status']);
164
    }
165
166
    /**
167
     * Test standHand method.
168
     */
169
    public function testStandHand()
170
    {
171
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00);
172
        $game->dealInitialCards();
173
174
        $game->standHand(0);
175
        $hands = $game->getHands();
176
        $this->assertEquals('standing', $hands[0]['status']);
177
178
        $this->expectException(\InvalidArgumentException::class);
179
        $game->standHand(5);
180
    }
181
}
182