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