Test Failed
Push — main ( 99efcd...df6632 )
by Alex
14:53
created

TwentyOneGameControllerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 77
c 4
b 0
f 0
dl 0
loc 179
rs 10
wmc 10
1
<?php
2
3
namespace App\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
use Symfony\Component\HttpFoundation\Response;
7
use App\Tests\SessionHelperTrait;
8
use App\Game\GameManager;
9
use App\Game\BettingManager;
10
11
/**
12
 * Test cases for TwentyOneGameController.
13
 */
14
class TwentyOneGameControllerTest extends WebTestCase
15
{
16
    use SessionHelperTrait;
17
18
    public function testIndex(): void
19
    {
20
        if (getenv('APP_ENV') !== 'test') {
21
            $this->markTestSkipped(
22
                'This test can only be run on the test environment.'
23
            );
24
        }
25
26
        $client = static::createClient();
27
28
        $client->request('GET', '/game');
29
30
        $this->assertResponseIsSuccessful();
31
    }
32
33
    /**
34
     * Provide data sets to simulte form submitted with different bankers.
35
     * @return mixed[] As data set
36
     */
37
    public static function bankerProvider(): array
38
    {
39
        return [
40
            'Easy Banker'   => ['easy'],
41
            'Medium Banker' => ['medium'],
42
            'Hard Banker'   => ['hard'],
43
        ];
44
    }
45
46
    /**
47
     * Test setting up game with different bankers submitted.
48
     * @dataProvider bankerProvider
49
     */
50
    public function testInitBankerSuccess(string $banker): void
51
    {
52
        if (getenv('APP_ENV') !== 'test') {
53
            $this->markTestSkipped(
54
                'This test can only be run on the test environment.'
55
            );
56
        }
57
58
        $client = static::createClient();
59
60
        $client->request('POST', '/game/init', [
61
            'banker' => $banker,
62
        ]);
63
64
        // Assert that the response redirects to /play if successful
65
        $this->assertTrue($client->getResponse()->isRedirect('/game/play'));
66
    }
67
68
    /**
69
     * Test redirects back when no valid banker has been submitted.
70
     */
71
    public function testInitBankerFail(): void
72
    {
73
        if (getenv('APP_ENV') !== 'test') {
74
            $this->markTestSkipped(
75
                'This test can only be run on the test environment.'
76
            );
77
        }
78
79
        $client = static::createClient();
80
81
        $client->request('POST', '/game/init', [
82
            'banker' => null
83
        ]);
84
85
        // Assert that the response redirects back to /init if failed
86
        $this->assertTrue($client->getResponse()->isRedirect('/game/init'));
87
    }
88
89
    /**
90
     * Provide data sets to follow both banker and player win paths.
91
     * @return mixed[] As data set.
92
     */
93
    public static function winProvider(): array
94
    {
95
        return [
96
            'Banker wins' => [-1, 'bankerWinsStake'],
97
            'Player wins' => [1, 'playerWinsStake']
98
        ];
99
    }
100
101
    /**
102
     * Test stay redirects when banker or player wins.
103
     * @dataProvider winProvider
104
     */
105
    public function testStay(int $hasWon, string $winMethod): void
106
    {
107
        $client = static::createClient();
108
109
        // Create stubs for manager classes
110
        $gameManager = $this->getMockBuilder(GameManager::class)
111
            ->disableOriginalConstructor()
112
            ->onlyMethods(['dealBanker', 'checkBankerWon'])
113
            ->getMock();
114
115
        $bettingManager = $this->getMockBuilder(BettingManager::class)
116
            ->disableOriginalConstructor()
117
            ->onlyMethods([$winMethod, 'isBetting'])
118
            ->getMock();
119
120
        // Set up expectation for methods
121
        $gameManager->expects($this->any())
122
            ->method('dealBanker');
123
124
        $gameManager->expects($this->any())
125
            ->method('checkBankerWon')
126
            ->willReturn($hasWon);
127
128
        $bettingManager->expects($this->any())
129
            ->method('isBetting')
130
            ->willReturn(true);
131
132
        $bettingManager->expects($this->any())
133
            ->method($winMethod);
134
135
        // Create mock session
136
        $this->createSession($client, [
137
            'gameManager' => $gameManager,
138
            'bettingManager' => $bettingManager
139
        ]);
140
141
        // Make a POST request to the /game/play/stay endpoint
142
        $client->request('POST', '/game/play/stay');
143
144
        // Assert that the response redirects back to /game/play
145
        $this->assertResponseRedirects('/game/play');
146
    }
147
148
    /**
149
     * Test hit redirects when banker or player wins.
150
     * @dataProvider winProvider
151
     */
152
    public function testHit(int $hasWon, string $winMethod): void
153
    {
154
        $client = static::createClient();
155
156
        // Create stubs for manager classes
157
        $gameManager = $this->getMockBuilder(GameManager::class)
158
            ->disableOriginalConstructor()
159
            ->onlyMethods(['dealPlayer', 'checkPlayerWon'])
160
            ->getMock();
161
162
        $bettingManager = $this->getMockBuilder(BettingManager::class)
163
            ->disableOriginalConstructor()
164
            ->onlyMethods([$winMethod, 'isBetting'])
165
            ->getMock();
166
167
        // Set up expectation for methods
168
        $gameManager->expects($this->any())
169
            ->method('dealPlayer');
170
171
        $gameManager->expects($this->any())
172
            ->method('checkPlayerWon')
173
            ->willReturn($hasWon);
174
175
        $bettingManager->expects($this->any())
176
            ->method('isBetting')
177
            ->willReturn(true);
178
179
        $bettingManager->expects($this->any())
180
            ->method($winMethod);
181
182
        // Create mock session
183
        $this->createSession($client, [
184
            'gameManager' => $gameManager,
185
            'bettingManager' => $bettingManager
186
        ]);
187
188
        // Make a POST request to the /game/play/hit endpoint
189
        $client->request('POST', '/game/play/hit');
190
191
        // Assert that the response redirects back to /game/play
192
        $this->assertResponseRedirects('/game/play');
193
    }
194
}
195