Skip to content

Commit d4ab904

Browse files
committed
minor #1086 Simplify form tests, add scalar return types, remove unused variables (ker0x)
This PR was merged into the master branch. Discussion ---------- Simplify form tests, add scalar return types, remove unused variables Commits ------- 44dbaee Simplify form tests, add scalar return types, remove unused variables
1 parent ccf71d8 commit d4ab904

File tree

7 files changed

+67
-82
lines changed

7 files changed

+67
-82
lines changed

tests/Command/AddUserCommandTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace App\Tests\Command;
1313

1414
use App\Command\AddUserCommand;
15-
use App\Entity\User;
15+
use App\Repository\UserRepository;
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
1717
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1818
use Symfony\Component\Console\Tester\CommandTester;
@@ -42,7 +42,7 @@ protected function setUp(): void
4242
* This test provides all the arguments required by the command, so the
4343
* command runs non-interactively and it won't ask for any argument.
4444
*/
45-
public function testCreateUserNonInteractive(bool $isAdmin)
45+
public function testCreateUserNonInteractive(bool $isAdmin): void
4646
{
4747
$input = $this->userData;
4848
if ($isAdmin) {
@@ -61,7 +61,7 @@ public function testCreateUserNonInteractive(bool $isAdmin)
6161
* arguments.
6262
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
6363
*/
64-
public function testCreateUserInteractive(bool $isAdmin)
64+
public function testCreateUserInteractive(bool $isAdmin): void
6565
{
6666
$this->executeCommand(
6767
// these are the arguments (only 1 is passed, the rest are missing)
@@ -78,7 +78,7 @@ public function testCreateUserInteractive(bool $isAdmin)
7878
* This is used to execute the same test twice: first for normal users
7979
* (isAdmin = false) and then for admin users (isAdmin = true).
8080
*/
81-
public function isAdminDataProvider()
81+
public function isAdminDataProvider(): ?\Generator
8282
{
8383
yield [false];
8484
yield [true];
@@ -88,12 +88,12 @@ public function isAdminDataProvider()
8888
* This helper method checks that the user was correctly created and saved
8989
* in the database.
9090
*/
91-
private function assertUserCreated(bool $isAdmin)
91+
private function assertUserCreated(bool $isAdmin): void
9292
{
93-
$container = self::$kernel->getContainer();
93+
$container = self::$container;
9494

95-
/** @var User $user */
96-
$user = $container->get('doctrine')->getRepository(User::class)->findOneByEmail($this->userData['email']);
95+
/** @var \App\Entity\User $user */
96+
$user = $container->get(UserRepository::class)->findOneByEmail($this->userData['email']);
9797
$this->assertNotNull($user);
9898

9999
$this->assertSame($this->userData['full-name'], $user->getFullName());
@@ -109,7 +109,7 @@ private function assertUserCreated(bool $isAdmin)
109109
* @param array $arguments All the arguments passed when executing the command
110110
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
111111
*/
112-
private function executeCommand(array $arguments, array $inputs = [])
112+
private function executeCommand(array $arguments, array $inputs = []): void
113113
{
114114
self::bootKernel();
115115

tests/Controller/Admin/BlogControllerTest.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace App\Tests\Controller\Admin;
1313

14-
use App\Entity\Post;
14+
use App\Repository\PostRepository;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1616
use Symfony\Component\HttpFoundation\Response;
1717

@@ -35,7 +35,7 @@ class BlogControllerTest extends WebTestCase
3535
/**
3636
* @dataProvider getUrlsForRegularUsers
3737
*/
38-
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
38+
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url): void
3939
{
4040
$client = static::createClient([], [
4141
'PHP_AUTH_USER' => 'john_user',
@@ -47,22 +47,21 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
4747
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
4848
}
4949

50-
public function getUrlsForRegularUsers()
50+
public function getUrlsForRegularUsers(): ?\Generator
5151
{
5252
yield ['GET', '/en/admin/post/'];
5353
yield ['GET', '/en/admin/post/1'];
5454
yield ['GET', '/en/admin/post/1/edit'];
5555
yield ['POST', '/en/admin/post/1/delete'];
5656
}
5757

58-
public function testAdminBackendHomePage()
58+
public function testAdminBackendHomePage(): void
5959
{
6060
$client = static::createClient([], [
6161
'PHP_AUTH_USER' => 'jane_admin',
6262
'PHP_AUTH_PW' => 'kitten',
6363
]);
64-
65-
$crawler = $client->request('GET', '/en/admin/post/');
64+
$client->request('GET', '/en/admin/post/');
6665

6766
$this->assertResponseIsSuccessful();
6867
$this->assertSelectorExists(
@@ -77,7 +76,7 @@ public function testAdminBackendHomePage()
7776
* to the database are rolled back when this test completes. This means that
7877
* all the application tests begin with the same database contents.
7978
*/
80-
public function testAdminNewPost()
79+
public function testAdminNewPost(): void
8180
{
8281
$postTitle = 'Blog Post Title '.mt_rand();
8382
$postSummary = $this->generateRandomString(255);
@@ -87,25 +86,23 @@ public function testAdminNewPost()
8786
'PHP_AUTH_USER' => 'jane_admin',
8887
'PHP_AUTH_PW' => 'kitten',
8988
]);
90-
$crawler = $client->request('GET', '/en/admin/post/new');
91-
$form = $crawler->selectButton('Create post')->form([
89+
$client->request('GET', '/en/admin/post/new');
90+
$client->submitForm('Create post', [
9291
'post[title]' => $postTitle,
9392
'post[summary]' => $postSummary,
9493
'post[content]' => $postContent,
9594
]);
96-
$client->submit($form);
9795

9896
$this->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
9997

100-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->findOneBy([
101-
'title' => $postTitle,
102-
]);
98+
/** @var \App\Entity\Post $post */
99+
$post = self::$container->get(PostRepository::class)->findOneByTitle($postTitle);
103100
$this->assertNotNull($post);
104101
$this->assertSame($postSummary, $post->getSummary());
105102
$this->assertSame($postContent, $post->getContent());
106103
}
107104

108-
public function testAdminNewDuplicatedPost()
105+
public function testAdminNewDuplicatedPost(): void
109106
{
110107
$postTitle = 'Blog Post Title '.mt_rand();
111108
$postSummary = $this->generateRandomString(255);
@@ -130,7 +127,7 @@ public function testAdminNewDuplicatedPost()
130127
$this->assertSelectorTextContains('form .form-group.has-error .help-block', 'This title was already used in another blog post, but they must be unique.');
131128
}
132129

133-
public function testAdminShowPost()
130+
public function testAdminShowPost(): void
134131
{
135132
$client = static::createClient([], [
136133
'PHP_AUTH_USER' => 'jane_admin',
@@ -147,24 +144,23 @@ public function testAdminShowPost()
147144
* to the database are rolled back when this test completes. This means that
148145
* all the application tests begin with the same database contents.
149146
*/
150-
public function testAdminEditPost()
147+
public function testAdminEditPost(): void
151148
{
152149
$newBlogPostTitle = 'Blog Post Title '.mt_rand();
153150

154151
$client = static::createClient([], [
155152
'PHP_AUTH_USER' => 'jane_admin',
156153
'PHP_AUTH_PW' => 'kitten',
157154
]);
158-
$crawler = $client->request('GET', '/en/admin/post/1/edit');
159-
$form = $crawler->selectButton('Save changes')->form([
155+
$client->request('GET', '/en/admin/post/1/edit');
156+
$client->submitForm('Save changes', [
160157
'post[title]' => $newBlogPostTitle,
161158
]);
162-
$client->submit($form);
163159

164160
$this->assertResponseRedirects('/en/admin/post/1/edit', Response::HTTP_FOUND);
165161

166-
/** @var Post $post */
167-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
162+
/** @var \App\Entity\Post $post */
163+
$post = self::$container->get(PostRepository::class)->find(1);
168164
$this->assertSame($newBlogPostTitle, $post->getTitle());
169165
}
170166

@@ -174,7 +170,7 @@ public function testAdminEditPost()
174170
* to the database are rolled back when this test completes. This means that
175171
* all the application tests begin with the same database contents.
176172
*/
177-
public function testAdminDeletePost()
173+
public function testAdminDeletePost(): void
178174
{
179175
$client = static::createClient([], [
180176
'PHP_AUTH_USER' => 'jane_admin',
@@ -185,7 +181,7 @@ public function testAdminDeletePost()
185181

186182
$this->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
187183

188-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
184+
$post = self::$container->get(PostRepository::class)->find(1);
189185
$this->assertNull($post);
190186
}
191187

tests/Controller/BlogControllerTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
class BlogControllerTest extends WebTestCase
2828
{
29-
public function testIndex()
29+
public function testIndex(): void
3030
{
3131
$client = static::createClient();
3232
$crawler = $client->request('GET', '/en/blog/');
@@ -40,7 +40,7 @@ public function testIndex()
4040
);
4141
}
4242

43-
public function testRss()
43+
public function testRss(): void
4444
{
4545
$client = static::createClient();
4646
$crawler = $client->request('GET', '/en/blog/rss.xml');
@@ -60,7 +60,7 @@ public function testRss()
6060
* to the database are rolled back when this test completes. This means that
6161
* all the application tests begin with the same database contents.
6262
*/
63-
public function testNewComment()
63+
public function testNewComment(): void
6464
{
6565
$client = static::createClient([], [
6666
'PHP_AUTH_USER' => 'john_user',
@@ -72,19 +72,17 @@ public function testNewComment()
7272
$crawler = $client->request('GET', '/en/blog/');
7373
$postLink = $crawler->filter('article.post > h2 a')->link();
7474

75-
$crawler = $client->click($postLink);
76-
77-
$form = $crawler->selectButton('Publish comment')->form([
75+
$client->click($postLink);
76+
$crawler = $client->submitForm('Publish comment', [
7877
'comment[content]' => 'Hi, Symfony!',
7978
]);
80-
$crawler = $client->submit($form);
8179

8280
$newComment = $crawler->filter('.post-comment')->first()->filter('div > p')->text();
8381

8482
$this->assertSame('Hi, Symfony!', $newComment);
8583
}
8684

87-
public function testAjaxSearch()
85+
public function testAjaxSearch(): void
8886
{
8987
$client = static::createClient();
9088
$client->xmlHttpRequest('GET', '/en/blog/search', ['q' => 'lorem']);

tests/Controller/DefaultControllerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DefaultControllerTest extends WebTestCase
3434
*
3535
* @dataProvider getPublicUrls
3636
*/
37-
public function testPublicUrls(string $url)
37+
public function testPublicUrls(string $url): void
3838
{
3939
$client = static::createClient();
4040
$client->request('GET', $url);
@@ -49,7 +49,7 @@ public function testPublicUrls(string $url)
4949
* blog post fixtures are randomly generated and there's no guarantee that
5050
* a given blog post slug will be available.
5151
*/
52-
public function testPublicBlogPost()
52+
public function testPublicBlogPost(): void
5353
{
5454
$client = static::createClient();
5555
// the service container is always available via the test client
@@ -66,28 +66,26 @@ public function testPublicBlogPost()
6666
*
6767
* @dataProvider getSecureUrls
6868
*/
69-
public function testSecureUrls(string $url)
69+
public function testSecureUrls(string $url): void
7070
{
7171
$client = static::createClient();
7272
$client->request('GET', $url);
7373

74-
$response = $client->getResponse();
75-
7674
$this->assertResponseRedirects(
7775
'http://localhost/en/login',
7876
Response::HTTP_FOUND,
7977
sprintf('The %s secure URL redirects to the login form.', $url)
8078
);
8179
}
8280

83-
public function getPublicUrls()
81+
public function getPublicUrls(): ?\Generator
8482
{
8583
yield ['/'];
8684
yield ['/en/blog/'];
8785
yield ['/en/login'];
8886
}
8987

90-
public function getSecureUrls()
88+
public function getSecureUrls(): ?\Generator
9189
{
9290
yield ['/en/admin/post/'];
9391
yield ['/en/admin/post/new'];

tests/Controller/UserControllerTest.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace App\Tests\Controller;
1313

14-
use App\Entity\User;
14+
use App\Repository\UserRepository;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1616
use Symfony\Component\HttpFoundation\Response;
1717

@@ -35,67 +35,60 @@ class UserControllerTest extends WebTestCase
3535
/**
3636
* @dataProvider getUrlsForAnonymousUsers
3737
*/
38-
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url)
38+
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
3939
{
4040
$client = static::createClient();
4141
$client->request($httpMethod, $url);
4242

43-
$response = $client->getResponse();
44-
4543
$this->assertResponseRedirects(
4644
'http://localhost/en/login',
4745
Response::HTTP_FOUND,
4846
sprintf('The %s secure URL redirects to the login form.', $url)
4947
);
5048
}
5149

52-
public function getUrlsForAnonymousUsers()
50+
public function getUrlsForAnonymousUsers(): ?\Generator
5351
{
5452
yield ['GET', '/en/profile/edit'];
5553
yield ['GET', '/en/profile/change-password'];
5654
}
5755

58-
public function testEditUser()
56+
public function testEditUser(): void
5957
{
6058
$newUserEmail = '[email protected]';
6159

6260
$client = static::createClient([], [
6361
'PHP_AUTH_USER' => 'jane_admin',
6462
'PHP_AUTH_PW' => 'kitten',
6563
]);
66-
$crawler = $client->request('GET', '/en/profile/edit');
67-
$form = $crawler->selectButton('Save changes')->form([
64+
$client->request('GET', '/en/profile/edit');
65+
$client->submitForm('Save changes', [
6866
'user[email]' => $newUserEmail,
6967
]);
70-
$client->submit($form);
7168

7269
$this->assertResponseRedirects('/en/profile/edit', Response::HTTP_FOUND);
7370

74-
/** @var User $user */
75-
$user = $client->getContainer()->get('doctrine')->getRepository(User::class)->findOneBy([
76-
'email' => $newUserEmail,
77-
]);
71+
/** @var \App\Entity\User $user */
72+
$user = self::$container->get(UserRepository::class)->findOneByEmail($newUserEmail);
73+
7874
$this->assertNotNull($user);
7975
$this->assertSame($newUserEmail, $user->getEmail());
8076
}
8177

82-
public function testChangePassword()
78+
public function testChangePassword(): void
8379
{
8480
$newUserPassword = 'new-password';
8581

8682
$client = static::createClient([], [
8783
'PHP_AUTH_USER' => 'jane_admin',
8884
'PHP_AUTH_PW' => 'kitten',
8985
]);
90-
$crawler = $client->request('GET', '/en/profile/change-password');
91-
$form = $crawler->selectButton('Save changes')->form([
86+
$client->request('GET', '/en/profile/change-password');
87+
$client->submitForm('Save changes', [
9288
'change_password[currentPassword]' => 'kitten',
9389
'change_password[newPassword][first]' => $newUserPassword,
9490
'change_password[newPassword][second]' => $newUserPassword,
9591
]);
96-
$client->submit($form);
97-
98-
$response = $client->getResponse();
9992

10093
$this->assertResponseRedirects(
10194
'/en/logout',

0 commit comments

Comments
 (0)