getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $entityManager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); $injectableFactory = $this->createMock(InjectableFactory::class); $transportFactory = $this->getMockBuilder(SmtpTransportFactory::class)->disableOriginalConstructor()->getMock(); $this->transport = $this->getMockBuilder(SmtpTransport::class)->disableOriginalConstructor()->getMock(); $log = $this->createMock(Log::class); $emailSender = new EmailSender( $config, $entityManager, $injectableFactory, $transportFactory, $log ); $transportFactory ->expects($this->any()) ->method('create') ->willReturn($this->transport); $this->emailSender = $emailSender; $this->config = $config; $config ->expects($this->any()) ->method('get') ->will( $this->returnValueMap([ ['outboundEmailFromAddress', null, null], ['smtpServer', null, 'test-server'], ['smtpPort', null, '85'], ]) ); $entityManager ->expects($this->any()) ->method('getRepository') ->will( $this->returnValueMap([ ['outboundEmailFromAddress', null, 'test@from.com'], ['smtpServer', null, 'test-server'], ]) ); $inboundEmailService = $this->createMock(InboundEmailService::class); $injectableFactory ->expects($this->any()) ->method('create') ->will( $this->returnValueMap([ [InboundEmailService::class, $inboundEmailService], ]) ); } protected function createEmail(array $data) : Email { $email = $this->getMockBuilder(Email::class)->disableOriginalConstructor()->getMock(); $email ->expects($this->any()) ->method('get') ->will( $this->returnCallback( function ($name) use ($data) { return $data[$name] ?? null; } ) ); $email ->expects($this->any()) ->method('getBodyPlainForSending') ->willReturn('test'); $email ->expects($this->any()) ->method('isNew') ->willReturn(true); return $email; } public function testSend1() { $email = $this->createEmail([ 'name' => 'test', 'from' => 'test@tester.com', ]); $this->transport ->expects($this->once()) ->method('send'); $this->emailSender->send($email); } }