Skip to content

Commit 8a7b12b

Browse files
committed
Create module class.
1 parent 3fefc16 commit 8a7b12b

File tree

5 files changed

+323
-2
lines changed

5 files changed

+323
-2
lines changed

codeception.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
settings:
9+
bootstrap: _bootstrap.php
10+
colors: false
11+
memory_limit: 1024M
12+
extensions:
13+
enabled:
14+
- Codeception\Extension\RunFailed
15+
modules:
16+
config:
17+
Db:
18+
dsn: ''
19+
user: ''
20+
password: ''
21+
dump: tests/_data/dump.sql

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"codeception/codeception": "2.*"
1919
},
2020
"require": {
21-
"guzzle/guzzle": "^3.9"
21+
"guzzle/guzzle": "^3.9",
22+
"rezouce/mailcatcher": "^1.0"
2223
}
2324
}

src/MailCatcher.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
namespace Codeception\Module;
3+
4+
use Codeception\Module;
5+
use Codeception\TestCase;
6+
use Guzzle\Http\Client;
7+
use MailCatcher\Mail;
8+
use MailCatcher\MailCatcherAdapter;
9+
use MailCatcher\MailCollection;
10+
11+
class MailCatcher extends Module
12+
{
13+
14+
private $url = '127.0.0.1';
15+
16+
private $port = '1080';
17+
18+
private $resetBeforeEachTest = false;
19+
20+
private $mailCatcher;
21+
22+
private $emails;
23+
24+
/**
25+
* Initialize from configuration.
26+
*/
27+
public function _initialize()
28+
{
29+
if (isset($this->config['url'])) {
30+
$this->url = trim($this->config['url'], '/');
31+
}
32+
33+
if (isset($this->config['port'])) {
34+
$this->port = $this->config['port'];
35+
}
36+
37+
if (isset($this->config['resetBeforeEachTest'])) {
38+
$this->resetBeforeEachTest = $this->config['resetBeforeEachTest'];
39+
}
40+
}
41+
42+
/**
43+
* Drop the previous email from MailCatcher if not disabled by configuration.
44+
*
45+
* @param TestCase $test
46+
*/
47+
public function _before(TestCase $test)
48+
{
49+
if ($this->resetBeforeEachTest) {
50+
$this->resetEmails();
51+
}
52+
}
53+
54+
/**
55+
* Guzzle client to talk with MailCatcher.
56+
*
57+
* @return \MailCatcher\MailCatcher
58+
*/
59+
private function mailCatcher()
60+
{
61+
if (null === $this->mailCatcher) {
62+
$this->mailCatcher = new \MailCatcher\MailCatcher(
63+
new MailCatcherAdapter(new Client, $this->url . ':' . $this->port)
64+
);
65+
}
66+
67+
return $this->mailCatcher;
68+
}
69+
70+
/**
71+
* Get all messages from MailCatcher.
72+
*
73+
* @return MailCollection
74+
*/
75+
private function emails()
76+
{
77+
if (null === $this->emails) {
78+
$this->emails = $this->mailCatcher()->messages();
79+
}
80+
81+
return $this->emails;
82+
}
83+
84+
/**
85+
* Remove all emails in MailCatcher.
86+
*/
87+
public function removeEmails()
88+
{
89+
$this->mailCatcher()->removeMessages();
90+
}
91+
92+
/**
93+
* Assert that there is at least one email.
94+
* @param MailCollection $emails
95+
*/
96+
public function hasEmails(MailCollection $emails = null)
97+
{
98+
if (null === $emails) {
99+
$emails = $this->emails();
100+
}
101+
102+
$this->assertNotEmpty($emails->count(), 'Has an email');
103+
}
104+
105+
/**
106+
* Assert the number of emails sent.
107+
*
108+
* @param int $count
109+
* @param MailCollection $emails
110+
*/
111+
public function seeNumberEmails($count, MailCollection $emails = null)
112+
{
113+
if (null === $emails) {
114+
$emails = $this->emails();
115+
}
116+
117+
$this->assertEquals($count, $emails->count(), 'Number of emails');
118+
}
119+
120+
/**
121+
* Assert that at least an email contains some text.
122+
*
123+
* @param string $expected
124+
* @param MailCollection $emails
125+
*/
126+
public function seeInEmail($expected, MailCollection $emails = null)
127+
{
128+
if (null === $emails) {
129+
$emails = $this->emails();
130+
}
131+
132+
$emails = $emails->filter(function(Mail $mail) use ($expected) {
133+
return false !== strstr($mail->source(), $expected);
134+
});
135+
136+
$this->assertNotEmpty($emails->count(), 'Email contains');
137+
}
138+
139+
/**
140+
* Get all emails with the given subject.
141+
*
142+
* @param string $subject
143+
* @param bool $strict
144+
* @return MailCollection
145+
*/
146+
public function getEmailsBySubject($subject, $strict = false)
147+
{
148+
return $this->emails()->filter(function(Mail $mail) use ($subject, $strict) {
149+
return $strict
150+
? $subject === $mail->subject()
151+
: false !== strstr($mail->subject(), $subject);
152+
});
153+
}
154+
155+
/**
156+
* Get all emails with the given sender.
157+
*
158+
* @param string $sender
159+
* @param bool $strict
160+
* @return MailCollection
161+
*/
162+
public function getEmailsBySender($sender, $strict = false)
163+
{
164+
return $this->emails()->filter(function(Mail $mail) use ($sender, $strict) {
165+
return $strict
166+
? $sender === $mail->sender()
167+
: false !== strstr($mail->sender(), $sender);
168+
});
169+
}
170+
171+
/**
172+
* Get all emails with the given recipients.
173+
*
174+
* @param array $recipients
175+
* @param bool $strict
176+
* @return MailCollection
177+
*/
178+
public function getEmailsByRecipients(array $recipients, $strict = false)
179+
{
180+
return $this->emails()->filter(function(Mail $mail) use ($recipients, $strict) {
181+
$found = count(array_intersect($recipients, $mail->recipients()));
182+
183+
return $strict
184+
? $found === count($recipients)
185+
: $found > 0;
186+
});
187+
}
188+
}

