CollectionsServiceTest::testGetAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.0254
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Nextcloud - Tasks
4
 *
5
 * @author Raimund Schlüßler
6
 * @copyright 2018 Raimund Schlüßler <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Tasks\Tests\Unit\Service;
24
25
use OCA\Tasks\Service\CollectionsService;
26
use OCP\IConfig;
27
use OCP\IL10N;
28
29
use PHPUnit\Framework\TestCase;
30
31
class CollectionsServiceTest extends TestCase {
32
	private $collectionsService;
33
	private $settings;
34
	private $userId;
35
	private $l10n;
36
	private $appName;
37
38
	/**
39
	 * Gets run before each test
40
	 */
41 View Code Duplication
	protected function setUp(): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
		$this->appName = 'tasks';
43
		$this->userId = 'admin';
44
		$this->settings = $this->getMockBuilder(IConfig::class)
45
			->disableOriginalConstructor()
46
			->getMock();
47
		$this->l10n = $this->createMock(IL10N::class);
48
		$this->collectionsService = new CollectionsService(
49
			$this->userId,
50
			$this->l10n,
0 ignored issues
show
Documentation introduced by
$this->l10n is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<OCP\IL10N>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
			$this->settings,
52
			$this->appName
53
		);
54
	}
55
56
	public function testGetAll() {
57
		$return = [
58
			[
59
				'id' => "starred",
60
				'displayName' => 'Important',
61
				'show' => 2,
62
				'icon' => 'Star'],
63
			[
64
				'id' => "today",
65
				'displayName' => 'Today',
66
				'show' => 2,
67
				'icon' => 'CalendarToday'],
68
			[
69
				'id' => "week",
70
				'displayName' => 'Week',
71
				'show' => 2,
72
				'icon' => 'CalendarWeek'],
73
			[
74
				'id' => "all",
75
				'displayName' => 'All',
76
				'show' => 2,
77
				'icon' => 'CircleOutline'],
78
			[
79
				'id' => "current",
80
				'displayName' => 'Current',
81
				'show' => 2,
82
				'icon' => 'TrendingUp'],
83
			[
84
				'id' => "completed",
85
				'displayName' => 'Completed',
86
				'show' => 2,
87
				'icon' => 'Check']
88
		];
89
		
90
		$map = [
91
			['Important', [],'Important'],
92
			['Today', [], 'Today'],
93
			['Week', [], 'Week'],
94
			['All', [], 'All'],
95
			['Completed', [], 'Completed'],
96
			['Current', [], 'Current']
97
		];
98
99
		$this->l10n->expects($this->any())
100
			->method('t')
101
			->will(
102
				$this->returnValueMap($map)
103
			);
104
105
		$result = $this->collectionsService->getAll();
106
107
		$this->assertEquals($return, $result);
108
	}
109
110
111
	public function testSetVisibility() {
112
		$return = true;
113
		
114
		$this->settings->expects($this->once())
115
			->method('setUserValue')
116
			->with(
117
				$this->equalTo($this->userId),
118
				$this->equalTo($this->appName),
119
				'show_starred',
120
				0
121
			);
122
		
123
		$result = $this->collectionsService->setVisibility('starred', 0);
124
		// These lines should not lead to a call of '$this->settings->setUserValue'
125
		$this->collectionsService->setVisibility('starred', -1);
126
		$this->collectionsService->setVisibility('starred', '3');
127
128
		$this->assertEquals($return, $result);
129
	}
130
}
131