Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)

This commit is contained in:
Kevin Papst
2022-12-31 21:19:55 +01:00
committed by GitHub
parent 95e06746bd
commit 90a0fd8a22
2164 changed files with 83700 additions and 86426 deletions

View File

@@ -47,10 +47,6 @@ class ActivateUserCommandTest extends KernelTestCase
$command = $application->find('kimai:user:activate');
self::assertInstanceOf(ActivateUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:activate');
self::assertInstanceOf(ActivateUserCommand::class, $command);
}
protected function callCommand(?string $username)
@@ -80,7 +76,7 @@ class ActivateUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('chris_user');
$user = $userRepository->loadUserByIdentifier('chris_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isEnabled());
}

View File

@@ -116,23 +116,12 @@ class BundleInstallerCommandTest extends KernelTestCase
class FakeCommand extends Command
{
/**
* @var null|string
*/
private $exception = null;
/**
* @var int
*/
private $exitCode = 0;
public function __construct(string $commandName, int $exitCode, ?string $executeThrows = null)
public function __construct(string $commandName, private int $exitCode, private ?string $exception = null)
{
parent::__construct($commandName);
$this->exitCode = $exitCode;
$this->exception = $executeThrows;
}
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (null !== $this->exception) {
throw new \Exception($this->exception);

View File

@@ -17,6 +17,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
/**
* @covers \App\Command\ChangePasswordCommand
@@ -25,10 +26,7 @@ use Symfony\Component\Console\Tester\CommandTester;
*/
class ChangePasswordCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
private Application $application;
protected function setUp(): void
{
@@ -42,19 +40,15 @@ class ChangePasswordCommandTest extends KernelTestCase
$this->application->add(new ChangePasswordCommand($userService));
}
public function testCommandName()
public function testCommandName(): void
{
$application = $this->application;
$command = $application->find('kimai:user:password');
self::assertInstanceOf(ChangePasswordCommand::class, $command);
// test alias
$command = $application->find('fos:user:change-password');
self::assertInstanceOf(ChangePasswordCommand::class, $command);
}
protected function callCommand(?string $username, ?string $password)
protected function callCommand(?string $username, ?string $password): CommandTester
{
$command = $this->application->find('kimai:user:password');
$input = [
@@ -85,25 +79,32 @@ class ChangePasswordCommandTest extends KernelTestCase
return $commandTester;
}
public function testChangePassword()
public function testChangePassword(): void
{
$commandTester = $this->callCommand('john_user', '0987654321');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Changed password for user "john_user".', $output);
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
$userRepository = self::getContainer()->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByIdentifier('john_user');
self::assertInstanceOf(User::class, $user);
$container = self::$kernel->getContainer();
$encoderService = $container->get('security.password_encoder');
self::assertTrue($encoderService->isPasswordValid($user, '0987654321'));
/** @var PasswordHasherFactoryInterface $passwordEncoder */
$passwordEncoder = self::getContainer()->get('security.password_hasher_factory');
self::assertTrue($passwordEncoder->getPasswordHasher($user)->verify($user->getPassword(), '0987654321'));
}
public function testWithMissingUsername()
public function testChangePasswordFailsOnShortPassword(): void
{
$commandTester = $this->callCommand('john_user', '1');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] plainPassword: This value is too short.', $output);
}
public function testWithMissingUsername(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username").');
@@ -111,7 +112,7 @@ class ChangePasswordCommandTest extends KernelTestCase
$this->callCommand(null, '1234567890');
}
public function testWithMissingPasswordAsksForPassword()
public function testWithMissingPasswordAsksForPassword(): void
{
$commandTester = $this->callCommand('john_user', null);
$output = $commandTester->getDisplay();

View File

@@ -23,10 +23,7 @@ use Symfony\Component\Console\Tester\CommandTester;
*/
class CreateUserCommandTest extends KernelTestCase
{
/**
* @var Application
*/
private $application;
private Application $application;
protected function setUp(): void
{
@@ -40,16 +37,15 @@ class CreateUserCommandTest extends KernelTestCase
));
}
public function testCreateUserFailsForShortPassword()
public function testCreateUserFailsForShortPassword(): void
{
$commandTester = $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] plainPassword (foobar)', $output);
$this->assertStringContainsString('This value is too short. It should have 8 characters or more.', $output);
$this->assertStringContainsString('[ERROR] plainPassword: This value is too short.', $output);
}
public function testCreateUser()
public function testCreateUser(): void
{
$commandTester = $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar12');
@@ -59,12 +55,12 @@ class CreateUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('MyTestUser');
$user = $userRepository->loadUserByIdentifier('MyTestUser');
self::assertInstanceOf(User::class, $user);
self::assertNotNull($user);
}
protected function createUser($username, $email, $role, $password)
protected function createUser($username, $email, $role, $password): CommandTester
{
$command = $this->application->find('kimai:user:create');
$commandTester = new CommandTester($command);
@@ -79,44 +75,38 @@ class CreateUserCommandTest extends KernelTestCase
return $commandTester;
}
public function testUserWithEmptyFieldsTriggersValidationProblem()
public function testUserWithEmptyFieldsTriggersValidationProblem(): void
{
$commandTester = $this->createUser('xx', '', 'ROLE_USER', '');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email ()', $output);
$this->assertStringContainsString('This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword ()', $output);
$this->assertStringContainsString('This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword ()', $output);
$this->assertStringContainsString('This value is too short. It should have 8 characters or more', $output);
$this->assertStringContainsString('[ERROR] email: This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword: This value should not be blank', $output);
$this->assertStringContainsString('[ERROR] plainPassword: This value is too short.', $output);
}
public function testUserAlreadyExisting()
public function testUserAlreadyExisting(): void
{
$this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar123');
$commandTester = $this->createUser('MyTestUser', 'user2@example.com', 'ROLE_USER', 'foobar123');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] username (MyTestUser)', $output);
$this->assertStringContainsString('The username is already used.', $output);
$this->assertStringContainsString('[ERROR] username: The username is already used.', $output);
}
public function testEmailAlreadyExisting()
public function testEmailAlreadyExisting(): void
{
$this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar12');
$commandTester = $this->createUser('MyTestUser2', 'user@example.com', 'ROLE_USER', 'foobar');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email (MyTestUser2)', $output);
$this->assertStringContainsString(' The email is already used.', $output);
$this->assertStringContainsString('[ERROR] email: The email is already used.', $output);
}
public function testUserEmail()
public function testUserEmail(): void
{
$commandTester = $this->createUser('MyTestUser', 'ROLE_USER', 'ROLE_USER', 'foobar12');
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] email (ROLE_USER)', $output);
$this->assertStringContainsString('This value is not a valid email address', $output);
$this->assertStringContainsString('[ERROR] email: This value is not a valid email address', $output);
}
}