tests/functional.suite.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class_name: FunctionalTester
88
modules:
99
enabled:
1010
# add framework module here
11-
- \Helper\Functional
11+
- \Helper\Functional
12+
- MailCatcher

tests/functional/MailCatcherTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
4+
class MailCatcherTest extends \Codeception\TestCase\Test
5+
{
6+
/**
7+
* @var \FunctionalTester
8+
*/
9+
protected $tester;
10+
11+
protected function _before()
12+
{
13+
// $this->removeEmails();
14+
}
15+
16+
/** @test */
17+
public function it_count_the_emails()
18+
{
19+
mail('user@example.com', 'Subject', 'Body.');
20+
mail('user@example.com', 'Subject', 'Body.');
21+
mail('user@example.com', 'Subject', 'Body.');
22+
23+
$this->seeNumberEmails(3);
24+
}
25+
26+
/** @test */
27+
public function it_remove_the_emails()
28+
{
29+
mail('user@example.com', 'Subject', 'Body.');
30+
$this->seeNumberEmails(1);
31+
32+
$this->removeEmails();
33+
$this->seeNumberEmails(0);
34+
}
35+
36+
/** @test */
37+
public function it_has_emails()
38+
{
39+
mail('user@example.com', 'Subject', 'Body.');
40+
41+
$this->seeHasEmails();
42+
}
43+
44+
/** @test */
45+
public function it_has_emails_which_contain_a_stringin_its_source()
46+
{
47+
mail('user@example.com', 'Subject', 'Body.');
48+
49+
$this->seeInEmail('example');
50+
}
51+
52+
/** @test */
53+
public function it_retrieve_the_emails_by_their_subject()
54+
{
55+
mail('user@example.com', 'Subject 1', 'Body.');
56+
mail('user@example.com', 'Subject 2', 'Body.');
57+
mail('user@example.com', 'Other name', 'Body.');
58+
59+
$this->assertEquals(2, $this->getEmailsBySubject('Subject')->count());
60+
}
61+
62+
/** @test */
63+
public function it_retrieve_the_emails_by_their_subject_strictly()
64+
{
65+
mail('user@example.com', 'Subject 1', 'Body.');
66+
mail('user@example.com', 'Subject 2', 'Body.');
67+
mail('user@example.com', 'Subject', 'Body.');
68+
69+
$this->assertEquals(1, $this->getEmailsBySubject('Subject', true)->count());
70+
}
71+
72+
/** @test */
73+
public function it_retrieve_the_emails_by_their_sender()
74+
{
75+
mail('user@example.com', 'Subject', 'Body.', 'From: user@example.com');
76+
mail('user@example.com', 'Subject', 'Body.', 'From: user@example.com');
77+
mail('user@example.com', 'Subject', 'Body.', 'From: user@other.com');
78+
79+
$this->assertEquals(2, $this->getEmailsBySender('example.com')->count());
80+
}
81+
82+
/** @test */
83+
public function it_retrieve_the_emails_by_their_sender_strictly()
84+
{
85+
mail('user@example.com', 'Subject', 'Body.', 'From: user@example.coma');
86+
mail('user@example.com', 'Subject', 'Body.', 'From: user@example.comb');
87+
mail('user@example.com', 'Subject', 'Body.', 'From: user@example.com');
88+
89+
$this->assertEquals(1, $this->getEmailsBySender('user@example.com', true)->count());
90+
}
91+
92+
/** @test */
93+
public function it_retrieve_the_emails_by_their_recipients()
94+
{
95+
mail('user1@example.com, user2@example.com, user3@example.com', 'Subject', 'Body.');
96+
mail('user1@example.com, user4@example.com', 'Subject', 'Body.');
97+
mail('user4@example.com', 'Subject', 'Body.');
98+
99+
$this->assertEquals(2, $this->getEmailsByRecipients(['user1@example.com', 'user2@example.com'])->count());
100+
}
101+
102+
/** @test */
103+
public function it_retrieve_the_emails_by_their_recipients_strictly()
104+
{
105+
mail('user1@example.com, user2@example.com, user3@example.com', 'Subject', 'Body.');
106+
mail('user1@example.com, user2@example.com', 'Subject', 'Body.');
107+
108+
$this->assertEquals(1, $this->getEmailsByRecipients(['user1@example.com', 'user2@example.com'], true)->count());
109+
}
110+
}

0 commit comments

Comments
 (0)