1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seblegall\ApiValidatorBundle\Tests\Request; |
4
|
|
|
|
5
|
|
|
use Seblegall\ApiValidatorBundle\Request\ApiParameterBag; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
class ApiParameterBagTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ApiParameterBag |
12
|
|
|
*/ |
13
|
|
|
protected $bag; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ApiParameterBag |
17
|
|
|
*/ |
18
|
|
|
protected $extendedBag; |
19
|
|
|
|
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->bag = new ApiParameterBag(); |
23
|
|
|
$this->extendedBag = new TestApiParameterBag(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider providerPopulateFromRequest |
28
|
|
|
*/ |
29
|
|
|
public function testPopulateFromRequest($get, $post, $headers, $expected, $extended = false) |
30
|
|
|
{ |
31
|
|
|
$request = new Request(); |
32
|
|
|
$request->query->add($get); |
33
|
|
|
$request->request->add($post); |
34
|
|
|
$request->headers->add($headers); |
35
|
|
|
|
36
|
|
|
$bag = $extended ? $this->extendedBag : $this->bag; |
37
|
|
|
$bag->populateFromRequest($request); |
38
|
|
|
|
39
|
|
|
$this->assertEquals($expected, $bag->all()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider |
44
|
|
|
*/ |
45
|
|
|
public function providerPopulateFromRequest() |
46
|
|
|
{ |
47
|
|
|
return array( |
48
|
|
|
// dataset #0 |
49
|
|
|
array( |
50
|
|
|
array('bar' => 'a', 'id' => 1), |
51
|
|
|
array('foo' => 'b', 'content' => 'lorem'), |
52
|
|
|
array('sort' => '-id', 'foobar' => 'lorem'), |
53
|
|
|
array( |
54
|
|
|
'bar' => 'a', |
55
|
|
|
'id' => 1, |
56
|
|
|
'foo' => 'b', |
57
|
|
|
'content' => 'lorem', |
58
|
|
|
'sort' => '-id', |
59
|
|
|
'foobar' => 'lorem', |
60
|
|
|
), |
61
|
|
|
false, |
62
|
|
|
), |
63
|
|
|
// dataset #1 |
64
|
|
|
array( |
65
|
|
|
array('bar' => 'a', 'id' => 1), |
66
|
|
|
array('foo' => 'b', 'content' => 'lorem'), |
67
|
|
|
array('sort' => '-id', 'foobar' => 'lorem'), |
68
|
|
|
array( |
69
|
|
|
'bar' => 'a', |
70
|
|
|
'foo' => 'b', |
71
|
|
|
'sort' => '-id', |
72
|
|
|
), |
73
|
|
|
true, |
74
|
|
|
), |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
class TestApiParameterBag extends ApiParameterBag |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
public function getFilteredKeys() |
82
|
|
|
{ |
83
|
|
|
return array( |
84
|
|
|
'foo', |
85
|
|
|
'bar', |
86
|
|
|
'sort', |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.