Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.57% covered (success)
98.57%
69 / 70
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Base
98.57% covered (success)
98.57%
69 / 70
83.33% covered (warning)
83.33%
5 / 6
22
0.00% covered (danger)
0.00%
0 / 1
 setUp
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 tearDown
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApiMockup
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
1 / 1
14
 getApiCalls
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGraphQL
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 setApiCalls
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 *
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 *
7 */
8
9namespace BO\Zmsclient\PhpUnit;
10
11use Prophecy\PhpUnit\ProphecyTrait;
12use Prophecy\Argument;
13use BO\Zmsclient\GraphQL\GraphQLInterpreter;
14use BO\Zmsclient\Http;
15use BO\Zmsentities\Session;
16use BO\Zmsclient\SessionHandler;
17
18abstract class Base extends \BO\Slim\PhpUnit\Base
19{
20    /**
21     * An array of API-Calls, e.g.:
22     * [
23     * [
24     * 'function' => 'readGetResult',
25     * 'url' => '/status/',
26     * 'response' => '{}'
27     * ],
28     * ]
29     */
30    use ProphecyTrait;
31
32    protected array $apiCalls = array();
33
34    public function setUp(): void
35    {
36        \App::$http = $this->getApiMockup();
37        $this->sessionClass = new Session();
38        $handler = SessionHandler::getLastInstance();
39        if ($handler instanceof SessionHandler) {
40            $handler->setHttpHandler(\App::$http);
41        }
42    }
43
44    public function tearDown(): void
45    {
46    }
47
48    /**
49     * @SuppressWarnings(Cyclomatic)
50     * @psalm-api
51     */
52    protected function getApiMockup(): Http
53    {
54        $mock = $this->prophesize(Http::class);
55        foreach ($this->getApiCalls() as $options) {
56            $parameters = isset($options['parameters']) ? $options['parameters'] : null;
57            $xtoken = isset($options['xtoken']) ? $options['xtoken'] : null;
58            $function = $options['function'];
59            if ($function == 'readGetResult' || $function == 'readDeleteResult') {
60                $function = $mock->__call(
61                    $function,
62                    [
63                        $options['url'],
64                        $parameters,
65                        $xtoken
66                    ]
67                );
68            } elseif ($function == 'readPostResult') {
69                $function = $mock->__call(
70                    $function,
71                    [
72                        $options['url'],
73                        Argument::that(function (mixed $value): bool {
74                            return
75                                is_array($value) ||
76                                ($value instanceof \BO\Zmsentities\Schema\Entity) ||
77                                ($value instanceof \BO\Zmsentities\Collection\Base);
78                        }),
79                        $parameters
80                    ]
81                );
82            } else {
83                $function = $mock->__call(
84                    $function,
85                    [
86                        $parameters
87                    ]
88                );
89            }
90            if (isset($options['exception'])) {
91                $function->will(new \Prophecy\Promise\ThrowPromise($options['exception']));
92            } elseif (isset($options['response'])) {
93                $responseData = json_decode($options['response'], true);
94                $graphqlInterpreter = $this->getGraphQL($parameters);
95                if ($graphqlInterpreter) {
96                    $encodedData = json_encode($responseData['data']);
97                    $responseData['data'] = $graphqlInterpreter->setJson(
98                        $encodedData === false ? 'null' : $encodedData
99                    );
100                }
101                $encoded = json_encode($responseData);
102                $function->shouldBeCalled()
103                    ->willReturn(
104                        new \BO\Zmsclient\Result(
105                            $this->getResponse($encoded === false ? '{}' : $encoded, 200),
106                            static::createBasicRequest()
107                        )
108                    );
109            } else {
110                $function->shouldBeCalled();
111            }
112        }
113        $api = $mock->reveal();
114        return $api;
115    }
116
117    /**
118     * Overwrite this function if api calls definition needs function calls
119     * @psalm-api
120     */
121    protected function getApiCalls(): array
122    {
123        return $this->apiCalls;
124    }
125
126    /**
127     * @psalm-api
128     */
129    protected function getGraphQL(?array $parameters): GraphQLInterpreter|null
130    {
131        if (isset($parameters['gql'])) {
132            $gqlString = $parameters['gql'];
133            if ($gqlString) {
134                $graphqlInterpreter = new GraphQLInterpreter($gqlString);
135                return $graphqlInterpreter;
136            }
137        }
138        return null;
139    }
140
141    public function setApiCalls(array $apiCalls): void
142    {
143        $this->apiCalls = $apiCalls;
144        \App::$http = $this->getApiMockup();
145    }
146}