Passed
Push — main ( e730b7...fedd50 )
by Alex
04:56
created

TwentyOneGameControllerTest::testIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 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()
19
    {
20
        if (getenv('APP_ENV') !== 'dev') {
21
            $this->markTestSkipped(
22
                'This test can only be run on the local host.'
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)
51
    {
52
        if (getenv('APP_ENV') !== 'dev') {
53
            $this->markTestSkipped(
54
                'This test can only be run on the local host.'
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
        // Follow the redirect to /play
68
        $crawler = $client->followRedirect();
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
69
70
        // Assert that the /play page is displayed
71
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
72
    }
73
74
    /**
75
     * Test redirects back when no valid banker has been submitted.
76
     */
77
    public function testInitBankerFail()
78
    {
79
        if (getenv('APP_ENV') !== 'dev') {
80
            $this->markTestSkipped(
81
                'This test can only be run on the local host.'
82
            );
83
        }
84
85
        $client = static::createClient();
86
87
        $client->request('POST', '/game/init', [
88
            'banker' => null
89
        ]);
90
91
        // Assert that the response redirects back to /init if failed
92
        $this->assertTrue($client->getResponse()->isRedirect('/game/init'));
93
94
        // Follow the redirect to /init
95
        $crawler = $client->followRedirect();
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
96
97
        // Assert that the /init page is displayed
98
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
99
    }
100
101
    /**
102
     * Provide data sets to follow both banker and player win paths.
103
     * @return mixed[] As data set.
104
     */
105
    public static function winProvider(): array
106
    {
107
        return [
108
            'Banker wins' => [-1, 'bankerWinsStake'],
109
            'Player wins' => [1, 'playerWinsStake']
110
        ];
111
    }
112
113
    /**
114
     * Test stay redirects when banker or player wins.
115
     * @dataProvider winProvider
116
     */
117
    public function testStay(int $hasWon, string $winMethod)
118
    {
119
        $client = static::createClient();
120
121
        // Create stubs for manager classes
122
        $gameManager = $this->getMockBuilder(GameManager::class)
123
            ->disableOriginalConstructor()
124
            ->onlyMethods(['dealBanker', 'updateHasWonStatus'])
125
            ->getMock();
126
127
        $bettingManager = $this->getMockBuilder(BettingManager::class)
128
            ->disableOriginalConstructor()
129
            ->onlyMethods([$winMethod, 'isBetting'])
130
            ->getMock();
131
   
132
        // Set up expectation for methods
133
        $gameManager->expects($this->any())
134
            ->method('dealBanker');
135
136
        $gameManager->expects($this->any())
137
            ->method('updateHasWonStatus')
138
            ->willReturn($hasWon);
139
140
        $bettingManager->expects($this->any())
141
            ->method('isBetting')
142
            ->willReturn(true);
143
144
        $bettingManager->expects($this->any())
145
            ->method($winMethod);
146
147
        // Create mock session
148
        $session = $this->createSession($client, [
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
149
            'gameManager' => $gameManager,
150
            'bettingManager' => $bettingManager
151
        ]);
152
153
        // Make a POST request to the /game/play/stay endpoint
154
        $client->request('POST', '/game/play/stay');
155
    
156
        // Assert that the response redirects back to /game/play
157
        $this->assertResponseRedirects('/game/play');
158
    }
159
160
    /**
161
     * Test hit redirects when banker or player wins.
162
     * @dataProvider winProvider
163
     */
164
    public function testHit(int $hasWon, string $winMethod)
165
    {
166
        $client = static::createClient();
167
168
        // Create stubs for manager classes
169
        $gameManager = $this->getMockBuilder(GameManager::class)
170
            ->disableOriginalConstructor()
171
            ->onlyMethods(['dealPlayer', 'updateHasWonStatus'])
172
            ->getMock();
173
174
        $bettingManager = $this->getMockBuilder(BettingManager::class)
175
            ->disableOriginalConstructor()
176
            ->onlyMethods([$winMethod, 'isBetting'])
177
            ->getMock();
178
   
179
        // Set up expectation for methods
180
        $gameManager->expects($this->any())
181
            ->method('dealPlayer');
182
183
        $gameManager->expects($this->any())
184
            ->method('updateHasWonStatus')
185
            ->willReturn($hasWon);
186
187
        $bettingManager->expects($this->any())
188
            ->method('isBetting')
189
            ->willReturn(true);
190
191
        $bettingManager->expects($this->any())
192
            ->method($winMethod);
193
194
        // Create mock session
195
        $session = $this->createSession($client, [
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
196
            'gameManager' => $gameManager,
197
            'bettingManager' => $bettingManager
198
        ]);
199
200
        // Make a POST request to the /game/play/hit endpoint
201
        $client->request('POST', '/game/play/hit');
202
    
203
        // Assert that the response redirects back to /game/play
204
        $this->assertResponseRedirects('/game/play');
205
    }
206
}
207