Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AliasesControllerTest.php « Controller « Unit « tests - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 342de1e481d894df5678c7bd10742a1c815e1b9d (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

/**
 * @author Tahaa Karim <tahaalibra@gmail.com>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\Mail\Tests\Unit\Controller;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Controller\AliasesController;
use OCA\Mail\Db\Alias;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;

class AliasesControllerTest extends TestCase {
	private $controller;
	private $appName = 'mail';
	private $request;
	private $aliasService;
	private $userId = 'user12345';
	private $userSession;
	private $user;
	private $alias;

	public function setUp(): void {
		parent::setUp();
		$this->request = $this->getMockBuilder('OCP\IRequest')
			->getMock();
		$this->aliasService = $this->getMockBuilder('OCA\Mail\Service\AliasesService')
			->disableOriginalConstructor()
			->getMock();
		$this->userSession = $this->getMockBuilder('OCP\IUserSession')
			->getMock();
		$this->user = $this->getMockBuilder('OCP\IUser')
			->getMock();
		$this->alias = $this->getMockBuilder('\OCA\Mail\Db\Alias')
			->disableOriginalConstructor()
			->getMock();
		$this->userSession->expects($this->once())
			->method('getUser')
			->will($this->returnValue($this->user));

		$this->controller = new AliasesController($this->appName, $this->request, $this->aliasService, $this->userSession);
	}

	public function testIndex() {
		$accountId = 123;
		$this->user->expects($this->once())
			->method('getUID')
			->will($this->returnValue($this->userId));
		$this->aliasService->expects($this->once())
			->method('findAll')
			->with($accountId, $this->userId)
			->will($this->returnValue([$this->alias]));

		$response = $this->controller->index($accountId);

		$expectedResponse = new JSONResponse([
			$this->alias
		]);
		$this->assertEquals($expectedResponse, $response);
	}

	public function testDestroy() {
		$aliasId = 123;
		$alias = $this->createMock(Alias::class);
		$this->user->expects($this->once())
			->method('getUID')
			->will($this->returnValue($this->userId));
		$this->aliasService->expects($this->once())
			->method('delete')
			->with($this->equalTo($aliasId), $this->equalTo($this->userId))
			->will($this->returnValue($alias));

		$response = $this->controller->destroy($aliasId);

		$expectedResponse = new JSONResponse($alias);
		$this->assertEquals($expectedResponse, $response);
	}

	public function testCreate() {
		$accountId = 123;
		$alias = "alias@marvel.com";
		$aliasName = "Peter Parker";
		$this->aliasService->expects($this->once())
			->method('create')
			->with($this->equalTo($accountId), $this->equalTo($alias), $this->equalTo($aliasName))
			->will($this->returnValue([
				'accountId' => $accountId,
				'name' => $aliasName,
				'alias' => $alias,
				'id' => 123
			]));

		$response = $this->controller->create($accountId, $alias, $aliasName);

		$expected = new JSONResponse([
			'accountId' => $accountId,
			'name' => $aliasName,
			'alias' => $alias,
			'id' => 123
		], Http::STATUS_CREATED);
		$this->assertEquals($expected, $response);
	}
}