|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bartlett\Tests; |
| 4 | + |
| 5 | +use Bartlett\LoggerTestListener; |
| 6 | + |
| 7 | +use Monolog\Handler\TestHandler; |
| 8 | +use Monolog\Logger; |
| 9 | + |
| 10 | +use PHPUnit\Framework\AssertionFailedError; |
| 11 | +use PHPUnit\Framework\Test; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use PHPUnit\Framework\TestSuite; |
| 14 | +use PHPUnit\Framework\Warning; |
| 15 | +use PHPUnit\Runner\BaseTestRunner; |
| 16 | + |
| 17 | +use Psr\Log\LogLevel; |
| 18 | + |
| 19 | +use Error; |
| 20 | +use function sprintf; |
| 21 | +use function strtoupper; |
| 22 | +use function time; |
| 23 | + |
| 24 | +/** |
| 25 | + * @since Release 2.2.0 |
| 26 | + */ |
| 27 | +class LoggerTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var TestHandler */ |
| 30 | + private $testHandler; |
| 31 | + |
| 32 | + /** @var LoggerTestListener */ |
| 33 | + private $loggerTestListener; |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritDoc} |
| 37 | + */ |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->testHandler = new TestHandler(); |
| 41 | + |
| 42 | + $this->loggerTestListener = new LoggerTestListener( |
| 43 | + new Logger('loggerTest', [$this->testHandler]) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function testAddError(): void |
| 48 | + { |
| 49 | + $testName = 'testCanAddError'; |
| 50 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_ERROR); |
| 51 | + |
| 52 | + $this->loggerTestListener->addError($test, new Error('Error.'), time()); |
| 53 | + |
| 54 | + // addError operation expectation |
| 55 | + $record = sprintf("Error while running test '%s'.", $testName); |
| 56 | + $this->assertRecordThatContains('addError', $record, LogLevel::ERROR); |
| 57 | + } |
| 58 | + |
| 59 | + public function testAddWarning(): void |
| 60 | + { |
| 61 | + $testName = 'testCanAddWarning'; |
| 62 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_WARNING); |
| 63 | + |
| 64 | + $this->loggerTestListener->addWarning($test, new Warning('Warning.'), time()); |
| 65 | + |
| 66 | + // addWarning operation expectation |
| 67 | + $record = sprintf("Warning while running test '%s'.", $testName); |
| 68 | + $this->assertRecordThatContains('addWarning', $record, LogLevel::WARNING); |
| 69 | + } |
| 70 | + |
| 71 | + public function testAddFailure(): void |
| 72 | + { |
| 73 | + $testName = 'testCanAddFailure'; |
| 74 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_FAILURE); |
| 75 | + |
| 76 | + $this->loggerTestListener->addFailure($test, new AssertionFailedError('Failure.'), time()); |
| 77 | + |
| 78 | + // addFailure operation expectation |
| 79 | + $record = sprintf("Test '%s' failed.", $testName); |
| 80 | + $this->assertRecordThatContains('addFailure', $record, LogLevel::ERROR); |
| 81 | + } |
| 82 | + |
| 83 | + public function testAddIncompleteTest(): void |
| 84 | + { |
| 85 | + $testName = 'testCanAddIncompleteTest'; |
| 86 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_INCOMPLETE); |
| 87 | + |
| 88 | + $this->loggerTestListener->addIncompleteTest($test, new Error('Incomplete.'), time()); |
| 89 | + |
| 90 | + // addIncompleteTest operation expectation |
| 91 | + $record = sprintf("Test '%s' is incomplete.", $testName); |
| 92 | + $this->assertRecordThatContains('addIncompleteTest', $record, LogLevel::WARNING); |
| 93 | + } |
| 94 | + |
| 95 | + public function testAddRiskyTest(): void |
| 96 | + { |
| 97 | + $testName = 'testCanAddRiskyTest'; |
| 98 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_RISKY); |
| 99 | + |
| 100 | + $this->loggerTestListener->addRiskyTest($test, new Error('Risky.'), time()); |
| 101 | + |
| 102 | + // addRiskyTest operation expectation |
| 103 | + $record = sprintf("Test '%s' is risky.", $testName); |
| 104 | + $this->assertRecordThatContains('addRiskyTest', $record, LogLevel::WARNING); |
| 105 | + } |
| 106 | + |
| 107 | + public function testAddSkippedTest(): void |
| 108 | + { |
| 109 | + $testName = 'testCanAddSkippedTest'; |
| 110 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_SKIPPED); |
| 111 | + |
| 112 | + $this->loggerTestListener->addSkippedTest($test, new Error('Skipped.'), time()); |
| 113 | + |
| 114 | + // addSkippedTest operation expectation |
| 115 | + $record = sprintf("Test '%s' has been skipped.", $testName); |
| 116 | + $this->assertRecordThatContains('addSkippedTest', $record, LogLevel::WARNING); |
| 117 | + } |
| 118 | + |
| 119 | + public function testStartTestSuite(): void |
| 120 | + { |
| 121 | + $testName = 'testCanStartTestSuite'; |
| 122 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_UNKNOWN); |
| 123 | + |
| 124 | + $testSuite = $this->getTestSuite(__FUNCTION__, [$test]); |
| 125 | + |
| 126 | + $this->loggerTestListener->startTestSuite($testSuite); |
| 127 | + |
| 128 | + // startTestSuite operation expectation |
| 129 | + $record = sprintf("TestSuite '%s' started with %d tests.", $testSuite->getName(), $testSuite->count()); |
| 130 | + $this->assertRecordThatContains('startTestSuite', $record, LogLevel::NOTICE); |
| 131 | + } |
| 132 | + |
| 133 | + public function testEndTestSuite(): void |
| 134 | + { |
| 135 | + $testName = 'testCanEndTestSuite'; |
| 136 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_UNKNOWN); |
| 137 | + |
| 138 | + $testSuite = $this->getTestSuite(__FUNCTION__, [$test]); |
| 139 | + |
| 140 | + $this->loggerTestListener->startTestSuite($testSuite); |
| 141 | + $this->loggerTestListener->endTestSuite($testSuite); |
| 142 | + |
| 143 | + // endTestSuite operation expectation |
| 144 | + $record = sprintf("TestSuite '%s' ended.", $testSuite->getName()); |
| 145 | + $this->assertRecordThatContains('endTestSuite', $record, LogLevel::NOTICE); |
| 146 | + } |
| 147 | + |
| 148 | + public function testStartTest(): void |
| 149 | + { |
| 150 | + $testName = 'testCanStartTest'; |
| 151 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_UNKNOWN); |
| 152 | + |
| 153 | + $this->loggerTestListener->startTest($test); |
| 154 | + |
| 155 | + // startTest operation expectation |
| 156 | + $record = sprintf("Test '%s' started.", $testName); |
| 157 | + $this->assertRecordThatContains('startTest', $record, LogLevel::INFO); |
| 158 | + } |
| 159 | + |
| 160 | + public function testEndTest(): void |
| 161 | + { |
| 162 | + $testName = 'testCanEndTest'; |
| 163 | + $test = $this->getTest($testName, BaseTestRunner::STATUS_UNKNOWN); |
| 164 | + |
| 165 | + $this->loggerTestListener->endTest($test, time()); |
| 166 | + |
| 167 | + // endTest operation expectation |
| 168 | + $record = sprintf("Test '%s' ended.", $testName); |
| 169 | + $this->assertRecordThatContains('endTest', $record, LogLevel::INFO); |
| 170 | + } |
| 171 | + |
| 172 | + private function getTestSuite(string $testName, array $tests): TestSuite |
| 173 | + { |
| 174 | + $testTestSuite = $this->getMockBuilder(TestSuite::class)->getMock(); |
| 175 | + $testTestSuite->method('getName')->willReturn($testName); |
| 176 | + $testTestSuite->method('count')->willReturn(count($tests)); |
| 177 | + |
| 178 | + /** @var TestSuite $testTestSuite */ |
| 179 | + return $testTestSuite; |
| 180 | + } |
| 181 | + |
| 182 | + private function getTest(string $testName, int $status, ?int $dataSet = null): Test |
| 183 | + { |
| 184 | + $test = $this->getMockBuilder(Test::class) |
| 185 | + ->setMethods(['getName', 'getStatus', 'toString']) |
| 186 | + ->getMockForAbstractClass(); |
| 187 | + $test->method('getName')->willReturn( |
| 188 | + sprintf( |
| 189 | + '%s%s', |
| 190 | + $testName, |
| 191 | + (null !== $dataSet) ? " with data set #{$dataSet}" : '' |
| 192 | + ) |
| 193 | + ); |
| 194 | + $test->method('getStatus')->willReturn($status); |
| 195 | + $test->method('toString')->willReturn( |
| 196 | + sprintf('%s::%s', $this->getName(), $testName) |
| 197 | + ); |
| 198 | + |
| 199 | + /** @var Test $test */ |
| 200 | + return $test; |
| 201 | + } |
| 202 | + |
| 203 | + private function assertRecordThatContains(string $operation, string $record, string $level): void |
| 204 | + { |
| 205 | + $this->assertTrue( |
| 206 | + $this->testHandler->hasRecordThatContains($record, $level), |
| 207 | + sprintf( |
| 208 | + '%s expect to raise %s log message "%s", but nothing equals was found', |
| 209 | + $operation, |
| 210 | + strtoupper($level), |
| 211 | + $record |
| 212 | + ) |
| 213 | + ); |
| 214 | + } |
| 215 | +} |
0 commit comments