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

TestCase::makeDummyResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 15
c 3
b 1
f 0
nc 1
nop 2
dl 0
loc 26
rs 9.7666
1
<?php namespace Scriptotek\Sru;
2
3
use GuzzleHttp\Psr7\Response as HttpResponse;
4
use function GuzzleHttp\Psr7\stream_for;
5
use Http\Mock\Client as MockHttp;
6
7
class TestCase extends \PHPUnit\Framework\TestCase
8
{
9
    protected $recordTpl = '<srw:record>
10
            <srw:recordSchema>marcxchange</srw:recordSchema>
11
            <srw:recordPacking>xml</srw:recordPacking>
12
            <srw:recordPosition>{{position}}</srw:recordPosition>
13
            <srw:recordData>{{data}}</srw:recordData>
14
          </srw:record>';
15
16
    protected $mainTpl = '<?xml version="1.0" encoding="UTF-8" ?>
17
      <srw:searchRetrieveResponse
18
        xmlns:srw="http://www.loc.gov/zing/srw/"
19
        xmlns:xcql="http://www.loc.gov/zing/cql/xcql/"
20
      >
21
        <srw:version>1.1</srw:version>
22
        <srw:numberOfRecords>{{numberOfRecords}}</srw:numberOfRecords>
23
        <srw:records>
24
          {{records}}
25
        </srw:records>
26
        <srw:echoedSearchRetrieveRequest>
27
          <srw:operation>searchRetrieve</srw:operation>
28
          <srw:version>1.1</srw:version>
29
          <srw:query>{{cql}}</srw:query>
30
          <srw:startRecord>{{startRecord}}</srw:startRecord>
31
          <srw:maximumRecords>{{maxRecords}}</srw:maximumRecords>
32
          <srw:recordSchema>marcxchange</srw:recordSchema>
33
        </srw:echoedSearchRetrieveRequest>
34
        <srw:extraResponseData>
35
          <responseDate>2014-03-28T12:09:50Z</responseDate>
36
        </srw:extraResponseData>
37
      </srw:searchRetrieveResponse>';
38
39
    /**
40
     * Get an item from an array using "dot" notation.
41
     * Source: http://laravel.com/api/source-function-array_get.html#226-251
42
     *
43
     * @param  array   $array
44
     * @param  string  $key
45
     * @param  mixed   $default
46
     * @return mixed
47
     */
48
    private function array_get($array, $key, $default = null)
49
    {
50
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
51
            return $array;
52
        }
53
54
        if (isset($array[$key])) {
55
            return $array[$key];
56
        }
57
58
        foreach (explode('.', $key) as $segment) {
59
            if (! is_array($array) or ! array_key_exists($segment, $array)) {
60
                return $default;
61
            }
62
63
            $array = $array[$segment];
64
        }
65
66
        return $array;
67
    }
68
69
    /**
70
     *  numberOfRecords : Total number of records in response
71
     */
72
    public function makeDummyResponse($numberOfRecords = 10, $options = array())
73
    {
74
        // Request: CQL
75
        $cql = $this->array_get($options, 'cql', 'dummy');
76
77
        // Request: First record to fetch
78
        $startRecord = $this->array_get($options, 'startRecord', 1);
79
80
        // Request: Max number of records to return
81
        $maxRecords = $this->array_get($options, 'maxRecords', 10);
82
83
        $endRecord = $startRecord + min($maxRecords - 1, $numberOfRecords - $startRecord);
84
85
        $recordTpl = $this->recordTpl;
86
        $records = implode('', array_map(function ($n) use ($recordTpl) {
87
            return str_replace(
88
                array('{{position}}', '{{data}}'),
89
                array($n, 'RecordData #' . $n),
90
                $recordTpl
91
            );
92
        }, range($startRecord, $endRecord)));
93
94
        return str_replace(
95
            array('{{records}}', '{{cql}}', '{{startRecord}}', '{{maxRecords}}', '{{numberOfRecords}}'),
96
            array($records, $cql, $startRecord, $maxRecords, $numberOfRecords),
97
            $this->mainTpl
98
        );
99
    }
100
101
    /**
102
     * Returns a series of responses (no matter what request)
103
     */
104
    protected function httpMockWithResponses($bodies)
105
    {
106
        if (!is_array($bodies)) {
107
            $bodies = [$bodies];
108
        }
109
        $http = new MockHttp();
110
        foreach ($bodies as $body) {
111
            $http->addResponse(
112
                (new HttpResponse())
113
                    ->withBody(stream_for($body))
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\stream_for() has been deprecated: stream_for will be removed in guzzlehttp/psr7:2.0. Use Utils::streamFor instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

113
                    ->withBody(/** @scrutinizer ignore-deprecated */ stream_for($body))

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
114
            );
115
        }
116
117
        return $http;
118
    }
119
}
120