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

DiceGameControllerTest::testInitPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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