summaryrefslogtreecommitdiffstats
path: root/test/Iterator/IteratorQueryTest.php
blob: 1f1953eed96873e9a335022b24877672becbd101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

namespace League\Csv\Iterator;

use ArrayIterator;
use ReflectionClass;
use PHPUnit_Framework_TestCase;

/**
 * @group iterator
 */
class IteratorQueryTest extends PHPUnit_Framework_TestCase
{
    private $traitQuery;
    private $iterator;
    private $data = ['john', 'jane', 'foo', 'bar'];

    public function invokeMethod(&$object, $methodName, array $parameters = [])
    {
        $reflection = new ReflectionClass(get_class($object));
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(true);

        return $method->invokeArgs($object, $parameters);
    }

    private function createTraitObject()
    {
        return $this->getObjectForTrait('\League\Csv\Iterator\IteratorQuery');
    }

    public function setUp()
    {
        $this->traitQuery = $this->createTraitObject();
        $this->iterator = new ArrayIterator($this->data);
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testSetLimit()
    {
        $this->traitQuery->setLimit(1);
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);
        $this->assertCount(1, $res);

        $this->traitQuery->setLimit(-4);
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testSetOffset()
    {
        $this->traitQuery->setOffset(1);
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);
        $this->assertCount(3, $res);

        $this->traitQuery->setOffset('toto');
    }

    public function testIntervalLimitTooLong()
    {
        $this->traitQuery->setOffset(3);
        $this->traitQuery->setLimit(10);
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);
        $this->assertSame([3 => 'bar'], $res);
        $this->assertCount(1, $res);
    }

    public function testInterval()
    {
        $this->traitQuery->setOffset(1);
        $this->traitQuery->setLimit(1);
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);
        $this->assertCount(1, $res);
    }

    public function testFilter()
    {
        $func = function ($row) {
            return $row == 'john';
        };
        $this->traitQuery->setFilter($func);

        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);
        $this->assertCount(1, $res);
    }

    public function testSortBy()
    {
        $this->traitQuery->setSortBy('strcmp');
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
        $res = iterator_to_array($iterator);

        $this->assertSame(['bar', 'foo', 'jane', 'john'], array_values($res));
    }

    public function testExecuteWithCallback()
    {
        $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator, function ($value) {
            return strtoupper($value);
        }]);
        $this->assertSame(array_map('strtoupper', $this->data), iterator_to_array($iterator));
    }
}