aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Calendar/Resource/ManagerTest.php
blob: d9d6669971001d325bcee2cd81ff1a0d013676ad (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
121
122
123
124
125
126
127
128
129
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\Calendar\Resource;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Calendar\Resource\Manager;
use OCP\Calendar\Resource\IBackend;
use OCP\IServerContainer;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ManagerTest extends TestCase {
	/** @var Coordinator|MockObject */
	private $coordinator;

	/** @var IServerContainer|MockObject */
	private $server;

	/** @var Manager */
	private $manager;

	protected function setUp(): void {
		parent::setUp();

		$this->coordinator = $this->createMock(Coordinator::class);
		$this->server = $this->createMock(IServerContainer::class);
		$this->manager = new Manager(
			$this->coordinator,
			$this->server,
		);
	}

	public function testRegisterUnregisterBackend(): void {
		$backend1 = $this->createMock(IBackend::class);
		$backend1->method('getBackendIdentifier')->willReturn('backend_1');
		$backend2 = $this->createMock(IBackend::class);
		$backend2->method('getBackendIdentifier')->willReturn('backend_2');
		$this->server->expects(self::exactly(2))
			->method('query')
			->willReturnMap([
				['calendar_resource_backend1', true, $backend1,],
				['calendar_resource_backend2', true, $backend2,],
			]);

		$this->manager->registerBackend('calendar_resource_backend1');
		$this->manager->registerBackend('calendar_resource_backend2');

		self::assertEquals([
			$backend1, $backend2
		], $this->manager->getBackends());
		$this->manager->unregisterBackend('calendar_resource_backend1');
		self::assertEquals([
			$backend2
		], $this->manager->getBackends());
	}

	public function testGetBackendFromBootstrapRegistration(): void {
		$backendClass = '\\OCA\\CalendarResourceFoo\\Backend';
		$backend = $this->createMock(IBackend::class);
		$backend->method('getBackendIdentifier')->willReturn('from_bootstrap');
		$context = $this->createMock(RegistrationContext::class);
		$this->coordinator->expects(self::once())
			->method('getRegistrationContext')
			->willReturn($context);
		$context->expects(self::once())
			->method('getCalendarResourceBackendRegistrations')
			->willReturn([
				new ServiceRegistration('calendar_resource_foo', $backendClass)
			]);
		$this->server->expects(self::once())
			->method('query')
			->with($backendClass)
			->willReturn($backend);

		self::assertEquals($backend, $this->manager->getBackend('from_bootstrap'));
	}

	public function testGetBackend(): void {
		$backend1 = $this->createMock(IBackend::class);
		$backend1->method('getBackendIdentifier')->willReturn('backend_1');
		$backend2 = $this->createMock(IBackend::class);
		$backend2->method('getBackendIdentifier')->willReturn('backend_2');
		$this->server->expects(self::exactly(2))
			->method('query')
			->willReturnMap([
				['calendar_resource_backend1', true, $backend1,],
				['calendar_resource_backend2', true, $backend2,],
			]);

		$this->manager->registerBackend('calendar_resource_backend1');
		$this->manager->registerBackend('calendar_resource_backend2');

		self::assertEquals($backend1, $this->manager->getBackend('backend_1'));
		self::assertEquals($backend2, $this->manager->getBackend('backend_2'));
	}

	public function testClear(): void {
		$backend1 = $this->createMock(IBackend::class);
		$backend1->method('getBackendIdentifier')->willReturn('backend_1');
		$backend2 = $this->createMock(IBackend::class);
		$backend2->method('getBackendIdentifier')->willReturn('backend_2');
		$this->server->expects(self::exactly(2))
			->method('query')
			->willReturnMap([
				['calendar_resource_backend1', true, $backend1,],
				['calendar_resource_backend2', true, $backend2,],
			]);

		$this->manager->registerBackend('calendar_resource_backend1');
		$this->manager->registerBackend('calendar_resource_backend2');

		self::assertEquals([
			$backend1, $backend2
		], $this->manager->getBackends());

		$this->manager->clear();

		self::assertEquals([], $this->manager->getBackends());
	}
}