|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Afup\Tests\Behat\Bootstrap; |
| 6 | + |
| 7 | +use AppBundle\Event\Model\Event; |
| 8 | +use Behat\Gherkin\Node\PyStringNode; |
| 9 | +use Behat\Gherkin\Node\TableNode; |
| 10 | +use Behat\Hook\BeforeScenario; |
| 11 | +use Behat\Mink\Exception\ExpectationException; |
| 12 | +use Behat\Step\Then; |
| 13 | +use Symfony\Component\Filesystem\Filesystem; |
| 14 | + |
| 15 | +trait EmailContext |
| 16 | +{ |
| 17 | + private const MAILCATCHER_URL = 'http://mailcatcher:1080'; |
| 18 | + |
| 19 | + #[BeforeScenario('@clearAllMailInscriptionAttachments')] |
| 20 | + public function beforeScenarioClearAllMailInscriptionAttachments(): void |
| 21 | + { |
| 22 | + $filesystem = new Filesystem(); |
| 23 | + $filesystem->remove(Event::getInscriptionAttachmentDir()); |
| 24 | + } |
| 25 | + |
| 26 | + #[BeforeScenario('@clearAllSponsorFiles')] |
| 27 | + public function beforeScenarioClearAllSponsorFiles(): void |
| 28 | + { |
| 29 | + $filesystem = new Filesystem(); |
| 30 | + $filesystem->remove(Event::getSponsorFileDir()); |
| 31 | + } |
| 32 | + |
| 33 | + #[BeforeScenario('@clearEmails')] |
| 34 | + public function clearEmails(): void |
| 35 | + { |
| 36 | + $ch = curl_init(); |
| 37 | + |
| 38 | + curl_setopt($ch, CURLOPT_URL, self::MAILCATCHER_URL . '/messages'); |
| 39 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 40 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
| 41 | + |
| 42 | + curl_exec($ch); |
| 43 | + if (curl_errno($ch) !== 0) { |
| 44 | + throw new \RuntimeException('Error : ' . curl_error($ch)); |
| 45 | + } |
| 46 | + |
| 47 | + curl_close($ch); |
| 48 | + } |
| 49 | + |
| 50 | + #[Then('I should only receive the following emails:')] |
| 51 | + public function theFollowingEmailsShouldBeReceived(TableNode $expectedEmails): void |
| 52 | + { |
| 53 | + $expectedEmailsArray = []; |
| 54 | + foreach ($expectedEmails as $expectedEmail) { |
| 55 | + $expectedEmailsArray[] = [ |
| 56 | + 'to' => $expectedEmail['to'], |
| 57 | + 'subject' => $expectedEmail['subject'], |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + $content = file_get_contents(self::MAILCATCHER_URL . '/messages'); |
| 63 | + $decodedContent = json_decode($content, true); |
| 64 | + |
| 65 | + $foundEmails = []; |
| 66 | + foreach ($decodedContent as $mail) { |
| 67 | + $foundEmails[] = [ |
| 68 | + 'to' => implode(',', $mail['recipients']), |
| 69 | + 'subject' => $mail['subject'], |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + if ($foundEmails !== $expectedEmailsArray) { |
| 74 | + throw new ExpectationException( |
| 75 | + sprintf( |
| 76 | + 'The emails are not the expected ones "%s" (expected "%s")', |
| 77 | + var_export($foundEmails, true), |
| 78 | + var_export($expectedEmailsArray, true), |
| 79 | + ), |
| 80 | + $this->minkContext->getSession()->getDriver(), |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + #[Then('the checksum of the attachment :filename of the message of id :id should be :md5sum')] |
| 86 | + public function theChecksumOfTheAttachmentOfTheMessageOfIdShouldBe(string $filename, string $id, string $md5sum): void |
| 87 | + { |
| 88 | + $infos = json_decode(file_get_contents(self::MAILCATCHER_URL . '/messages/' . $id . '.json'), true); |
| 89 | + |
| 90 | + $cid = null; |
| 91 | + foreach ($infos['attachments'] as $attachment) { |
| 92 | + if ($attachment['filename'] === $filename) { |
| 93 | + $cid = $attachment['cid']; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (null === $cid) { |
| 98 | + throw new ExpectationException( |
| 99 | + sprintf('Attachment with name %s not found', $filename), |
| 100 | + $this->minkContext->getSession()->getDriver(), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + $attachmentContent = file_get_contents(self::MAILCATCHER_URL . '/messages/' . $id . '/parts/' . $cid); |
| 105 | + $actualMd5sum = md5($attachmentContent); |
| 106 | + |
| 107 | + if ($actualMd5sum !== $md5sum) { |
| 108 | + throw new ExpectationException( |
| 109 | + sprintf('The md5sum of %s, if not %s (found %s)', $filename, $md5sum, $actualMd5sum), |
| 110 | + $this->minkContext->getSession()->getDriver(), |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #[Then('the plain text content of the message of id :id should be :')] |
| 116 | + public function thePlainTextContentOfTheMessageOfIdShouldBe(string $id, PyStringNode $expectedContent): void |
| 117 | + { |
| 118 | + $content = file_get_contents(self::MAILCATCHER_URL . '/messages/' . $id . '.plain'); |
| 119 | + $expectedContentString = $expectedContent->getRaw(); |
| 120 | + |
| 121 | + $content = str_replace("\r\n", "\n", $content); |
| 122 | + |
| 123 | + if ($content !== $expectedContentString) { |
| 124 | + throw new ExpectationException( |
| 125 | + sprintf( |
| 126 | + "The content \n%s\nis not the expected one \n%s\n", |
| 127 | + var_export($content, true), |
| 128 | + var_export($expectedContentString, true), |
| 129 | + ), |
| 130 | + $this->minkContext->getSession()->getDriver(), |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + #[Then('I should receive an email')] |
| 136 | + public function iShouldReceiveAnEmail(): void |
| 137 | + { |
| 138 | + $content = file_get_contents(self::MAILCATCHER_URL . '/messages'); |
| 139 | + $emails = json_decode($content, true); |
| 140 | + |
| 141 | + if (count($emails) !== 1) { |
| 142 | + throw new ExpectationException( |
| 143 | + 'The email has not been received.', |
| 144 | + $this->minkContext->getSession()->getDriver(), |
| 145 | + ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + #[Then('the email should contain a full URL starting with :arg1')] |
| 150 | + public function theEmailShouldContainAFullUrlStartingWith(string $arg1): void |
| 151 | + { |
| 152 | + $content = file_get_contents(self::MAILCATCHER_URL . '/messages'); |
| 153 | + $decodedContent = json_decode($content, true); |
| 154 | + |
| 155 | + $foundEmails = []; |
| 156 | + foreach ($decodedContent as $mail) { |
| 157 | + $foundEmails[] = [ |
| 158 | + 'id' => $mail['id'], |
| 159 | + 'to' => $mail['to'] ?? $mail['recipients'][0], |
| 160 | + 'subject' => $mail['subject'], |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + if (count($foundEmails) !== 1) { |
| 165 | + throw new ExpectationException( |
| 166 | + 'The email has not been received.', |
| 167 | + $this->minkContext->getSession()->getDriver(), |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + $content = file_get_contents(self::MAILCATCHER_URL . '/messages/' . $foundEmails[0]['id'] . '.plain'); |
| 172 | + if (!str_contains($content, $arg1)) { |
| 173 | + throw new ExpectationException( |
| 174 | + sprintf( |
| 175 | + 'The email content does not contain the expected URL "%s" (expected "%s")', |
| 176 | + $content, |
| 177 | + $arg1, |
| 178 | + ), $this->minkContext->getSession()->getDriver(), |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments