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