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

testValidateInputInvalidData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
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\Service\GameBlackJackValidateService;
8
9
/**
10
 * Unit tests for the GameBlackJackValidateService class.
11
 */
12
class GameBlackJackValidateServiceTest extends TestCase
13
{
14
    private GameBlackJackValidateService $validateService;
15
16
    /**
17
     * Set up a fresh GameBlackJackValidateService before each test.
18
     */
19
    protected function setUp(): void
20
    {
21
        $this->validateService = new GameBlackJackValidateService();
22
    }
23
24
    /**
25
     * Test validating correct input data.
26
     * Should return an array with playerName, betAmount, and numHands.
27
     */
28
    public function testValidateInput(): void
29
    {
30
        $data = [
31
            'playerName' => 'Ved',
32
            'betAmount' => '100',
33
            'numHands' => '2',
34
        ];
35
36
        $res = $this->validateService->validateInput($data);
37
38
        $this->assertIsArray($res);
39
        $this->assertEquals('Ved', $res['playerName']);
40
        $this->assertEquals('100', $res['betAmount']);
41
        $this->assertEquals('2', $res['numHands']);
42
    }
43
44
    /**
45
     * Test validating input with invalid data.
46
     */
47
    public function testValidateInputInvalidData(): void
48
    {
49
        $data = [
50
            'playerName' => '',
51
            'betAmount' => '0',
52
            'numHands' => '5',
53
        ];
54
55
        $res = $this->validateService->validateInput($data);
56
57
        $this->assertIsString($res);
58
        $this->assertEquals('Invalid input.', $res);
59
    }
60
61
    /**
62
     * Test bet validation when bet exceeds the player's balance.
63
     */
64
    public function testValidateBetExceedsBalance(): void
65
    {
66
        /** @var \App\Card\BlackJack&\PHPUnit\Framework\MockObject\MockObject $mockGame */
67
        $mockGame = $this->createMock(BlackJack::class);
68
        $mockGame->method('getBalance')->willReturn(100);
69
70
        $error = $this->validateService->validateBet($mockGame, 200, 1);
71
72
        $this->assertEquals('Your bet exceeds your current balance.', $error);
73
    }
74
75
    /**
76
     * Test valid bet scenario.
77
     * Should return null (no error).
78
     */
79
    public function testValidateBet(): void
80
    {
81
        /** @var \App\Card\BlackJack&\PHPUnit\Framework\MockObject\MockObject $mockGame */
82
        $mockGame = $this->createMock(BlackJack::class);
83
        $mockGame->method('getBalance')->willReturn(500);
84
85
        $error = $this->validateService->validateBet($mockGame, 100, 2);
86
87
        $this->assertNull($error);
88
    }
89
90
    /**
91
     * Test bet validation when bet amount is zero.
92
     */
93
    public function testValidateBetZero(): void
94
    {
95
        /** @var \App\Card\BlackJack&\PHPUnit\Framework\MockObject\MockObject $mockGame */
96
        $mockGame = $this->createMock(BlackJack::class);
97
        $mockGame->method('getBalance')->willReturn(500);
98
99
        $error = $this->validateService->validateBet($mockGame, 0, 1);
100
101
        $this->assertEquals('Invalid bet amount.', $error);
102
    }
103
}
104