33namespace App \Tests \Functional ;
44
55use App \Entity \AddressBook ;
6+ use App \Entity \User ;
67use App \Security \AdminUser ;
78use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
89
910class 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 ' );
0 commit comments