ApplicationOutboundEventContract::verifyContract()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Tests\TestUtils\Contracts;
4
5
use App\Infrastructure\Events\ApplicationOutboundEvent;
6
use PHPUnit\Framework\TestCase;
7
8
abstract class ApplicationOutboundEventContract extends TestCase
9
{
10
11
    use ApplicationEventContractLoader;
12
13
    protected function verifyContracts(ApplicationOutboundEvent $event, array $contracts): bool
14
    {
15
        $invalidContracts = [];
16
        foreach ($contracts as $contract) {
17
            $msg = $this->verifyContract($event, $contract);
18
            if ($msg !== null) {
19
                $invalidContracts[$contract] = $msg;
20
            }
21
        }
22
        if (count($invalidContracts) > 0) {
23
            $this->fail(sprintf("The '%s' Output Event doesn't fulfill the following Contracts: %s", $event::class, json_encode($invalidContracts)));
24
        }
25
        return true;
26
    }
27
28
    private function verifyContract(ApplicationOutboundEvent $event, string $contract): ?string
29
    {
30
        $eventData = $event->getData();
31
        $contractData = $this->readContract($contract);
32
        $missingKeys = [];
33
        foreach (array_keys($contractData) as $key) {
34
            if (!isset($eventData[$key])) {
35
                array_push($missingKeys, $key);
36
            }
37
        }
38
        return count($missingKeys) == 0 ? null : implode(", ", $missingKeys);
39
    }
40
41
}