BaseExceptionTest   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 70
c 1
b 0
f 1
dl 0
loc 150
rs 10
wmc 21

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testChainedExceptions() 0 7 1
A testGetMessageReturnsOriginalMessage() 0 5 1
A testInheritedGetTraceMethod() 0 5 1
A testExceptionCanBeCaught() 0 11 2
A testGetFileReturnsCurrentFile() 0 5 1
A testSetHandledFlag() 0 5 1
A testInheritedGetTraceAsStringMethod() 0 5 1
A testSetShowDetailsMessage() 0 5 1
A testConstructorWithMessageAndCode() 0 5 1
A testToStringContainsClassName() 0 5 1
A testConstructorWithAllParameters() 0 7 1
A testExceptionCanBeCaughtAsException() 0 11 2
A testConstructorWithMessage() 0 5 1
A testSetHandledFlagMultipleTimes() 0 6 1
A testHandledFlagDefaultsToFalse() 0 4 1
A testGetLineReturnsLineNumber() 0 6 1
A testShowDetailsMessageDefaultsToFalse() 0 4 1
A testShowDetailsMessageWhenEnabled() 0 7 1
A testToStringContainsMessage() 0 5 1
1
<?php
2
3
/*
4
 * SPDX-License-Identifier: AGPL-3.0-only
5
 * SPDX-FileCopyrightText: Copyright 2025 grommunio GmbH
6
 *
7
 * Unit tests for BaseException class
8
 */
9
10
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * @internal
14
 *
15
 * @coversNothing
16
 */
17
class BaseExceptionTest extends TestCase {
18
	public function testConstructorWithMessage(): void {
19
		$exception = new BaseException('Test error message');
20
21
		$this->assertEquals('Test error message', $exception->getMessage());
22
		$this->assertEquals(0, $exception->getCode());
23
	}
24
25
	public function testConstructorWithMessageAndCode(): void {
26
		$exception = new BaseException('Test error', 500);
27
28
		$this->assertEquals('Test error', $exception->getMessage());
29
		$this->assertEquals(500, $exception->getCode());
30
	}
31
32
	public function testConstructorWithAllParameters(): void {
33
		$previous = new Exception('Previous exception');
34
		$exception = new BaseException('Test error', 500, $previous);
35
36
		$this->assertEquals('Test error', $exception->getMessage());
37
		$this->assertEquals(500, $exception->getCode());
38
		$this->assertSame($previous, $exception->getPrevious());
39
	}
40
41
	public function testHandledFlagDefaultsToFalse(): void {
42
		$exception = new BaseException('Test');
43
44
		$this->assertFalse($exception->isHandled());
45
	}
46
47
	public function testSetHandledFlag(): void {
48
		$exception = new BaseException('Test');
49
50
		$exception->setHandled();
51
		$this->assertTrue($exception->isHandled());
52
	}
53
54
	public function testSetHandledFlagMultipleTimes(): void {
55
		$exception = new BaseException('Test');
56
57
		$exception->setHandled();
58
		$exception->setHandled();
59
		$this->assertTrue($exception->isHandled());
60
	}
61
62
	public function testShowDetailsMessageDefaultsToFalse(): void {
63
		$exception = new BaseException('Test');
64
65
		$this->assertEquals('', $exception->getDetailsMessage());
66
	}
67
68
	public function testShowDetailsMessageWhenEnabled(): void {
69
		$exception = new BaseException('Test error');
70
		$exception->setShowDetailsMessage();
71
72
		$details = $exception->getDetailsMessage();
73
		$this->assertStringContainsString('Test error', $details);
74
		$this->assertStringContainsString('BaseException', $details);
75
	}
76
77
	public function testSetShowDetailsMessage(): void {
78
		$exception = new BaseException('Test');
79
80
		$exception->setShowDetailsMessage();
81
		$this->assertNotEmpty($exception->getDetailsMessage());
82
	}
83
84
	public function testGetMessageReturnsOriginalMessage(): void {
85
		$message = 'Detailed error message';
86
		$exception = new BaseException($message);
87
88
		$this->assertEquals($message, $exception->getMessage());
89
	}
90
91
	public function testToStringContainsClassName(): void {
92
		$exception = new BaseException('Test');
93
94
		$string = (string) $exception;
95
		$this->assertStringContainsString('BaseException', $string);
96
	}
97
98
	public function testToStringContainsMessage(): void {
99
		$exception = new BaseException('Test error message');
100
101
		$string = (string) $exception;
102
		$this->assertStringContainsString('Test error message', $string);
103
	}
104
105
	public function testInheritedGetTraceMethod(): void {
106
		$exception = new BaseException('Test');
107
108
		$trace = $exception->getTrace();
109
		$this->assertIsArray($trace);
110
	}
111
112
	public function testInheritedGetTraceAsStringMethod(): void {
113
		$exception = new BaseException('Test');
114
115
		$traceString = $exception->getTraceAsString();
116
		$this->assertIsString($traceString);
117
	}
118
119
	public function testGetFileReturnsCurrentFile(): void {
120
		$exception = new BaseException('Test');
121
122
		$file = $exception->getFile();
123
		$this->assertStringContainsString('BaseExceptionTest.php', $file);
124
	}
125
126
	public function testGetLineReturnsLineNumber(): void {
127
		$exception = new BaseException('Test');
128
129
		$line = $exception->getLine();
130
		$this->assertIsInt($line);
131
		$this->assertGreaterThan(0, $line);
132
	}
133
134
	public function testExceptionCanBeCaught(): void {
135
		$caught = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $caught is dead and can be removed.
Loading history...
136
137
		try {
138
			throw new BaseException('Test');
139
		}
140
		catch (BaseException $e) {
141
			$caught = true;
142
		}
143
144
		$this->assertTrue($caught);
145
	}
146
147
	public function testExceptionCanBeCaughtAsException(): void {
148
		$caught = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $caught is dead and can be removed.
Loading history...
149
150
		try {
151
			throw new BaseException('Test');
152
		}
153
		catch (Exception $e) {
154
			$caught = true;
155
		}
156
157
		$this->assertTrue($caught);
158
	}
159
160
	public function testChainedExceptions(): void {
161
		$first = new Exception('First error');
162
		$second = new BaseException('Second error', 0, $first);
163
		$third = new BaseException('Third error', 0, $second);
164
165
		$this->assertSame($second, $third->getPrevious());
166
		$this->assertSame($first, $third->getPrevious()->getPrevious());
167
	}
168
}
169