TestListener::getClosureDump()   B
last analyzed

Complexity

Conditions 9
Paths 64

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
nc 64
nop 2
dl 0
loc 38
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\ConformanceTest;
6
7
use PHPUnit\Framework\Test;
8
use PHPUnit\Framework\TestSuite;
9
use PHPUnit\Framework\TestListenerDefaultImplementation;
10
use PHPUnit\Framework\TestListener as TestListenerInterface;
11
use Psr\Container\ContainerInterface;
12
use ReflectionFunction;
13
use Facile\OpenIDClient\ConformanceTest\RpTest\ResponseTypeAndResponseMode\RPResponseTypeCodeTest;
14
use Facile\OpenIDClient\ConformanceTest\RpTest\RpTestInterface;
15
16
class TestListener implements TestListenerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated: Use the `TestHook` interfaces instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

16
class TestListener implements /** @scrutinizer ignore-deprecated */ TestListenerInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
17
{
18
    use TestListenerDefaultImplementation;
0 ignored issues
show
Deprecated Code introduced by
The trait PHPUnit\Framework\TestLi...erDefaultImplementation has been deprecated: The `TestListener` interface is deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

18
    use /** @scrutinizer ignore-deprecated */ TestListenerDefaultImplementation;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
19
20
    /** @var ContainerInterface */
21
    private $container;
22
23
    /** @var RpTestUtil */
24
    private $rpTestUtil;
25
26
    /** @var null|string */
27
    private $rpProfile;
0 ignored issues
show
introduced by
The private property $rpProfile is not used, and could be removed.
Loading history...
28
29
    /** @var null|string */
30
    private $rpTestId;
0 ignored issues
show
introduced by
The private property $rpTestId is not used, and could be removed.
Loading history...
31
32
    /**
33
     * TestListener constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->container = require __DIR__ . '/../config/container.php';
38
        $this->rpTestUtil = $this->container->get(RpTestUtil::class);
39
    }
40
41
    public function getRoot(): string
42
    {
43
        return 'https://rp.certification.openid.net:8080/';
44
    }
45
46
    public function getRpId(): string
47
    {
48
        return 'tmv_php-openid-client';
49
    }
50
51
    /**
52
     * A test suite started.
53
     */
54
    public function startTestSuite(TestSuite $suite): void
55
    {
56
        if (0 !== \strpos($suite->getName(), 'rp-')) {
57
            return;
58
        }
59
60
        $profile = \substr($suite->getName(), 3);
61
62
        $testMap = [
63
            'code-basic' => [
64
                'rp-response_type-code' => new RPResponseTypeCodeTest($this->container)
65
            ]
66
        ];
67
68
        $this->rpTestUtil->clearLogs($this->getRoot(), $this->getRpId());
69
70
        $tests = $testMap[$profile] ?? [];
71
72
        foreach ($tests as $test) {
73
            $suite->addTest($test);
74
        }
75
    }
76
77
    /**
78
     * A test suite ended.
79
     */
80
    public function endTestSuite(TestSuite $suite): void
81
    {
82
        // TODO: Implement endTestSuite() method.
83
    }
84
85
    /**
86
     * A test started.
87
     */
88
    public function startTest(Test $test): void
89
    {
90
        if (! $test instanceof RpTestInterface) {
91
            return;
92
        }
93
94
    }
95
96
    /**
97
     * A test ended.
98
     */
99
    public function endTest(Test $test, float $time): void
100
    {
101
        if (! $test instanceof RpTestInterface) {
102
            return;
103
        }
104
105
        $testUtil = $this->container->get(RpTestUtil::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $testUtil is dead and can be removed.
Loading history...
106
107
        $implementation = $this->getClosureDump([$test, 'execute']);
0 ignored issues
show
Unused Code introduced by
The assignment to $implementation is dead and can be removed.
Loading history...
108
    }
109
110
    protected function getClosureDump(callable $closure, int $indent = 4) {
111
        if (\is_callable($closure)) {
112
            $closure = \Closure::fromCallable($closure);
113
        }
114
115
        $r = new ReflectionFunction($closure);
116
        $lines = \file($r->getFileName());
117
        $lines = \array_slice($lines, $r->getStartLine(), $r->getEndLine() - $r->getStartLine());
118
        if (\preg_match('/^ *{ *$/', $lines[0] ?? '')) {
119
            unset($lines[0]);
120
        }
121
122
        $firstLine = \array_shift($lines) ?: '';
123
124
        if (! \preg_match('/^ *{ *$/', $firstLine)) {
125
            \array_unshift($lines, $firstLine);
126
        }
127
128
        $lastLine = \array_pop($lines) ?: '';
129
        if (! \preg_match('/^ *} *$/', $lastLine)) {
130
            \array_push($lines, $lastLine);
131
        }
132
133
        // remove spaces based on first line
134
        if (\preg_match('/^( +)/', $lines[0] ?? '', $matches)) {
135
            $toTrim = \strlen($matches[1]);
136
            $lines = \array_map(static function (string $line) use ($toTrim) {
137
                return \preg_replace(sprintf('/^ {0,%d}/', $toTrim), '', $line);
138
            }, $lines);
139
        }
140
141
        if ($indent) {
142
            $lines = \array_map(static function (string $line) use ($indent) {
143
                return \str_repeat(' ', $indent) . $line;
144
            }, $lines);
145
        }
146
147
        return \implode('', $lines);
148
    }
149
}
150