Skip to content

Commit 2227440

Browse files
committed
feature #1058 Use new functional test specific assertions (voronkovich)
This PR was merged into the master branch. Discussion ---------- Use new functional test specific assertions See https://symfony.com/doc/current/testing/functional_tests_assertions.html Commits ------- 8bc4be5 Use new functional test specific assertions
2 parents 52ef1b7 + 8bc4be5 commit 2227440

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

tests/Controller/Admin/BlogControllerTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
4343
]);
4444

4545
$client->request($httpMethod, $url);
46-
$this->assertSame(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode());
46+
47+
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
4748
}
4849

4950
public function getUrlsForRegularUsers()
@@ -62,11 +63,10 @@ public function testAdminBackendHomePage()
6263
]);
6364

6465
$crawler = $client->request('GET', '/en/admin/post/');
65-
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
6666

67-
$this->assertGreaterThanOrEqual(
68-
1,
69-
$crawler->filter('body#admin_post_index #main tbody tr')->count(),
67+
$this->assertResponseIsSuccessful();
68+
$this->assertSelectorExists(
69+
'body#admin_post_index #main tbody tr',
7070
'The backend homepage displays all the available posts.'
7171
);
7272
}
@@ -95,7 +95,7 @@ public function testAdminNewPost()
9595
]);
9696
$client->submit($form);
9797

98-
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
98+
$this->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
9999

100100
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->findOneBy([
101101
'title' => $postTitle,
@@ -113,7 +113,7 @@ public function testAdminShowPost()
113113
]);
114114
$client->request('GET', '/en/admin/post/1');
115115

116-
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
116+
$this->assertResponseIsSuccessful();
117117
}
118118

119119
/**
@@ -136,7 +136,7 @@ public function testAdminEditPost()
136136
]);
137137
$client->submit($form);
138138

139-
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
139+
$this->assertResponseRedirects('/en/admin/post/1/edit', Response::HTTP_FOUND);
140140

141141
/** @var Post $post */
142142
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
@@ -158,7 +158,7 @@ public function testAdminDeletePost()
158158
$crawler = $client->request('GET', '/en/admin/post/1');
159159
$client->submit($crawler->filter('#delete-form')->form());
160160

161-
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
161+
$this->assertResponseRedirects('/en/admin/post/', Response::HTTP_FOUND);
162162

163163
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
164164
$this->assertNull($post);

tests/Controller/BlogControllerTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ public function testRss()
4343
$client = static::createClient();
4444
$crawler = $client->request('GET', '/en/blog/rss.xml');
4545

46-
$this->assertSame(
47-
'text/xml; charset=UTF-8',
48-
$client->getResponse()->headers->get('Content-Type')
49-
);
46+
$this->assertResponseHeaderSame('Content-Type', 'text/xml; charset=UTF-8');
5047

5148
$this->assertCount(
5249
Post::NUM_ITEMS,
@@ -92,7 +89,7 @@ public function testAjaxSearch()
9289

9390
$results = json_decode($client->getResponse()->getContent(), true);
9491

95-
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
92+
$this->assertResponseHeaderSame('Content-Type', 'application/json');
9693
$this->assertCount(1, $results);
9794
$this->assertSame('Lorem ipsum dolor sit amet consectetur adipiscing elit', $results[0]['title']);
9895
$this->assertSame('Jane Doe', $results[0]['author']);

tests/Controller/DefaultControllerTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public function testPublicUrls(string $url)
3939
$client = static::createClient();
4040
$client->request('GET', $url);
4141

42-
$this->assertSame(
43-
Response::HTTP_OK,
44-
$client->getResponse()->getStatusCode(),
45-
sprintf('The %s public URL loads correctly.', $url)
46-
);
42+
$this->assertResponseIsSuccessful(sprintf('The %s public URL loads correctly.', $url));
4743
}
4844

4945
/**
@@ -60,7 +56,7 @@ public function testPublicBlogPost()
6056
$blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
6157
$client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug()));
6258

63-
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
59+
$this->assertResponseIsSuccessful();
6460
}
6561

6662
/**
@@ -76,10 +72,10 @@ public function testSecureUrls(string $url)
7672
$client->request('GET', $url);
7773

7874
$response = $client->getResponse();
79-
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
80-
$this->assertSame(
75+
76+
$this->assertResponseRedirects(
8177
'http://localhost/en/login',
82-
$response->getTargetUrl(),
78+
Response::HTTP_FOUND,
8379
sprintf('The %s secure URL redirects to the login form.', $url)
8480
);
8581
}

tests/Controller/UserControllerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $ur
4141
$client->request($httpMethod, $url);
4242

4343
$response = $client->getResponse();
44-
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
45-
$this->assertSame(
44+
45+
$this->assertResponseRedirects(
4646
'http://localhost/en/login',
47-
$response->getTargetUrl(),
47+
Response::HTTP_FOUND,
4848
sprintf('The %s secure URL redirects to the login form.', $url)
4949
);
5050
}
@@ -69,7 +69,7 @@ public function testEditUser()
6969
]);
7070
$client->submit($form);
7171

72-
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
72+
$this->assertResponseRedirects('/en/profile/edit', Response::HTTP_FOUND);
7373

7474
/** @var User $user */
7575
$user = $client->getContainer()->get('doctrine')->getRepository(User::class)->findOneBy([
@@ -96,10 +96,10 @@ public function testChangePassword()
9696
$client->submit($form);
9797

9898
$response = $client->getResponse();
99-
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
100-
$this->assertSame(
99+
100+
$this->assertResponseRedirects(
101101
'/en/logout',
102-
$response->getTargetUrl(),
102+
Response::HTTP_FOUND,
103103
'Changing password logout the user.'
104104
);
105105
}

0 commit comments

Comments
 (0)