1 | <?php |
||||
2 | |||||
3 | namespace Nip\Http\Tests; |
||||
4 | |||||
5 | use Nip\Request; |
||||
6 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||||
7 | |||||
8 | /** |
||||
9 | * Class RequestTest |
||||
10 | * @package Nip\Tests |
||||
11 | */ |
||||
12 | class RequestTest extends AbstractTest |
||||
13 | { |
||||
14 | |||||
15 | /** |
||||
16 | * @var \Nip\Request |
||||
17 | */ |
||||
18 | protected $request; |
||||
19 | |||||
20 | public function testAttributeSet() |
||||
21 | { |
||||
22 | $this->request->setAttribute('bar', 'foo'); |
||||
23 | |||||
24 | static::assertEquals('foo', $this->request->attributes->get('bar')); |
||||
25 | static::assertEquals('foo', $this->request->get('bar')); |
||||
26 | } |
||||
27 | |||||
28 | public function testQuerySet() |
||||
29 | { |
||||
30 | $this->request->query->set('bar', 'foo'); |
||||
31 | |||||
32 | static::assertEquals('foo', $this->request->query->get('bar')); |
||||
33 | static::assertEquals('foo', $this->request->get('bar')); |
||||
34 | } |
||||
35 | |||||
36 | // tests |
||||
37 | |||||
38 | public function testBodySet() |
||||
39 | { |
||||
40 | $this->request->request->set('bar', 'foo'); |
||||
41 | |||||
42 | static::assertEquals('foo', $this->request->request->get('bar')); |
||||
43 | static::assertEquals('foo', $this->request->get('bar')); |
||||
44 | } |
||||
45 | |||||
46 | public function testGetOrder() |
||||
47 | { |
||||
48 | $this->testInitialize(); |
||||
49 | |||||
50 | static::assertEquals('value44', $this->request->get('var4')); |
||||
51 | } |
||||
52 | |||||
53 | public function testInitialize() |
||||
54 | { |
||||
55 | $get['var1'] = 'value1'; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||||
56 | $get['var2'] = 'value2'; |
||||
57 | $post['var3'] = 'value3'; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
58 | $post['var4'] = 'value4'; |
||||
59 | $attributes['var4'] = 'value44'; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
60 | |||||
61 | $this->request->initialize($get, $post, $attributes); |
||||
62 | |||||
63 | static::assertEquals($get, $this->request->query->all()); |
||||
64 | static::assertEquals($post, $this->request->request->all()); |
||||
65 | static::assertEquals($attributes, $this->request->attributes->all()); |
||||
66 | } |
||||
67 | |||||
68 | public function testCreateFromGlobals() |
||||
69 | { |
||||
70 | $_GET['foo1'] = 'bar1'; |
||||
71 | $_POST['foo2'] = 'bar2'; |
||||
72 | $_COOKIE['foo3'] = 'bar3'; |
||||
73 | $_FILES['foo4'] = ['bar4']; |
||||
74 | $_SERVER['foo5'] = 'bar5'; |
||||
75 | |||||
76 | $request = Request::createFromGlobals(); |
||||
77 | |||||
78 | static::assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET'); |
||||
79 | static::assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST'); |
||||
80 | static::assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE'); |
||||
81 | static::assertEquals(['bar4'], $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES'); |
||||
82 | static::assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER'); |
||||
83 | } |
||||
84 | |||||
85 | public function testDuplicateWithParams() |
||||
86 | { |
||||
87 | $request = new Request(); |
||||
0 ignored issues
–
show
The class
Nip\Request has been deprecated: use \Nip\Http\Request
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
88 | $request->setActionName('action1'); |
||||
89 | $request->setControllerName('controller1'); |
||||
90 | $request->setModuleName('module1'); |
||||
91 | $attributes = ['attrb1' => 'val1', 'attrb2' => 'val2']; |
||||
92 | $request->attributes->add($attributes); |
||||
93 | |||||
94 | $duplicateAction = $request->duplicateWithParams('action2'); |
||||
0 ignored issues
–
show
'action2' of type string is incompatible with the type boolean expected by parameter $action of Nip\Http\Request::duplicateWithParams() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
95 | static::assertEquals('action2', $duplicateAction->getActionName()); |
||||
96 | static::assertEquals('controller1', $duplicateAction->getControllerName()); |
||||
97 | static::assertEquals('module1', $duplicateAction->getModuleName()); |
||||
98 | |||||
99 | $duplicateAction = $request->duplicateWithParams('action2', 'controller2'); |
||||
0 ignored issues
–
show
'controller2' of type string is incompatible with the type boolean expected by parameter $controller of Nip\Http\Request::duplicateWithParams() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
100 | static::assertEquals('action2', $duplicateAction->getActionName()); |
||||
101 | static::assertEquals('controller2', $duplicateAction->getControllerName()); |
||||
102 | static::assertEquals('module1', $duplicateAction->getModuleName()); |
||||
103 | |||||
104 | $duplicateAction = $request->duplicateWithParams('action2', 'controller2', 'module2'); |
||||
0 ignored issues
–
show
'module2' of type string is incompatible with the type boolean expected by parameter $module of Nip\Http\Request::duplicateWithParams() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
105 | static::assertEquals('action2', $duplicateAction->getActionName()); |
||||
106 | static::assertEquals('controller2', $duplicateAction->getControllerName()); |
||||
107 | static::assertEquals('module2', $duplicateAction->getModuleName()); |
||||
108 | } |
||||
109 | |||||
110 | public function testCreateFromGlobalsFiles() |
||||
111 | { |
||||
112 | $_FILES = [ |
||||
113 | 'file1' => [ |
||||
114 | 'name' => 'MyFile.txt', |
||||
115 | 'type' => 'text/plain', |
||||
116 | 'tmp_name' => TEST_FIXTURE_PATH.'/Request/php1h4j1o', |
||||
117 | 'error' => UPLOAD_ERR_OK, |
||||
118 | 'size' => 123, |
||||
119 | ], |
||||
120 | 'file2' => [ |
||||
121 | 'name' => 'MyFile.jpg', |
||||
122 | 'type' => 'image/jpeg', |
||||
123 | 'tmp_name' => TEST_FIXTURE_PATH.'/Request/php1h4j1o', |
||||
124 | 'error' => UPLOAD_ERR_OK, |
||||
125 | 'size' => 300, |
||||
126 | ], |
||||
127 | ]; |
||||
128 | |||||
129 | $request = Request::createFromGlobals(); |
||||
130 | |||||
131 | static::assertInstanceOf( |
||||
132 | UploadedFile::class, |
||||
133 | $request->files->get('file2'), |
||||
134 | '::fromGlobals() uses values from $_FILES'); |
||||
135 | } |
||||
136 | |||||
137 | public function testIsMalicious() |
||||
138 | { |
||||
139 | $this->request = Request::create('/wp-admin/'); |
||||
140 | static::assertTrue($this->request->isMalicious()); |
||||
141 | |||||
142 | $this->request = Request::create('/controller/action'); |
||||
143 | static::assertFalse($this->request->isMalicious()); |
||||
144 | } |
||||
145 | |||||
146 | protected function setUp(): void |
||||
147 | { |
||||
148 | parent::setUp(); |
||||
149 | $this->request = new Request(); |
||||
0 ignored issues
–
show
The class
Nip\Request has been deprecated: use \Nip\Http\Request
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
150 | } |
||||
151 | } |
||||
152 |