ControllerTestUtility::assertAnnotations()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.3142
cc 3
eloc 13
nc 4
nop 4
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 * https://raw.githubusercontent.com/owncloud/news/master/utility/controllertestutility.php
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\Chat\Utility;
15
16
use OCP\IRequest;
17
use OCP\AppFramework\Http\Response;
18
19
20
/**
21
 * Simple utility class for testing controllers
22
 */
23
abstract class ControllerTestUtility extends \PHPUnit_Framework_TestCase {
24
25
26
	/**
27
	 * Checks if a controllermethod has the expected annotations
28
	 * @param Controller/string $controller name or instance of the controller
0 ignored issues
show
Documentation introduced by
The doc-type Controller/string could not be parsed: Unknown type name "Controller/string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
	 * @param array $expected an array containing the expected annotations
30
	 * @param array $valid if you define your own annotations, pass them here
31
	 */
32
	protected function assertAnnotations($controller, $method, array $expected,
33
										 array $valid=array()){
34
		$standard = array(
35
			'PublicPage',
36
			'NoAdminRequired',
37
			'NoCSRFRequired',
38
			'API'
39
		);
40
41
		$possible = array_merge($standard, $valid);
42
43
		// check if expected annotations are valid
44
		foreach($expected as $annotation){
45
			$this->assertTrue(in_array($annotation, $possible));
46
		}
47
48
		$reader = new MethodAnnotationReader($controller, $method);
49
		foreach($expected as $annotation){
50
			$this->assertTrue($reader->hasAnnotation($annotation));
51
		}
52
	}
53
54
55
	/**
56
	 * Shortcut for testing expected headers of a response
57
	 * @param array $expected an array with the expected headers
58
	 * @param Response $response the response which we want to test for headers
59
	 */
60
	protected function assertHeaders(array $expected=array(), Response $response){
61
		$headers = $response->getHeaders();
62
		foreach($expected as $header){
63
			$this->assertTrue(in_array($header, $headers));
64
		}
65
	}
66
67
68
	/**
69
	 * Instead of using positional parameters this function instantiates
70
	 * a request by using a hashmap so its easier to only set specific params
71
	 * @param array $params a hashmap with the parameters for request
72
	 * @return Request a request instance
73
	 */
74
	protected function getRequest(array $params=array()) {
75
		$mock = $this->getMockBuilder('\OCP\IRequest')
76
			->getMock();
77
78
		$merged = array();
79
80
		foreach ($params as $key => $value) {
81
			$merged = array_merge($value, $merged);
82
		}
83
84
		$mock->expects($this->any())
85
			->method('getParam')
86
			->will($this->returnCallback(function($index, $default) use ($merged) {
87
				if (array_key_exists($index, $merged)) {
88
					return $merged[$index];
89
				} else {
90
					return $default;
91
				}
92
			}));
93
94
		// attribute access
95
		if(array_key_exists('server', $params)) {
96
			$mock->server = $params['server'];
97
		}
98
99
		return $mock;
100
	}
101
102
}
103