Passed
Push — main ( 6731d6...4efee0 )
by Dan Michael O.
11:38
created

RecordsTest::testRepeatSameResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 10
1
<?php namespace Scriptotek\Sru;
2
3
class RecordsTest extends TestCase
4
{
5
    public function testIterating()
6
    {
7
        $cql = 'dummy';
8
        $uri = 'http://localhost';
9
        $n = 8;
10
        $response = $this->makeDummyResponse($n);
11
        $http = $this->httpMockWithResponses([$response, $response]);
12
13
        $client = new Client($uri, [], $http);
14
        $records = new Records($cql, $client, 10);
15
        $this->assertEquals(8, $records->numberOfRecords());
16
        $records->rewind();
17
18
        $this->assertEquals(1, $records->key());
19
        $this->assertTrue($records->valid());
20
        $records->next();
21
        $records->next();
22
        $this->assertEquals(3, $records->key());
23
        $this->assertTrue($records->valid());
24
        $records->rewind();
25
        $this->assertEquals(1, $records->key());
26
        $this->assertTrue($records->valid());
27
28
        $i = 0;
29
        foreach ($records as $rec) {
30
            $i++;
31
        }
32
        $this->assertEquals($n, $i);
33
    }
34
35
    public function testRepeatSameResponse()
36
    {
37
        // Result set contains two records
38
        $response = $this->makeDummyResponse(2, array('maxRecords' => 1));
39
40
        $http = $this->httpMockWithResponses([$response, $response]);
41
        $uri = 'http://localhost';
42
        $cql = 'dummy';
43
44
        // Request only one record in each request
45
        $client = new Client($uri, [], $http);
46
        $rec = new Records($cql, $client, 1);
47
48
        // Jumping to position 2 should call fetchMore() and throw
49
        // an InvalidResponseException on getting the same response
50
        // as we got for position 1
51
        $this->expectException(\Scriptotek\Sru\Exceptions\InvalidResponseException::class);
52
        $rec->next();
53
    }
54
55
    public function testMultipleRequests()
56
    {
57
        $nrecs = 5;
58
59
        $responses = array(
60
            $this->makeDummyResponse($nrecs, array('startRecord' => 1, 'maxRecords' => 2)),
61
            $this->makeDummyResponse($nrecs, array('startRecord' => 3, 'maxRecords' => 2)),
62
            $this->makeDummyResponse($nrecs, array('startRecord' => 5, 'maxRecords' => 2))
63
        );
64
65
        $http = $this->httpMockWithResponses($responses);
66
        $uri = 'http://localhost';
67
        $cql = 'dummy';
68
69
        $client = new Client($uri, [], $http);
70
        $records = new Records($cql, $client, 10);
71
72
        $records->rewind();
73
        foreach (range(1, $nrecs) as $n) {
74
            $this->assertEquals($n, $records->key());
75
            $this->assertTrue($records->valid());
76
            $this->assertEquals($n, $records->current()->position);
77
            $records->next();
78
        }
79
        $this->assertFalse($records->valid());
80
    }
81
}
82