Passed
Push — master ( ddbf8b...086098 )
by Robbie
11:17
created

HttpRequestMockBuilder::buildRequestMock()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 6
nop 5
dl 0
loc 35
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\Session;
7
8
trait HttpRequestMockBuilder
9
{
10
    /**
11
     * Builds and returns a new mock instance of HTTPRequest
12
     *
13
     * @param string $url
14
     * @param array $getVars GET parameters
15
     * @param array $postVars POST parameters
16
     * @param string|null $method HTTP method
17
     * @param Session|null $session Session instance
18
     *
19
     * @return HTTPRequest
20
     */
21
    public function buildRequestMock($url, $getVars = [], $postVars = [], $method = null, Session $session = null)
22
    {
23
        if (is_null($session)) {
24
            $session = new Session([]);
25
        }
26
27
        $request = $this->createMock(HTTPRequest::class);
0 ignored issues
show
Bug introduced by
It seems like createMock() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $request = $this->createMock(HTTPRequest::class);
Loading history...
28
29
        $request->method('getSession')->willReturn($session);
30
31
        $request->method('getURL')->will($this->returnCallback(static function ($addParams) use ($url, $getVars) {
0 ignored issues
show
Bug introduced by
It seems like returnCallback() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        $request->method('getURL')->will($this->/** @scrutinizer ignore-call */ returnCallback(static function ($addParams) use ($url, $getVars) {
Loading history...
32
            return $addParams && count($getVars) ? $url.'?'.http_build_query($getVars) : $url;
33
        }));
34
35
        $request->method('getVars')->willReturn($getVars);
36
        $request->method('getVar')->will($this->returnCallback(static function ($key) use ($getVars) {
37
            return isset($getVars[$key]) ? $getVars[$key] : null;
38
        }));
39
40
        $request->method('postVars')->willReturn($postVars);
41
        $request->method('postVar')->will($this->returnCallback(static function ($key) use ($postVars) {
42
            return isset($postVars[$key]) ? $postVars[$key] : null;
43
        }));
44
45
        if (is_null($method)) {
46
            if (count($postVars)) {
47
                $method = 'POST';
48
            } else {
49
                $method = 'GET';
50
            }
51
        }
52
53
        $request->method('httpMethod')->willReturn($method);
54
55
        return $request;
56
    }
57
}
58