Completed
Push — master ( c77917...12b0e9 )
by Jörn Friedrich
57:44
created

AppsTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 9
dl 8
loc 82
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 9 1
A testGetAppInfo() 0 5 1
A testGetAppInfoOnBadAppID() 6 6 1
D testGetApps() 1 15 1
A testGetAppsEnabled() 0 7 1
A testGetAppsDisabled() 0 17 2
A testGetAppsInvalidFilter() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Roeland Jago Douma <[email protected]>
6
 * @author Tom Needham <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2015, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Provisioning_API\Tests;
26
use OC\OCSClient;
27
use OCA\Provisioning_API\Apps;
28
use OCP\API;
29
use OCP\App\IAppManager;
30
use OCP\IUserSession;
31
32
/**
33
 * Class AppsTest
34
 *
35
 * @group DB
36
 *
37
 * @package OCA\Provisioning_API\Tests
38
 */
39
class AppsTest extends TestCase {
40
	/** @var IAppManager */
41
	private $appManager;
42
	/** @var Apps */
43
	private $api;
44
	/** @var IUserSession */
45
	private $userSession;
46
	/** @var OCSClient */
47
	private $ocsClient;
48
49
	public function setup() {
50
		parent::setup();
51
		$this->appManager = \OC::$server->getAppManager();
52
		$this->groupManager = \OC::$server->getGroupManager();
53
		$this->userSession = \OC::$server->getUserSession();
54
		$this->ocsClient = $this->getMockBuilder('\OC\OCSClient')
55
				->disableOriginalConstructor()->getMock();
56
		$this->api = new Apps($this->appManager, $this->ocsClient);
57
	}
58
59
	public function testGetAppInfo() {
60
		$result = $this->api->getAppInfo(['appid' => 'provisioning_api']);
61
		$this->assertInstanceOf('OC_OCS_Result', $result);
62
		$this->assertTrue($result->succeeded());
63
	}
64
65 View Code Duplication
	public function testGetAppInfoOnBadAppID() {
66
		$result = $this->api->getAppInfo(['appid' => 'not_provisioning_api']);
67
		$this->assertInstanceOf('OC_OCS_Result', $result);
68
		$this->assertFalse($result->succeeded());
69
		$this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode());
70
	}
71
72
	public function testGetApps() {
73
		$this->ocsClient
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OC\OCSClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
				->expects($this->any())
75
				->method($this->anything())
76
				->will($this->returnValue(null));
77
		$user = $this->generateUsers();
78
		$this->groupManager->get('admin')->addUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->generateUsers() on line 77 can also be of type array<integer,object<OCP\IUser>>; however, OCP\IGroup::addUser() does only seem to accept object<OCP\IUser>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
		$this->userSession->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->generateUsers() on line 77 can also be of type array<integer,object<OCP\IUser>>; however, OCP\IUserSession::setUser() does only seem to accept object<OCP\IUser>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
81
		$result = $this->api->getApps([]);
82
83
		$this->assertTrue($result->succeeded());
84
		$data = $result->getData();
85
		$this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps']));
86
	}
87
88
	public function testGetAppsEnabled() {
89
		$_GET['filter'] = 'enabled';
90
		$result = $this->api->getApps(['filter' => 'enabled']);
91
		$this->assertTrue($result->succeeded());
92
		$data = $result->getData();
93
		$this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps']));
94
	}
95
96
	public function testGetAppsDisabled() {
97
		$this->ocsClient
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OC\OCSClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
				->expects($this->any())
99
				->method($this->anything())
100
				->will($this->returnValue(null));
101
		$_GET['filter'] = 'disabled';
102
		$result = $this->api->getApps(['filter' => 'disabled']);
103
		$this->assertTrue($result->succeeded());
104
		$data = $result->getData();
105
		$apps = \OC_App::listAllApps(false, true, $this->ocsClient);
106
		$list =  array();
107
		foreach($apps as $app) {
108
			$list[] = $app['id'];
109
		}
110
		$disabled = array_diff($list, \OC_App::getEnabledApps());
111
		$this->assertEquals(count($disabled), count($data['apps']));
112
	}
113
114
	public function testGetAppsInvalidFilter() {
115
		$_GET['filter'] = 'foo';
116
		$result = $this->api->getApps([]);
117
		$this->assertFalse($result->succeeded());
118
		$this->assertEquals(101, $result->getStatusCode());
119
	}
120
}
121