Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.44% |
63 / 64 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Base | |
98.44% |
63 / 64 |
|
83.33% |
5 / 6 |
19 | |
0.00% |
0 / 1 |
| setUp | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| tearDown | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getApiMockup | |
100.00% |
50 / 50 |
|
100.00% |
1 / 1 |
11 | |||
| getApiCalls | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGraphQL | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| setApiCalls | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | namespace BO\Zmsclient\PhpUnit; |
| 10 | |
| 11 | use Prophecy\PhpUnit\ProphecyTrait; |
| 12 | use Prophecy\Argument; |
| 13 | use BO\Zmsclient\GraphQL\GraphQLInterpreter; |
| 14 | |
| 15 | abstract class Base extends \BO\Slim\PhpUnit\Base |
| 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 $apiCalls = array(); |
| 30 | |
| 31 | public function setUp(): void |
| 32 | { |
| 33 | \App::$http = $this->getApiMockup(); |
| 34 | $this->sessionClass = new \BO\Zmsentities\Session(); |
| 35 | if (\BO\Zmsclient\SessionHandler::getLastInstance() instanceof \BO\Zmsclient\SessionHandler) { |
| 36 | \BO\Zmsclient\SessionHandler::getLastInstance()->setHttpHandler(\App::$http); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function tearDown(): void |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @SuppressWarnings(Cyclomatic) |
| 46 | * @return String |
| 47 | */ |
| 48 | protected function getApiMockup() |
| 49 | { |
| 50 | $mock = $this->prophesize('BO\Zmsclient\Http'); |
| 51 | foreach ($this->getApiCalls() as $options) { |
| 52 | $parameters = isset($options['parameters']) ? $options['parameters'] : null; |
| 53 | $xtoken = isset($options['xtoken']) ? $options['xtoken'] : null; |
| 54 | $function = $options['function']; |
| 55 | if ($function == 'readGetResult' || $function == 'readDeleteResult') { |
| 56 | $function = $mock->__call( |
| 57 | $function, |
| 58 | [ |
| 59 | $options['url'], |
| 60 | $parameters, |
| 61 | $xtoken |
| 62 | ] |
| 63 | ); |
| 64 | } elseif ($function == 'readPostResult') { |
| 65 | $function = $mock->__call( |
| 66 | $function, |
| 67 | [ |
| 68 | $options['url'], |
| 69 | Argument::that(function ($value) { |
| 70 | return |
| 71 | ($value instanceof \BO\Zmsentities\Schema\Entity) || |
| 72 | ($value instanceof \BO\Zmsentities\Collection\Base); |
| 73 | }), |
| 74 | $parameters |
| 75 | ] |
| 76 | ); |
| 77 | } else { |
| 78 | $function = $mock->__call( |
| 79 | $function, |
| 80 | [ |
| 81 | $parameters |
| 82 | ] |
| 83 | ); |
| 84 | } |
| 85 | if (isset($options['exception'])) { |
| 86 | $function->will(new \Prophecy\Promise\ThrowPromise($options['exception'])); |
| 87 | } elseif (isset($options['response'])) { |
| 88 | $responseData = json_decode($options['response'], true); |
| 89 | $graphqlInterpreter = $this->getGraphQL($parameters); |
| 90 | if ($graphqlInterpreter) { |
| 91 | $responseData['data'] = $graphqlInterpreter->setJson(json_encode($responseData['data'])); |
| 92 | } |
| 93 | $function->shouldBeCalled() |
| 94 | ->willReturn( |
| 95 | new \BO\Zmsclient\Result( |
| 96 | $this->getResponse(json_encode($responseData), 200), |
| 97 | static::createBasicRequest() |
| 98 | ) |
| 99 | ); |
| 100 | } else { |
| 101 | $function->shouldBeCalled(); |
| 102 | } |
| 103 | } |
| 104 | $api = $mock->reveal(); |
| 105 | return $api; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Overwrite this function if api calls definition needs function calls |
| 110 | */ |
| 111 | protected function getApiCalls() |
| 112 | { |
| 113 | return $this->apiCalls; |
| 114 | } |
| 115 | |
| 116 | protected function getGraphQL($parameters) |
| 117 | { |
| 118 | if (isset($parameters['gql'])) { |
| 119 | $gqlString = $parameters['gql']; |
| 120 | if ($gqlString) { |
| 121 | $graphqlInterpreter = new GraphQLInterpreter($gqlString); |
| 122 | return $graphqlInterpreter; |
| 123 | } |
| 124 | } |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | public function setApiCalls($apiCalls) |
| 129 | { |
| 130 | $this->apiCalls = $apiCalls; |
| 131 | \App::$http = $this->getApiMockup(); |
| 132 | } |
| 133 | } |