Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
Base
n/a
0 / 0
n/a
0 / 0
13
n/a
0 / 0
 setUp
n/a
0 / 0
n/a
0 / 0
1
 tearDown
n/a
0 / 0
n/a
0 / 0
1
 getApiMockup
n/a
0 / 0
n/a
0 / 0
9
 getApiCalls
n/a
0 / 0
n/a
0 / 0
1
 getResponse
n/a
0 / 0
n/a
0 / 0
0
 getRequest
n/a
0 / 0
n/a
0 / 0
0
 setApiCalls
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace BO\Zmsmessaging\PhpUnit;
4
5use BO\Zmsentities\Schema\Entity;
6use BO\Zmsentities\Collection\Base as BaseCollection;
7use BO\Zmsclient\Http;
8use PHPUnit\Framework\TestCase;
9use Prophecy\PhpUnit\ProphecyTrait;
10use Prophecy\Argument;
11
12/**
13 * @codeCoverageIgnore
14 */
15abstract class Base extends TestCase
16{
17    /**
18     * An array of API-Calls, e.g.:
19     * [
20     * [
21     * 'function' => 'readGetResult',
22     * 'url' => '/status/',
23     * 'response' => '{}'
24     * ],
25     * ]
26     */
27    use ProphecyTrait;
28
29    protected mixed $apiCalls = array();
30
31    public function setUp(): void
32    {
33        \App::$http = $this->getApiMockup();
34    }
35
36    public function tearDown(): void
37    {
38    }
39
40    protected function getApiMockup(): Http
41    {
42        $mock = $this->prophesize(Http::class);
43        foreach ($this->getApiCalls() as $options) {
44            $parameters = isset($options['parameters']) ? $options['parameters'] : null;
45            $function = $options['function'];
46            if ($function == 'readGetResult' || $function == 'readDeleteResult') {
47                $function = $mock->__call(
48                    $function,
49                    [
50                        $options['url'],
51                        $parameters
52                    ]
53                );
54            } elseif ($function == 'readPostResult') {
55                $function = $mock->__call(
56                    $function,
57                    [
58                        $options['url'],
59                        Argument::that(function (mixed $value) {
60                            return
61                                ($value instanceof Entity) ||
62                                ($value instanceof BaseCollection);
63                        }),
64                        $parameters
65                    ]
66                );
67            } else {
68                $function = $mock->__call(
69                    $function,
70                    $parameters ?? []
71                );
72            }
73            if (isset($options['exception'])) {
74                $function->will(new \Prophecy\Promise\ThrowPromise($options['exception']));
75            } elseif (isset($options['response'])) {
76                $function->shouldBeCalled()
77                    ->willReturn(
78                        new \BO\Zmsclient\Result(
79                            $this->getResponse($options['response'], 200),
80                            $this->getRequest()
81                        )
82                    );
83            } else {
84                $function->shouldBeCalled();
85            }
86        }
87        $api = $mock->reveal();
88        return $api;
89    }
90
91    /**
92     * Overwrite this function if api calls definition needs function calls
93     */
94    protected function getApiCalls(): mixed
95    {
96        return $this->apiCalls;
97    }
98
99    abstract protected function getResponse(mixed $content = '', mixed $status = 200): mixed;
100
101    abstract protected function getRequest(mixed $method = "GET", mixed $uri = ''): mixed;
102
103    public function setApiCalls(mixed $apiCalls): void
104    {
105        $this->apiCalls = $apiCalls;
106        \App::$http = $this->getApiMockup();
107    }
108}