ModuleEntryPointTest::responsesProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 91
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 2
Metric Value
c 9
b 0
f 2
dl 0
loc 91
rs 8.518
cc 1
eloc 71
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PPP\Module;
4
5
use Exception;
6
use PPP\DataModel\MissingNode;
7
use PPP\DataModel\ResourceListNode;
8
use PPP\DataModel\StringResourceNode;
9
use PPP\DataModel\TripleNode;
10
use PPP\Module\DataModel\ModuleRequest;
11
use PPP\Module\DataModel\ModuleResponse;
12
13
/**
14
 * @covers PPP\Module\ModuleEntryPoint
15
 *
16
 * @licence MIT
17
 * @author Thomas Pellissier Tanon
18
 */
19
class ModuleEntryPointTest extends \PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @dataProvider responsesProvider
23
	 */
24
	public function testGetResponse(RequestHandler $handler, $request, $response) {
25
		$entryPointMock = $this->getMock('PPP\Module\ModuleEntryPoint', array('getRequestBody'), array($handler));
26
		$entryPointMock->expects($this->any())
27
			->method('getRequestBody')
28
			->will($this->returnValue($request));
29
30
		$entryPointMock->exec();
31
32
		$this->expectOutputString($response);
33
	}
34
35
	public function responsesProvider() {
36
		$tests = array();
37
38
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
39
		$handlerMock->expects($this->any())
40
			->method('buildResponse')
41
			->with($this->equalTo(new ModuleRequest('en', new MissingNode(), 'a')))
42
			->will($this->returnValue(array()));
43
		$tests[] = array(
44
			$handlerMock,
45
			'{"language":"en","tree":{"type":"missing"},"id":"a"}',
46
			'[]'
47
		);
48
49
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
50
		$handlerMock->expects($this->any())
51
			->method('buildResponse')
52
			->with($this->equalTo(new ModuleRequest('en', new MissingNode(), 'a')))
53
			->will($this->returnValue(array(new ModuleResponse(
54
				'en',
55
				new ResourceListNode(array(new StringResourceNode('p'))),
56
				array('accuracy' => 1)
57
			))));
58
		$tests[] = array(
59
			$handlerMock,
60
			'{"language":"en","tree":{"type":"missing"},"id":"a"}',
61
			'[{"language":"en","tree":{"type":"resource","value":"p","value-type":"string"},"measures":{"accuracy":1},"trace":[]}]'
62
		);
63
64
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
65
		$handlerMock->expects($this->any())
66
			->method('buildResponse')
67
			->with($this->equalTo(new ModuleRequest('en', new MissingNode(), 'a')))
68
			->will($this->returnValue(array(
69
				new ModuleResponse(
70
					'en',
71
					new ResourceListNode(array(new StringResourceNode('p'))),
72
					array('accuracy' => 1)
73
				),
74
				new ModuleResponse(
75
					'en',
76
					new ResourceListNode(array(new StringResourceNode('p'))),
77
					array('accuracy' => 0.9)
78
				)
79
			)));
80
		$tests[] = array(
81
			$handlerMock,
82
			'{"language":"en","tree":{"type":"missing"},"id":"a"}',
83
			'[{"language":"en","tree":{"type":"resource","value":"p","value-type":"string"},"measures":{"accuracy":1},"trace":[]}]'
84
		);
85
86
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
87
		$handlerMock->expects($this->any())
88
			->method('buildResponse')
89
			->with($this->equalTo(new ModuleRequest('en', new MissingNode(), 'a', array('accuracy' => 1))))
90
			->will($this->returnValue(array(new ModuleResponse('en', new ResourceListNode(array(new StringResourceNode('p')))))));
91
		$tests[] = array(
92
			$handlerMock,
93
			'{"language":"en","tree":{"type":"missing"},"measures":{"accuracy":1},"id":"a"}',
94
			'[{"language":"en","tree":{"type":"resource","value":"p","value-type":"string"},"measures":{"accuracy":1},"trace":[]}]'
95
		);
96
97
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
98
		$handlerMock->expects($this->any())
99
			->method('buildResponse')
100
			->with($this->equalTo(new ModuleRequest(
101
				'en',
102
				new TripleNode(
103
					new ResourceListNode(array(new StringResourceNode('s'))),
104
					new ResourceListNode(array(new StringResourceNode('p'))),
105
					new ResourceListNode(array(new StringResourceNode('o')))
106
				),
107
				'a'
108
			)))
109
			->will($this->returnValue(array(new ModuleResponse(
110
				'en',
111
				new TripleNode(
112
					new ResourceListNode(array(new StringResourceNode('s1'))),
113
					new ResourceListNode(array(new StringResourceNode('p1'))),
114
					new ResourceListNode(array(new StringResourceNode('o1')))
115
				),
116
				array('accuracy' => 1)
117
			))));
118
		$tests[] = array(
119
			$handlerMock,
120
			'{"language":"en","tree":{"type":"triple","subject":{"type":"resource","value":"s","value-type":"string"},"predicate":{"type":"resource","value":"p","value-type":"string"},"object":{"type":"resource","value":"o","value-type":"string"}},"id":"a"}',
121
			'[{"language":"en","tree":{"type":"triple","subject":{"type":"resource","value":"s1","value-type":"string"},"predicate":{"type":"resource","value":"p1","value-type":"string"},"object":{"type":"resource","value":"o1","value-type":"string"}},"measures":{"accuracy":1},"trace":[]}]'
122
		);
123
124
		return $tests;
125
	}
126
127
	public function testWithInvalidRequest() {
128
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
129
		$entryPointMock = $this->getMock('PPP\Module\ModuleEntryPoint', array('getRequestBody'), array($handlerMock));
130
		$entryPointMock->expects($this->any())
131
			->method('getRequestBody')
132
			->will($this->returnValue('{}'));
133
134
		$entryPointMock->exec();
135
136
		$this->expectOutputString('The serialization is invalid!');
137
	}
138
139
	public function testWithRequestHandlerException() {
140
		$handlerMock = $this->getMockForAbstractClass('PPP\Module\AbstractRequestHandler');
141
		$handlerMock->expects($this->any())
142
			->method('buildResponse')
143
			->with($this->equalTo(new ModuleRequest('en', new MissingNode(), 'a')))
144
			->will($this->throwException(new Exception('foo')));
145
146
		$entryPointMock = $this->getMock('PPP\Module\ModuleEntryPoint', array('getRequestBody'), array($handlerMock));
147
		$entryPointMock->expects($this->any())
148
			->method('getRequestBody')
149
			->will($this->returnValue('{"language":"en","tree":{"type":"missing"},"id":"a"}'));
150
151
		$entryPointMock->exec();
152
153
		$this->expectOutputString('foo');
154
	}
155
}
156