Lines
98.57%
69 / 70
Functions and Methods
83.33%
5 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Base | 98.57% | 69 / 70 | 83.33% | 5 / 6 | 22 | 0.00% | 0 / 1 | |||
| setUp | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | |||||
| tearDown | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getApiMockup | 100.00% | 55 / 55 | 100.00% | 1 / 1 | 14 | |||||
| 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 | use BO\Zmsclient\Http; | |
| 15 | use BO\Zmsentities\Session; | |
| 16 | use BO\Zmsclient\SessionHandler; | |
| 17 | ||
| 18 | abstract class Base extends \BO\Slim\PhpUnit\Base | |
| 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 array $apiCalls = array(); | |
| 33 | ||
| 34 | #[\Override] | |
| 35 | public function setUp(): void | |
| 36 | { | |
| 37 | \App::$http = $this->getApiMockup(); | |
| 38 | $this->sessionClass = new Session(); | |
| 39 | $handler = SessionHandler::getLastInstance(); | |
| 40 | if ($handler instanceof SessionHandler) { | |
| 41 | $handler->setHttpHandler(\App::$http); | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | #[\Override] | |
| 46 | public function tearDown(): void | |
| 47 | { | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * @SuppressWarnings(Cyclomatic) | |
| 52 | * @psalm-api | |
| 53 | */ | |
| 54 | protected function getApiMockup(): Http | |
| 55 | { | |
| 56 | $mock = $this->prophesize(Http::class); | |
| 57 | foreach ($this->getApiCalls() as $options) { | |
| 58 | $parameters = isset($options['parameters']) ? $options['parameters'] : null; | |
| 59 | $xtoken = isset($options['xtoken']) ? $options['xtoken'] : null; | |
| 60 | $function = $options['function']; | |
| 61 | if ($function == 'readGetResult' || $function == 'readDeleteResult') { | |
| 62 | $function = $mock->__call( | |
| 63 | $function, | |
| 64 | [ | |
| 65 | $options['url'], | |
| 66 | $parameters, | |
| 67 | $xtoken | |
| 68 | ] | |
| 69 | ); | |
| 70 | } elseif ($function == 'readPostResult') { | |
| 71 | $function = $mock->__call( | |
| 72 | $function, | |
| 73 | [ | |
| 74 | $options['url'], | |
| 75 | Argument::that(function (mixed $value): bool { | |
| 76 | return | |
| 77 | is_array($value) || | |
| 78 | ($value instanceof \BO\Zmsentities\Schema\Entity) || | |
| 79 | ($value instanceof \BO\Zmsentities\Collection\Base); | |
| 80 | }), | |
| 81 | $parameters | |
| 82 | ] | |
| 83 | ); | |
| 84 | } else { | |
| 85 | $function = $mock->__call( | |
| 86 | $function, | |
| 87 | [ | |
| 88 | $parameters | |
| 89 | ] | |
| 90 | ); | |
| 91 | } | |
| 92 | if (isset($options['exception'])) { | |
| 93 | $function->will(new \Prophecy\Promise\ThrowPromise($options['exception'])); | |
| 94 | } elseif (isset($options['response'])) { | |
| 95 | $responseData = json_decode($options['response'], true); | |
| 96 | $graphqlInterpreter = $this->getGraphQL($parameters); | |
| 97 | if ($graphqlInterpreter) { | |
| 98 | $encodedData = json_encode($responseData['data']); | |
| 99 | $responseData['data'] = $graphqlInterpreter->setJson( | |
| 100 | $encodedData === false ? 'null' : $encodedData | |
| 101 | ); | |
| 102 | } | |
| 103 | $encoded = json_encode($responseData); | |
| 104 | $function->shouldBeCalled() | |
| 105 | ->willReturn( | |
| 106 | new \BO\Zmsclient\Result( | |
| 107 | $this->getResponse($encoded === false ? '{}' : $encoded, 200), | |
| 108 | static::createBasicRequest() | |
| 109 | ) | |
| 110 | ); | |
| 111 | } else { | |
| 112 | $function->shouldBeCalled(); | |
| 113 | } | |
| 114 | } | |
| 115 | $api = $mock->reveal(); | |
| 116 | return $api; | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Overwrite this function if api calls definition needs function calls | |
| 121 | * @psalm-api | |
| 122 | */ | |
| 123 | protected function getApiCalls(): array | |
| 124 | { | |
| 125 | return $this->apiCalls; | |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * @psalm-api | |
| 130 | */ | |
| 131 | protected function getGraphQL(?array $parameters): GraphQLInterpreter|null | |
| 132 | { | |
| 133 | if (isset($parameters['gql'])) { | |
| 134 | $gqlString = $parameters['gql']; | |
| 135 | if ($gqlString) { | |
| 136 | $graphqlInterpreter = new GraphQLInterpreter($gqlString); | |
| 137 | return $graphqlInterpreter; | |
| 138 | } | |
| 139 | } | |
| 140 | return null; | |
| 141 | } | |
| 142 | ||
| 143 | public function setApiCalls(array $apiCalls): void | |
| 144 | { | |
| 145 | $this->apiCalls = $apiCalls; | |
| 146 | \App::$http = $this->getApiMockup(); | |
| 147 | } | |
| 148 | } |