userManager = static::createMock(IUserManager::class); $this->groupManager = static::createStub(IGroupManager::class); $this->mailer = static::createMock(IMailer::class); $this->appConfig = static::createMock(IAppConfig::class); $this->mailHelper = static::createMock(NewUserMailHelper::class); $this->eventDispatcher = static::createStub(IEventDispatcher::class); $this->secureRandom = static::createStub(ISecureRandom::class); $this->user = static::createMock(IUser::class); $this->consoleInput = static::createMock(InputInterface::class); $this->consoleOutput = static::createMock(OutputInterface::class); $this->addCommand = new Add( $this->userManager, $this->groupManager, $this->mailer, $this->appConfig, $this->mailHelper, $this->eventDispatcher, $this->secureRandom ); } /** * @dataProvider addEmailDataProvider */ public function testAddEmail( ?string $email, bool $isEmailValid, bool $shouldSendEmail, ): void { $this->user->expects($isEmailValid ? static::once() : static::never()) ->method('setSystemEMailAddress') ->with(static::equalTo($email)); $this->userManager->method('createUser') ->willReturn($this->user); $this->appConfig->method('getValueString') ->willReturn($shouldSendEmail ? 'yes' : 'no'); $this->mailer->method('validateMailAddress') ->willReturn($isEmailValid); $this->mailHelper->method('generateTemplate') ->willReturn(static::createMock(IEMailTemplate::class)); $this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never()) ->method('sendMail'); $this->consoleInput->method('getOption') ->will(static::returnValueMap([ ['generate-password', 'true'], ['email', $email], ['group', []], ])); $this->invokePrivate($this->addCommand, 'execute', [ $this->consoleInput, $this->consoleOutput ]); } /** * @return array */ public function addEmailDataProvider(): array { return [ 'Valid E-Mail' => [ 'info@example.com', true, true, ], 'Invalid E-Mail' => [ 'info@@example.com', false, false, ], 'No E-Mail' => [ '', false, false, ], 'Valid E-Mail, but no mail should be sent' => [ 'info@example.com', true, false, ], ]; } }