Test Failed
Push — main ( 49ffe2...63a966 )
by Vedrana
07:24 queued 02:15
created

CardGameControllerTest::testEmptyDeck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
/**
8
 * Class CardGameControllerTest
9
 */
10
class CardGameControllerTest extends WebTestCase
11
{
12
    /**
13
     * Test the /card route loads successfully.
14
     *
15
     * @return void
16
     */
17
    public function testCardStart(): void
18
    {
19
        $client = static::createClient();
20
        $client->request('GET', '/card');
21
22
        $this->assertResponseIsSuccessful();
23
    }
24
25
     /**
26
     * Test the /card/deck/ route displays a sorted deck.
27
     *
28
     * @return void
29
     */
30
    public function testSortedDeck(): void
31
    {
32
        $client = static::createClient();
33
        $client->request('GET', '/card/deck/');
34
35
        $this->assertResponseIsSuccessful();
36
    }
37
38
    /**
39
     * Test the /card/deck/shuffle route shuffles the deck.
40
     *
41
     * @return void
42
     */
43
    public function testShuffleDeck(): void
44
    {
45
        $client = static::createClient();
46
        $client->request('GET', '/card/deck/shuffle');
47
48
        $this->assertResponseIsSuccessful();
49
    }
50
51
    /**
52
     * Test draw a single card.
53
     *
54
     * @return void
55
     */
56
    public function testDrawCard(): void
57
    {
58
        $client = static::createClient();
59
        $client->request('GET', '/card/deck/shuffle');
60
        $client->request('GET', '/card/deck/draw');
61
62
        $this->assertResponseIsSuccessful();
63
    }
64
65
    /**
66
     * Test draw five cards.
67
     *
68
     * @return void
69
     */
70
    public function testDrawCards(): void
71
    {
72
        $client = static::createClient();
73
        $client->request('GET', '/card/deck/shuffle');
74
        // test to draw 5 cards
75
        $client->request('GET', '/card/deck/draw/5');
76
77
        $this->assertResponseIsSuccessful();
78
    }
79
80
    /**
81
     * Test the /card/deck/empty route shows when deck is empty.
82
     *
83
     * @return void
84
     */
85
    public function testEmptyDeck(): void
86
    {
87
        $client = static::createClient();
88
        $client->request('GET', '/card/deck/empty');
89
90
        $this->assertResponseIsSuccessful();
91
    }
92
93
    /**
94
     * Test that the /card/session route loads correctly.
95
     *
96
     * @return void
97
     */
98
    public function testShowSession()
99
    {
100
        $client = static::createClient();
101
        $client->request('GET', '/card/session');
102
103
        $this->assertResponseIsSuccessful();
104
    }
105
106
    /**
107
     * Test that delete session data redirects correctly and shows a flash message.
108
     *
109
     * @return void
110
     */
111
    public function testDeleteSession()
112
    {
113
        $client = static::createClient();
114
        $client->request('GET', '/card/session/delete');
115
116
        // Assert redirection to /card/session
117
        $this->assertResponseRedirects('/card/session');
118
        
119
        $client->followRedirect();
120
        $this->assertResponseIsSuccessful();
121
122
        // Check for flash message
123
        $this->assertSelectorTextContains('.flash-success', 'Session data has been deleted.');
124
    }
125
126
    /**
127
     * Test that drawing a card when the deck is empty redirects to /card/deck/empty.
128
     *
129
     * @return void
130
     */
131
    public function testRedirectWhenDeckIsEmptyDrawCard()
132
    {
133
        $client = static::createClient();
134
135
        $client->request('GET', '/card/deck/shuffle');
136
137
        // Draw 52 cards to emtpy it
138
        $client->request('GET', '/card/deck/draw/52');
139
140
        // Next draw -> no cards redirect to empty deck
141
        $client->request('GET', '/card/deck/draw');
142
143
        $this->assertTrue($client->getResponse()->isRedirect());
144
        $location = $client->getResponse()->headers->get('Location') ?? '';
145
        $this->assertStringContainsString('/card/deck/empty', $location);
146
    }
147
148
    /**
149
     * Test that drawing multiple cards from an empty deck redirects to /card/deck/empty.
150
     *
151
     * @return void
152
     */
153
    public function testRedirectWhenDeckIsEmptyDrawCards()
154
    {
155
        $client = static::createClient();
156
157
        $client->request('GET', '/card/deck/shuffle');
158
159
        $client->request('GET', '/card/deck/draw/52');
160
161
        $client->request('GET', '/card/deck/draw/5');
162
163
        $this->assertTrue($client->getResponse()->isRedirect());
164
        $location = $client->getResponse()->headers->get('Location') ?? '';
165
        $this->assertStringContainsString('/card/deck/empty', $location);
166
    }
167
}