View File

@@ -47,10 +47,6 @@ class DeactivateUserCommandTest extends KernelTestCase
$command = $application->find('kimai:user:deactivate');
self::assertInstanceOf(DeactivateUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:deactivate');
self::assertInstanceOf(DeactivateUserCommand::class, $command);
}
protected function callCommand(?string $username)
@@ -80,7 +76,7 @@ class DeactivateUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
$user = $userRepository->loadUserByIdentifier('john_user');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->isEnabled());
}

View File

@@ -48,10 +48,6 @@ class DemoteUserCommandTest extends KernelTestCase
$command = $application->find('kimai:user:demote');
self::assertInstanceOf(DemoteUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:demote');
self::assertInstanceOf(DemoteUserCommand::class, $command);
}
protected function callCommand(?string $username, ?string $role, bool $super = false)
@@ -89,7 +85,7 @@ class DemoteUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('tony_teamlead');
$user = $userRepository->loadUserByIdentifier('tony_teamlead');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->hasTeamleadRole());
}
@@ -104,7 +100,7 @@ class DemoteUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('susan_super');
$user = $userRepository->loadUserByIdentifier('susan_super');
self::assertInstanceOf(User::class, $user);
self::assertFalse($user->isSuperAdmin());
}

View File

@@ -26,6 +26,7 @@ use App\Tests\KernelTestTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
@@ -36,10 +37,7 @@ class ExportCreateCommandTest extends KernelTestCase
{
use KernelTestTrait;
/**
* @var Application
*/
protected $application;
protected Application $application;
private function clearExportFiles()
{
@@ -70,7 +68,7 @@ class ExportCreateCommandTest extends KernelTestCase
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$container = self::$container;
$container = self::getContainer();
$application->add(new ExportCreateCommand(
$container->get(ServiceExport::class),
@@ -236,7 +234,7 @@ class ExportCreateCommandTest extends KernelTestCase
$this->prepareFixtures($start);
$options = ['--template' => 'csv', '--email' => ['foo@example.com', 'foo2@example.com'], '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')];
$mailer = $this->createMock(KimaiMailer::class);
$mailer = $this->createMock(MailerInterface::class);
$mailer->expects($this->exactly(2))->method('send');
$application = $this->createApplication($mailer);

View File

@@ -1,225 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* 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\ImportCustomerCommand;
use App\Importer\ImporterService;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @group integration
*/
class ImportCustomerCommandTest extends KernelTestCase
{
/**
* @var Application
*/
protected $application;
protected function setUp(): void
{
parent::setUp();
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$importer = $container->get(ImporterService::class);
$this->application->add(new ImportCustomerCommand($importer));
}
public function testCommandName()
{
$command = $this->application->find('kimai:import:customer');
self::assertInstanceOf(ImportCustomerCommand::class, $command);
}
public function testImportWithMissingFile()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/foo_bar.csv1'
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Kimai importer: Customers', $result);
self::assertStringContainsString('[ERROR] File not existing or not readable', $result);
self::assertStringContainsString('_data/foo_bar', $result);
self::assertEquals(2, $commandTester->getStatusCode());
}
public function testDefaultImportWithUnknownReader()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--reader' => 'fooo',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('[ERROR] Unknown import reader: fooo', $result);
self::assertEquals(1, $commandTester->getStatusCode());
}
public function testImportWithUnknownImporter()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--importer' => 'fooo',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('[ERROR] Unknown customer importer: fooo', $result);
self::assertEquals(1, $commandTester->getStatusCode());
}
public function testDefaultImport()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers.csv'
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 10 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 10 customers, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 9 customer', $result);
self::assertStringContainsString('[OK] Updated 1 customer', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testDefaultImportSkipUpdate()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers.csv',
'--no-update' => true,
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 10 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 10 customers, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 9 customer', $result);
self::assertStringContainsString('[OK] Skipped 1 existing customer', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testDefaultImportWithSemicolon()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--importer' => 'default',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 10 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 10 customers, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 10 customer', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testGrandtotalImportWithInvalidCsvFile()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--importer' => 'grandtotal',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Invalid row 1: Missing customer name', $result);
self::assertStringContainsString('! [CAUTION] Not importing, previous 10 errors need to be fixed first.', $result);
self::assertEquals(3, $commandTester->getStatusCode());
}
public function testGrandtotalImport()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/grandtotal_en.csv',
'--importer' => 'grandtotal',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 1 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 1 customers, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 1 customer', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testGrandtotalImportGerman()
{
$command = $this->application->find('kimai:import:customer');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/grandtotal_de.csv',
'--importer' => 'grandtotal',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 2 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 2 customers, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 1 customer', $result);
self::assertStringContainsString('[OK] Updated 1 customer', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
}

View File

@@ -1,251 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* 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\ImportProjectCommand;
use App\Importer\ImporterService;
use App\Repository\TeamRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @group integration
*/
class ImportProjectCommandTest extends KernelTestCase
{
/**
* @var Application
*/
protected $application;
protected function setUp(): void
{
parent::setUp();
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$kernel->getContainer();
$importer = $container->get(ImporterService::class);
$teams = $this->createMock(TeamRepository::class);
/** @var UserRepository $users */
$users = $container->get(UserRepository::class);
$this->application->add(new ImportProjectCommand($importer, $teams, $users));
}
public function testCommandName()
{
$command = $this->application->find('kimai:import:project');
self::assertInstanceOf(ImportProjectCommand::class, $command);
}
public function testImportWithMissingFile()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/foo_bar.csv1'
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Kimai importer: Projects', $result);
self::assertStringContainsString('[ERROR] File not existing or not readable', $result);
self::assertStringContainsString('_data/foo_bar', $result);
self::assertEquals(2, $commandTester->getStatusCode());
}
public function testDefaultImportWithUnknownReader()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--reader' => 'fooo',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('[ERROR] Unknown import reader: fooo', $result);
self::assertEquals(1, $commandTester->getStatusCode());
}
public function testImportWithUnknownImporter()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/customers2.csv',
'--importer' => 'grandtotal',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('[ERROR] Unknown project importer: grandtotal', $result);
self::assertEquals(1, $commandTester->getStatusCode());
}
public function testDefaultImport()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects.csv',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 3 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 3 projects, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 2 projects', $result);
self::assertStringContainsString('[OK] Updated 1 projects', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testDefaultImportSkipUpdate()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects.csv',
'--no-update' => true,
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 3 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 3 projects, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 2 projects', $result);
self::assertStringContainsString('[OK] Skipped 1 existing projects', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testDefaultImportWithInvalidCustomerMapping()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects_invalid.csv',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 3 rows to process, converting now ...', $result);
self::assertStringContainsString('[ERROR] Invalid row 2: Customer mismatch for project', $result);
self::assertStringContainsString('[CAUTION] Not importing, previous 1 errors need to be fixed first.', $result);
self::assertEquals(3, $commandTester->getStatusCode());
}
public function testDefaultImportWithSemicolon()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects2.csv',
'--importer' => 'default',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 39 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 39 projects, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 39 projects', $result);
self::assertStringContainsString('[OK] Imported 10 customers', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testGrandtotalImportWithInvalidCsvFile()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects.csv',
'--reader' => 'csv-semicolon',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Invalid row 1: Missing customer name', $result);
self::assertStringContainsString('! [CAUTION] Not importing, previous 3 errors need to be fixed first.', $result);
self::assertEquals(3, $commandTester->getStatusCode());
}
public function testDefaultImportWithSemicolonAndTeamlead()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects2.csv',
'--importer' => 'default',
'--reader' => 'csv-semicolon',
'--teamlead' => 'clara_customer',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('Found 39 rows to process, converting now ...', $result);
self::assertStringContainsString('Converted 39 projects, importing into Kimai now ...', $result);
self::assertStringContainsString('[OK] Imported 39 projects', $result);
self::assertStringContainsString('[OK] Imported 10 customers', $result);
self::assertStringContainsString('[OK] Created 39 teams', $result);
self::assertEquals(0, $commandTester->getStatusCode());
}
public function testDefaultImportWithSemicolonAndMissingTeamlead()
{
$command = $this->application->find('kimai:import:project');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['no']);
$commandTester->execute([
'command' => $command->getName(),
'file' => __DIR__ . '/../Importer/_data/projects2.csv',
'--importer' => 'default',
'--reader' => 'csv-semicolon',
'--teamlead' => 'foobar',
]);
$result = $commandTester->getDisplay();
self::assertStringContainsString('You requested to create empty teams for each project', $result);
self::assertStringContainsString('Please create a user with the name (or email) foobar', $result);
self::assertEquals(3, $commandTester->getStatusCode());
}
}

