Lines n/a 0 / 0
Functions and Methods n/a 0 / 0
Classes and Traits n/a 0 / 0
Name Lines Functions and Methods CRAP Classes and Traits
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    #[\Override]
32    public function setUp(): void
33    {
34        \App::$http = $this->getApiMockup();
35    }
36
37    #[\Override]
38    public function tearDown(): void
39    {
40    }
41
42    protected function getApiMockup(): Http
43    {
44        $mock = $this->prophesize(Http::class);
45        foreach ($this->getApiCalls() as $options) {
46            $parameters = isset($options['parameters']) ? $options['parameters'] : null;
47            $function = $options['function'];
48            if ($function == 'readGetResult' || $function == 'readDeleteResult') {
49                $function = $mock->__call(
50                    $function,
51                    [
52                        $options['url'],
53                        $parameters
54                    ]
55                );
56            } elseif ($function == 'readPostResult') {
57                $function = $mock->__call(
58                    $function,
59                    [
60                        $options['url'],
61                        Argument::that(function (mixed $value) {
62                            return
63                                ($value instanceof Entity) ||
64                                ($value instanceof BaseCollection);
65                        }),
66                        $parameters
67                    ]
68                );
69            } else {
70                $function = $mock->__call(
71                    $function,
72                    $parameters ?? []
73                );
74            }
75            if (isset($options['exception'])) {
76                $function->will(new \Prophecy\Promise\ThrowPromise($options['exception']));
77            } elseif (isset($options['response'])) {
78                $function->shouldBeCalled()
79                    ->willReturn(
80                        new \BO\Zmsclient\Result(
81                            $this->getResponse($options['response'], 200),
82                            $this->getRequest()
83                        )
84                    );
85            } else {
86                $function->shouldBeCalled();
87            }
88        }
89        $api = $mock->reveal();
90        return $api;
91    }
92
93    /**
94     * Overwrite this function if api calls definition needs function calls
95     */
96    protected function getApiCalls(): mixed
97    {
98        return $this->apiCalls;
99    }
100
101    abstract protected function getResponse(mixed $content = '', mixed $status = 200): mixed;
102
103    abstract protected function getRequest(mixed $method = "GET", mixed $uri = ''): mixed;
104
105    public function setApiCalls(mixed $apiCalls): void
106    {
107        $this->apiCalls = $apiCalls;
108        \App::$http = $this->getApiMockup();
109    }
110}