Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 39
ClientFake
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 39
 setResultData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 getRequestParams
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 send
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 9
 systemSend
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 createResponse
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 22
1<?php
2
3namespace Qmp\Laravel\MicroService\Client;
4
5use Qmp\Laravel\MicroService\Client\Exceptions\ClientException;
6use Qmp\Laravel\MicroService\Client\Tools\Http;
7use Qmp\Laravel\MicroService\Client\Tools\Request;
8
9class ClientFake extends ClientCommon
10{
11    private static $awaitingResult = [];
12
13    public static function setResultData(string $service, array $result, int $code)
14    {
15        static::$awaitingResult[$service] = [
16            'result' => $result,
17            'code' => $code,
18            'request_params' => []
19        ];
20    }
21
22    public static function getRequestParams($service)
23    {
24        if (isset(static::$awaitingResult[$domain])) {
25            return static::$awaitingResult[$domain]['request_params'];
26        }
27
28        return [];
29    }
30
31    protected static function send($method, Request $request, $id = null)
32    {
33        $domain = str_replace('http://', '', $request->getUrl());
34
35        $result = [
36            'result' => [],
37            'code' => 200
38        ];
39        if (isset(static::$awaitingResult[$domain])) {
40            $result = static::$awaitingResult[$domain];
41
42            static::$awaitingResult[$domain]['request_params'] = [
43                'method' => $method,
44                'request' => $request,
45                'id' => $id
46            ];
47        }
48
49        return static::createResponse($result);
50    }
51
52    public static function systemSend($method, Request $request, $id = null)
53    {
54        return static::send($method, $request, $id);
55    }
56
57    private static function createResponse($result)
58    {
59        return new class ($result) {
60            private $response = [
61                'code' => null,
62                'reason' => null,
63                'body' => null,
64                'content' => []
65            ];
66
67            public function __construct($response)
68            {
69                $this->response = [
70                    'code' => $response['code'],
71                    'reason' => '',
72                    'body' => $response['result'],
73                    'content' => $response['result'],
74                    'headers' => []
75                ];
76            }
77
78            public function __get($property)
79            {
80                if (isset($this->response[$property])) {
81                    return $this->response[$property];
82                }
83
84                return null;
85            }
86
87            public function __set($property, $value)
88            {
89                if (isset($this->response[$property])) {
90                    $this->response[$property] = $value;
91                }
92            }
93
94            public function __call($method, array $arguments)
95            {
96                $field = reset($arguments);
97
98                if (!in_array($field, array_keys($this->response['content']))) {
99                    throw new ClientException('Field ' . $field . ' not exist', 31);
100                }
101
102                switch ($method) {
103                    case 'get':
104                        return $this->response['content'][$field];
105                    case 'has':
106                        return $this->response['content'][$field] !== null;
107
108                    default:
109                        throw new ClientException('Method ' . $method  .' is not allowed', 32);
110                }
111            }
112        };
113    }
114}