add(new MailTestCommand($mailer, $inviteAddress)); return new CommandTester($application->find('davis:mail:test')); } public function testItSendsOneMessageFromTheConfiguredAddress(): void { $tester = $this->tester($mailer = $this->mailer()); $this->assertSame(Command::SUCCESS, $tester->execute(['to' => 'someone@example.org'])); $this->assertCount(1, $mailer->sent); $message = $mailer->sent[0]; $this->assertInstanceOf(Email::class, $message); $this->assertSame('no-reply@example.org', $message->getFrom()[0]->getAddress()); $this->assertSame('someone@example.org', $message->getTo()[0]->getAddress()); } public function testAnAddressThatIsNotOneIsReportedAsSuch(): void { $tester = $this->tester($mailer = $this->mailer()); $this->assertSame(Command::FAILURE, $tester->execute(['to' => 'not-an-address'])); $this->assertStringContainsString('not-an-address', $tester->getDisplay()); $this->assertSame([], $mailer->sent, 'Nothing should be handed to the transport'); } public function testATransportThatIsNotAnsweringIsReportedAsSuch(): void { $tester = $this->tester($this->mailer(new TransportException('Connection refused'))); $this->assertSame(Command::FAILURE, $tester->execute(['to' => 'someone@example.org'])); $display = $tester->getDisplay(); $this->assertStringContainsString('Connection refused', $display); $this->assertStringContainsString('MAILER_DSN', $display, 'It should point at the setting to check'); } /** * Without a sender address Davis sends no invitation at all, so testing the transport would * prove nothing. */ public function testItRefusesToRunWithoutASenderAddress(): void { $tester = $this->tester($mailer = $this->mailer(), null); $this->assertSame(Command::FAILURE, $tester->execute(['to' => 'someone@example.org'])); $this->assertStringContainsString('INVITE_FROM_ADDRESS', $tester->getDisplay()); $this->assertSame([], $mailer->sent); } }