BagTest::jsonSerialize()   A
last analyzed

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
namespace SubjectivePHPTest\Spl\DataStructures;
3
4
use SubjectivePHP\Spl\DataStructures\Bag;
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * Unit tests for the \SubjectivePHP\Spl\DataStructures\Bag class.
9
 *
10
 * @coversDefaultClass \SubjectivePHP\Spl\DataStructures\Bag
11
 */
12
final class BagTest extends TestCase
13
{
14
    /**
15
     * Verify basic behavior of add().
16
     *
17
     * @test
18
     * @covers ::add
19
     * @uses \SubjectivePHP\Spl\DataStructures\Bag::jsonSerialize
20
     *
21
     * @return void
22
     */
23
    public function add()
24
    {
25
        $bag = new Bag();
26
        $this->assertSame($bag, $bag->add('foo'));
27
        $this->assertSame(['foo'], $bag->jsonSerialize());
28
    }
29
30
    /**
31
     * Verify basic behavior of remove().
32
     *
33
     * @test
34
     * @covers ::remove
35
     * @uses \SubjectivePHP\Spl\DataStructures\Bag
36
     *
37
     * @return void
38
     */
39
    public function remove()
40
    {
41
        $bag = new Bag();
42
        $bag->add('a')->add('b')->add('c');
43
        $this->assertSame(3, $bag->count());
44
        $this->assertTrue(in_array($bag->remove(), ['a', 'b', 'c']));
45
        $this->assertSame(2, $bag->count());
46
    }
47
48
    /**
49
     * Verify behavior of remove() with empty bag.
50
     *
51
     * @test
52
     * @covers ::remove
53
     * @expectedException \RuntimeException
54
     * @expectedExceptionMessage Bag is empty
55
     *
56
     * @return void
57
     */
58
    public function removeEmpty()
59
    {
60
        (new Bag())->remove();
61
    }
62
63
    /**
64
     * Verify basic behavior of count().
65
     *
66
     * @test
67
     * @covers ::count
68
     * @uses \SubjectivePHP\Spl\DataStructures\Bag::add
69
     *
70
     * @return void
71
     */
72
    public function testCount()
73
    {
74
        $bag = new Bag();
75
        foreach (['a', 'b', 'c'] as $key => $value) {
76
            $this->assertSame($key, $bag->count());
77
            $bag->add($value);
78
        }
79
    }
80
81
    /**
82
     * Verify basic behavior of isEmpty();
83
     *
84
     * @test
85
     * @covers ::isEmpty
86
     * @uses \SubjectivePHP\Spl\DataStructures\Bag::add
87
     *
88
     * @return void
89
     */
90
    public function testIsEmpty()
91
    {
92
        $bag = new Bag();
93
        $this->assertTrue($bag->isEmpty());
94
        $bag->add('foo');
95
        $this->assertFalse($bag->isEmpty());
96
    }
97
98
    /**
99
     * Verify basic behavior of jsonSerialize()
100
     *
101
     * @test
102
     * @covers ::jsonSerialize
103
     * @uses \SubjectivePHP\Spl\DataStructures\Bag::add
104
     *
105
     * @return void
106
     */
107
    public function jsonSerialize()
108
    {
109
        $bag = new Bag();
110
        $bag->add('a')->add('b')->add('c');
111
        $this->assertSame([], array_diff(['a', 'b', 'c'], $bag->jsonSerialize()));
112
    }
113
114
    /**
115
     * Verify \Iterator implementation.
116
     *
117
     * @test
118
     * @covers ::rewind
119
     * @covers ::current
120
     * @covers ::key
121
     * @covers ::next
122
     * @covers ::valid
123
     * @uses \SubjectivePHP\Spl\DataStructures\Bag::add
124
     *
125
     * @return void
126
     */
127
    public function iteratator()
128
    {
129
        $bag = new Bag();
130
        $bag->add('a')->add('b')->add('c');
131
        foreach ($bag as $key => $value) {
132
            $this->assertTrue(in_array($value, ['a', 'b', 'c']));
133
        }
134
    }
135
136
    /**
137
     * Verify basic behavior of clear().
138
     *
139
     * @test
140
     * @covers ::clear
141
     * @uses \SubjectivePHP\Spl\DataStructures\Bag
142
     *
143
     * @return void
144
     */
145
    public function clear()
146
    {
147
        $bag = new Bag();
148
        $bag->add('a')->add('b')->add('c');
149
        $this->assertFalse($bag->isEmpty());
150
        $bag->clear();
151
        $this->assertTrue($bag->isEmpty());
152
        $this->assertSame([], $bag->jsonSerialize());
153
    }
154
}
155