Skip to content

Commit 177d7e9

Browse files
author
tchapi
committed
tests
1 parent 64b4ac4 commit 177d7e9

3 files changed

Lines changed: 76 additions & 19 deletions

File tree

tests/Functional/AddressBookControllerTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
namespace App\Tests\Functional;
44

55
use App\Entity\AddressBook;
6+
use App\Entity\User;
67
use App\Security\AdminUser;
78
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
89

910
class AddressBookControllerTest extends WebTestCase
1011
{
12+
private function getUserId($client, string $username): int
13+
{
14+
$userRepository = static::getContainer()->get('doctrine.orm.entity_manager')->getRepository(User::class);
15+
$user = $userRepository->findOneByUsername($username);
16+
17+
return $user->getId();
18+
}
19+
1120
public function testAddressBookIndex(): void
1221
{
1322
$user = new AdminUser('admin', 'test');
1423

1524
$client = static::createClient();
1625
$client->loginUser($user);
17-
$client->request('GET', '/addressbooks/test_user');
26+
27+
$userId = $this->getUserId($client, 'test_user');
28+
29+
$client->request('GET', '/addressbooks/'.$userId);
1830

1931
$this->assertResponseIsSuccessful();
2032

@@ -31,10 +43,12 @@ public function testAddressBookEdit(): void
3143
$client = static::createClient();
3244
$client->loginUser($user);
3345

46+
$userId = $this->getUserId($client, 'test_user');
47+
3448
$addressbookRepository = static::getContainer()->get('doctrine.orm.entity_manager')->getRepository(AddressBook::class);
3549
$addressbook = $addressbookRepository->findOneByDisplayName('default.addressbook.title');
3650

37-
$client->request('GET', '/addressbooks/test_user/edit/'.$addressbook->getId());
51+
$client->request('GET', '/addressbooks/'.$userId.'/edit/'.$addressbook->getId());
3852

3953
$this->assertResponseIsSuccessful();
4054

@@ -43,7 +57,7 @@ public function testAddressBookEdit(): void
4357

4458
$client->submitForm('address_book_save');
4559

46-
$this->assertResponseRedirects('/addressbooks/test_user');
60+
$this->assertResponseRedirects('/addressbooks/'.$userId);
4761
$client->followRedirect();
4862

4963
$this->assertSelectorTextContains('h5', 'default.addressbook.title');
@@ -55,7 +69,10 @@ public function testAddressBookNew(): void
5569

5670
$client = static::createClient();
5771
$client->loginUser($user);
58-
$crawler = $client->request('GET', '/addressbooks/test_user/new');
72+
73+
$userId = $this->getUserId($client, 'test_user');
74+
75+
$crawler = $client->request('GET', '/addressbooks/'.$userId.'/new');
5976

6077
$this->assertResponseIsSuccessful();
6178

@@ -71,7 +88,7 @@ public function testAddressBookNew(): void
7188
'address_book[description]' => 'new address book',
7289
]);
7390

74-
$this->assertResponseRedirects('/addressbooks/test_user');
91+
$this->assertResponseRedirects('/addressbooks/'.$userId);
7592
$client->followRedirect();
7693

7794
$this->assertSelectorTextContains('h5', 'default.addressbook.title');
@@ -85,12 +102,14 @@ public function testAddressBookDelete(): void
85102
$client = static::createClient();
86103
$client->loginUser($user);
87104

105+
$userId = $this->getUserId($client, 'test_user');
106+
88107
$addressbookRepository = static::getContainer()->get('doctrine.orm.entity_manager')->getRepository(AddressBook::class);
89108
$addressbook = $addressbookRepository->findOneByDisplayName('default.addressbook.title');
90109

91-
$client->request('GET', '/addressbooks/test_user/delete/'.$addressbook->getId());
110+
$client->request('GET', '/addressbooks/'.$userId.'/delete/'.$addressbook->getId());
92111

93-
$this->assertResponseRedirects('/addressbooks/test_user');
112+
$this->assertResponseRedirects('/addressbooks/'.$userId);
94113
$client->followRedirect();
95114

96115
$this->assertSelectorTextNotContains('h5', 'default.addressbook.title');

tests/Functional/CalendarControllerTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22

33
namespace App\Tests\Functional;
44

5+
use App\Entity\User;
56
use App\Repository\CalendarInstanceRepository;
67
use App\Security\AdminUser;
78
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
89

