Completed
Push — master ( dc93ef...18d0c0 )
by Kevin
02:08
created

Logger::getTestRunId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 0
1
<?php
2
3
namespace Magium\Util\Log;
4
5
use Exception;
6
use Magium\Assertions\AbstractAssertion;
7
use Magium\Util\Phpunit\MasterListenerAware;
8
use Magium\Util\Phpunit\MasterListenerInterface;
9
use PHPUnit\Framework\AssertionFailedError;
10
use PHPUnit\Framework\Test;
11
use PHPUnit\Framework\TestCase;
12
use PHPUnit\Framework\TestListener;
13
use PHPUnit\Framework\TestSuite;
14
use PHPUnit\Framework\Warning;
15
16
17
class Logger extends \Zend\Log\Logger implements TestListener, MasterListenerAware, LoggerInterface
18
{
19
20
    protected $status = self::STATUS_PASSED;
21
22
    protected $testName = self::NAME_DEFAULT;
23
24
    protected $testId;
25
26
    protected $invokedTest;
27
28
    protected $selectorConfig = null;
29
30
    protected static $testRunId;
31
32
    public function setMasterListener(MasterListenerInterface $listener)
33
    {
34
        $listener->addListener($this);
35
    }
36
37
38 View Code Duplication
    public function addCharacteristic($type, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $this->info(
41
            sprintf('Set %s to %s', $type, $value),
42
            [
43
                'type'              => 'characteristic',
44
                'characteristic'    => $type,
45
                'value'             => $value
46
            ]
47
        );
48
    }
49
50
    public function getInvokedTest()
51
    {
52
        return $this->invokedTest;
53
    }
54
55
    public function setTestName($name)
56
    {
57
        $this->testName = $name;
58
    }
59
60
    public function setTestStatus($status)
61
    {
62
        $this->status = $status;
63
    }
64
65
    public function setTestId($testId)
66
    {
67
        $this->testId = $testId;
68
    }
69
70
    public function logStep($stepId)
71
    {
72
        $this->log($stepId, $this->createExtra(['type' => 'step']));
73
    }
74
75
    public function logAssertionSuccess(AbstractAssertion $assertion, array $extra)
76
    {
77
        $extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_PASSED]);
78
        $this->info(get_class($assertion) . ' - passed', $this->createExtra($extra));
79
    }
80
81
    public function logAssertionFailure(Exception $e, AbstractAssertion $assertion, array $extra)
82
    {
83
        $extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_FAILED, 'stack_trace' => $e->getTrace()]);
84
        $this->err(get_class($assertion) . ' - ' . $e->getMessage(), $this->createExtra($extra));
85
    }
86
87 View Code Duplication
    public function createExtra($includeArray = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $defaultArray = [
90
            'type'      => 'message',
91
            'status'    => $this->status,
92
            'name'     => $this->testName,
93
            'test_id'    => $this->testId,
94
            'test_run_id' => $this->getTestRunId()
95
        ];
96
97
        if ($this->selectorConfig) {
98
            $defaultArray = array_merge($this->selectorConfig, $defaultArray);
99
        }
100
101
        return array_merge($defaultArray, $includeArray);
102
    }
103
104 View Code Duplication
    public function getTestRunId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (!self::$testRunId) {
107
            // See https://github.com/ircmaxell/RandomLib/issues/55
108
            if (function_exists('random_bytes')) {
109
                $unique = uniqid(substr(bin2hex(random_bytes(64)), 0, 64));
110
111
            } else if (function_exists('openssl_random_pseudo_bytes')) {
112
                $unique = uniqid(openssl_random_pseudo_bytes(64));
113
            } else {
114
                $unique = uniqid('', true);
115
            }
116
            self::$testRunId = $unique;
117
        }
118
119
        return self::$testRunId;
120
    }
121
122
    public function addWarning(Test $test, Warning $e, $time)
123
    {
124
        $this->warn($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()]));
125
    }
126
127
    public function addError(Test $test, \Exception $e, $time)
128
    {
129
        $this->setTestStatus(self::STATUS_FAILED);
130
        $this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()]));
131
    }
132
133
    public function addFailure(Test $test, AssertionFailedError $e, $time)
134
    {
135
        $this->setTestStatus(self::STATUS_FAILED);
136
        $this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()]));
137
    }
138
139
    public function addIncompleteTest(Test $test, Exception $e, $time)
140
    {
141
        $this->setTestStatus(self::STATUS_INCOMPLETE);
142
        $this->notice($e->getMessage(), $this->createExtra());
143
    }
144
145
    public function addRiskyTest(Test $test, Exception $e, $time)
146
    {
147
        $this->setTestStatus(self::STATUS_RISKY);
148
        $this->notice($e->getMessage(), $this->createExtra());
149
    }
150
151
    public function addSkippedTest(Test $test, Exception $e, $time)
152
    {
153
        self::setTestStatus(self::STATUS_SKIPPED);
154
        $this->notice($e->getMessage(), $this->createExtra());
155
    }
156
157
    public function startTestSuite(TestSuite $suite)
158
    {
159
        // TODO: Implement startTestSuite() method.
160
    }
161
162
    public function endTestSuite(TestSuite $suite)
163
    {
164
        $this->testName = self::NAME_DEFAULT;
165
    }
166
167 View Code Duplication
    public function startTest(Test $test)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        if ($test instanceof TestCase) {
170
            if (!$this->testName) {
171
                $this->setTestName(get_class($test) . '::' . $test->getName());
172
            }
173
            $this->invokedTest = get_class($test) . '::' . $test->getName();
174
            $this->setTestStatus(self::STATUS_PASSED);
175
        }
176
177
    }
178
179
    public function endTest(Test $test, $time)
180
    {
181
        $this->info(sprintf('Test completed with status: %s', $this->status), $this->createExtra());
182
        $this->testName = self::NAME_DEFAULT;
183
    }
184
185
186
}
187