DiceGameControllerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testRollDicesThrowsExceptionWhenTooMany() 0 9 1
A testInitPost() 0 12 1
A testInit() 0 7 1
A testRollDices() 0 7 1
A testRollDiceHandThrowsExceptionWhenTooMany() 0 9 1
A testTestRollDice() 0 7 1
A testHomePage() 0 7 1
A testRollDiceHand() 0 7 1
1
<?php
2
3
namespace App\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
/**
8
 * Class DiceGameControllerTest
9
 */
10
class DiceGameControllerTest extends WebTestCase
11
{
12
    /**
13
     * Test that the Pig game home page.
14
     */
15
    public function testHomePage(): void
16
    {
17
        $client = static::createClient();
18
        $client->request('GET', '/game/pig');
19
20
        $this->assertResponseIsSuccessful();
21
        $this->assertSelectorTextContains('h1', 'Pig game');
22
    }
23
24
    /**
25
     * Test that the dice roll test page.
26
     */
27
    public function testTestRollDice(): void
28
    {
29
        $client = static::createClient();
30
        $client->request('GET', '/game/pig/test/roll');
31
32
        $this->assertResponseIsSuccessful();
33
        $this->assertSelectorTextContains('h1', 'Roll a dice');
34
    }
35
36
    /**
37
     * Test rolling multiple dices
38
     * and that the dice result elements exist in the page.
39
     */
40
    public function testRollDices(): void
41
    {
42
        $client = static::createClient();
43
        $client->request('GET', '/game/pig/test/roll/5');
44
45
        $this->assertResponseIsSuccessful();
46
        $this->assertSelectorExists('.die');
47
    }
48
49
    /**
50
     * Test rolling a dice hand.
51
     */
52
    public function testRollDiceHand(): void
53
    {
54
        $client = static::createClient();
55
        $client->request('GET', '/game/pig/test/dicehand/4');
56
57
        $this->assertResponseIsSuccessful();
58
        $this->assertSelectorExists('ul > li');
59
    }
60
61
    /**
62
     * Test the Pig game init GET page loads
63
     * and contains a form.
64
     */
65
    public function testInit(): void
66
    {
67
        $client = static::createClient();
68
        $client->request('GET', '/game/pig/init');
69
70
        $this->assertResponseIsSuccessful();
71
        $this->assertSelectorExists('form');
72
    }
73
74
    /**
75
     * Test submitting the Pig game init form
76
     * redirects to the game play route.
77
     */
78
    public function testInitPost(): void
79
    {
80
        $client = static::createClient();
81
        $crawler = $client->request('GET', '/game/pig/init');
82
83
        $form = $crawler->selectButton('Start playing')->form();
84
        $form['num_dices'] = '2';
85
86
        $client->submit($form);
87
        $client->followRedirect();
88
89
        $this->assertRouteSame('pig_play');
90
    }
91
92
    /**
93
     * Test that rolling more than 99 dices throws an exception.
94
     */
95
    public function testRollDicesThrowsExceptionWhenTooMany(): void
96
    {
97
        $client = static::createClient();
98
        $client->catchExceptions(false);
99
100
        $this->expectException(\Exception::class);
101
        $this->expectExceptionMessage('Can not roll more than 99 dices!');
102
103
        $client->request('GET', '/game/pig/test/roll/100');
104
    }
105
106
    /**
107
     * Test that rolling a dice hand with more than 99 dices throws an exception.
108
     */
109
    public function testRollDiceHandThrowsExceptionWhenTooMany(): void
110
    {
111
        $client = static::createClient();
112
        $client->catchExceptions(false);
113
114
        $this->expectException(\Exception::class);
115
        $this->expectExceptionMessage('Can not roll more than 99 dices!');
116
117
        $client->request('GET', '/game/pig/test/dicehand/100');
118
    }
119
}
120