Code

< 40 %
40-60 %
> 60 %
1
<?php
2
/**
3
 * This file is part of the Axstrad library.
4
 *
5
 * (c) Dan Kempster <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axstrad\Component\Test;
12
13
/**
14
 * Axstrad\Component\Test\MockedIteratorTest
15
 *
16
 * Tests for _mock_ Iterator
17
 *
18
 * Here we have added a helper method (which you could add to your own abstract
19
 * base test class) that will help us mock iterators quickly and easily.
20
 *
21
 * @author Dave Gardner <[email protected]>
22
 * @author Dan Kempster <[email protected]>
23
 * @license http://opensource.org/licenses/MIT MIT
24
 * @package Axstrad/Test
25
 * @subpackage Iterator
26
 */
27
class MockedIteratorTest extends \PHPUnit_Framework_TestCase
28
{
29 6
    protected function mockGenericIteratableList()
30
    {
31 6
        return $this->getMock('Axstrad\Component\Test\Example\ExampleList');
32
    }
33
34
    /**
35
     * Mock iterator
36
     *
37
     * This attaches all the required expectations in the right order so that
38
     * our iterator will act like an iterator!
39
     *
40
     * @param \Iterator $iterator The iterator object; this is what we attach
41
     *      all the expectations to
42
     * @param array An array of items that we will mock up, we will use the
43
     *      keys (if needed) and values of this array to return
44
     * @param boolean $includeCallsToKey Whether we want to mock up the calls
45
     *      to "key"; only needed if you are doing foreach ($foo as $k => $v)
46
     *      as opposed to foreach ($foo as $v)
47
     */
48 4
    protected function mockIterator(
49
        \Iterator $iterator,
50
        array $items,
51
        $includeCallsToKey = FALSE
52
    ) {
53
        $iterator
54 4
            ->expects(
55 4
                $this->at(0)
56
            )
57 4
            ->method('rewind')
58
        ;
59 4
        $counter = 1;
60
61 4
        foreach ($items as $k => $v) {
62
            $iterator
63 4
                ->expects(
64 4
                    $this->at($counter++)
65
                )
66 4
                ->method('valid')
67 4
                ->will(
68 4
                    $this->returnValue(TRUE)
69
                )
70
            ;
71
            $iterator
72 4
                ->expects(
73 4
                    $this->at($counter++)
74
                )
75 4
                ->method('current')
76 4
                ->will(
77 4
                    $this->returnValue($v)
78
                )
79
            ;
80 4
            if ($includeCallsToKey) {
81
                $iterator
82 2
                    ->expects(
83 2
                        $this->at($counter++)
84
                    )
85 2
                    ->method('key')
86 2
                    ->will(
87 2
                        $this->returnValue($k)
88
                    )
89
                ;
90
            }
91
            $iterator
92 4
                ->expects(
93 4
                    $this->at($counter++)
94
                )
95 4
                ->method('next')
96
            ;
97
        }
98
99
        $iterator
100 4
            ->expects(
101 4
                $this->at($counter)
102
            )
103 4
            ->method('valid')
104 4
            ->will(
105 4
                $this->returnValue(FALSE)
106
            )
107
        ;
108 4
    }
109
110 1
    public function testConstructs()
111
    {
112 1
        $list = $this->mockGenericIteratableList();
113 1
        $this->assertInstanceOf('Axstrad\Component\Test\Example\ExampleList', $list);
114 1
        $this->assertInstanceOf('Iterator', $list);
115 1
    }
116
117 1
    public function testHasZeroItemsWhenWeHaventMockedMethods()
118
    {
119 1
        $list = $this->mockGenericIteratableList();
120 1
        $counter = 0;
121 1
        foreach ($list as $item) {
122
            // @codeCoverageIgnoreStart
123
            $counter++;
124
            // @codeCoverageIgnoreEnd
125
        }
126 1
        $this->assertEquals(0, $counter);
127 1
    }
128
129 1
    public function testWhenMockOneIterationWithNoKey()
130
    {
131 1
        $list = $this->mockGenericIteratableList();
132
133 1
        $this->mockIterator($list, array('This is the first item'));
134
135 1
        $counter = 0;
136 1
        foreach ($list as $value) {
137 1
            $counter++;
138
        }
139 1
        $this->assertEquals(1, $counter);
140 1
        $this->assertEquals('This is the first item', $value);
141 1
    }
142
143 1
    public function testWhenMockOneIterationWithKey()
144
    {
145 1
        $list = $this->mockGenericIteratableList();
146
147 1
        $this->mockIterator($list, array('key1' => 'This is the first item'), TRUE);
148
149 1
        $counter = 0;
150 1
        foreach ($list as $key => $value) {
151 1
            $counter++;
152
        }
153 1
        $this->assertEquals(1, $counter);
154 1
        $this->assertEquals('key1', $key);
155 1
        $this->assertEquals('This is the first item', $value);
156 1
    }
157
158 1
    public function testWhenMockThreeIterationWithNoKey()
159
    {
160 1
        $list = $this->mockGenericIteratableList();
161
162
        $expectedValues = array(
163 1
            'This is the first item',
164
            'This is the second item',
165
            'And the final item'
166
        );
167 1
        $this->mockIterator($list, $expectedValues);
168
169 1
        $counter = 0;
170 1
        $values = array();
171 1
        foreach ($list as $value) {
172 1
            $values[] = $value;
173 1
            $counter++;
174
        }
175 1
        $this->assertEquals(3, $counter);
176
177 1
        $this->assertEquals($expectedValues, $values);
178 1
    }
179
180 1
    public function testWhenMockThreeIterationWithKey()
181
    {
182 1
        $list = $this->mockGenericIteratableList();
183
184
        $expectedValues = array(
185 1
            'item1' => 'This is the first item',
186
            'foo'   => 'This is the second item!!',
187
            'bar'   => 'And the final item'
188
        );
189 1
        $this->mockIterator($list, $expectedValues, TRUE);
190
191 1
        $counter = 0;
192 1
        $values = array();
193 1
        foreach ($list as $key => $value) {
194 1
            $values[$key] = $value;
195 1
            $counter++;
196
        }
197 1
        $this->assertEquals(3, $counter);
198
199 1
        $this->assertEquals($expectedValues, $values);
200 1
    }
201
}
202