910
class CalendarControllerTest extends WebTestCase
1011
{
12+
private function getUserId($client, string $username): int
13+
{
14+
$userRepository = static::getContainer()->get('doctrine.orm.entity_manager')->getRepository(User::class);
15+
$user = $userRepository->findOneByUsername($username);
16+
17+
return $user->getId();
18+
}
19+
1120
public function testCalendarIndex(): void
1221
{
1322
$user = new AdminUser('admin', 'test');
1423

1524
$client = static::createClient();
1625
$client->loginUser($user);
17-
$client->request('GET', '/calendars/test_user');
26+
27+
$userId = $this->getUserId($client, 'test_user');
28+
29+
$client->request('GET', '/calendars/'.$userId);
1830

1931
$this->assertResponseIsSuccessful();
2032

@@ -31,10 +43,12 @@ public function testCalendarEdit(): void
3143
$client = static::createClient();
3244
$client->loginUser($user);
3345

46+
$userId = $this->getUserId($client, 'test_user');
47+
3448
$calendarRepository = static::getContainer()->get(CalendarInstanceRepository::class);
3549
$calendar = $calendarRepository->findOneByDisplayName('default.calendar.title');
3650

37-
$client->request('GET', '/calendars/test_user/edit/'.$calendar->getId());
51+
$client->request('GET', '/calendars/'.$userId.'/edit/'.$calendar->getId());
3852

3953
$this->assertResponseIsSuccessful();
4054

@@ -43,7 +57,7 @@ public function testCalendarEdit(): void
4357

4458
$client->submitForm('calendar_instance_save');
4559

46-
$this->assertResponseRedirects('/calendars/test_user');
60+
$this->assertResponseRedirects('/calendars/'.$userId);
4761
$client->followRedirect();
4862

4963
$this->assertSelectorTextContains('h5', 'default.calendar.title');
@@ -55,7 +69,10 @@ public function testCalendarNew(): void
5569

5670
$client = static::createClient();
5771
$client->loginUser($user);
58-
$crawler = $client->request('GET', '/calendars/test_user/new');
72+
73+
$userId = $this->getUserId($client, 'test_user');
74+
75+
$crawler = $client->request('GET', '/calendars/'.$userId.'/new');
5976

6077
$this->assertResponseIsSuccessful();
6178

@@ -72,7 +89,7 @@ public function testCalendarNew(): void
7289
'calendar_instance[calendarColor]' => '#00112233',
7390
]);
7491

75-
$this->assertResponseRedirects('/calendars/test_user');
92+
$this->assertResponseRedirects('/calendars/'.$userId);
7693
$client->followRedirect();
7794

7895
$this->assertSelectorTextContains('h5', 'default.calendar.title');
@@ -86,12 +103,14 @@ public function testCalendarDelete(): void
86103
$client = static::createClient();
87104
$client->loginUser($user);
88105

106+
$userId = $this->getUserId($client, 'test_user');
107+
89108
$calendarRepository = static::getContainer()->get(CalendarInstanceRepository::class);
90109
$calendar = $calendarRepository->findOneByDisplayName('default.calendar.title');
91110

92-
$client->request('GET', '/calendars/test_user/delete/'.$calendar->getId());
111+
$client->request('GET', '/calendars/'.$userId.'/delete/'.$calendar->getId());
93112

94-
$this->assertResponseRedirects('/calendars/test_user');
113+
$this->assertResponseRedirects('/calendars/'.$userId);
95114
$client->followRedirect();
96115

97116
$this->assertSelectorTextNotContains('h5', 'default.calendar.title');

tests/Functional/UserControllerTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
namespace App\Tests\Functional;
44

5+
use App\Entity\User;
56
use App\Security\AdminUser;
67
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
78

89
class UserControllerTest extends WebTestCase
910
{
11+
private function getUserId($client, string $username): int
12+
{
13+
$userRepository = static::getContainer()->get('doctrine.orm.entity_manager')->getRepository(User::class);
14+
$user = $userRepository->findOneByUsername($username);
15+
16+
return $user->getId();
17+
}
18+
1019
public function testUserIndex(): void
1120
{
1221
$user = new AdminUser('admin', 'test');
@@ -29,7 +38,10 @@ public function testUserEdit(): void
2938

3039
$client = static::createClient();
3140
$client->loginUser($user);
32-
$client->request('GET', '/users/edit/test_user');
41+
42+
$userId = $this->getUserId($client, 'test_user');
43+
44+
$client->request('GET', '/users/edit/'.$userId);
3345

3446
$this->assertResponseIsSuccessful();
3547

@@ -82,14 +94,18 @@ public function testUserDelete(): void
8294

8395
$client = static::createClient();
8496
$client->loginUser($user);
85-
$client->request('GET', '/users/delete/test_user');
97+
98+
$userId1 = $this->getUserId($client, 'test_user');
99+
$userId2 = $this->getUserId($client, 'test_user2');
100+
101+
$client->request('GET', '/users/delete/'.$userId1);
86102

87103
$this->assertResponseRedirects('/users/');
88104
$client->followRedirect();
89105

90106
$this->assertAnySelectorTextContains('h5', 'Test User 2');
91107

92-
$client->request('GET', '/users/delete/test_user2');
108+
$client->request('GET', '/users/delete/'.$userId2);
93109

94110
$this->assertResponseRedirects('/users/');
95111
$client->followRedirect();
@@ -103,7 +119,10 @@ public function testUserDelegates(): void
103119

104120
$client = static::createClient();
105121
$client->loginUser($user);
106-
$client->request('GET', '/users/delegates/test_user');
122+
123+
$userId = $this->getUserId($client, 'test_user');
124+
125+
$client->request('GET', '/users/delegates/'.$userId);
107126

108127
$this->assertResponseIsSuccessful();
109128

@@ -115,7 +134,7 @@ public function testUserDelegates(): void
115134

116135
$client->clickLink('Disable it');
117136

118-
$this->assertResponseRedirects('/users/delegates/test_user');
137+
$this->assertResponseRedirects('/users/delegates/'.$userId);
119138
$client->followRedirect();
120139

121140
$this->assertSelectorExists('nav.navbar');

0 commit comments

Comments
 (0)