grommunio /
mapi-header-php
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * SPDX-License-Identifier: AGPL-3.0-only |
||
| 5 | * SPDX-FileCopyrightText: Copyright 2025 grommunio GmbH |
||
| 6 | * |
||
| 7 | * Unit tests for MAPIException class |
||
| 8 | */ |
||
| 9 | |||
| 10 | use PHPUnit\Framework\TestCase; |
||
|
0 ignored issues
–
show
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * @internal |
||
| 14 | * |
||
| 15 | * @coversNothing |
||
| 16 | */ |
||
| 17 | class MAPIExceptionTest extends TestCase { |
||
| 18 | public function testConstructorWithMessage(): void { |
||
| 19 | $exception = new MAPIException('MAPI error occurred'); |
||
| 20 | |||
| 21 | $this->assertEquals('MAPI error occurred', $exception->getMessage()); |
||
| 22 | $this->assertEquals(0, $exception->getCode()); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testConstructorWithMessageAndCode(): void { |
||
| 26 | $exception = new MAPIException('Access denied', MAPI_E_NO_ACCESS); |
||
| 27 | |||
| 28 | $this->assertEquals('Access denied', $exception->getMessage()); |
||
| 29 | $this->assertEquals(MAPI_E_NO_ACCESS, $exception->getCode()); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testExtendsBaseException(): void { |
||
| 33 | $exception = new MAPIException('Test'); |
||
| 34 | |||
| 35 | $this->assertInstanceOf(BaseException::class, $exception); |
||
| 36 | $this->assertInstanceOf(Exception::class, $exception); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testGetDisplayMessageWithNoAccess(): void { |
||
| 40 | $exception = new MAPIException('Test', MAPI_E_NO_ACCESS); |
||
| 41 | |||
| 42 | $displayMessage = $exception->getDisplayMessage(); |
||
| 43 | $this->assertIsString($displayMessage); |
||
| 44 | $this->assertNotEmpty($displayMessage); |
||
| 45 | $this->assertStringContainsString('privileges', $displayMessage); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function testGetDisplayMessageWithLogonFailed(): void { |
||
| 49 | $exception = new MAPIException('Test', MAPI_E_LOGON_FAILED); |
||
| 50 | |||
| 51 | $displayMessage = $exception->getDisplayMessage(); |
||
| 52 | $this->assertStringContainsString('Logon', $displayMessage); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testGetDisplayMessageWithNetworkError(): void { |
||
| 56 | $exception = new MAPIException('Test', MAPI_E_NETWORK_ERROR); |
||
| 57 | |||
| 58 | $displayMessage = $exception->getDisplayMessage(); |
||
| 59 | $this->assertStringContainsString('connect', $displayMessage); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testGetDisplayMessageWithUnknownEntryId(): void { |
||
| 63 | $exception = new MAPIException('Test', MAPI_E_UNKNOWN_ENTRYID); |
||
| 64 | |||
| 65 | $displayMessage = $exception->getDisplayMessage(); |
||
| 66 | $this->assertStringContainsString('id', $displayMessage); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testGetDisplayMessageWithNoRecipients(): void { |
||
| 70 | $exception = new MAPIException('Test', MAPI_E_NO_RECIPIENTS); |
||
| 71 | |||
| 72 | $displayMessage = $exception->getDisplayMessage(); |
||
| 73 | $this->assertStringContainsString('recipients', $displayMessage); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testGetDisplayMessageWithNotFound(): void { |
||
| 77 | $exception = new MAPIException('Test', MAPI_E_NOT_FOUND); |
||
| 78 | |||
| 79 | $displayMessage = $exception->getDisplayMessage(); |
||
| 80 | $this->assertStringContainsString('find', $displayMessage); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testGetDisplayMessageWithNotEnoughMemory(): void { |
||
| 84 | $exception = new MAPIException('Test', MAPI_E_NOT_ENOUGH_MEMORY); |
||
| 85 | |||
| 86 | $displayMessage = $exception->getDisplayMessage(); |
||
| 87 | $this->assertStringContainsString('memory', $displayMessage); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testGetDisplayMessageWithUnknownError(): void { |
||
| 91 | $exception = new MAPIException('Test', 0x80040FFF); |
||
| 92 | |||
| 93 | $displayMessage = $exception->getDisplayMessage(); |
||
| 94 | $this->assertStringContainsString('Unknown MAPI Error', $displayMessage); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testGetDisplayMessageWithCustomDisplayMessage(): void { |
||
| 98 | $exception = new MAPIException('Test', MAPI_E_NO_ACCESS); |
||
| 99 | $exception->setDisplayMessage('Custom display message'); |
||
| 100 | |||
| 101 | $displayMessage = $exception->getDisplayMessage(); |
||
| 102 | $this->assertStringContainsString('Custom display message', $displayMessage); |
||
| 103 | $this->assertStringContainsString(mapi_strerror(MAPI_E_NO_ACCESS), $displayMessage); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testGetDisplayMessageWithZeroCode(): void { |
||
| 107 | $exception = new MAPIException('Test', 0); |
||
| 108 | |||
| 109 | $displayMessage = $exception->getDisplayMessage(); |
||
| 110 | $this->assertIsString($displayMessage); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testGetDisplayMessageWithNegativeCode(): void { |
||
| 114 | $exception = new MAPIException('Test', -1); |
||
| 115 | |||
| 116 | $displayMessage = $exception->getDisplayMessage(); |
||
| 117 | $this->assertIsString($displayMessage); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testCanBeCaughtAsMAPIException(): void { |
||
| 121 | $caught = false; |
||
|
0 ignored issues
–
show
|
|||
| 122 | |||
| 123 | try { |
||
| 124 | throw new MAPIException('Test error'); |
||
| 125 | } |
||
| 126 | catch (MAPIException $e) { |
||
| 127 | $caught = true; |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->assertTrue($caught); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testCanBeCaughtAsBaseException(): void { |
||
| 134 | $caught = false; |
||
|
0 ignored issues
–
show
|
|||
| 135 | |||
| 136 | try { |
||
| 137 | throw new MAPIException('Test error'); |
||
| 138 | } |
||
| 139 | catch (BaseException $e) { |
||
| 140 | $caught = true; |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->assertTrue($caught); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testCanBeCaughtAsException(): void { |
||
| 147 | $caught = false; |
||
|
0 ignored issues
–
show
|
|||
| 148 | |||
| 149 | try { |
||
| 150 | throw new MAPIException('Test error'); |
||
| 151 | } |
||
| 152 | catch (Exception $e) { |
||
| 153 | $caught = true; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->assertTrue($caught); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function testInheritsHandledFlag(): void { |
||
| 160 | $exception = new MAPIException('Test'); |
||
| 161 | |||
| 162 | $this->assertFalse($exception->isHandled()); |
||
| 163 | $exception->setHandled(); |
||
| 164 | $this->assertTrue($exception->isHandled()); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function testInheritsShowDetailsMessage(): void { |
||
| 168 | $exception = new MAPIException('Test'); |
||
| 169 | $exception->setShowDetailsMessage(); |
||
| 170 | |||
| 171 | $details = $exception->getDetailsMessage(); |
||
| 172 | $this->assertStringContainsString('MAPIException', $details); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function testChainedMAPIExceptions(): void { |
||
| 176 | $first = new MAPIException('First error', MAPI_E_NOT_FOUND); |
||
| 177 | $second = new MAPIException('Second error', MAPI_E_NO_ACCESS, $first); |
||
| 178 | |||
| 179 | $this->assertSame($first, $second->getPrevious()); |
||
| 180 | $this->assertEquals(MAPI_E_NOT_FOUND, $second->getPrevious()->getCode()); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function testGetDisplayMessagePreservesOriginalMessage(): void { |
||
| 184 | $originalMessage = 'Technical error details'; |
||
| 185 | $exception = new MAPIException($originalMessage, MAPI_E_NO_ACCESS); |
||
| 186 | |||
| 187 | $displayMessage = $exception->getDisplayMessage(); |
||
| 188 | $technicalMessage = $exception->getMessage(); |
||
| 189 | |||
| 190 | $this->assertEquals($originalMessage, $technicalMessage); |
||
| 191 | $this->assertNotEquals($displayMessage, $technicalMessage); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testMultipleDifferentErrorCodes(): void { |
||
| 195 | $errors = [ |
||
| 196 | MAPI_E_NO_ACCESS, |
||
| 197 | MAPI_E_LOGON_FAILED, |
||
| 198 | MAPI_E_NETWORK_ERROR, |
||
| 199 | MAPI_E_NOT_FOUND, |
||
| 200 | ]; |
||
| 201 | |||
| 202 | foreach ($errors as $errorCode) { |
||
| 203 | $exception = new MAPIException('Test', $errorCode); |
||
| 204 | $displayMessage = $exception->getDisplayMessage(); |
||
| 205 | |||
| 206 | $this->assertIsString($displayMessage); |
||
| 207 | $this->assertNotEmpty($displayMessage); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | public function testExceptionWithLargeErrorCode(): void { |
||
| 212 | $exception = new MAPIException('Test', 0xFFFFFFFF); |
||
| 213 | |||
| 214 | $displayMessage = $exception->getDisplayMessage(); |
||
| 215 | $this->assertIsString($displayMessage); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function testGetTraceIncludesFileAndLine(): void { |
||
| 219 | $exception = new MAPIException('Test'); |
||
| 220 | |||
| 221 | $trace = $exception->getTrace(); |
||
| 222 | $this->assertIsArray($trace); |
||
| 223 | |||
| 224 | $file = $exception->getFile(); |
||
| 225 | $this->assertStringContainsString('MAPIExceptionTest.php', $file); |
||
| 226 | |||
| 227 | $line = $exception->getLine(); |
||
| 228 | $this->assertGreaterThan(0, $line); |
||
| 229 | } |
||
| 230 | } |
||
| 231 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths