Completed
Pull Request — 2.x (#328)
by
unknown
01:24
created

AttributeBagTest::testAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[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 Sonata\SeoBundle\Tests\Seo;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\SeoBundle\Seo\AttributeBag;
18
19
class AttributeBagTest extends TestCase
20
{
21
    public function testConstructor()
22
    {
23
        $this->testAll();
24
    }
25
26
    public function testAll()
27
    {
28
        $bag = new AttributeBag(['foo' => 'bar']);
29
        $this->assertSame(['foo' => 'bar'], $bag->all());
30
    }
31
32
    public function testHas()
33
    {
34
        $bag = new AttributeBag(['foo' => 'bar']);
35
        $this->assertTrue($bag->has('foo'));
36
        $this->assertFalse($bag->has('unknown'));
37
    }
38
39
    public function testSet()
40
    {
41
        $bag = new AttributeBag([]);
42
43
        $bag->set(['foo' => 'bar', 'lorem' => 'ipsum']);
44
        $this->assertSame('bar', $bag->get('foo'));
45
46
        $bag->set(['foo' => 'baz']);
47
        $this->assertSame('baz', $bag->get('foo'));
48
        $this->assertSame(['foo' => 'baz'], $bag->all());
49
    }
50
51
    public function testAdd()
52
    {
53
        $bag = new AttributeBag(['foo' => 'bar']);
54
        $bag->add('bar', 'bas');
55
        $this->assertSame(['foo' => 'bar', 'bar' => 'bas'], $bag->all());
56
    }
57
58
    public function testGet()
59
    {
60
        $bag = new AttributeBag(['foo' => 'bar', 'null' => null]);
61
        $this->assertSame('bar', $bag->get('foo'));
62
        $this->assertSame('default', $bag->get('unknown', 'default'));
63
        $this->assertNull($bag->get('null'));
64
    }
65
66
    public function testRemove()
67
    {
68
        $bag = new AttributeBag(['foo' => 'bar']);
69
        $bag->add('bar', 'bas');
70
        $this->assertSame(['foo' => 'bar', 'bar' => 'bas'], $bag->all());
71
        $bag->remove('bar');
72
        $this->assertSame(['foo' => 'bar'], $bag->all());
73
    }
74
75
    public function testGetIterator()
76
    {
77
        $attributes = ['foo' => 'bar', 'bar' => 'bas'];
78
        $bag = new AttributeBag($attributes);
79
80
        $i = 0;
81
        foreach ($bag as $key => $val) {
82
            ++$i;
83
            $this->assertSame($attributes[$key], $val);
84
        }
85
        $this->assertSame(\count($attributes), $i);
86
    }
87
}
88