1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouterTests\Request; |
4
|
|
|
|
5
|
|
|
use \PHPUnit_Framework_TestCase; |
6
|
|
|
use Vectorface\SnappyRouter\Request\HttpRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests the HttpRequest class. |
10
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
11
|
|
|
* @author Dan Bruce <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class HttpRequestTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* An overview of how to use the RPCRequest class. |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function synopsis() |
20
|
|
|
{ |
21
|
|
|
// instantiate the class |
22
|
|
|
$request = new HttpRequest('MyService', 'MyMethod', 'GET', 'php://memory'); |
23
|
|
|
|
24
|
|
|
$this->assertEquals('GET', $request->getVerb()); |
25
|
|
|
$this->assertEquals('POST', $request->setVerb('POST')->getVerb()); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$queryData = array('id' => '1234'); |
28
|
|
|
$this->assertTrue( |
29
|
|
|
1234 === $request->setQuery($queryData)->getQuery('id', 0, 'int') |
|
|
|
|
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$postData = array('username' => ' TEST_USER '); |
33
|
|
|
$this->assertEquals( |
34
|
|
|
'test_user', |
35
|
|
|
$request->setPost($postData)->getPost('username', '', array('trim', 'lower')) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->assertEquals( |
39
|
|
|
array('id' => '1234', 'username' => ' TEST_USER '), |
40
|
|
|
$request->getAllInput() |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests successful input stream set and fetch cases |
46
|
|
|
*/ |
47
|
|
|
public function testInputStream() |
48
|
|
|
{ |
49
|
|
|
$tempStream = fopen('php://memory', 'w'); |
50
|
|
|
fwrite($tempStream, "test"); |
51
|
|
|
rewind($tempStream); |
52
|
|
|
|
53
|
|
|
/* Mock a stream in memory */ |
54
|
|
|
$request = new HttpRequest('TestService', 'TestMethod', 'GET', $tempStream); |
55
|
|
|
$this->assertEquals("test", $request->getBody()); |
56
|
|
|
fclose($tempStream); |
57
|
|
|
|
58
|
|
|
/* Fetch previously stored value */ |
59
|
|
|
$this->assertEquals("test", $request->getBody()); |
60
|
|
|
|
61
|
|
|
/* Specify php://memory as a string */ |
62
|
|
|
$request = new HttpRequest('TestService', 'TestMethod', 'GET', 'php://memory'); |
63
|
|
|
$this->assertEquals("", $request->getBody()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Tests the input stream functionality where the stream source is not a string or a stream |
68
|
|
|
* @expectedException Vectorface\SnappyRouter\Exception\InternalErrorException |
69
|
|
|
*/ |
70
|
|
|
public function testInputStreamIncorrectTypeFailure() |
71
|
|
|
{ |
72
|
|
|
$request = new HttpRequest('TestService', 'TestMethod', 'GET', 1); |
73
|
|
|
$request->getBody(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Tests the input stream functionality where the stream source does not exist |
78
|
|
|
* @expectedException Vectorface\SnappyRouter\Exception\InternalErrorException |
79
|
|
|
*/ |
80
|
|
|
public function testInputStreamIncorrectFileFailure() |
81
|
|
|
{ |
82
|
|
|
$request = new HttpRequest('TestService', 'TestMethod', 'GET', 'file'); |
83
|
|
|
$request->getBody(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Tests the various filters. |
88
|
|
|
* @dataProvider filtersProvider |
89
|
|
|
*/ |
90
|
|
|
public function testInputFilters($expected, $value, $filters) |
91
|
|
|
{ |
92
|
|
|
$request = new HttpRequest('TestService', 'TestMethod', 'GET', 'php://input'); |
93
|
|
|
$request->setQuery(array('key' => $value)); |
94
|
|
|
$this->assertTrue($expected === $request->getQuery('key', null, $filters)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The data provider for testInputFilters. |
99
|
|
|
*/ |
100
|
|
|
public function filtersProvider() |
101
|
|
|
{ |
102
|
|
|
return array( |
103
|
|
|
array( |
104
|
|
|
1234, |
105
|
|
|
'1234', |
106
|
|
|
'int' |
107
|
|
|
), |
108
|
|
|
array( |
109
|
|
|
1234.5, |
110
|
|
|
' 1234.5 ', |
111
|
|
|
'float' |
112
|
|
|
), |
113
|
|
|
array( |
114
|
|
|
'hello world', |
115
|
|
|
"\t".'hello world '.PHP_EOL, |
116
|
|
|
'trim' |
117
|
|
|
), |
118
|
|
|
array( |
119
|
|
|
'hello world', |
120
|
|
|
'HELLO WORLD', |
121
|
|
|
'lower' |
122
|
|
|
), |
123
|
|
|
array( |
124
|
|
|
'HELLO WORLD', |
125
|
|
|
'hello world', |
126
|
|
|
'upper' |
127
|
|
|
), |
128
|
|
|
array( |
129
|
|
|
'test'.PHP_EOL.'string', |
130
|
|
|
'test'.PHP_EOL.' '.PHP_EOL.PHP_EOL.'string', |
131
|
|
|
'squeeze' |
132
|
|
|
), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: