Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

GameBlackJackServiceTest::testNewRound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Tests\Service;
4
5
use PHPUnit\Framework\TestCase;
6
use App\Card\BlackJack;
7
use App\Card\Deck;
8
use App\Card\Card;
9
use App\Card\PayoutBlackJack;
10
use App\Card\BlackJackRules;
11
use App\Service\GameBlackJackService;
12
13
/**
14
 * Unit tests for the GameBlackJackService class.
15
 */
16
class GameBlackJackServiceTest extends TestCase
17
{
18
    private GameBlackJackService $service;
19
20
    /**
21
     * Set up a fresh GameBlackJackService before each test.
22
     */
23
    protected function setUp(): void
24
    {
25
        $this->service = new GameBlackJackService();
26
    }
27
28
29
    /**
30
     * Test to init or create game with new name, bet and chosen number of hands.
31
     */
32
    public function testInitorCreate(): void
33
    {
34
        $game = $this->service->initOrCreate(null, 'Ved', 100, 1);
35
36
        $this->assertInstanceOf(BlackJack::class, $game);
37
        $this->assertEquals('Ved', $game->getPlayerName());
38
        $this->assertEquals(900, $game->getBalance()); // 1000 - 100 bet
39
        $this->assertEquals(100, $game->getBet());
40
    }
41
42
43
    /**
44
     * Test starting a new round: preserved name and updated balance.
45
     */
46
    public function testNewRound(): void
47
    {
48
        $game = $this->service->initOrCreate(null, 'Daj', 100, 1);
49
        $game->setBalance(600);
50
51
        $newGame = $this->service->newRound($game, 200, 2);
52
53
        $this->assertEquals('Daj', $newGame->getPlayerName());
54
        $this->assertEquals(200, $newGame->getBalance()); // 600 - 2x200 bet
55
        $this->assertEquals(200, $newGame->getBet());
56
    }
57
58
59
    /**
60
     * Test process action stay/draw.
61
     * Draw adds another card to the hand.
62
     * Stay moves to next hand/bank.
63
     */
64
    public function testProcessAction(): void
65
    {
66
        /** @var \PHPUnit\Framework\MockObject\MockObject&\App\Card\Deck $deck */
67
        $deck = $this->createMock(Deck::class);
68
69
        $deck->method('drawCard')->willReturnOnConsecutiveCalls(
70
            new Card('Spades', 'Ace'),
71
            new Card('Clubs', '7'),
72
            new Card('Hearts', '4'),
73
            new Card('Diamonds', '10'),
74
            new Card('Hearts', '2'),
75
            new Card('Hearts', '5'),
76
            new Card('Spades', '5'),
77
            new Card('Clubs', '3')
78
        );
79
80
        $rules = new BlackJackRules();
81
        $payout = new PayoutBlackJack();
82
83
        $game = new BlackJack($deck, $rules, $payout);
84
        $game->setBet(100);
85
        $game->setBalance(800);
86
87
        $game->startGame(1);
88
89
        $handBefore = $game->getCurrentHand();
90
        $this->assertNotNull($handBefore, "Expected initial hand to exist.");
91
        $this->assertCount(2, $handBefore->getCards(), "Hand should start with 2 cards");
92
93
        $updatedGame = $this->service->processAction($game, 'draw');
94
95
        $hand = $updatedGame->getCurrentHand();
96
        $this->assertNotNull($hand, "Expected current hand to exist after draw.");
97
        $this->assertCount(3, $hand->getCards(), "After draw, hand should have 3 cards");
98
99
        $stayGame = $this->service->processAction($updatedGame, 'stay');
100
        $this->assertEquals(1, $stayGame->getCurrentHandIndex(), "After stay, current hand index moves to next hand");
101
    }
102
}
103