ApiParameterBagTest::providerPopulateFromRequest()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
80
{
81
    public function getFilteredKeys()
82
    {
83
        return array(
84
            'foo',
85
            'bar',
86
            'sort',
87
        );
88
    }
89
}
90