|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK - Unit Tests |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace CoreTest\View\Helper; |
|
10
|
|
|
|
|
11
|
|
|
use Zend\Mvc\MvcEvent; |
|
12
|
|
|
use Core\View\Helper\Params as Helper; |
|
13
|
|
|
use Zend\Mvc\Router\RouteMatch; |
|
14
|
|
|
use Zend\Http\PhpEnvironment\Request; |
|
15
|
|
|
|
|
16
|
|
|
class ParamsTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
public function getHelper(MvcEvent $e=null) |
|
20
|
|
|
{ |
|
21
|
|
|
if (null == $e) { |
|
22
|
|
|
$e = new MvcEvent(); |
|
23
|
|
|
} |
|
24
|
|
|
return new Helper($e); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testHelperInvokationWithoutParamsReturnsHelperInstance() |
|
28
|
|
|
{ |
|
29
|
|
|
$e = new MvcEvent(); |
|
30
|
|
|
$helper = new Helper($e); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertInstanceOf('Core\View\Helper\Params', $helper()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testHelperInvokationReturnsParamFromRouteIfPresentOrFromEventOrDefault() |
|
36
|
|
|
{ |
|
37
|
|
|
$e = new MvcEvent(); |
|
38
|
|
|
$routeMatch = new RouteMatch(array( |
|
39
|
|
|
'testParam1' => 'routeValue', |
|
40
|
|
|
)); |
|
41
|
|
|
$e->setRouteMatch($routeMatch); |
|
42
|
|
|
|
|
43
|
|
|
$e->setParam('testParam2', 'eventValue'); |
|
44
|
|
|
|
|
45
|
|
|
$helper = new Helper($e); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals('routeValue', $helper('testParam1')); |
|
48
|
|
|
$this->assertEquals('eventValue', $helper('testParam2')); |
|
49
|
|
|
$this->assertEquals('defaultValue', $helper('testParam3', 'defaultValue')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testFromFilesMethod() |
|
53
|
|
|
{ |
|
54
|
|
|
$_FILES=array( |
|
55
|
|
|
'testFile1' => array( |
|
56
|
|
|
'name' => 'testfile1.txt', |
|
57
|
|
|
'type' => 'text/plain', |
|
58
|
|
|
'size' => 128, |
|
59
|
|
|
'tmp_name' => '/tmp/123456', |
|
60
|
|
|
'error' => UPLOAD_ERR_OK |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
$r = new Request(); |
|
64
|
|
|
|
|
65
|
|
|
$e = new MvcEvent(); |
|
66
|
|
|
$e->setRequest($r); |
|
67
|
|
|
|
|
68
|
|
|
$helper = new Helper($e); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals($_FILES['testFile1'], $helper->fromFiles('testFile1'), 'testFile1 differs!'); |
|
71
|
|
|
$this->assertEquals($_FILES, $helper->fromFiles(), 'Whole file array differs!'); |
|
72
|
|
|
$this->assertEquals('default', $helper->fromFiles('not_there', 'default'), 'default value differs!'); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testFromHeaderMethod() |
|
77
|
|
|
{ |
|
78
|
|
|
$headers = "X-Test-Header-1: Header1Value\r\n" |
|
79
|
|
|
. "X-Test-Header-2: Header2Value\r\n"; |
|
80
|
|
|
|
|
81
|
|
|
$r = new Request(); |
|
82
|
|
|
$r->setHeaders(\Zend\Http\Headers::fromString($headers)); |
|
83
|
|
|
|
|
84
|
|
|
$e = new MvcEvent(); |
|
85
|
|
|
$e->setRequest($r); |
|
86
|
|
|
|
|
87
|
|
|
$helper = new Helper($e); |
|
88
|
|
|
|
|
89
|
|
|
$header = $helper->fromHeader('X-Test-Header-1'); |
|
90
|
|
|
$this->assertInstanceOf('\Zend\Http\Header\GenericHeader', $header); |
|
91
|
|
|
$this->assertEquals('Header1Value', $header->getFieldValue()); |
|
92
|
|
|
|
|
93
|
|
|
$expect = array( |
|
94
|
|
|
'X-Test-Header-1' => 'Header1Value', |
|
95
|
|
|
'X-Test-Header-2' => 'Header2Value', |
|
96
|
|
|
); |
|
97
|
|
|
$this->assertEquals($expect, $helper->fromHeader()); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals('default', $helper->fromHeader('X-Not-Here', 'default')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testFromPostMethod() |
|
103
|
|
|
{ |
|
104
|
|
|
$_POST = array( |
|
105
|
|
|
'test1' => 'value1', |
|
106
|
|
|
'test2' => 'value2', |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$r = new Request(); |
|
110
|
|
|
|
|
111
|
|
|
$e = new MvcEvent(); |
|
112
|
|
|
$e->setRequest($r); |
|
113
|
|
|
|
|
114
|
|
|
$helper = new Helper($e); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertEquals('value1', $helper->fromPost('test1')); |
|
117
|
|
|
$this->assertEquals($_POST, $helper->fromPost()); |
|
118
|
|
|
$this->assertEquals('default', $helper->fromPost('not_there', 'default')); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testFromQueryMethod() |
|
122
|
|
|
{ |
|
123
|
|
|
$_GET = array( |
|
124
|
|
|
'test1' => 'value1', |
|
125
|
|
|
'test2' => 'value2', |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$r = new Request(); |
|
129
|
|
|
|
|
130
|
|
|
$e = new MvcEvent(); |
|
131
|
|
|
$e->setRequest($r); |
|
132
|
|
|
|
|
133
|
|
|
$helper = new Helper($e); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals('value1', $helper->fromQuery('test1')); |
|
136
|
|
|
$this->assertEquals($_GET, $helper->fromQuery()); |
|
137
|
|
|
$this->assertEquals('default', $helper->fromQuery('not_there', 'default')); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testFromRouteMethod() |
|
141
|
|
|
{ |
|
142
|
|
|
$e = new MvcEvent(); |
|
143
|
|
|
$helper = new Helper($e); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertNull($helper->fromRoute('test')); |
|
146
|
|
|
$this->assertEquals('default', $helper->fromRoute('test', 'default')); |
|
147
|
|
|
|
|
148
|
|
|
$params = array( |
|
149
|
|
|
'test' => 'value', |
|
150
|
|
|
'test1' => 'value1', |
|
151
|
|
|
); |
|
152
|
|
|
$rm = new RouteMatch($params); |
|
153
|
|
|
|
|
154
|
|
|
$e->setRouteMatch($rm); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertEquals('value', $helper->fromRoute('test')); |
|
157
|
|
|
$this->assertEquals($params, $helper->fromRoute()); |
|
158
|
|
|
$this->assertEquals('default', $helper->fromRoute('not_there', 'default')); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |