Skip to content

Commit 9d79832

Browse files
authored
Merge pull request #63 from DirectoryTree/test-improvements
Testing improvements
2 parents f2c535e + 04de564 commit 9d79832

File tree

8 files changed

+155
-19
lines changed

8 files changed

+155
-19
lines changed

src/FileMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function __toString(): string
3131
return $this->contents;
3232
}
3333

34+
/**
35+
* Determine if this message is equal to another.
36+
*/
37+
public function is(MessageInterface $message): bool
38+
{
39+
return $message instanceof self
40+
&& $this->contents === $message->contents;
41+
}
42+
3443
/**
3544
* Determine if the message is empty.
3645
*/

src/Message.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public function hasBody(): bool
9090
return ! empty($this->body);
9191
}
9292

93+
/**
94+
* {@inheritDoc}
95+
*/
96+
public function is(MessageInterface $message): bool
97+
{
98+
return $message instanceof self
99+
&& $this->uid === $message->uid
100+
&& $this->head === $message->head
101+
&& $this->body === $message->body
102+
&& $this->folder->is($message->folder);
103+
}
104+
93105
/**
94106
* Determine if the message is marked as seen.
95107
*/

src/MessageInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public function header(string $name, int $offset = 0): ?IHeader;
119119
*/
120120
public function parse(): MailMimeMessage;
121121

122+
/**
123+
* Determine if the message is the same as another message.
124+
*/
125+
public function is(MessageInterface $message): bool;
126+
122127
/**
123128
* Get the string representation of the message.
124129
*/

src/Testing/FakeFolder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@ public function messages(): MessageQueryInterface
8383
// Ensure the folder is selected.
8484
$this->select(true);
8585

86-
return new FakeMessageQuery($this, $this->messages);
86+
return new FakeMessageQuery($this);
8787
}
8888

8989
/**
9090
* {@inheritDoc}
9191
*/
9292
public function idle(callable $callback, ?callable $query = null, int $timeout = 300): void
9393
{
94-
throw new Exception('Unsupported');
94+
foreach ($this->messages as $message) {
95+
$callback($message);
96+
}
9597
}
9698

9799
/**

src/Testing/FakeMessage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public function uid(): int
2626
return $this->uid;
2727
}
2828

29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function is(MessageInterface $message): bool
33+
{
34+
return $message instanceof self
35+
&& $this->uid === $message->uid
36+
&& $this->flags === $message->flags
37+
&& $this->contents === $message->contents;
38+
}
39+
2940
/**
3041
* {@inheritDoc}
3142
*/

tests/Unit/FileMessageTest.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Message-ID: <[email protected]>
2020
MIME-Version: 1.0
2121
Content-Type: text/plain; charset="UTF-8"
22-
22+
2323
Hello World
2424
EOT;
2525

@@ -47,15 +47,15 @@
4747
MIME-Version: 1.0
4848
Content-Type: multipart/alternative;
4949
boundary="----BOUNDARY-ID----"
50-
50+
5151
------BOUNDARY-ID----
5252
Content-Type: text/plain; charset="UTF-8"
53-
53+
5454
Hello Plain
55-
55+
5656
------BOUNDARY-ID----
5757
Content-Type: text/html; charset="UTF-8"
58-
58+
5959
<html><body><p>Hello <strong>HTML</strong></p></body></html>
6060
------BOUNDARY-ID------
6161
EOT;
@@ -77,17 +77,17 @@
7777
MIME-Version: 1.0
7878
Content-Type: multipart/mixed;
7979
boundary="----BOUNDARY-ID----"
80-
80+
8181
------BOUNDARY-ID----
8282
Content-Type: text/plain; charset="UTF-8"
83-
83+
8484
Hello with Attachment
85-
85+
8686
------BOUNDARY-ID----
8787
Content-Type: application/pdf; name="file.pdf"
8888
Content-Disposition: attachment; filename="file.pdf"
8989
Content-Transfer-Encoding: base64
90-
90+
9191
JVBERi0xLjUKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNCAgIC9GaWx0ZXIvQXNjaWlIYXgg
9292
ICAgPj5zdHJlYW0Kc3R1ZmYKZW5kc3RyZWFtCmVuZG9iajAK
9393
------BOUNDARY-ID------
@@ -114,7 +114,7 @@
114114
Subject: No Attachments
115115
Date: Wed, 19 Feb 2025 12:34:56 -0500
116116
Content-Type: text/plain; charset="UTF-8"
117-
117+
118118
Just a plain text email without attachments.
119119
EOT;
120120

@@ -132,7 +132,7 @@
132132
Subject: In-Reply-To Check
133133
Date: Wed, 19 Feb 2025 12:34:56 -0500
134134
Content-Type: text/plain; charset="UTF-8"
135-
135+
136136
Check the in-reply-to header
137137
EOT;
138138

@@ -149,7 +149,7 @@
149149
Subject: Stringable Test
150150
Date: Wed, 19 Feb 2025 12:34:56 -0500
151151
Content-Type: text/plain; charset="UTF-8"
152-
152+
153153
Testing __toString
154154
EOT;
155155

