Passed
Pull Request — master (#306)
by Tobias
02:05
created

ParameterBagTest::testAddCurl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Symfony package.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Buzz\Test\Unit\Configuration;
15
16
use Buzz\Configuration\ParameterBag;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * @author Fabien Potencier <[email protected]>
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class ParameterBagTest extends TestCase
24
{
25
    public function testConstructor()
26
    {
27
        $this->testAll();
28
    }
29
30
    public function testAll()
31
    {
32
        $bag = new ParameterBag(['foo' => 'bar']);
33
        $this->assertEquals(['foo' => 'bar'], $bag->all(), '->all() gets all the input');
34
    }
35
36
    public function testKeys()
37
    {
38
        $bag = new ParameterBag(['foo' => 'bar']);
39
        $this->assertEquals(['foo'], $bag->keys());
40
    }
41
42
    public function testAdd()
43
    {
44
        $bag = new ParameterBag(['foo' => 'bar']);
45
        $newBag = $bag->add(['bar' => 'bas']);
46
        $this->assertEquals(['foo' => 'bar', 'bar' => 'bas'], $newBag->all());
47
        $this->assertEquals(['foo' => 'bar'], $bag->all());
48
    }
49
50
    public function testAddCurl()
51
    {
52
        $bag = new ParameterBag(['foo' => 'bar', 'curl' => [1 => 'foo', 2 => 'bar']]);
53
        $newBag = $bag->add(['curl' => [2 => 'updated', 3 => 'biz']]);
54
        $this->assertEquals(['foo' => 'bar', 'curl' => [1 => 'foo', 2 => 'updated', 3 => 'biz']], $newBag->all());
55
56
        $bag = new ParameterBag(['foo' => 'bar']);
57
        $newBag = $bag->add(['curl' => [2 => 'updated', 3 => 'biz']]);
58
        $this->assertEquals(['foo' => 'bar', 'curl' => [2 => 'updated', 3 => 'biz']], $newBag->all());
59
    }
60
61
    public function testGet()
62
    {
63
        $bag = new ParameterBag(['foo' => 'bar', 'null' => null]);
64
65
        $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
66
        $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
67
        $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
68
    }
69
70
    public function testGetDoesNotUseDeepByDefault()
71
    {
72
        $bag = new ParameterBag(['foo' => ['bar' => 'moo']]);
73
74
        $this->assertNull($bag->get('foo[bar]'));
75
    }
76
77
    public function testHas()
78
    {
79
        $bag = new ParameterBag(['foo' => 'bar']);
80
81
        $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
82
        $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
83
    }
84
85
    public function testGetIterator()
86
    {
87
        $parameters = ['foo' => 'bar', 'hello' => 'world'];
88
        $bag = new ParameterBag($parameters);
89
90
        $i = 0;
91
        foreach ($bag as $key => $val) {
92
            ++$i;
93
            $this->assertEquals($parameters[$key], $val);
94
        }
95
96
        $this->assertEquals(count($parameters), $i);
97
    }
98
99
    public function testCount()
100
    {
101
        $parameters = ['foo' => 'bar', 'hello' => 'world'];
102
        $bag = new ParameterBag($parameters);
103
104
        $this->assertCount(count($parameters), $bag);
105
    }
106
}
107