Passed
Push — main ( 8bc290...60b955 )
by Peter
04:33
created

BlackjackGameProjEndTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 123
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsComputerHand() 0 6 1
A testGetCurrentHandAndNextHand() 0 12 1
A testCheckForBlackjack() 0 22 1
A testIsGameOver() 0 11 1
A testGetCurrentHandAllHandsFinished() 0 12 2
A testFinishGame() 0 15 2
A testIsHandBusted() 0 10 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
        while ($game->getCurrentHand() !== null) {
69
            $currentHand = $game->getCurrentHand();
70
            $game->standHand($currentHand);
71
            $game->nextHand();
72
        }
73
74
        $this->assertNull($game->getCurrentHand());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $game->getCurrentHand() targeting App\GameProj\BlackjackGameProj::getCurrentHand() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
    }
76
77
    /**
78
     * Test isComputerHand method.
79
     */
80
    public function testIsComputerHand()
81
    {
82
        $game = new BlackjackGameProj('Player', 1000.00, 2, 20.00, 1, 'smart');
83
84
        $this->assertFalse($game->isComputerHand(0));
85
        $this->assertTrue($game->isComputerHand(1));
86
    }
87
88
    /**
89
     * Test checking for Blackjack.
90
     */
91
    public function testCheckForBlackjack()
92
    {
93
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
94
95
        // Create stubs for the Card class
96
        $aceStub = $this->createMock(Card::class);
97
        $aceStub->method('getBlackjackValue')->willReturn(11);
98
        $aceStub->method('getValue')->willReturn('Ace');
99
100
        $kingStub = $this->createMock(Card::class);
101
        $kingStub->method('getBlackjackValue')->willReturn(10);
102
        $kingStub->method('getValue')->willReturn('King');
103
104
        $hand = [$aceStub, $kingStub];
105
106
        $game->setPlayerHand($hand);
107
108
        $game->checkForBlackjack();
109
110
        $hands = $game->getHands();
111
112
        $this->assertEquals('blackjack', $hands[0]['status']);
113
    }
114
115
    /**
116
     * Test finishing the game and settling bets.
117
     */
118
    public function testFinishGame()
119
    {
120
        $game = new BlackjackGameProj('Player', 1000.00, 1, 10.00);
121
122
        while (!$game->isHandBusted(0)) {
123
            $game->hitHand(0);
124
        }
125
126
        $game->finishGame();
127
128
        $hands = $game->getHands();
129
        $dealerHand = $game->getDealerHand();
130
        $this->assertNotEquals('playing', $hands[0]['status']);
131
        $this->assertGreaterThanOrEqual(2, count($dealerHand));
132
        $this->assertEquals(990.00, $game->getPlayerBank());
133
    }
134
135
}
136