Skip to content

Commit

Permalink
feature symfony#1058 Use new functional test specific assertions (vor…
Browse files Browse the repository at this point in the history
…onkovich)

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
  • Loading branch information
javiereguiluz committed Jan 3, 2020
2 parents 52ef1b7 + 8bc4be5 commit 2227440
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
18 changes: 9 additions & 9 deletions tests/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
]);

$client->request($httpMethod, $url);
$this->assertSame(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode());

$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
}

public function getUrlsForRegularUsers()
Expand All @@ -62,11 +63,10 @@ public function testAdminBackendHomePage()
]);

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

$this->assertGreaterThanOrEqual(
1,
$crawler->filter('body#admin_post_index #main tbody tr')->count(),
$this->assertResponseIsSuccessful();
$this->assertSelectorExists(
'body#admin_post_index #main tbody tr',
'The backend homepage displays all the available posts.'
);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testAdminNewPost()
]);
$client->submit($form);

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

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

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

/**
Expand All @@ -136,7 +136,7 @@ public function testAdminEditPost()
]);
$client->submit($form);

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

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

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

$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
$this->assertNull($post);
Expand Down
7 changes: 2 additions & 5 deletions tests/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public function testRss()
$client = static::createClient();
$crawler = $client->request('GET', '/en/blog/rss.xml');

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

$this->assertCount(
Post::NUM_ITEMS,
Expand Down Expand Up @@ -92,7 +89,7 @@ public function testAjaxSearch()

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

$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$this->assertResponseHeaderSame('Content-Type', 'application/json');
$this->assertCount(1, $results);
$this->assertSame('Lorem ipsum dolor sit amet consectetur adipiscing elit', $results[0]['title']);
$this->assertSame('Jane Doe', $results[0]['author']);
Expand Down
14 changes: 5 additions & 9 deletions tests/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public function testPublicUrls(string $url)
$client = static::createClient();
$client->request('GET', $url);

$this->assertSame(
Response::HTTP_OK,
$client->getResponse()->getStatusCode(),
sprintf('The %s public URL loads correctly.', $url)
);
$this->assertResponseIsSuccessful(sprintf('The %s public URL loads correctly.', $url));
}

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

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

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

$response = $client->getResponse();
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
$this->assertSame(

$this->assertResponseRedirects(
'http://localhost/en/login',
$response->getTargetUrl(),
Response::HTTP_FOUND,
sprintf('The %s secure URL redirects to the login form.', $url)
);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $ur
$client->request($httpMethod, $url);

$response = $client->getResponse();
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
$this->assertSame(

$this->assertResponseRedirects(
'http://localhost/en/login',
$response->getTargetUrl(),
Response::HTTP_FOUND,
sprintf('The %s secure URL redirects to the login form.', $url)
);
}
Expand All @@ -69,7 +69,7 @@ public function testEditUser()
]);
$client->submit($form);

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

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

$response = $client->getResponse();
$this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
$this->assertSame(

$this->assertResponseRedirects(
'/en/logout',
$response->getTargetUrl(),
Response::HTTP_FOUND,
'Changing password logout the user.'
);
}
Expand Down

0 comments on commit 2227440

Please sign in to comment.