forked from ddeboer/imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockabilityTest.php
90 lines (74 loc) · 2.66 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
<?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\TestCase;
/**
* @covers \Ddeboer\Imap\Test\RawMessageIterator
*/
final class MockabilityTest extends TestCase
{
public function testFullMockedBehaviour()
{
// Setup
$username = \uniqid('username_');
$password = \uniqid('password_');
$inboxName = \uniqid('INBOX_');
$attachmentFilename = \uniqid('filename_');
$attachmentMock = $this->createMock(AttachmentInterface::class);
$attachmentMock
->expects($this->once())
->method('getFilename')
->willReturn($attachmentFilename)
;
$messageMock = $this->createMock(MessageInterface::class);
$messageMock
->expects($this->once())
->method('getAttachments')
->willReturn([$attachmentMock])
;
$mailboxMock = $this->createMock(MailboxInterface::class);
$mailboxMock
->expects($this->once())
->method('getMessages')
->willReturn(new RawMessageIterator([$messageMock]))
;
$connectionMock = $this->createMock(ConnectionInterface::class);
$connectionMock
->expects($this->once())
->method('getMailbox')
->with($this->identicalTo($inboxName))
->willReturn($mailboxMock)
;
$serverMock = $this->createMock(ServerInterface::class);
$serverMock
->expects($this->once())
->method('authenticate')
->with(
$this->identicalTo($username),
$this->identicalTo($password)
)
->willReturn($connectionMock)
;
// Run
$connection = $serverMock->authenticate($username, $password);
$mailbox = $connection->getMailbox($inboxName);
$messages = $mailbox->getMessages();
$this->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
foreach ($messages as $message) {
break;
}
$attachments = $message->getAttachments();
$this->assertCount(1, $attachments);
$attachment = \current($attachments);
$this->assertSame($attachmentFilename, $attachment->getFilename());
}
}