/
QueryParameterBagTest.php
169 lines (128 loc) · 4.44 KB
/
QueryParameterBagTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
namespace HeimrichHannot\RequestBundle\Test\Component\HttpFoundation;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\RequestBundle\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestStack;
class QueryParameterBagTest extends ContaoTestCase
{
/**
* @var Request
*/
protected $request;
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
unset($_GET);
}
public function setUp()
{
parent::setUp();
if (!\defined('TL_MODE')) {
\define('TL_MODE', 'FE');
}
$_GET = [];
$requestStack = new RequestStack();
$requestStack->push(new \Symfony\Component\HttpFoundation\Request());
$backendMatcher = new RequestMatcher('/contao');
$frontendMatcher = new RequestMatcher('/index');
$scopeMatcher = new ScopeMatcher($backendMatcher, $frontendMatcher);
$this->request = new Request($this->mockContaoFramework(), $requestStack, $scopeMatcher);
}
public function testAddUnusedParameters()
{
$_GET = ['id' => 12];
$this->assertSame(['id' => 12], $this->request->query->all());
$_GET = ['id' => 12, 'test' => 'test'];
$this->assertSame(['id' => 12, 'test' => 'test'], $this->request->query->all());
}
public function testKeys()
{
$_GET = ['id' => 12, 'test' => 'test'];
$this->assertSame(['id', 'test'], $this->request->query->keys());
}
public function testReplace()
{
$_GET = ['test' => 'test'];
$this->request->query->replace(['test' => 13]);
$this->assertSame(['test' => 13], $this->request->query->all());
}
public function testAdd()
{
$_GET = ['id' => 12, 'test' => 'test'];
$this->request->query->add(['number' => 13]);
$this->assertSame(['id' => 12, 'test' => 'test', 'number' => 13], $this->request->query->all());
}
public function testRemove()
{
$_GET = ['name' => 'foo']; // unused $_GET (auto_item…)
$this->request->query->add(['foo' => 'bar']);
$this->request->query->remove('name');
$this->assertSame(['foo' => 'bar'], $this->request->query->all());
}
public function testGet()
{
$this->request->query->set('id', 13);
$this->assertSame(13, $this->request->query->get('id'));
}
public function testSet()
{
$_GET = ['id' => 12];
$this->request->query->set('id', 13);
$this->assertSame(13, $this->request->query->get('id'));
}
public function testHas()
{
$_GET = ['id' => 12];
$this->assertTrue($this->request->query->has('id'));
}
public function testGetAlpha()
{
$_GET = ['test' => '123Test', 'id' => 13];
$this->assertSame('Test', $this->request->query->getAlpha('test'));
$this->assertSame('', $this->request->query->getAlpha('id'));
}
public function testGetAlnum()
{
$_GET = ['test' => '123', 'id' => 13];
$this->assertSame('123', $this->request->query->getAlnum('test'));
$this->assertSame('13', $this->request->query->getAlnum('id'));
}
public function testGetDigits()
{
$_GET = ['test' => '123Test'];
$this->assertSame('123', $this->request->query->getDigits('test'));
}
public function testGetBoolean()
{
$_GET = ['false' => '123Test', 'true' => 1];
$this->assertFalse($this->request->query->getBoolean('false'));
$this->assertTrue($this->request->query->getBoolean('true'));
}
public function testCount()
{
$_GET = ['false' => '123Test', 'true' => 1];
$this->assertSame(2, $this->request->query->count());
}
public function testGetIterator()
{
$_GET = ['false' => '123Test', 'true' => 1];
$this->assertInstanceOf(\ArrayIterator::class, $this->request->query->getIterator());
}
public function testFilter()
{
$_GET = ['test' => 'on', 'id' => 13];
$this->assertTrue($this->request->query->filter('test', null, FILTER_VALIDATE_BOOLEAN));
}
public function testGetInt()
{
$_GET = ['test' => 'on', 'id' => 13];
$this->assertSame(13, $this->request->query->getInt('id'));
}
}