Completed
Push — master ( 7ed290...011fe4 )
by Pauli
07:23
created

ControllerTestUtility   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 74
ccs 9
cts 27
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertHeaders() 0 4 2
A assertAnnotations() 0 19 3
A getRequest() 0 26 4
1
<?php
2
/**
3
 * ownCloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\Music\AppFramework\Utility;
15
16
use OCP\AppFramework\Http\Response;
17
18
/**
19
 * Simple utility class for testing controllers
20
 */
21
abstract class ControllerTestUtility extends \PHPUnit_Framework_TestCase {
22
23
	/**
24
	 * Checks if a controllermethod has the expected annotations
25
	 * @param Controller|string $controller name or instance of the controller
26
	 * @param array $expected an array containing the expected annotations
27
	 * @param array $valid if you define your own annotations, pass them here
28
	 */
29 1
	protected function assertAnnotations($controller, $method, array $expected,
30
										array $valid=[]) {
31
		$standard = [
32 1
			'PublicPage',
33
			'NoAdminRequired',
34
			'NoCSRFRequired',
35
			'API'
36
		];
37
38 1
		$possible = \array_merge($standard, $valid);
39
40
		// check if expected annotations are valid
41 1
		foreach ($expected as $annotation) {
42 1
			$this->assertTrue(\in_array($annotation, $possible));
43
		}
44
45 1
		$reader = new MethodAnnotationReader($controller, $method);
46 1
		foreach ($expected as $annotation) {
47 1
			$this->assertTrue($reader->hasAnnotation($annotation));
48
		}
49 1
	}
50
51
	/**
52
	 * Shortcut for testing expected headers of a response
53
	 * @param array $expected an array with the expected headers
54
	 * @param Response $response the response which we want to test for headers
55
	 */
56
	protected function assertHeaders(array $expected=[], Response $response) {
57
		$headers = $response->getHeaders();
58
		foreach ($expected as $header) {
59
			$this->assertTrue(\in_array($header, $headers));
60
		}
61
	}
62
63
	/**
64
	 * Instead of using positional parameters this function instantiates
65
	 * a request by using a hashmap so its easier to only set specific params
66
	 * @param array $params a hashmap with the parameters for request
67
	 * @return \OCP\IRequest a request instance
68
	 */
69
	protected function getRequest(array $params=[]) {
70
		$mock = $this->getMockBuilder('\OCP\IRequest')
71
			->getMock();
72
73
		$merged = [];
74
75
		foreach ($params as $key => $value) {
76
			$merged = \array_merge($value, $merged);
77
		}
78
79
		$mock->expects($this->any())
80
			->method('getParam')
81
			->will($this->returnCallback(function ($index, $default) use ($merged) {
82
				if (\array_key_exists($index, $merged)) {
83
					return $merged[$index];
84
				} else {
85
					return $default;
86
				}
87
			}));
88
89
		// attribute access
90
		if (\array_key_exists('server', $params)) {
91
			$mock->server = $params['server'];
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
92
		}
93
94
		return $mock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mock returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the documented return type OCP\IRequest.
Loading history...
95
	}
96
}
97