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     */
50    protected function getApiMockup()
51    {
52        $mock = $this->prophesize('BO\Zmsclient\Http');
53        foreach ($this->getApiCalls() as $options) {
54            $parameters = isset($options['parameters']) ? $options['parameters'] : null;
55            $xtoken = isset($options['xtoken']) ? $options['xtoken'] : null;
56            $function = $options['function'];
57            if ($function == 'readGetResult' || $function == 'readDeleteResult') {
58                $function = $mock->__call(
59                    $function,
60                    [
61                        $options['url'],
62                        $parameters,
63                        $xtoken
64                    ]
65                );
66            } elseif ($function == 'readPostResult') {
67                $function = $mock->__call(
68                    $function,
69                    [
70                        $options['url'],
71                        Argument::that(function ($value) {
72                            return
73                                ($value instanceof \BO\Zmsentities\Schema\Entity) ||
74                                ($value instanceof \BO\Zmsentities\Collection\Base);
75                        }),
76                        $parameters
77                    ]
78                );
79            } else {
80                $function = $mock->__call(
81                    $function,
82                    [
83                        $parameters
84                    ]
85                );
86            }
87            if (isset($options['exception'])) {
88                $function->will(new \Prophecy\Promise\ThrowPromise($options['exception']));
89            } elseif (isset($options['response'])) {
90                $responseData = json_decode($options['response'], true);
91                $graphqlInterpreter = $this->getGraphQL($parameters);
92                if ($graphqlInterpreter) {
93                    $responseData['data'] = $graphqlInterpreter->setJson(json_encode($responseData['data']));
94                }
95                $function->shouldBeCalled()
96                    ->willReturn(
97                        new \BO\Zmsclient\Result(
98                            $this->getResponse(json_encode($responseData), 200),
99                            static::createBasicRequest()
100                        )
101                    );
102            } else {
103                $function->shouldBeCalled();
104            }
105        }
106        $api = $mock->reveal();
107        return $api;
108    }
109
110    /**
111     * Overwrite this function if api calls definition needs function calls
112     */
113    protected function getApiCalls()
114    {
115        return $this->apiCalls;
116    }
117
118    protected function getGraphQL($parameters)
119    {
120        if (isset($parameters['gql'])) {
121            $gqlString = $parameters['gql'];
122            if ($gqlString) {
123                $graphqlInterpreter = new GraphQLInterpreter($gqlString);
124                return $graphqlInterpreter;
125            }
126        }
127        return null;
128    }
129
130    public function setApiCalls($apiCalls)
131    {
132        $this->apiCalls = $apiCalls;
133        \App::$http = $this->getApiMockup();
134    }
135}