xref: /plugin/pureldap/vendor/psr/log/Psr/Log/Test/TestLogger.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
1*dad993c5SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
3*dad993c5SAndreas Gohrnamespace Psr\Log\Test;
4*dad993c5SAndreas Gohr
5*dad993c5SAndreas Gohruse Psr\Log\AbstractLogger;
6*dad993c5SAndreas Gohr
7*dad993c5SAndreas Gohr/**
8*dad993c5SAndreas Gohr * Used for testing purposes.
9*dad993c5SAndreas Gohr *
10*dad993c5SAndreas Gohr * It records all records and gives you access to them for verification.
11*dad993c5SAndreas Gohr *
12*dad993c5SAndreas Gohr * @method bool hasEmergency($record)
13*dad993c5SAndreas Gohr * @method bool hasAlert($record)
14*dad993c5SAndreas Gohr * @method bool hasCritical($record)
15*dad993c5SAndreas Gohr * @method bool hasError($record)
16*dad993c5SAndreas Gohr * @method bool hasWarning($record)
17*dad993c5SAndreas Gohr * @method bool hasNotice($record)
18*dad993c5SAndreas Gohr * @method bool hasInfo($record)
19*dad993c5SAndreas Gohr * @method bool hasDebug($record)
20*dad993c5SAndreas Gohr *
21*dad993c5SAndreas Gohr * @method bool hasEmergencyRecords()
22*dad993c5SAndreas Gohr * @method bool hasAlertRecords()
23*dad993c5SAndreas Gohr * @method bool hasCriticalRecords()
24*dad993c5SAndreas Gohr * @method bool hasErrorRecords()
25*dad993c5SAndreas Gohr * @method bool hasWarningRecords()
26*dad993c5SAndreas Gohr * @method bool hasNoticeRecords()
27*dad993c5SAndreas Gohr * @method bool hasInfoRecords()
28*dad993c5SAndreas Gohr * @method bool hasDebugRecords()
29*dad993c5SAndreas Gohr *
30*dad993c5SAndreas Gohr * @method bool hasEmergencyThatContains($message)
31*dad993c5SAndreas Gohr * @method bool hasAlertThatContains($message)
32*dad993c5SAndreas Gohr * @method bool hasCriticalThatContains($message)
33*dad993c5SAndreas Gohr * @method bool hasErrorThatContains($message)
34*dad993c5SAndreas Gohr * @method bool hasWarningThatContains($message)
35*dad993c5SAndreas Gohr * @method bool hasNoticeThatContains($message)
36*dad993c5SAndreas Gohr * @method bool hasInfoThatContains($message)
37*dad993c5SAndreas Gohr * @method bool hasDebugThatContains($message)
38*dad993c5SAndreas Gohr *
39*dad993c5SAndreas Gohr * @method bool hasEmergencyThatMatches($message)
40*dad993c5SAndreas Gohr * @method bool hasAlertThatMatches($message)
41*dad993c5SAndreas Gohr * @method bool hasCriticalThatMatches($message)
42*dad993c5SAndreas Gohr * @method bool hasErrorThatMatches($message)
43*dad993c5SAndreas Gohr * @method bool hasWarningThatMatches($message)
44*dad993c5SAndreas Gohr * @method bool hasNoticeThatMatches($message)
45*dad993c5SAndreas Gohr * @method bool hasInfoThatMatches($message)
46*dad993c5SAndreas Gohr * @method bool hasDebugThatMatches($message)
47*dad993c5SAndreas Gohr *
48*dad993c5SAndreas Gohr * @method bool hasEmergencyThatPasses($message)
49*dad993c5SAndreas Gohr * @method bool hasAlertThatPasses($message)
50*dad993c5SAndreas Gohr * @method bool hasCriticalThatPasses($message)
51*dad993c5SAndreas Gohr * @method bool hasErrorThatPasses($message)
52*dad993c5SAndreas Gohr * @method bool hasWarningThatPasses($message)
53*dad993c5SAndreas Gohr * @method bool hasNoticeThatPasses($message)
54*dad993c5SAndreas Gohr * @method bool hasInfoThatPasses($message)
55*dad993c5SAndreas Gohr * @method bool hasDebugThatPasses($message)
56*dad993c5SAndreas Gohr */
57*dad993c5SAndreas Gohrclass TestLogger extends AbstractLogger
58*dad993c5SAndreas Gohr{
59*dad993c5SAndreas Gohr    /**
60*dad993c5SAndreas Gohr     * @var array
61*dad993c5SAndreas Gohr     */
62*dad993c5SAndreas Gohr    public $records = [];
63*dad993c5SAndreas Gohr
64*dad993c5SAndreas Gohr    public $recordsByLevel = [];
65*dad993c5SAndreas Gohr
66*dad993c5SAndreas Gohr    /**
67*dad993c5SAndreas Gohr     * @inheritdoc
68*dad993c5SAndreas Gohr     */
69*dad993c5SAndreas Gohr    public function log($level, $message, array $context = [])
70*dad993c5SAndreas Gohr    {
71*dad993c5SAndreas Gohr        $record = [
72*dad993c5SAndreas Gohr            'level' => $level,
73*dad993c5SAndreas Gohr            'message' => $message,
74*dad993c5SAndreas Gohr            'context' => $context,
75*dad993c5SAndreas Gohr        ];
76*dad993c5SAndreas Gohr
77*dad993c5SAndreas Gohr        $this->recordsByLevel[$record['level']][] = $record;
78*dad993c5SAndreas Gohr        $this->records[] = $record;
79*dad993c5SAndreas Gohr    }
80*dad993c5SAndreas Gohr
81*dad993c5SAndreas Gohr    public function hasRecords($level)
82*dad993c5SAndreas Gohr    {
83*dad993c5SAndreas Gohr        return isset($this->recordsByLevel[$level]);
84*dad993c5SAndreas Gohr    }
85*dad993c5SAndreas Gohr
86*dad993c5SAndreas Gohr    public function hasRecord($record, $level)
87*dad993c5SAndreas Gohr    {
88*dad993c5SAndreas Gohr        if (is_string($record)) {
89*dad993c5SAndreas Gohr            $record = ['message' => $record];
90*dad993c5SAndreas Gohr        }
91*dad993c5SAndreas Gohr        return $this->hasRecordThatPasses(function ($rec) use ($record) {
92*dad993c5SAndreas Gohr            if ($rec['message'] !== $record['message']) {
93*dad993c5SAndreas Gohr                return false;
94*dad993c5SAndreas Gohr            }
95*dad993c5SAndreas Gohr            if (isset($record['context']) && $rec['context'] !== $record['context']) {
96*dad993c5SAndreas Gohr                return false;
97*dad993c5SAndreas Gohr            }
98*dad993c5SAndreas Gohr            return true;
99*dad993c5SAndreas Gohr        }, $level);
100*dad993c5SAndreas Gohr    }
101*dad993c5SAndreas Gohr
102*dad993c5SAndreas Gohr    public function hasRecordThatContains($message, $level)
103*dad993c5SAndreas Gohr    {
104*dad993c5SAndreas Gohr        return $this->hasRecordThatPasses(function ($rec) use ($message) {
105*dad993c5SAndreas Gohr            return strpos($rec['message'], $message) !== false;
106*dad993c5SAndreas Gohr        }, $level);
107*dad993c5SAndreas Gohr    }
108*dad993c5SAndreas Gohr
109*dad993c5SAndreas Gohr    public function hasRecordThatMatches($regex, $level)
110*dad993c5SAndreas Gohr    {
111*dad993c5SAndreas Gohr        return $this->hasRecordThatPasses(function ($rec) use ($regex) {
112*dad993c5SAndreas Gohr            return preg_match($regex, $rec['message']) > 0;
113*dad993c5SAndreas Gohr        }, $level);
114*dad993c5SAndreas Gohr    }
115*dad993c5SAndreas Gohr
116*dad993c5SAndreas Gohr    public function hasRecordThatPasses(callable $predicate, $level)
117*dad993c5SAndreas Gohr    {
118*dad993c5SAndreas Gohr        if (!isset($this->recordsByLevel[$level])) {
119*dad993c5SAndreas Gohr            return false;
120*dad993c5SAndreas Gohr        }
121*dad993c5SAndreas Gohr        foreach ($this->recordsByLevel[$level] as $i => $rec) {
122*dad993c5SAndreas Gohr            if (call_user_func($predicate, $rec, $i)) {
123*dad993c5SAndreas Gohr                return true;
124*dad993c5SAndreas Gohr            }
125*dad993c5SAndreas Gohr        }
126*dad993c5SAndreas Gohr        return false;
127*dad993c5SAndreas Gohr    }
128*dad993c5SAndreas Gohr
129*dad993c5SAndreas Gohr    public function __call($method, $args)
130*dad993c5SAndreas Gohr    {
131*dad993c5SAndreas Gohr        if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
132*dad993c5SAndreas Gohr            $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
133*dad993c5SAndreas Gohr            $level = strtolower($matches[2]);
134*dad993c5SAndreas Gohr            if (method_exists($this, $genericMethod)) {
135*dad993c5SAndreas Gohr                $args[] = $level;
136*dad993c5SAndreas Gohr                return call_user_func_array([$this, $genericMethod], $args);
137*dad993c5SAndreas Gohr            }
138*dad993c5SAndreas Gohr        }
139*dad993c5SAndreas Gohr        throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
140*dad993c5SAndreas Gohr    }
141*dad993c5SAndreas Gohr
142*dad993c5SAndreas Gohr    public function reset()
143*dad993c5SAndreas Gohr    {
144*dad993c5SAndreas Gohr        $this->records = [];
145*dad993c5SAndreas Gohr        $this->recordsByLevel = [];
146*dad993c5SAndreas Gohr    }
147*dad993c5SAndreas Gohr}
148