View File

@@ -1,58 +0,0 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* 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\ImportTimesheetCommand;
use App\Configuration\SystemConfiguration;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\TagRepository;
use App\Repository\TimesheetRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @covers \App\Command\ImportTimesheetCommand
* @group integration
*/
class ImportTimesheetCommandTest extends KernelTestCase
{
/**
* @var Application
*/
protected $application;
protected function setUp(): void
{
parent::setUp();
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$customers = $this->createMock(CustomerRepository::class);
$projects = $this->createMock(ProjectRepository::class);
$activities = $this->createMock(ActivityRepository::class);
$users = $this->createMock(UserRepository::class);
$tagRepository = $this->createMock(TagRepository::class);
$timesheets = $this->createMock(TimesheetRepository::class);
$configuration = $this->createMock(SystemConfiguration::class);
$encoder = $this->createMock(UserPasswordEncoderInterface::class);
$this->application->add(new ImportTimesheetCommand($customers, $projects, $activities, $users, $tagRepository, $timesheets, $configuration, $encoder));
}
public function testCommandName()
{
$command = $this->application->find('kimai:import:timesheet');
self::assertInstanceOf(ImportTimesheetCommand::class, $command);
}
}

View File

@@ -32,7 +32,8 @@ class InstallCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
$this->application->add(new InstallCommand(
$container->get('doctrine')->getConnection()
$container->get('doctrine')->getConnection(),
$this->application->getKernel()->getEnvironment()
));
}

