application = new Application($kernel); $container = self::$kernel->getContainer(); $passwordEncoder = $container->get('security.password_encoder'); $this->application->add(new CreateUserCommand( $passwordEncoder, $container->get('doctrine'), $container->get('validator') )); } public function testCreateUser() { $commandTester = $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar'); $output = $commandTester->getDisplay(); $this->assertContains('[OK] Success! Created user: MyTestUser', $output); $container = self::$kernel->getContainer(); $user = $container->get('doctrine')->getRepository(User::class)->loadUserByUsername('MyTestUser'); $this->assertNotNull($user); } protected function createUser($username, $email, $role, $password) { $command = $this->application->find('kimai:create-user'); $commandTester = new CommandTester($command); $commandTester->execute([ 'command' => $command->getName(), 'username' => $username, 'email' => $email, 'role' => $role, 'password' => $password ]); return $commandTester; } public function testUserWithValidationProblem() { $commandTester = $this->createUser('xx', '', 'ROLE_USER', ''); $output = $commandTester->getDisplay(); $this->assertContains('[ERROR] email ()', $output); $this->assertContains('Please enter an email', $output); $this->assertContains('[ERROR] plainPassword ()', $output); $this->assertContains('Please enter a password', $output); } public function testUserAlreadyExisting() { $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar'); $commandTester = $this->createUser('MyTestUser', 'user@example.com', 'ROLE_USER', 'foobar'); $output = $commandTester->getDisplay(); $this->assertContains('[ERROR] username (mytestuser)', $output); $this->assertContains('The username is already used', $output); } public function testUserEmail() { $commandTester = $this->createUser('MyTestUser', 'ROLE_USER', 'ROLE_USER', 'foobar'); $output = $commandTester->getDisplay(); $this->assertContains('[ERROR] email (ROLE_USER)', $output); $this->assertContains('The email is not valid', $output); } }