|
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/') { |
|
|
|
|
|
|
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
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&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 are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.