-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
MockabilityTest.php
91 lines (76 loc) · 2.87 KB
/
MockabilityTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace Ddeboer\Imap\Tests;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\Message\AttachmentInterface;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\ServerInterface;
use Ddeboer\Imap\Test\RawMessageIterator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(RawMessageIterator::class)]
final class MockabilityTest extends TestCase
{
public function testFullMockedBehaviour(): void
{
// Setup
$username = \uniqid('username_');
$password = \uniqid('password_');
$inboxName = \uniqid('INBOX_');
$attachmentFilename = \uniqid('filename_');
$attachmentMock = $this->createMock(AttachmentInterface::class);
$attachmentMock
->expects(self::once())
->method('getFilename')
->willReturn($attachmentFilename)
;
$messageMock = $this->createMock(MessageInterface::class);
$messageMock
->expects(self::once())
->method('getAttachments')
->willReturn([$attachmentMock])
;
$mailboxMock = $this->createMock(MailboxInterface::class);
$mailboxMock
->expects(self::once())
->method('getMessages')
->willReturn(new RawMessageIterator([$messageMock]))
;
$connectionMock = $this->createMock(ConnectionInterface::class);
$connectionMock
->expects(self::once())
->method('getMailbox')
->with(self::identicalTo($inboxName))
->willReturn($mailboxMock)
;
$serverMock = $this->createMock(ServerInterface::class);
$serverMock
->expects(self::once())
->method('authenticate')
->with(
self::identicalTo($username),
self::identicalTo($password)
)
->willReturn($connectionMock)
;
// Run
$connection = $serverMock->authenticate($username, $password);
$mailbox = $connection->getMailbox($inboxName);
$messages = $mailbox->getMessages();
self::assertCount(1, $messages);
// This foreach has the solely purpose to trigger code-coverage for
// RawMessageIterator::current() and prove RawMessageIterator is
// iterable. There is no need to do this in your app test suite
$loopedMessages = [];
foreach ($messages as $message) {
$loopedMessages[] = $message;
}
self::assertCount(1, $loopedMessages);
$foundMessage = \current($loopedMessages);
$attachments = $foundMessage->getAttachments();
self::assertCount(1, $attachments);
$attachment = \current($attachments);
self::assertSame($attachmentFilename, $attachment->getFilename());
}
}