@@ -165,26 +165,26 @@
165165
Subject: Test Email With Inline Image
166166
MIME-Version: 1.0
167167
Content-Type: multipart/related; boundary="BOUNDARY_STRING"
168-
168+
169169
--BOUNDARY_STRING
170170
Content-Type: text/html; charset=UTF-8
171-
171+
172172
<html>
173173
<body>
174174
<p>This is a test email with an inline image:</p>
175175
<img src="cid:inline-image-id" alt="Inline Image" />
176176
</body>
177177
</html>
178-
178+
179179
--BOUNDARY_STRING
180180
Content-Type: image/png; name="inline_image.png"
181181
Content-Transfer-Encoding: base64
182182
Content-ID: <inline-image-id>
183183
Content-Disposition: inline; filename="inline_image.png"
184-
184+
185185
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8
186186
z8BQDwABAgEA0xzY2QAAAABJRU5ErkJggg==
187-
187+
188188
--BOUNDARY_STRING--
189189
EOT;
190190

@@ -197,3 +197,42 @@
197197
expect($attachments[0]->contentId())->toBe('inline-image-id');
198198
expect($attachments[0]->filename())->toBe('inline_image.png');
199199
});
200+
201+
test('it can determine if two messages are the same', function () {
202+
$contents1 = <<<'EOT'
203+
From: "John Doe" <[email protected]>
204+
Subject: Test Subject
205+
Date: Wed, 19 Feb 2025 12:34:56 -0500
206+
Content-Type: text/plain; charset="UTF-8"
207+
208+
Test content
209+
EOT;
210+
211+
$contents2 = <<<'EOT'
212+
From: "John Doe" <[email protected]>
213+
Subject: Test Subject
214+
Date: Wed, 19 Feb 2025 12:34:56 -0500
215+
Content-Type: text/plain; charset="UTF-8"
216+
217+
Test content
218+
EOT;
219+
220+
$contents3 = <<<'EOT'
221+
From: "John Doe" <[email protected]>
222+
Subject: Different Subject
223+
Date: Wed, 19 Feb 2025 12:34:56 -0500
224+
Content-Type: text/plain; charset="UTF-8"
225+
226+
Different content
227+
EOT;
228+
229+
$message1 = new FileMessage($contents1);
230+
$message2 = new FileMessage($contents2);
231+
$message3 = new FileMessage($contents3);
232+
233+
// Same content
234+
expect($message1->is($message2))->toBeTrue();
235+
236+
// Different content
237+
expect($message1->is($message3))->toBeFalse();
238+
});

tests/Unit/MessageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,41 @@
107107
expect($message->flags())->not->toContain('\\Flagged');
108108
expect($message->hasFlag(ImapFlag::Flagged))->toBeFalse();
109109
});
110+
111+
test('it can determine if two messages are the same', function () {
112+
$mailbox = Mailbox::make([
113+
'username' => 'foo',
114+
'password' => 'bar',
115+
]);
116+
117+
$mailbox->connect(ImapConnection::fake([
118+
'* OK Welcome to IMAP',
119+
'TAG1 OK Logged in',
120+
]));
121+
122+
$folder1 = new Folder($mailbox, 'INBOX', [], '/');
123+
$folder2 = new Folder($mailbox, 'INBOX.Sent', [], '/');
124+
125+
// Create messages with different properties
126+
$message1 = new Message($folder1, 1, [], 'header1', 'body1');
127+
$message2 = new Message($folder1, 1, [], 'header1', 'body1'); // Same as message1
128+
$message3 = new Message($folder1, 2, [], 'header1', 'body1'); // Different UID
129+
$message4 = new Message($folder2, 1, [], 'header1', 'body1'); // Different folder
130+
$message5 = new Message($folder1, 1, [], 'header2', 'body1'); // Different header
131+
$message6 = new Message($folder1, 1, [], 'header1', 'body2'); // Different body
132+
133+
// Same message
134+
expect($message1->is($message2))->toBeTrue();
135+
136+
// Different UID
137+
expect($message1->is($message3))->toBeFalse();
138+
139+
// Different folder
140+
expect($message1->is($message4))->toBeFalse();
141+
142+
// Different header
143+
expect($message1->is($message5))->toBeFalse();
144+
145+
// Different body
146+
expect($message1->is($message6))->toBeFalse();
147+
});

tests/Unit/Testing/FakeMessageTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@
5353
expect($message->uid())->toBe(1);
5454
expect((string) $message)->toBe('');
5555
});
56+
57+
test('it can determine if two messages are the same', function () {
58+
$message1 = new FakeMessage(1, ['\\Seen'], 'Test content');
59+
$message2 = new FakeMessage(1, ['\\Seen'], 'Test content');
60+
$message3 = new FakeMessage(2, ['\\Seen'], 'Test content');
61+
$message4 = new FakeMessage(1, ['\\Draft'], 'Test content');
62+
$message5 = new FakeMessage(1, ['\\Seen'], 'Different content');
63+
64+
// Same messages
65+
expect($message1->is($message2))->toBeTrue();
66+
67+
// Different UID
68+
expect($message1->is($message3))->toBeFalse();
69+
70+
// Different flags
71+
expect($message1->is($message4))->toBeFalse();
72+
73+
// Different content
74+
expect($message1->is($message5))->toBeFalse();
75+
});

0 commit comments

Comments
 (0)