Skip to content

Commit cf675c2

Browse files
committed
test: add controller tests
Signed-off-by: Jana Peper <jana.peper@nextcloud.com>
1 parent 3c18a7a commit cf675c2

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace Unit\Controller;
11+
12+
use ChristophWurst\Nextcloud\Testing\TestCase;
13+
use OCA\Mail\Account;
14+
use OCA\Mail\Contracts\IMailManager;
15+
use OCA\Mail\Contracts\IMailSearch;
16+
use OCA\Mail\Controller\MailboxesApiController;
17+
use OCA\Mail\Db\MailAccount;
18+
use OCA\Mail\Db\Mailbox;
19+
use OCA\Mail\Db\Message as DbMessage;
20+
use OCA\Mail\Folder;
21+
use OCA\Mail\Service\AccountService;
22+
use OCP\AppFramework\Http;
23+
use OCP\IRequest;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
26+
class MailboxesApiControllerTest extends TestCase {
27+
private const USER_ID = 'user';
28+
29+
private MailboxesApiController $controller;
30+
31+
private IRequest&MockObject $request;
32+
private IMailManager|MockObject $mailManager;
33+
private AccountService&MockObject $accountService;
34+
private MockObject|IMailSearch $mailSearch;
35+
36+
protected function setUp(): void {
37+
parent::setUp();
38+
39+
$this->request = $this->createMock(IRequest::class);
40+
$this->accountService = $this->createMock(AccountService::class);
41+
$this->mailManager = $this->createMock(IMailManager::class);
42+
$this->mailSearch = $this->createMock(IMailSearch::class);
43+
44+
$this->controller = new MailboxesApiController(
45+
'mail',
46+
$this->request,
47+
self::USER_ID,
48+
$this->mailManager,
49+
$this->accountService,
50+
$this->mailSearch,
51+
52+
);
53+
}
54+
55+
public function testListMailboxesWithoutUser() {
56+
$controller = new MailboxesApiController(
57+
'mail',
58+
$this->request,
59+
null,
60+
$this->mailManager,
61+
$this->accountService,
62+
$this->mailSearch,
63+
);
64+
65+
$this->accountService->expects(self::never())
66+
->method('find');
67+
68+
$accountId = 28;
69+
70+
$actual = $controller->list($accountId);
71+
$this->assertEquals(Http::STATUS_NOT_FOUND, $actual->getStatus());
72+
}
73+
74+
75+
public function testListMailboxes() {
76+
$accountId = 42;
77+
$mailAccount = new MailAccount();
78+
$mailAccount->setId($accountId);
79+
$mailAccount->setEmail('foo@bar.com');
80+
81+
$account = new Account($mailAccount);
82+
$folder = $this->createMock(Folder::class);
83+
# $accountId = 28;
84+
$this->accountService->expects($this->once())
85+
->method('find')
86+
->with($this->equalTo(self::USER_ID), $this->equalTo($accountId))
87+
->willReturn($account);
88+
$this->mailManager->expects($this->once())
89+
->method('getMailboxes')
90+
->with($this->equalTo($account))
91+
->willReturn([
92+
$folder
93+
]);
94+
$folder->expects($this->once())
95+
->method('getDelimiter')
96+
->willReturn('.');
97+
98+
$actual = $this->controller->list($accountId);
99+
print($actual->getData());
100+
101+
$this->assertEquals(Http::STATUS_OK, $actual->getStatus());
102+
$this->assertEquals(json_encode([
103+
[
104+
'id' => 42,
105+
'email' => 'foo@bar.com',
106+
'mailboxes' => [
107+
$folder,
108+
],
109+
'delimiter' => '.',
110+
]
111+
]), $actual->getData());
112+
}
113+
114+
public function testListMessagesWithoutUser() {
115+
$controller = new MailboxesApiController(
116+
'mail',
117+
$this->request,
118+
null,
119+
$this->mailManager,
120+
$this->accountService,
121+
$this->mailSearch,
122+
);
123+
124+
$this->accountService->expects(self::never())
125+
->method('find');
126+
127+
$accountId = 28;
128+
129+
$actual = $controller->listMessages($accountId);
130+
$this->assertEquals(Http::STATUS_NOT_FOUND, $actual->getStatus());
131+
}
132+
133+
134+
public function testListMessages(): void {
135+
$accountId = 100;
136+
$mailbox = new Mailbox();
137+
$mailbox->setAccountId($accountId);
138+
$this->mailManager->expects(self::once())
139+
->method('getMailbox')
140+
->with(SELF::USER_ID, $accountId)
141+
->willReturn($mailbox);
142+
$mailAccount = new MailAccount();
143+
$account = new Account($mailAccount);
144+
$this->accountService->expects(self::once())
145+
->method('find')
146+
->with(SELF::USER_ID, $accountId)
147+
->willReturn($account);
148+
149+
$messages = [
150+
new DbMessage(),
151+
new DbMessage(),
152+
];
153+
$this->mailSearch->expects(self::once())
154+
->method('findMessages')
155+
->with(
156+
$account,
157+
$mailbox,
158+
'DESC',
159+
null,
160+
null,
161+
null,
162+
SELF::USER_ID,
163+
'threaded',
164+
)->willReturn($messages);
165+
166+
$actual = $this->controller->listMessages($accountId);
167+
168+
$this->assertEquals(Http::STATUS_OK, $actual->getStatus());
169+
$this->assertEquals(json_encode($messages), $actual->getData());
170+
}
171+
172+
173+
174+
}

0 commit comments

Comments
 (0)