Completed
Push — master ( ddd761...82d0cd )
by Marcus
10s
created

QueuedJobsTest_RecordingLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace SilverStripe\QueuedJobs\Tests\QueuedJobsTest;
4
5
use Monolog\Logger;
6
use SilverStripe\Dev\TestOnly;
7
8
/**
9
 * Test logger for recording messages
10
 */
11
class QueuedJobsTest_RecordingLogger extends Logger implements TestOnly
12
{
13
    /**
14
     * @var QueuedJobsTest_Handler
15
     */
16
    protected $testHandler = null;
17
18
    public function __construct($name = 'testlogger', array $handlers = array(), array $processors = array())
19
    {
20
        parent::__construct($name, $handlers, $processors);
21
22
        $this->testHandler = new QueuedJobsTest_Handler();
23
        $this->pushHandler($this->testHandler);
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    public function getMessages()
30
    {
31
        return $this->testHandler->getMessages();
32
    }
33
34
    /**
35
     * Clear all messages
36
     */
37
    public function clear()
38
    {
39
        $this->testHandler->clear();
40
    }
41
42
    /**
43
     * Get messages with the given filter
44
     *
45
     * @param string $containing
46
     * @return array Filtered array
47
     */
48
    public function filterMessages($containing)
49
    {
50
        return array_values(array_filter(
51
            $this->getMessages(),
52
            function ($content) use ($containing) {
53
                return stripos($content, $containing) !== false;
54
            }
55
        ));
56
    }
57
58
    /**
59
     * Count all messages containing the given substring
60
     *
61
     * @param string $containing Message to filter by
62
     * @return int
63
     */
64
    public function countMessages($containing = null)
65
    {
66
        if ($containing) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $containing of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
67
            $messages = $this->filterMessages($containing);
68
        } else {
69
            $messages = $this->getMessages();
70
        }
71
        return count($messages);
72
    }
73
}
74