67e5dba7f2681afb22b5a98b3ed8cdd3ecbac529
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / LogLevelTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Log;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class LogLevelTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @test
24 */
25 public function isValidLevelValidatesValidLevels()
26 {
27 $validLevels = [0, 1, 2, 3, 4, 5, 6, 7];
28 foreach ($validLevels as $validLevel) {
29 $this->assertTrue(\TYPO3\CMS\Core\Log\LogLevel::isValidLevel($validLevel));
30 }
31 }
32
33 /**
34 * @test
35 */
36 public function isValidLevelDoesNotValidateInvalidLevels()
37 {
38 $invalidLevels = [-1, 8, 1.5, 'string', [], new \stdClass(), false, null];
39 foreach ($invalidLevels as $invalidLevel) {
40 $this->assertFalse(\TYPO3\CMS\Core\Log\LogLevel::isValidLevel($invalidLevel));
41 }
42 }
43
44 /**
45 * Data provider or isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSo
46 */
47 public function isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSoDataProvider()
48 {
49 return [
50 'negative integer' => [-1],
51 'higher level than expected' => [8],
52 'float' => [1.5],
53 'string' => ['string'],
54 'array' => [[]],
55 'object' => [new \stdClass()],
56 'boolean FALSE' => [false],
57 'NULL' => [null]
58 ];
59 }
60
61 /**
62 * @test
63 * @dataProvider isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSoDataProvider
64 */
65 public function isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSo($inputValue)
66 {
67 $this->expectException(\Psr\Log\InvalidArgumentException::class);
68 $this->expectExceptionCode(1321637121);
69
70 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($inputValue);
71 }
72
73 /**
74 * @test
75 */
76 public function normalizeLevelConvertsValidLevelFromStringToInteger()
77 {
78 $this->assertEquals(7, \TYPO3\CMS\Core\Log\LogLevel::normalizeLevel('debug'));
79 }
80
81 /**
82 * @test
83 */
84 public function normalizeLevelDoesNotConvertInvalidLevel()
85 {
86 $levelString = 'invalid';
87 $this->assertEquals($levelString, \TYPO3\CMS\Core\Log\LogLevel::normalizeLevel($levelString));
88 }
89 }