Completed
Push — master ( a275c7...1793be )
by Laurent
25:09
created

DefaultControllerTest::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
11
     */
12
    public function getConnection() {
13
        $pdo = new \PDO("DB_DNS", "DB_USER", "DB_PASSWD", "DB_DBNAME");
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style Comprehensibility introduced by
The string literal DB_DNS does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal DB_USER does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal DB_PASSWD does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal DB_DBNAME does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

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