View File

@@ -12,7 +12,6 @@ namespace App\Tests\Command;
use App\Command\InvoiceCreateCommand;
use App\DataFixtures\UserFixtures;
use App\Entity\Customer;
use App\Entity\CustomerMeta;
use App\Entity\Project;
use App\Invoice\ServiceInvoice;
use App\Repository\CustomerRepository;
@@ -36,12 +35,9 @@ class InvoiceCreateCommandTest extends KernelTestCase
{
use KernelTestTrait;
/**
* @var Application
*/
protected $application;
protected Application $application;
private function clearInvoiceFiles()
private function clearInvoiceFiles(): void
{
$path = __DIR__ . '/../_data/invoices/';
@@ -65,7 +61,7 @@ class InvoiceCreateCommandTest extends KernelTestCase
$this->clearInvoiceFiles();
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$container = self::$container;
$container = self::getContainer();
$this->application->add(new InvoiceCreateCommand(
$container->get(ServiceInvoice::class),
@@ -94,7 +90,7 @@ class InvoiceCreateCommandTest extends KernelTestCase
* @param array $options
* @return CommandTester
*/
protected function createInvoice(array $options = [])
protected function createInvoice(array $options = []): CommandTester
{
$command = $this->application->find('kimai:invoice:create');
$commandTester = new CommandTester($command);
@@ -105,7 +101,7 @@ class InvoiceCreateCommandTest extends KernelTestCase
return $commandTester;
}
protected function assertCommandErrors(array $options = [], string $errorMessage = '')
protected function assertCommandErrors(array $options = [], string $errorMessage = ''): void
{
$commandTester = $this->createInvoice($options);
@@ -113,67 +109,62 @@ class InvoiceCreateCommandTest extends KernelTestCase
$this->assertStringContainsString('[ERROR] ' . $errorMessage, $output);
}
public function testCreateWithUnknownExportFilter()
public function testCreateWithUnknownExportFilter(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--exported' => 'foo'], 'Unknown "exported" filter given');
}
public function testCreateWithMissingUser()
public function testCreateWithMissingUser(): void
{
$this->assertCommandErrors([], 'You must set a "user" to create invoices');
}
public function testCreateWithInvalidUser()
public function testCreateWithInvalidUser(): void
{
$this->assertCommandErrors(['--user' => 'assdfd'], 'The given username "assdfd" could not be resolved');
}
public function testCreateWithMissingEnd()
public function testCreateWithMissingEnd(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--start' => '2020-01-01'], 'You need to supply a end date if a start date was given');
}
public function testCreateByCustomerAndByProject()
public function testCreateByCustomerAndByProject(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--by-customer' => null, '--by-project' => null], 'You cannot mix "by-customer" and "by-project"');
}
public function testCreateWithMissingGenerationMode()
public function testCreateWithMissingGenerationMode(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN], 'Could not determine generation mode');
}
public function testCreateWithMissingTemplate()
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => 1], 'You must either pass the "template" or "template-meta" option');
}
public function testCreateWithInvalidStart()
public function testCreateWithInvalidStart(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => 1, '--exported' => 'exported', '--template' => 'x', '--start' => 'öäüß', '--end' => '2020-01-01'], 'Invalid start date given');
}
public function testCreateWithInvalidEnd()
public function testCreateWithInvalidEnd(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => 1, '--template' => 'x', '--start' => '2020-01-01', '--end' => 'öäüß'], 'Invalid end date given');
}
public function testCreateWithInvalidPreviewDirectory()
public function testCreateWithInvalidPreviewDirectory(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => 1, '--template' => 'x', '--start' => '2020-01-01', '--end' => '2020-01-02', '--preview' => '/kjhg/'], 'Invalid preview directory given');
}
public function testCreateWithInvalidCustomer()
public function testCreateWithInvalidCustomer(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => 3, '--template' => 'x'], 'Unknown customer ID: 3');
}
public function testCreateWithInvalidProject()
public function testCreateWithInvalidProject(): void
{
$this->assertCommandErrors(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--project' => 3, '--template' => 'x'], 'Unknown project ID: 3');
}
public function testCreateInvoice()
public function testCreateInvoice(): void
{
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
@@ -190,15 +181,19 @@ class InvoiceCreateCommandTest extends KernelTestCase
$this->assertStringContainsString('/tests/_data/invoices/' . ((new \DateTime())->format('Y')) . '-001-Test.html |', $output);
}
protected function prepareFixtures(\DateTime $start)
/**
* @param \DateTime $start
* @return array<Customer>
*/
protected function prepareFixtures(\DateTime $start): array
{
$fixture = new InvoiceTemplateFixtures();
$invoiceTemplate = $this->importFixture($fixture);
$fixture = new CustomerFixtures();
$fixture->setAmount(1);
$fixture->setCallback(function (Customer $customer) {
$meta = new CustomerMeta();
$meta->setName('template');
$meta->setValue('Invoice');
$customer->setMetaField($meta);
$fixture->setCallback(function (Customer $customer) use ($invoiceTemplate) {
$customer->setInvoiceTemplate($invoiceTemplate[0]);
});
$customer = $this->importFixture($fixture)[0];
@@ -214,26 +209,23 @@ class InvoiceCreateCommandTest extends KernelTestCase
$fixture->setProjects($projects);
$this->importFixture($fixture);
$fixture = new InvoiceTemplateFixtures();
$this->importFixture($fixture);
return [$customer];
}
public function testCreateInvoiceByCustomer()
public function testCreateInvoiceByCustomer(): void
{
$start = new \DateTime('-2 months');
$end = new \DateTime();
$this->prepareFixtures($start);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--by-customer' => null, '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--by-customer' => null, '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Created 1 invoice(s) ', $output);
}
public function testCreateInvoiceByCustomerId()
public function testCreateInvoiceByCustomerId(): void
{
$start = new \DateTime('-2 months');
$end = new \DateTime();
@@ -241,26 +233,26 @@ class InvoiceCreateCommandTest extends KernelTestCase
$imports = $this->prepareFixtures($start);
$customer = $imports[0]->getId();
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => $customer . ',1', '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--customer' => $customer . ',1', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Created 1 invoice(s) ', $output);
}
public function testCreateInvoiceByProject()
public function testCreateInvoiceByProject(): void
{
$start = new \DateTime('-2 months');
$end = new \DateTime();
$this->prepareFixtures($start);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--exported' => 'all', '--by-project' => null, '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--exported' => 'all', '--by-project' => null, '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Created 1 invoice(s) ', $output);
}
public function testCreateInvoiceByProjectId()
public function testCreateInvoiceByProjectId(): void
{
$start = new \DateTime('-2 months');
$end = new \DateTime();
@@ -273,14 +265,14 @@ class InvoiceCreateCommandTest extends KernelTestCase
$this->assertStringContainsString('Created 1 invoice(s) ', $output);
}
public function testCreateInvoiceByProjectWithPreview()
public function testCreateInvoiceByProjectWithPreview(): void
{
$start = new \DateTime('-2 months');
$end = new \DateTime();
$this->prepareFixtures($start);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--exported' => 'all', '--preview' => sys_get_temp_dir(), '--by-project' => null, '--template-meta' => 'template', '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$commandTester = $this->createInvoice(['--user' => UserFixtures::USERNAME_SUPER_ADMIN, '--exported' => 'all', '--preview' => sys_get_temp_dir(), '--by-project' => null, '--start' => $start->format('Y-m-d'), '--end' => $end->format('Y-m-d')]);
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Created 1 invoice(s) ', $output);

View File

@@ -13,7 +13,7 @@ use App\Command\KimaiImporterCommand;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
@@ -33,7 +33,7 @@ class KimaiImporterCommandTest extends KernelTestCase
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$encoder = $this->createMock(UserPasswordEncoderInterface::class);
$encoder = $this->createMock(UserPasswordHasherInterface::class);
$registry = $this->createMock(ManagerRegistry::class);
$validator = $this->createMock(ValidatorInterface::class);

View File

@@ -30,19 +30,14 @@ class PluginCommandTest extends KernelTestCase
public function testWithPlugins()
{
$plugin1 = $this->getMockBuilder(PluginInterface::class)->onlyMethods(['getName', 'getPath'])->getMock();
$plugin1->expects($this->any())->method('getName')->willReturn('Test-Bundle');
$plugin1->expects($this->once())->method('getPath')->willReturn(__DIR__);
$plugin1->expects($this->any())->method('getName')->willReturn('TestBundle');
$plugin1->expects($this->once())->method('getPath')->willReturn(__DIR__ . '/../Plugin/Fixtures/TestPlugin');
$plugin2 = $this->getMockBuilder(PluginInterface::class)->onlyMethods(['getName', 'getPath'])->getMock();
$plugin2->expects($this->any())->method('getName')->willReturn('Another one');
$plugin2->expects($this->once())->method('getPath')->willReturn('BundleDirectory');
$commandTester = $this->getCommandTester([$plugin1, $plugin2], []);
$commandTester = $this->getCommandTester([$plugin1], []);
$output = $commandTester->getDisplay();
$this->assertStringContainsString(__DIR__, $output);
$this->assertStringContainsString('BundleDirectory', $output);
$this->assertStringContainsString('Test-Bundle', $output);
$this->assertStringContainsString('Another one', $output);
$this->assertStringContainsString('Plugin/Fixtures/TestPlugin', $output);
$this->assertStringContainsString('TestPlugin from composer.json', $output);
}
protected function getCommandTester(array $plugins, array $options = [])

View File

@@ -48,10 +48,6 @@ class PromoteUserCommandTest extends KernelTestCase
$command = $application->find('kimai:user:promote');
self::assertInstanceOf(PromoteUserCommand::class, $command);
// test alias
$command = $application->find('fos:user:promote');
self::assertInstanceOf(PromoteUserCommand::class, $command);
}
protected function callCommand(?string $username, ?string $role, bool $super = false)
@@ -89,7 +85,7 @@ class PromoteUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
$user = $userRepository->loadUserByIdentifier('john_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->hasTeamleadRole());
}
@@ -104,7 +100,7 @@ class PromoteUserCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get('doctrine')->getRepository(User::class);
$user = $userRepository->loadUserByUsername('john_user');
$user = $userRepository->loadUserByIdentifier('john_user');
self::assertInstanceOf(User::class, $user);
self::assertTrue($user->isSuperAdmin());
}

View File

@@ -29,7 +29,10 @@ class ReloadCommandTest extends KernelTestCase
parent::setUp();
$kernel = self::bootKernel();
$this->application = new Application($kernel);
$this->application->add(new ReloadCommand());
$this->application->add(new ReloadCommand(
$this->application->getKernel()->getProjectDir(),
$this->application->getKernel()->getEnvironment()
));
}
public function testCommandName()

View File

@@ -23,19 +23,16 @@ class ResetDevelopmentCommandTest extends KernelTestCase
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$application->add(new ResetDevelopmentCommand());
$application->add(new ResetDevelopmentCommand('dev'));
self::assertTrue($application->has('kimai:reset-dev'));
$command = $application->find('kimai:reset-dev');
self::assertTrue($application->has('kimai:reset:dev'));
$command = $application->find('kimai:reset:dev');
self::assertInstanceOf(ResetDevelopmentCommand::class, $command);
}
public function testCommandNameIsNotEnabledInProd()
{
$kernel = self::bootKernel(['environment' => 'prod']);
$application = new Application($kernel);
$application->add(new ResetDevelopmentCommand());
self::assertFalse($application->has('kimai:reset-dev'));
$sut = new ResetDevelopmentCommand('prod');
self::assertFalse($sut->isEnabled());
}
}

