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