From 1e56a16998c9bb3ebead3ebd6079910c02373e29 Mon Sep 17 00:00:00 2001 From: "Gerardo J. Montivero" Date: Mon, 29 Nov 2021 20:16:20 -0300 Subject: [PATCH] Se inicia un incipiente test --- tests/Controller/SecurityControllerTest.php | 32 +++++++++++ tests/Service/MailerTest.php | 61 +++++++++++---------- 2 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 tests/Controller/SecurityControllerTest.php diff --git a/tests/Controller/SecurityControllerTest.php b/tests/Controller/SecurityControllerTest.php new file mode 100644 index 0000000..fb3e1ec --- /dev/null +++ b/tests/Controller/SecurityControllerTest.php @@ -0,0 +1,32 @@ +request('GET', '/register'); + + $this->assertResponseIsSuccessful(); + + $button = $crawler->selectButton('Register'); + $form = $button->form(); + $form['user_registration_form[firstName]']->setValue('Ryan'); + $form['user_registration_form[email]']->setValue(sprintf('foo%s@example.com', rand())); + $form['user_registration_form[plainPassword]']->setValue('space_rocks'); + $form['user_registration_form[agreeTerms]']->tick(); + $client->submit($form); + + $this->assertResponseRedirects(); + + /* Symfony 4.4: + $this->assertEmailCount(1); + $email = $this->getMailerMessage(0); + $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com'); + */ + } +} diff --git a/tests/Service/MailerTest.php b/tests/Service/MailerTest.php index cdf3162..51ada60 100644 --- a/tests/Service/MailerTest.php +++ b/tests/Service/MailerTest.php @@ -2,62 +2,63 @@ namespace App\Tests\Service; -use App\Entity\Reservante; -use App\Repository\InvitadoRepository; -use App\Repository\WaitingListRepository; -use App\Service\Handler\Metabase\HandlerMetabase; +use App\Entity\Celebracion; +use App\Entity\User; use App\Service\Mailer; +use Knp\Snappy\Pdf; use PHPUnit\Framework\TestCase; -use Symfony\Component\Mailer\Exception\TransportExceptionInterface; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\NamedAddress; use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; use Twig\Environment; -class MailerTest extends TestCase +class MailerTest extends KernelTestCase { - /** - * @throws TransportExceptionInterface - */ public function testSendWelcomeMessage() { $symfonyMailer = $this->createMock(MailerInterface::class); $symfonyMailer->expects($this->once()) ->method('send'); -// $pdf = $this->createMock(Pdf::class); + + $pdf = $this->createMock(Pdf::class); $twig = $this->createMock(Environment::class); $entrypointLookup = $this->createMock(EntrypointLookupInterface::class); - $waitingListRepository = $this->createMock(WaitingListRepository::class); - $repository = $this->createMock(InvitadoRepository::class); - $handlerMetabase = $this->createMock(HandlerMetabase::class); - $reservante = new Reservante(); + $user = new User(); + $user->setPrimerNombre('Victor'); + $user->setEmail('victor@symfonycasts.com'); - $reservante->setEmail('uno@dos.com'); - $reservante->setNombre('uno'); + $mailer = new Mailer($symfonyMailer, $twig, $pdf, $entrypointLookup); + $email = $mailer->sendWelcomeMessage($user); - $mailer = new Mailer($symfonyMailer, $twig, $waitingListRepository, $repository,$handlerMetabase); - $mailer->sendReservaMessageTest($reservante); -// $this->assertTrue(true); + $this->assertSame('Welcome to the Space Bar!', $email->getSubject()); + $this->assertCount(1, $email->getTo()); + /** @var NamedAddress[] $namedAddresses */ + $namedAddresses = $email->getTo(); + $this->assertInstanceOf(NamedAddress::class, $namedAddresses[0]); + $this->assertSame('Victor', $namedAddresses[0]->getName()); + $this->assertSame('victor@symfonycasts.com', $namedAddresses[0]->getAddress()); } public function testIntegrationSendAuthorWeeklyReportMessage() { + self::bootKernel(); $symfonyMailer = $this->createMock(MailerInterface::class); $symfonyMailer->expects($this->once()) ->method('send'); -// $pdf = $this->createMock(Pdf::class); - $twig = $this->createMock(Environment::class); + $pdf = self::$container->get(Pdf::class); + $twig = self::$container->get(Environment::class); $entrypointLookup = $this->createMock(EntrypointLookupInterface::class); - $waitingListRepository = $this->createMock(WaitingListRepository::class); - $repository = $this->createMock(InvitadoRepository::class); - $handlerMetabase = $this->createMock(HandlerMetabase::class); - - $reservante = new Reservante(); - $reservante->setEmail('uno@dos.com'); - $reservante->setNombre('uno'); + $user = new User(); + $user->setFirstName('Victor'); + $user->setEmail('victor@symfonycasts.com'); + $article = new Article(); + $article->setTitle('Black Holes: Ultimate Party Pooper'); - $mailer = new Mailer($symfonyMailer, $twig, $waitingListRepository, $repository,$handlerMetabase); - $mailer->sendReservaMessageTest($reservante); + $mailer = new Mailer($symfonyMailer, $twig, $pdf, $entrypointLookup); + $email = $mailer->sendAuthorWeeklyReportMessage($user, [$article]); + $this->assertCount(1, $email->getAttachments()); } }