Skip to content

Commit b64bb75

Browse files
committed
Découpage du context Behat
La classe de context commence à devenir un peu difficile à gérer avec les ajouts récents de phrases. La recommandation officielle est de découper la classe avec des traits.
1 parent ffd10c1 commit b64bb75

File tree

6 files changed

+278
-336
lines changed

6 files changed

+278
-336
lines changed

behat.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ default:
66
paths:
77
- '%paths.base%/tests/behat'
88
contexts:
9-
- FeatureContext
9+
- Afup\Tests\Behat\Bootstrap\FeatureContext
1010
- Behat\MinkExtension\Context\MinkContext
1111
extensions:
1212
Behat\MinkExtension:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"Afup\\Site\\Tests\\": "tests/unit/Afup/",
118118
"Afup\\Tests\\Support\\": "tests/support/",
119119
"Afup\\Tests\\Stubs\\PHPStan\\": "tests/stubs/phpstan/",
120+
"Afup\\Tests\\Behat\\Bootstrap\\": "tests/behat/bootstrap/",
120121
"AppBundle\\Tests\\": "tests/unit/AppBundle/",
121122
"AppBundle\\IntegrationTests\\": "tests/integration/AppBundle/",
122123
"PlanetePHP\\IntegrationTests\\": "tests/integration/PlanetePHP/"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Afup\Tests\Behat\Bootstrap;
6+
7+
use Behat\Step\Then;
8+
use PHPUnit\Framework\Assert;
9+
use Symfony\Component\JsonPath\JsonCrawler;
10+
11+
trait ApiContext
12+
{
13+
#[Then('/^the response should be in json$/')]
14+
public function assertResponseShouldBeInJson(): void
15+
{
16+
$this->assertResponseHeaderEquals('Content-Type', 'application/json');
17+
Assert::assertJson($this->minkContext->getSession()->getPage()->getContent());
18+
}
19+
20+
#[Then('/^the json response has the key "(?P<key>[^"]*)" with value "(?P<value>(?:[^"]|\\")*)"$/')]
21+
public function assertResponseHasJsonKeyAndValue(string $key, string $value): void
22+
{
23+
$crawler = new JsonCrawler($this->minkContext->getSession()->getPage()->getContent());
24+
25+
$foundValue = $crawler->find($key);
26+
27+
Assert::assertCount(1, $foundValue);
28+
Assert::assertSame($value, $foundValue[0]);
29+
}
30+
31+
#[Then('/^the json response has no key "(?P<key>[^"]*)"$/')]
32+
public function assertResponseHasNoJsonKey(string $key): void
33+
{
34+
$crawler = new JsonCrawler($this->minkContext->getSession()->getPage()->getContent());
35+
36+
$foundValue = $crawler->find($key);
37+
38+
Assert::assertEmpty($foundValue);
39+
}
40+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)