Skip to content

Commit

Permalink
minor symfony#1086 Simplify form tests, add scalar return types, remo…
Browse files Browse the repository at this point in the history
…ve 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
  • Loading branch information
javiereguiluz committed Mar 12, 2020
1 parent ccf71d8 commit d4ab904
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 82 deletions.
18 changes: 9 additions & 9 deletions tests/Command/AddUserCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace App\Tests\Command;

use App\Command\AddUserCommand;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -42,7 +42,7 @@ protected function setUp(): void
* This test provides all the arguments required by the command, so the
* command runs non-interactively and it won't ask for any argument.
*/
public function testCreateUserNonInteractive(bool $isAdmin)
public function testCreateUserNonInteractive(bool $isAdmin): void
{
$input = $this->userData;
if ($isAdmin) {
Expand All @@ -61,7 +61,7 @@ public function testCreateUserNonInteractive(bool $isAdmin)
* arguments.
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
*/
public function testCreateUserInteractive(bool $isAdmin)
public function testCreateUserInteractive(bool $isAdmin): void
{
$this->executeCommand(
// these are the arguments (only 1 is passed, the rest are missing)
Expand All @@ -78,7 +78,7 @@ public function testCreateUserInteractive(bool $isAdmin)
* This is used to execute the same test twice: first for normal users
* (isAdmin = false) and then for admin users (isAdmin = true).
*/
public function isAdminDataProvider()
public function isAdminDataProvider(): ?\Generator
{
yield [false];
yield [true];
Expand All @@ -88,12 +88,12 @@ public function isAdminDataProvider()
* This helper method checks that the user was correctly created and saved
* in the database.
*/
private function assertUserCreated(bool $isAdmin)
private function assertUserCreated(bool $isAdmin): void
{
$container = self::$kernel->getContainer();
$container = self::$container;

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

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

Expand Down
42 changes: 19 additions & 23 deletions tests/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace App\Tests\Controller\Admin;

use App\Entity\Post;
use App\Repository\PostRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -35,7 +35,7 @@ class BlogControllerTest extends WebTestCase
/**
* @dataProvider getUrlsForRegularUsers
*/
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url): void
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'john_user',
Expand All @@ -47,22 +47,21 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
}

public function getUrlsForRegularUsers()
public function getUrlsForRegularUsers(): ?\Generator
{
yield ['GET', '/en/admin/post/'];
yield ['GET', '/en/admin/post/1'];
yield ['GET', '/en/admin/post/1/edit'];
yield ['POST', '/en/admin/post/1/delete'];
}

public function testAdminBackendHomePage()
public function testAdminBackendHomePage(): void
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);

$crawler = $client->request('GET', '/en/admin/post/');
$client->request('GET', '/en/admin/post/');

$this->assertResponseIsSuccessful();
$this->assertSelectorExists(
Expand All @@ -77,7 +76,7 @@ public function testAdminBackendHomePage()
* to the database are rolled back when this test completes. This means that
* all the application tests begin with the same database contents.
*/
public function testAdminNewPost()
public function testAdminNewPost(): void
{
$postTitle = 'Blog Post Title '.mt_rand();
$postSummary = $this->generateRandomString(255);
Expand All @@ -87,25 +86,23 @@ public function testAdminNewPost()
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);
$crawler = $client->request('GET', '/en/admin/post/new');
$form = $crawler->selectButton('Create post')->form([
$client->request('GET', '/en/admin/post/new');
$client->submitForm('Create post', [
'post[title]' => $postTitle,
'post[summary]' => $postSummary,
'post[content]' => $postContent,
]);
$client->submit($form);

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

$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->findOneBy([
'title' => $postTitle,
]);
/** @var \App\Entity\Post $post */
$post = self::$container->get(PostRepository::class)->findOneByTitle($postTitle);
$this->assertNotNull($post);
$this->assertSame($postSummary, $post->getSummary());
$this->assertSame($postContent, $post->getContent());
}

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

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

$client = static::createClient([], [
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);
$crawler = $client->request('GET', '/en/admin/post/1/edit');
$form = $crawler->selectButton('Save changes')->form([
$client->request('GET', '/en/admin/post/1/edit');
$client->submitForm('Save changes', [
'post[title]' => $newBlogPostTitle,
]);
$client->submit($form);

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

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

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

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

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

Expand Down
14 changes: 6 additions & 8 deletions tests/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class BlogControllerTest extends WebTestCase
{
public function testIndex()
public function testIndex(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/en/blog/');
Expand All @@ -40,7 +40,7 @@ public function testIndex()
);
}

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

$crawler = $client->click($postLink);

$form = $crawler->selectButton('Publish comment')->form([
$client->click($postLink);
$crawler = $client->submitForm('Publish comment', [
'comment[content]' => 'Hi, Symfony!',
]);
$crawler = $client->submit($form);

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

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

public function testAjaxSearch()
public function testAjaxSearch(): void
{
$client = static::createClient();
$client->xmlHttpRequest('GET', '/en/blog/search', ['q' => 'lorem']);
Expand Down
12 changes: 5 additions & 7 deletions tests/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DefaultControllerTest extends WebTestCase
*
* @dataProvider getPublicUrls
*/
public function testPublicUrls(string $url)
public function testPublicUrls(string $url): void
{
$client = static::createClient();
$client->request('GET', $url);
Expand All @@ -49,7 +49,7 @@ public function testPublicUrls(string $url)
* blog post fixtures are randomly generated and there's no guarantee that
* a given blog post slug will be available.
*/
public function testPublicBlogPost()
public function testPublicBlogPost(): void
{
$client = static::createClient();
// the service container is always available via the test client
Expand All @@ -66,28 +66,26 @@ public function testPublicBlogPost()
*
* @dataProvider getSecureUrls
*/
public function testSecureUrls(string $url)
public function testSecureUrls(string $url): void
{
$client = static::createClient();
$client->request('GET', $url);

$response = $client->getResponse();

$this->assertResponseRedirects(
'http://localhost/en/login',
Response::HTTP_FOUND,
sprintf('The %s secure URL redirects to the login form.', $url)
);
}

public function getPublicUrls()
public function getPublicUrls(): ?\Generator
{
yield ['/'];
yield ['/en/blog/'];
yield ['/en/login'];
}

public function getSecureUrls()
public function getSecureUrls(): ?\Generator
{
yield ['/en/admin/post/'];
yield ['/en/admin/post/new'];
Expand Down
31 changes: 12 additions & 19 deletions tests/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace App\Tests\Controller;

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -35,67 +35,60 @@ class UserControllerTest extends WebTestCase
/**
* @dataProvider getUrlsForAnonymousUsers
*/
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url)
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
{
$client = static::createClient();
$client->request($httpMethod, $url);

$response = $client->getResponse();

$this->assertResponseRedirects(
'http://localhost/en/login',
Response::HTTP_FOUND,
sprintf('The %s secure URL redirects to the login form.', $url)
);
}

public function getUrlsForAnonymousUsers()
public function getUrlsForAnonymousUsers(): ?\Generator
{
yield ['GET', '/en/profile/edit'];
yield ['GET', '/en/profile/change-password'];
}

public function testEditUser()
public function testEditUser(): void
{
$newUserEmail = 'admin_jane@symfony.com';

$client = static::createClient([], [
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);
$crawler = $client->request('GET', '/en/profile/edit');
$form = $crawler->selectButton('Save changes')->form([
$client->request('GET', '/en/profile/edit');
$client->submitForm('Save changes', [
'user[email]' => $newUserEmail,
]);
$client->submit($form);

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

/** @var User $user */
$user = $client->getContainer()->get('doctrine')->getRepository(User::class)->findOneBy([
'email' => $newUserEmail,
]);
/** @var \App\Entity\User $user */
$user = self::$container->get(UserRepository::class)->findOneByEmail($newUserEmail);

$this->assertNotNull($user);
$this->assertSame($newUserEmail, $user->getEmail());
}

public function testChangePassword()
public function testChangePassword(): void
{
$newUserPassword = 'new-password';

$client = static::createClient([], [
'PHP_AUTH_USER' => 'jane_admin',
'PHP_AUTH_PW' => 'kitten',
]);
$crawler = $client->request('GET', '/en/profile/change-password');
$form = $crawler->selectButton('Save changes')->form([
$client->request('GET', '/en/profile/change-password');
$client->submitForm('Save changes', [
'change_password[currentPassword]' => 'kitten',
'change_password[newPassword][first]' => $newUserPassword,
'change_password[newPassword][second]' => $newUserPassword,
]);
$client->submit($form);

$response = $client->getResponse();

$this->assertResponseRedirects(
'/en/logout',
Expand Down
Loading

0 comments on commit d4ab904

Please sign in to comment.