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