View File

@@ -24,19 +24,16 @@ class ResetTestCommandTest extends KernelTestCase
{
$kernel = self::bootKernel();
$application = new Application($kernel);
$application->add(new ResetTestCommand($this->createMock(EntityManagerInterface::class)));
$application->add(new ResetTestCommand($this->createMock(EntityManagerInterface::class), 'test'));
self::assertTrue($application->has('kimai:reset-test'));
$command = $application->find('kimai:reset-test');
self::assertTrue($application->has('kimai:reset:test'));
$command = $application->find('kimai:reset:test');
self::assertInstanceOf(ResetTestCommand::class, $command);
}
public function testCommandNameIsNotEnabledInProd()
{
$kernel = self::bootKernel(['environment' => 'prod']);
$application = new Application($kernel);
$application->add(new ResetTestCommand($this->createMock(EntityManagerInterface::class)));
self::assertFalse($application->has('kimai:reset-test'));
$sut = new ResetTestCommand($this->createMock(EntityManagerInterface::class), 'prod');
self::assertFalse($sut->isEnabled());
}
}

View File

@@ -34,7 +34,8 @@ class UpdateCommandTest extends KernelTestCase
$container = self::$kernel->getContainer();
$this->application->add(new UpdateCommand(
$container->get('doctrine')->getConnection()
$container->get('doctrine')->getConnection(),
$this->application->getKernel()->getEnvironment()
));
return $this->application->find('kimai:update');

View File

@@ -48,13 +48,9 @@ class VersionCommandTest extends KernelTestCase
public function getTestData()
{
return [
[[], 'Kimai ' . Constants::VERSION . ' by Kevin Papst and contributors.'],
[[], 'Kimai ' . Constants::VERSION . ' by Kevin Papst.'],
[['--short' => true], Constants::VERSION],
[['--number' => true], Constants::VERSION_ID],
// @deprecated since 1.14.1
[['--name' => true], Constants::NAME],
[['--candidate' => true], Constants::STATUS],
[['--semver' => true], Constants::VERSION . '-' . Constants::STATUS],
];
}