Skip to content

Add a test for command ListUsers (test emails) #1133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/Command/AbstractCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Tests\Command;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

abstract class AbstractCommandTest extends KernelTestCase
{
protected function setUp(): void
{
if ('Windows' === \PHP_OS_FAMILY) {
$this->markTestSkipped('`stty` is required to test this command.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this to the abstract class is wrong. Not all command tests require stty. And AFAICT, testing the list:users command will not require stty as it is not an interactive command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmm, you are right! Anybody willing to make a Pull Request to fix this? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1230

}
}

/**
* This helper method abstracts the boilerplate code needed to test the
* execution of a command.
*
* @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
*/
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
{
self::bootKernel();

// this uses a special testing container that allows you to fetch private services
$command = self::$container->get($this->getCommandFqcn());
$command->setApplication(new Application(self::$kernel));

$commandTester = new CommandTester($command);
$commandTester->setInputs($inputs);
$commandTester->execute($arguments);

return $commandTester;
}

abstract protected function getCommandFqcn(): string;
}
31 changes: 3 additions & 28 deletions tests/Command/AddUserCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@

use App\Command\AddUserCommand;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class AddUserCommandTest extends KernelTestCase
class AddUserCommandTest extends AbstractCommandTest
{
private $userData = [
'username' => 'chuck_norris',
Expand All @@ -26,13 +23,6 @@ class AddUserCommandTest extends KernelTestCase
'full-name' => 'Chuck Norris',
];

protected function setUp(): void
{
if ('Windows' === \PHP_OS_FAMILY) {
$this->markTestSkipped('Windows OS does not support testing this command.');
}
}

/**
* @dataProvider isAdminDataProvider
*
Expand Down Expand Up @@ -97,23 +87,8 @@ private function assertUserCreated(bool $isAdmin): void
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
}

/**
* This helper method abstracts the boilerplate code needed to test the
* execution of a command.
*
* @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 = []): void
protected function getCommandFqcn(): string
{
self::bootKernel();

// this uses a special testing container that allows you to fetch private services
$command = self::$container->get(AddUserCommand::class);
$command->setApplication(new Application(self::$kernel));

$commandTester = new CommandTester($command);
$commandTester->setInputs($inputs);
$commandTester->execute($arguments);
return AddUserCommand::class;
}
}
57 changes: 57 additions & 0 deletions tests/Command/ListUsersCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Tests\Command;

use App\Command\ListUsersCommand;

class ListUsersCommandTest extends AbstractCommandTest
{
/**
* @dataProvider maxResultsProvider
*
* This test verifies the amount of data is right according to the given parameter max results.
*/
public function testListUsers(int $maxResults): void
{
$tester = $this->executeCommand(
['--max-results' => $maxResults]
);

$emptyDisplayLines = 5;
$this->assertSame($emptyDisplayLines + $maxResults, mb_substr_count($tester->getDisplay(), "\n"));
}

public function maxResultsProvider(): ?\Generator
{
yield [1];
yield [2];
}

public function testItSendsNoEmailByDefault(): void
{
$this->executeCommand([]);

$this->assertEmailCount(0);
}

public function testItSendsAnEmailIfOptionProvided(): void
{
$this->executeCommand(['--send-to' => '[email protected]']);

$this->assertEmailCount(1);
}

protected function getCommandFqcn(): string
{
return ListUsersCommand::class;
}
}