AddressBookProviderTest::providesSearchData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
dl 0
loc 6
rs 9.4285
c 3
b 2
f 1
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2013 Thomas Tanghus ([email protected])
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Contacts;
10
11
use Test\TestCase;
12
13
class AddressBookProviderTest extends TestCase {
14
15
	/**
16
	* @var array
17
	*/
18
	protected $abinfo;
19
	/**
20
	* @var \OCA\Contacts\Addressbook
21
	*/
22
	protected $ab;
23
	/**
24
	* @var \OCA\Contacts\Backend\AbstractBackend
25
	*/
26
	protected $backend;
27
28
	/**
29
	 * @var \OCA\Contacts\AddressbookProvider
30
	 */
31
	private $provider;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $testUser;
37
38
	/**
39
	 * @var array
40
	 */
41
	private $contactIds = array();
42
43
	public function setUp() {
44
		parent::setUp();
45
46
		$this->testUser = $this->getUniqueID('user_');
47
		// needed because some parts of code call "getRequest()" and "getSession()"
48
		$session = $this->getMockBuilder('\OC\Session\Memory')
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$session->expects($this->any())
52
			->method('get')
53
			->with('user_id')
54
			->will($this->returnValue($this->testUser));
55
		$userObject = $this->getMock('\OCP\IUser');
56
		$userObject->expects($this->any())
57
			->method('getUId')
58
			->will($this->returnValue($this->testUser));
59
		$userSession = $this->getMockBuilder('\OC\User\Session')
60
			->disableOriginalConstructor()
61
			->getMock();
62
		$userSession->expects($this->any())
63
			->method('getUser')
64
			->will($this->returnValue($userObject));
65
		$userSession->expects($this->any())
66
			->method('getSession')
67
			->will($this->returnValue($session));
68
		\OC::$server->registerService('UserSession', function (\OCP\IServerContainer $c) use ($userSession){
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
			return $userSession;
70
		});
71
72
73
		$this->backend = new Backend\Database($this->testUser);
74
		$this->abinfo = array('displayname' => uniqid('display_'));
75
		$this->ab = new AddressBook($this->backend, $this->abinfo);
76
		$this->provider = new AddressbookProvider($this->ab);
77
78
		$card = new \OCA\Contacts\VObject\VCard();
0 ignored issues
show
Bug introduced by
The call to VCard::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
79
		$uid = substr(md5($this->getUniqueID()), 0, 10);
80
		$card->add('UID', $uid);
81
		$card->add('FN', 'Max Mustermann');
82
		$id = $this->ab->addChild($card);
83
		Utils\Properties::updateIndex($id, $card);
0 ignored issues
show
Bug introduced by
It seems like $id defined by $this->ab->addChild($card) on line 82 can also be of type false or null; however, OCA\Contacts\Utils\Properties::updateIndex() does only seem to accept string, 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...
84
		$this->contactIds[] = $id;
85
86
		// Add extra contact
87
		$card = new \OCA\Contacts\VObject\VCard();
0 ignored issues
show
Bug introduced by
The call to VCard::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
88
		$uid = substr(md5(rand().time()), 0, 10);
89
		$card->add('UID', $uid);
90
		$card->add('FN', 'Jan Janssens');
91
		$id = $this->ab->addChild($card);
92
		Utils\Properties::updateIndex($id, $card);
0 ignored issues
show
Bug introduced by
It seems like $id defined by $this->ab->addChild($card) on line 91 can also be of type false or null; however, OCA\Contacts\Utils\Properties::updateIndex() does only seem to accept string, 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...
93
		$this->contactIds[] = $id;
94
	}
95
96
	public function tearDown() {
97
		unset($this->backend);
98
		unset($this->ab);
99
		Utils\Properties::purgeIndexes($this->contactIds);
100
101
		parent::tearDown();
102
	}
103
104
	/**
105
	 * @dataProvider providesSearchData
106
	 */
107
	public function testSearch($expected, $pattern) {
108
		$result = $this->provider->search($pattern, ['FN'], array());
109
110
		$this->assertTrue(is_array($result));
111
		$this->assertEquals(count($expected), count($result));
112
		$result = array_map(function($c){
113
			return $c['FN'];
114
		}, $result);
115
		$this->assertEquals($expected, $result, '', 0.0, 10, true);
116
	}
117
118
	public function providesSearchData() {
119
		return [
120
			'empty pattern' => [['Max Mustermann', 'Jan Janssens'], ''],
121
			'case insensitive' => [['Max Mustermann'], 'max'],
122
		];
123
	}
124
125
}
126