Passed
Push — master ( 9f6f8b...b68386 )
by Pauli
03:11 queued 16s
created

ControllerTestUtility   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertHeaders() 0 4 2
A getRequest() 0 26 4
A assertAnnotations() 0 19 3
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;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	protected function assertAnnotations($controller, $method, array $expected,
30
										array $valid=[]) {
31
		$standard = [
32
			'PublicPage',
33
			'NoAdminRequired',
34
			'NoCSRFRequired',
35
			'API'
36
		];
37
38
		$possible = \array_merge($standard, $valid);
39
40
		// check if expected annotations are valid
41
		foreach ($expected as $annotation) {
42
			$this->assertTrue(\in_array($annotation, $possible));
43
		}
44
45
		$reader = new MethodAnnotationReader($controller, $method);
46
		foreach ($expected as $annotation) {
47
			$this->assertTrue($reader->hasAnnotation($annotation));
48
		}
49
	}
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 Request a request instance
0 ignored issues
show
Bug introduced by
The type OCA\Music\AppFramework\Utility\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 OCA\Music\AppFramework\Utility\Request.
Loading history...
95
	}
96
}
97