abacaphiliac /
doctrine-psr-sql-logger
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace AbacaphiliacTest\Doctrine; |
||
| 4 | |||
| 5 | use Abacaphiliac\Doctrine\LogLevelConfiguration; |
||
| 6 | use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels; |
||
| 7 | use Psr\Log\LoggerInterface; |
||
| 8 | use Psr\Log\Test\TestLogger; |
||
| 9 | use InvalidArgumentException; |
||
| 10 | use PHPUnit\Framework\TestCase; |
||
| 11 | use Psr\Log\LogLevel; |
||
| 12 | use TypeError; |
||
| 13 | use stdClass; |
||
| 14 | use function usleep; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @covers \Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels |
||
| 18 | */ |
||
| 19 | class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var TestLogger |
||
| 23 | */ |
||
| 24 | private $logger; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | private $sql = 'SELECT * FROM users WHERE id = :id'; |
||
| 28 | |||
| 29 | public function testLogLevel() : void |
||
| 30 | { |
||
| 31 | $defaultLogLevel = LogLevel::DEBUG; |
||
| 32 | $logLevelAfterReachingThreshold = LogLevel::INFO; |
||
| 33 | $thresholdInMilliseconds = 25; |
||
| 34 | $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
||
| 35 | $this->logger, |
||
| 36 | new LogLevelConfiguration([ |
||
| 37 | $logLevelAfterReachingThreshold => $thresholdInMilliseconds, |
||
| 38 | ]), |
||
| 39 | $defaultLogLevel |
||
| 40 | ); |
||
| 41 | |||
| 42 | $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
||
| 43 | $psrSqlLoggerConfigurableLogLevels->stopQuery(); |
||
| 44 | |||
| 45 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
||
| 46 | //No threshold is reached yet: default log level should be used |
||
| 47 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
||
| 48 | |||
| 49 | $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
||
| 50 | usleep($thresholdInMilliseconds * 1000); //Sleep to simulate query execution and reach the threshold |
||
| 51 | $psrSqlLoggerConfigurableLogLevels->stopQuery(); |
||
| 52 | |||
| 53 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(2)->level); |
||
| 54 | self::assertSame($logLevelAfterReachingThreshold, (string) $this->getRecordByIndex(3)->level); |
||
| 55 | } |
||
| 56 | |||
| 57 | private function getRecordByIndex(int $index): stdClass |
||
| 58 | { |
||
| 59 | $record = $this->logger->records[$index]; |
||
| 60 | |||
| 61 | self::assertIsArray($record); |
||
| 62 | |||
| 63 | return (object) $record; |
||
| 64 | } |
||
| 65 | |||
| 66 | View Code Duplication | public function testFallbackToDefaultLogLevel() : void |
|
| 67 | { |
||
| 68 | $defaultLogLevel = LogLevel::CRITICAL; |
||
| 69 | $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
||
| 70 | $this->logger, |
||
| 71 | new LogLevelConfiguration([]), |
||
| 72 | $defaultLogLevel |
||
| 73 | ); |
||
| 74 | |||
| 75 | $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
||
| 76 | $psrSqlLoggerConfigurableLogLevels->stopQuery(); |
||
| 77 | |||
| 78 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
||
| 79 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
||
| 80 | } |
||
| 81 | |||
| 82 | View Code Duplication | public function testFallbackToDefaultLogLevelWhenNoThresholdIsReached() : void |
|
| 83 | { |
||
| 84 | $defaultLogLevel = LogLevel::DEBUG; |
||
| 85 | $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
||
| 86 | $this->logger, |
||
| 87 | new LogLevelConfiguration([ |
||
| 88 | LogLevel::CRITICAL => 1000 * 60, //Use a huge threshold of one minute which should never be reached |
||
| 89 | ]), |
||
| 90 | $defaultLogLevel |
||
| 91 | ); |
||
| 92 | |||
| 93 | $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
||
| 94 | $psrSqlLoggerConfigurableLogLevels->stopQuery(); |
||
| 95 | |||
| 96 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
||
| 97 | self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testInvalidConfiguration() : void |
||
| 101 | { |
||
| 102 | $this->expectException(TypeError::class); |
||
| 103 | $loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
||
|
0 ignored issues
–
show
|
|||
| 104 | $this->logger, |
||
| 105 | new LogLevelConfiguration([ |
||
| 106 | 0.12345 => LogLevel::DEBUG, //Inverted key / value tuple |
||
| 107 | ]) |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function testInvalidLogLevelUsedInConfiguration() : void |
||
| 112 | { |
||
| 113 | $this->expectException(InvalidArgumentException::class); |
||
| 114 | $loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
||
|
0 ignored issues
–
show
$loggerWhichWillFailToInitialize is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 115 | $this->logger, |
||
| 116 | new LogLevelConfiguration([ |
||
| 117 | 'SOME_INVALID_LOG_LEVEL' => 100, |
||
| 118 | ]) |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testInvalidDefaultLogLevel() : void |
||
| 123 | { |
||
| 124 | $this->expectException(InvalidArgumentException::class); |
||
| 125 | new PsrSqlLoggerConfigurableLogLevels( |
||
| 126 | new class implements LoggerInterface { |
||
| 127 | public function emergency($message, array $context = array()) |
||
| 128 | { |
||
| 129 | } |
||
| 130 | |||
| 131 | public function alert($message, array $context = array()) |
||
| 132 | { |
||
| 133 | } |
||
| 134 | |||
| 135 | public function critical($message, array $context = array()) |
||
| 136 | { |
||
| 137 | } |
||
| 138 | |||
| 139 | public function error($message, array $context = array()) |
||
| 140 | { |
||
| 141 | } |
||
| 142 | |||
| 143 | public function warning($message, array $context = array()) |
||
| 144 | { |
||
| 145 | } |
||
| 146 | |||
| 147 | public function notice($message, array $context = array()) |
||
| 148 | { |
||
| 149 | } |
||
| 150 | |||
| 151 | public function info($message, array $context = array()) |
||
| 152 | { |
||
| 153 | } |
||
| 154 | |||
| 155 | public function debug($message, array $context = array()) |
||
| 156 | { |
||
| 157 | } |
||
| 158 | |||
| 159 | public function log($level, $message, array $context = array()) |
||
| 160 | { |
||
| 161 | } |
||
| 162 | }, |
||
| 163 | new LogLevelConfiguration([ |
||
| 164 | ]), |
||
| 165 | 'InvalidLevel' |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | protected function setUp(): void |
||
| 170 | { |
||
| 171 | $this->logger = new TestLogger(); |
||
| 172 | } |
||
| 173 | } |
||
| 174 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.