Passed
Push — master ( 02721e...1546a1 )
by SignpostMarv
02:51
created

Utilities   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Tests;
8
9
use PHPUnit\Framework\TestCase;
10
use ReflectionClass;
11
use SignpostMarv\DaftFramework\Framework;
12
use SignpostMarv\DaftFramework\HttpHandler;
13
14
class Utilities
15
{
16
    public static function ObtainFrameworkInstance(
17
        TestCase $testCase,
18
        string $implementation,
19
        ...$implementationArgs
20
    ) : Framework {
21
        $testCase->assertTrue(
22
            is_a($implementation, Framework::class, true),
23
            sprintf(
24
                'Argument %u passed to %s must be an implementation of %s',
25
                1,
26
                __METHOD__,
27
                Framework::class
28
            )
29
        );
30
31
        return new $implementation(...$implementationArgs);
32
    }
33
34
    public static function ObtainHttpHandlerInstance(
35
        TestCase $testCase,
36
        string $implementation,
37
        ...$implementationArgs
38
    ) : HttpHandler {
39
        $testCase->assertTrue(
40
            is_a($implementation, HttpHandler::class, true),
41
            sprintf(
42
                'Argument %u passed to %s must be an implementation of %s',
43
                1,
44
                __METHOD__,
45
                HttpHandler::class
46
            )
47
        );
48
49
        /**
50
        * @var HttpHandler $instance
51
        */
52
        $instance = static::ObtainFrameworkInstance(
53
            $testCase,
54
            $implementation,
55
            ...$implementationArgs
56
        );
57
58
        return $instance;
59
    }
60
61
    public static function ConfigureFrameworkInstance(
62
        TestCase $testCase,
63
        Framework $instance,
64
        array $postConstructionCalls
65
    ) : void {
66
        if (count($postConstructionCalls) > 0) {
67
            $reflector = new ReflectionClass($instance);
68
69
            foreach (array_keys($postConstructionCalls) as $method) {
70
                $testCase->assertTrue(method_exists($instance, $method), sprintf(
71
                    'Argument %u passed to %s must contain keys referring to methods on %s',
72
                    2,
73
                    __METHOD__,
74
                    get_class($instance)
75
                ));
76
77
                $instance->$method(...($postConstructionCalls[$method]));
78
            }
79
        }
80
    }
81
}
82