Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.95% |
17 / 21 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| GetAndSetObjectField | |
80.95% |
17 / 21 |
|
75.00% |
3 / 4 |
11.84 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInputValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| assertResponseValue | |
71.43% |
10 / 14 |
|
0.00% |
0 / 1 |
6.84 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\IntegrationTests\Requests\JsonFields; |
| 4 | |
| 5 | use PHPUnit\Framework\TestCase; |
| 6 | |
| 7 | class GetAndSetObjectField implements JsonSetFieldInterface, JsonGetFieldInterface |
| 8 | { |
| 9 | /** |
| 10 | * @var array<int, JsonGetFieldInterface|JsonSetFieldInterface> $fields |
| 11 | */ |
| 12 | private array $fields; |
| 13 | |
| 14 | public function __construct( |
| 15 | private readonly string $name, |
| 16 | JsonGetFieldInterface|JsonSetFieldInterface... $fields |
| 17 | ) { |
| 18 | $this->fields = $fields; |
| 19 | } |
| 20 | |
| 21 | public function getInputValue(): mixed |
| 22 | { |
| 23 | $data = []; |
| 24 | foreach ($this->fields as $field) { |
| 25 | if ($field instanceof JsonGetFieldInterface) { |
| 26 | $data[$field->getName()] = $field->getInputValue(); |
| 27 | } |
| 28 | } |
| 29 | return $data; |
| 30 | } |
| 31 | |
| 32 | public function getName(): string |
| 33 | { |
| 34 | return $this->name; |
| 35 | } |
| 36 | |
| 37 | public function assertResponseValue(mixed $responseValue): void |
| 38 | { |
| 39 | TestCase::assertIsArray($responseValue); |
| 40 | $testedFields = []; |
| 41 | foreach ($this->fields as $field) { |
| 42 | if ($field instanceof JsonSetFieldInterface) { |
| 43 | $fieldName = $field->getName(); |
| 44 | $testedFields[$fieldName] = $fieldName; |
| 45 | if (!array_key_exists($fieldName, $responseValue)) { |
| 46 | TestCase::fail( |
| 47 | 'field ' . $fieldName . ' is defined, but not found in the response, found: ' . implode(', ', array_keys($responseValue)) |
| 48 | ); |
| 49 | } |
| 50 | $field->assertResponseValue($responseValue[$fieldName]); |
| 51 | } |
| 52 | } |
| 53 | foreach (array_keys($responseValue) as $foundFieldname) { |
| 54 | if (!isset($testedFields[$foundFieldname])) { |
| 55 | TestCase::fail('Field ' . $foundFieldname . ' is found, but no assertions were added!'); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |