DiceGameControllerTest::testControllerPlay()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.7666
1
<?php
2
3
namespace App\Controller;
4
5
use App\Dice\DiceGame;
6
use App\Dice\DiceHand;
7
use Symfony\Component\BrowserKit\Cookie;
8
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\HttpFoundation\Session\SessionInterface;
11
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
12
13
/**
14
 * Test cases for class DiceGameController.
15
 */
16
class DiceGameControllerTest extends WebTestCase
17
{
18
    /**
19
     * Test instantiation of class
20
     */
21
    public function testInstantiateDiceGame(): void
22
    {
23
        $controller = new DiceGameController();
24
        $this->assertInstanceOf("\App\Controller\DiceGameController", $controller);
25
    }
26
27
    /**
28
     * Test route /game/pig
29
     */
30
    public function testControllerHome(): void
31
    {
32
        $client = static::createClient();
33
        $client->request('GET', '/game/pig');
34
        $this->assertResponseIsSuccessful();
35
    }
36
37
    /**
38
     * Test route /game/pig/test/roll
39
     */
40
    public function testControllerRollOneDice(): void
41
    {
42
        $client = static::createClient();
43
        $client->request('GET', '/game/pig/test/roll', [
44
            'dice' => 4,
45
            'diceString' => '⚃'
46
        ]);
47
48
        $this->assertResponseIsSuccessful();
49
    }
50
51
    /**
52
     * Test route /game/pig/test/roll/{num<\d+>}
53
     */
54
    public function testControllerRollNumDices(): void
55
    {
56
        $client = static::createClient();
57
        $client->request('GET', '/game/pig/test/roll/2', [
58
            'num_dices' => 2,
59
            'diceRoll' => ['⚁', '⚂']
60
        ]);
61
62
        $this->assertResponseIsSuccessful();
63
    }
64
65
    /**
66
     * Test route /game/pig/test/dicehand/{num<\d+>}
67
     */
68
    public function testControllerShowHand(): void
69
    {
70
        $client = static::createClient();
71
        $client->request('GET', '/game/pig/test/dicehand/5', [
72
            'num_dices' => 5,
73
            'diceRoll' => ['⚁', 1, '⚂', 4, '⚅']
74
        ]);
75
76
        $this->assertResponseIsSuccessful();
77
    }
78
79
    /**
80
     * Test route /game/pig/init
81
     */
82
    public function testControllerInit(): void
83
    {
84
        $client = static::createClient();
85
        $client->request('GET', '/game/pig/init');
86
        $this->assertResponseIsSuccessful();
87
    }
88
89
    /**
90
     * Test route /game/pig/init
91
     */
92
    public function testControllerInitCallback(): void
93
    {
94
        $client = static::createClient();
95
96
        $stub = $this->createMock(DiceGame::class);
97
        $stub->method('rollHand')
98
                ->with(5);
99
100
        $client->request('POST', '/game/pig/init', [
101
            'num_dices' => 5,
102
        ]);
103
        $this->assertResponseRedirects('/game/pig/play');
104
    }
105
106
    /**
107
     * Test route /game/pig/play
108
     */
109
    public function testControllerPlay(): void
110
    {
111
        $client = static::createClient();
112
113
        // Create session
114
        $sessionFactory = static::getContainer()->get('session.factory');
115
        $session = $sessionFactory->createSession();
116
117
        $stub = $this->createMock(DiceHand::class);
118
        $stub->method('getString')
119
            ->willReturn([4, 7, 9]);
120
121
        // Set session data for test
122
        $session->set('pig_dicehand', clone $stub);
123
        $session->set('pig_dices', 4);
124
        $session->set('pig_round', 3);
125
        $session->set('pig_total', 10);
126
127
        $session->save();
128
129
        $client->getCookieJar()->set(
130
            new Cookie($session->getName(), $session->getId())
131
        );
132
133
        $client->request('GET', '/game/pig/play');
134
        $this->assertResponseIsSuccessful();
135
    }
136
137
    /**
138
     * Test route /game/pig/roll
139
     */
140
    public function testControllerRoll(): void
141
    {
142
        $client = static::createClient();
143
144
        // Create session
145
        $sessionFactory = static::getContainer()->get('session.factory');
146
        $session = $sessionFactory->createSession();
147
148
        $stub = $this->createMock(DiceHand::class);
149
        $stub->method('roll');
150
151
        //Save to session
152
        $session->set('pig_dicehand', clone $stub);
153
        $session->save();
154
155
        $client->getCookieJar()->set(
156
            new Cookie($session->getName(), $session->getId())
157
        );
158
159
        $client->request('POST', '/game/pig/roll');
160
        $this->assertResponseRedirects('/game/pig/play');
161
    }
162
163
    /**
164
     * Test route /game/pig/save
165
     */
166
    public function testControllerSave(): void
167
    {
168
        $client = static::createClient();
169
        $client->request('POST', '/game/pig/save');
170
        $this->assertResponseRedirects('/game/pig/play');
171
    }
172
}
173