|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Tests\Libraries; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Comment; |
| 6 | +use \BNETDocs\Libraries\EventLog\EventTypes; |
| 7 | +use \PHPUnit\Framework\TestCase; |
| 8 | +use \UnexpectedValueException; |
| 9 | + |
| 10 | +class CommentTest extends TestCase |
| 11 | +{ |
| 12 | + private Comment $comment; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + // null id → allocate() returns true early; no database access. |
| 17 | + $this->comment = new Comment(null); |
| 18 | + } |
| 19 | + |
| 20 | + // Default state |
| 21 | + |
| 22 | + public function testDefaultIdIsNull(): void |
| 23 | + { |
| 24 | + $this->assertNull($this->comment->getId()); |
| 25 | + } |
| 26 | + |
| 27 | + public function testDefaultContentIsEmpty(): void |
| 28 | + { |
| 29 | + $this->assertSame('', $this->comment->getContent(false)); |
| 30 | + } |
| 31 | + |
| 32 | + public function testDefaultEditedCountIsZero(): void |
| 33 | + { |
| 34 | + $this->assertSame(0, $this->comment->getEditedCount()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testDefaultEditedDateTimeIsNull(): void |
| 38 | + { |
| 39 | + $this->assertNull($this->comment->getEditedDateTime()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testDefaultParentIdIsZero(): void |
| 43 | + { |
| 44 | + $this->assertSame(0, $this->comment->getParentId()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testDefaultParentTypeIsComment(): void |
| 48 | + { |
| 49 | + $this->assertSame(Comment::PARENT_TYPE_COMMENT, $this->comment->getParentType()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testDefaultUserIdIsNull(): void |
| 53 | + { |
| 54 | + $this->assertNull($this->comment->getUserId()); |
| 55 | + } |
| 56 | + |
| 57 | + // PARENT_TYPE_* constant values |
| 58 | + |
| 59 | + public function testParentTypeCommentIsZero(): void |
| 60 | + { |
| 61 | + $this->assertSame(0, Comment::PARENT_TYPE_COMMENT); |
| 62 | + } |
| 63 | + |
| 64 | + public function testParentTypeDocumentIsOne(): void |
| 65 | + { |
| 66 | + $this->assertSame(1, Comment::PARENT_TYPE_DOCUMENT); |
| 67 | + } |
| 68 | + |
| 69 | + public function testParentTypeNewsPostIsTwo(): void |
| 70 | + { |
| 71 | + $this->assertSame(2, Comment::PARENT_TYPE_NEWS_POST); |
| 72 | + } |
| 73 | + |
| 74 | + public function testParentTypePacketIsThree(): void |
| 75 | + { |
| 76 | + $this->assertSame(3, Comment::PARENT_TYPE_PACKET); |
| 77 | + } |
| 78 | + |
| 79 | + public function testParentTypeServerIsFour(): void |
| 80 | + { |
| 81 | + $this->assertSame(4, Comment::PARENT_TYPE_SERVER); |
| 82 | + } |
| 83 | + |
| 84 | + public function testParentTypeUserIsFive(): void |
| 85 | + { |
| 86 | + $this->assertSame(5, Comment::PARENT_TYPE_USER); |
| 87 | + } |
| 88 | + |
| 89 | + // validateParentType static |
| 90 | + |
| 91 | + public function testValidateParentTypeAllSixAreValid(): void |
| 92 | + { |
| 93 | + foreach ([ |
| 94 | + Comment::PARENT_TYPE_COMMENT, |
| 95 | + Comment::PARENT_TYPE_DOCUMENT, |
| 96 | + Comment::PARENT_TYPE_NEWS_POST, |
| 97 | + Comment::PARENT_TYPE_PACKET, |
| 98 | + Comment::PARENT_TYPE_SERVER, |
| 99 | + Comment::PARENT_TYPE_USER, |
| 100 | + ] as $type) { |
| 101 | + $this->assertTrue(Comment::validateParentType($type), "Expected type $type to be valid"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public function testValidateParentTypeUnknownReturnsFalse(): void |
| 106 | + { |
| 107 | + $this->assertFalse(Comment::validateParentType(99)); |
| 108 | + } |
| 109 | + |
| 110 | + public function testValidateParentTypeNegativeThrowsUnexpectedValue(): void |
| 111 | + { |
| 112 | + $this->expectException(UnexpectedValueException::class); |
| 113 | + Comment::validateParentType(-1); |
| 114 | + } |
| 115 | + |
| 116 | + // setParentType |
| 117 | + |
| 118 | + public function testSetParentTypeDocumentIsAllowed(): void |
| 119 | + { |
| 120 | + $this->comment->setParentType(Comment::PARENT_TYPE_DOCUMENT); |
| 121 | + $this->assertSame(Comment::PARENT_TYPE_DOCUMENT, $this->comment->getParentType()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testSetParentTypeInvalidThrowsUnexpectedValue(): void |
| 125 | + { |
| 126 | + $this->expectException(UnexpectedValueException::class); |
| 127 | + $this->comment->setParentType(99); |
| 128 | + } |
| 129 | + |
| 130 | + // getParentTypeCreatedEventId — all six parent types |
| 131 | + |
| 132 | + public function testCreatedEventIdForComment(): void |
| 133 | + { |
| 134 | + $this->comment->setParentType(Comment::PARENT_TYPE_COMMENT); |
| 135 | + $this->assertSame(EventTypes::COMMENT_CREATED_COMMENT, $this->comment->getParentTypeCreatedEventId()); |
| 136 | + } |
| 137 | + |
| 138 | + public function testCreatedEventIdForDocument(): void |
| 139 | + { |
| 140 | + $this->comment->setParentType(Comment::PARENT_TYPE_DOCUMENT); |
| 141 | + $this->assertSame(EventTypes::COMMENT_CREATED_DOCUMENT, $this->comment->getParentTypeCreatedEventId()); |
| 142 | + } |
| 143 | + |
| 144 | + public function testCreatedEventIdForNewsPost(): void |
| 145 | + { |
| 146 | + $this->comment->setParentType(Comment::PARENT_TYPE_NEWS_POST); |
| 147 | + $this->assertSame(EventTypes::COMMENT_CREATED_NEWS, $this->comment->getParentTypeCreatedEventId()); |
| 148 | + } |
| 149 | + |
| 150 | + public function testCreatedEventIdForPacket(): void |
| 151 | + { |
| 152 | + $this->comment->setParentType(Comment::PARENT_TYPE_PACKET); |
| 153 | + $this->assertSame(EventTypes::COMMENT_CREATED_PACKET, $this->comment->getParentTypeCreatedEventId()); |
| 154 | + } |
| 155 | + |
| 156 | + public function testCreatedEventIdForServer(): void |
| 157 | + { |
| 158 | + $this->comment->setParentType(Comment::PARENT_TYPE_SERVER); |
| 159 | + $this->assertSame(EventTypes::COMMENT_CREATED_SERVER, $this->comment->getParentTypeCreatedEventId()); |
| 160 | + } |
| 161 | + |
| 162 | + public function testCreatedEventIdForUser(): void |
| 163 | + { |
| 164 | + $this->comment->setParentType(Comment::PARENT_TYPE_USER); |
| 165 | + $this->assertSame(EventTypes::COMMENT_CREATED_USER, $this->comment->getParentTypeCreatedEventId()); |
| 166 | + } |
| 167 | + |
| 168 | + // getParentTypeEditedEventId — all six parent types |
| 169 | + |
| 170 | + public function testEditedEventIdForComment(): void |
| 171 | + { |
| 172 | + $this->comment->setParentType(Comment::PARENT_TYPE_COMMENT); |
| 173 | + $this->assertSame(EventTypes::COMMENT_EDITED_COMMENT, $this->comment->getParentTypeEditedEventId()); |
| 174 | + } |
| 175 | + |
| 176 | + public function testEditedEventIdForDocument(): void |
| 177 | + { |
| 178 | + $this->comment->setParentType(Comment::PARENT_TYPE_DOCUMENT); |
| 179 | + $this->assertSame(EventTypes::COMMENT_EDITED_DOCUMENT, $this->comment->getParentTypeEditedEventId()); |
| 180 | + } |
| 181 | + |
| 182 | + public function testEditedEventIdForNewsPost(): void |
| 183 | + { |
| 184 | + $this->comment->setParentType(Comment::PARENT_TYPE_NEWS_POST); |
| 185 | + $this->assertSame(EventTypes::COMMENT_EDITED_NEWS, $this->comment->getParentTypeEditedEventId()); |
| 186 | + } |
| 187 | + |
| 188 | + public function testEditedEventIdForPacket(): void |
| 189 | + { |
| 190 | + $this->comment->setParentType(Comment::PARENT_TYPE_PACKET); |
| 191 | + $this->assertSame(EventTypes::COMMENT_EDITED_PACKET, $this->comment->getParentTypeEditedEventId()); |
| 192 | + } |
| 193 | + |
| 194 | + public function testEditedEventIdForServer(): void |
| 195 | + { |
| 196 | + $this->comment->setParentType(Comment::PARENT_TYPE_SERVER); |
| 197 | + $this->assertSame(EventTypes::COMMENT_EDITED_SERVER, $this->comment->getParentTypeEditedEventId()); |
| 198 | + } |
| 199 | + |
| 200 | + public function testEditedEventIdForUser(): void |
| 201 | + { |
| 202 | + $this->comment->setParentType(Comment::PARENT_TYPE_USER); |
| 203 | + $this->assertSame(EventTypes::COMMENT_EDITED_USER, $this->comment->getParentTypeEditedEventId()); |
| 204 | + } |
| 205 | + |
| 206 | + // getParentTypeDeletedEventId — all six parent types |
| 207 | + |
| 208 | + public function testDeletedEventIdForComment(): void |
| 209 | + { |
| 210 | + $this->comment->setParentType(Comment::PARENT_TYPE_COMMENT); |
| 211 | + $this->assertSame(EventTypes::COMMENT_DELETED_COMMENT, $this->comment->getParentTypeDeletedEventId()); |
| 212 | + } |
| 213 | + |
| 214 | + public function testDeletedEventIdForDocument(): void |
| 215 | + { |
| 216 | + $this->comment->setParentType(Comment::PARENT_TYPE_DOCUMENT); |
| 217 | + $this->assertSame(EventTypes::COMMENT_DELETED_DOCUMENT, $this->comment->getParentTypeDeletedEventId()); |
| 218 | + } |
| 219 | + |
| 220 | + public function testDeletedEventIdForNewsPost(): void |
| 221 | + { |
| 222 | + $this->comment->setParentType(Comment::PARENT_TYPE_NEWS_POST); |
| 223 | + $this->assertSame(EventTypes::COMMENT_DELETED_NEWS, $this->comment->getParentTypeDeletedEventId()); |
| 224 | + } |
| 225 | + |
| 226 | + public function testDeletedEventIdForPacket(): void |
| 227 | + { |
| 228 | + $this->comment->setParentType(Comment::PARENT_TYPE_PACKET); |
| 229 | + $this->assertSame(EventTypes::COMMENT_DELETED_PACKET, $this->comment->getParentTypeDeletedEventId()); |
| 230 | + } |
| 231 | + |
| 232 | + public function testDeletedEventIdForServer(): void |
| 233 | + { |
| 234 | + $this->comment->setParentType(Comment::PARENT_TYPE_SERVER); |
| 235 | + $this->assertSame(EventTypes::COMMENT_DELETED_SERVER, $this->comment->getParentTypeDeletedEventId()); |
| 236 | + } |
| 237 | + |
| 238 | + public function testDeletedEventIdForUser(): void |
| 239 | + { |
| 240 | + $this->comment->setParentType(Comment::PARENT_TYPE_USER); |
| 241 | + $this->assertSame(EventTypes::COMMENT_DELETED_USER, $this->comment->getParentTypeDeletedEventId()); |
| 242 | + } |
| 243 | + |
| 244 | + // setContent validation |
| 245 | + |
| 246 | + public function testSetContentAtMaxLengthIsAllowed(): void |
| 247 | + { |
| 248 | + $value = str_repeat('x', Comment::MAX_CONTENT); |
| 249 | + $this->comment->setContent($value); |
| 250 | + $this->assertSame($value, $this->comment->getContent(false)); |
| 251 | + } |
| 252 | + |
| 253 | + public function testSetContentTooLongThrowsUnexpectedValue(): void |
| 254 | + { |
| 255 | + $this->expectException(UnexpectedValueException::class); |
| 256 | + $this->comment->setContent(str_repeat('x', Comment::MAX_CONTENT + 1)); |
| 257 | + } |
| 258 | + |
| 259 | + // incrementEdited |
| 260 | + |
| 261 | + public function testIncrementEditedUpdatesCount(): void |
| 262 | + { |
| 263 | + $this->comment->incrementEdited(); |
| 264 | + $this->assertSame(1, $this->comment->getEditedCount()); |
| 265 | + } |
| 266 | + |
| 267 | + public function testIncrementEditedSetsEditedDateTime(): void |
| 268 | + { |
| 269 | + $this->comment->incrementEdited(); |
| 270 | + $this->assertNotNull($this->comment->getEditedDateTime()); |
| 271 | + } |
| 272 | + |
| 273 | + // DB-free early-exit methods |
| 274 | + |
| 275 | + public function testGetTagsIsEmptyWhenIdIsNull(): void |
| 276 | + { |
| 277 | + $this->assertSame([], $this->comment->getTags()); |
| 278 | + } |
| 279 | + |
| 280 | + public function testDeallocateReturnsFalseWhenIdIsNull(): void |
| 281 | + { |
| 282 | + $this->assertFalse($this->comment->deallocate()); |
| 283 | + } |
| 284 | + |
| 285 | + // jsonSerialize |
| 286 | + |
| 287 | + public function testJsonSerializeHasExpectedKeys(): void |
| 288 | + { |
| 289 | + $data = $this->comment->jsonSerialize(); |
| 290 | + foreach ([ |
| 291 | + 'content', 'created_datetime', 'edited_count', 'edited_datetime', |
| 292 | + 'id', 'parent_id', 'parent_type', 'user_id', |
| 293 | + ] as $key) { |
| 294 | + $this->assertArrayHasKey($key, $data, "Missing key: $key"); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + public function testJsonSerializeDefaultValues(): void |
| 299 | + { |
| 300 | + $data = $this->comment->jsonSerialize(); |
| 301 | + $this->assertNull($data['id']); |
| 302 | + $this->assertNull($data['edited_datetime']); |
| 303 | + $this->assertNull($data['user_id']); |
| 304 | + $this->assertSame('', $data['content']); |
| 305 | + $this->assertSame(0, $data['edited_count']); |
| 306 | + $this->assertSame(0, $data['parent_id']); |
| 307 | + $this->assertSame(Comment::PARENT_TYPE_COMMENT, $data['parent_type']); |
| 308 | + } |
| 309 | +} |
0 commit comments