Completed
Push — master ( 32d64f...c5e2bf )
by Laurent
05:11
created

DefaultControllerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testPublicUrls() 0 27 5
A testSecureUrls() 0 11 1
A getPublicUrls() 0 10 1
A getSecureUrls() 0 15 1
A testIndex() 0 13 1
1
<?php
2
3
namespace AppBundle\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
class DefaultControllerTest extends WebTestCase
8
{
9
    /**
10
     * PHPUnit's data providers allow to execute the same tests repeated times
11
     * using a different set of data each time.
12
     * See http://symfony.com/doc/2.8/form/unit_testing.html#testing-against-different-sets-of-data
13
     *
14
     * @dataProvider getPublicUrls
15
     */
16
    public function testPublicUrls($url)
17
    {
18
        $client = self::createClient();
19
        if ($url === '/article/' or $url === '/supplier/') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
20
            $stub = $this->getMockForAbstractClass('\AppBundle\Controller\AbstractController');
21
            $stub->expects($this->any());
22
23
            if ($url === '/article/') {
24
                $this->assertTrue(
25
                    $stub->abstractIndexAction('Article', null),
26
                    sprintf('The "%s" public URL loads correctly.', $url)
27
                );
28
            }
29
            if ($url === '/supplier/') {
30
                $this->assertTrue(
31
                    $stub->abstractIndexAction('Supplier', null),
32
                    sprintf('The "%s" public URL loads correctly.', $url)
33
                );
34
            }
35
        } else {
36
            $client->request('GET', $url);
37
            $this->assertTrue(
38
                $client->getResponse()->isSuccessful(),
39
                sprintf('The "%s" public URL loads correctly.', $url)
40
            );
41
        }
42
    }
43
44
    /**
45
     * The application contains a lot of secure URLs which shouldn't be
46
     * publicly accessible. This tests ensures that whenever a user tries to
47
     * access one of those pages, a redirection to the login form is performed.
48
     *
49
     * @dataProvider getSecureUrls
50
     */
51
    public function testSecureUrls($url)
52
    {
53
        $client = self::createClient();
54
        $client->request('GET', $url);
55
        $this->assertTrue($client->getResponse()->isRedirect());
56
        $this->assertEquals(
57
            'http://symfony2.local/login',
58
            $client->getResponse()->getTargetUrl(),
59
            sprintf('The "%s" secure URL redirects to the login form.', $url)
60
        );
61
    }
62
63
    public function getPublicUrls()
64
    {
65
        return array(
66
            array('/'),
67
            array('/login'),
68
            array('/inventory/'),
69
            array('/article/'),
70
            array('/supplier/'),
71
        );
72
    }
73
74
    public function getSecureUrls()
75
    {
76
        return array(
77
            array('/admin/user/'),
78
            array('/admin/group/'),
79
            array('/admin/settings/company/'),
80
            array('/admin/settings/application/'),
81
            array('/admin/settings/divers/familylog/'),
82
            array('/admin/settings/divers/zonestorage/'),
83
            array('/admin/settings/divers/unitstorage/'),
84
            array('/admin/settings/divers/tva/'),
85
            array('/article/admin/new'),
86
            array('/supplier/admin/new'),
87
        );
88
    }
89
90
    public function testIndex() {
91
        $client = self::createClient(
92
            array(),
93
            array(
94
               'HTTP_HOST' => 'symfony2.local',
95
            )
96
        );
97
98
        $crawler = $client->request('GET', '/');
99
100
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
101
        $this->assertContains('Accueil', $crawler->filter('.container .row #content h2')->text());
102
    }
103
104
}
105