Completed
Pull Request — master (#3)
by Pavel
02:49
created

AbstractRpcTest::getRequestMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace ScayTrase\Api\Rpc\Tests;
4
5
use Prophecy\Argument;
6
use ScayTrase\Api\Rpc\ResponseCollectionInterface;
7
use ScayTrase\Api\Rpc\RpcClientInterface;
8
use ScayTrase\Api\Rpc\RpcErrorInterface;
9
use ScayTrase\Api\Rpc\RpcRequestInterface;
10
use ScayTrase\Api\Rpc\RpcResponseInterface;
11
12
abstract class AbstractRpcTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @param string $method
16
     * @param array  $params
17
     *
18
     * @return RpcRequestInterface
19
     */
20
    protected function getRequestMock($method, array $params = [])
21
    {
22
        $request = $this->prophesize(RpcRequestInterface::class);
23
        $request->getMethod()->willReturn($method);
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
24
        $request->getParameters()->willReturn((object)$params);
25
26
        return $request->reveal();
27
    }
28
29
    /**
30
     * @param bool                       $success
31
     * @param \stdClass|array|null|mixed $body
32
     * @param RpcErrorInterface          $error
33
     *
34
     * @return RpcResponseInterface
35
     */
36
    protected function getResponseMock($success = true, $body = null, RpcErrorInterface $error = null)
37
    {
38
        $mock = $this->prophesize(RpcResponseInterface::class);
39
        $mock->isSuccessful()->willReturn($success);
40
        $mock->getError()->willReturn($success ? null : $error);
41
        $mock->getBody()->willReturn($success ? $body : null);
42
43
        return $mock->reveal();
44
    }
45
46
    protected function getErrorMock($code, $message)
47
    {
48
        $mock = $this->prophesize(RpcErrorInterface::class);
49
        $mock->getCode()->willReturn($code);
50
        $mock->getMessage()->willReturn($message);
51
52
        return $mock->reveal();
53
    }
54
55
    /**
56
     * @param RpcRequestInterface[]  $requests
57
     * @param RpcResponseInterface[] $responses
58
     *
59
     * @return RpcClientInterface
60
     */
61
    protected function getClientMock(array $requests = [], array $responses = [])
62
    {
63
        self::assertEquals(count($requests), count($responses));
64
65
        $client = $this->prophesize(RpcClientInterface::class);
66
        $that   = $this;
67
        $client->invoke(Argument::type('array'))->will(
68
            function ($args) use ($that, $requests, $responses) {
69
                $collection = $that->prophesize(ResponseCollectionInterface::class);
70
                foreach ($requests as $key => $request) {
71
                    if (in_array($request, $args[0], true)) {
72
                        $collection->getResponse(Argument::exact($request))->willReturn($responses[$key]);
73
                    }
74
                }
75
76
                return $collection->reveal();
77
            }
78
        );
79
80
        return $client->reveal();
81
    }
82
}
83