|
12 | 12 | * limitations under the License. |
13 | 13 | **/ |
14 | 14 |
|
| 15 | +use App\Models\Foundation\Summit\Registration\PromoCodes\Strategies\RegularPromoCodeTicketTypesStrategy; |
| 16 | +use Doctrine\Common\Collections\ArrayCollection; |
15 | 17 | use models\exceptions\ValidationException; |
16 | 18 | use models\summit\DomainAuthorizedSummitRegistrationDiscountCode; |
17 | 19 | use models\summit\DomainAuthorizedSummitRegistrationPromoCode; |
| 20 | +use models\summit\Summit; |
| 21 | +use models\summit\SummitRegistrationPromoCode; |
18 | 22 | use models\summit\SummitTicketType; |
| 23 | +use models\main\Member; |
19 | 24 | use PHPUnit\Framework\TestCase; |
20 | 25 |
|
21 | 26 | /** |
@@ -227,4 +232,158 @@ public function testDiscountCodeDomainMatching(): void |
227 | 232 | $this->assertTrue($code->matchesEmailDomain('student@university.edu')); |
228 | 233 | $this->assertFalse($code->matchesEmailDomain('user@random.org')); |
229 | 234 | } |
| 235 | + |
| 236 | + // ----------------------------------------------------------------------- |
| 237 | + // RegularPromoCodeTicketTypesStrategy — audience filtering |
| 238 | + // ----------------------------------------------------------------------- |
| 239 | + |
| 240 | + private function buildMockSummit(array $audienceAllTypes = [], array $audienceWithoutInvitationTypes = []): Summit |
| 241 | + { |
| 242 | + $summit = $this->createMock(Summit::class); |
| 243 | + $summit->method('getId')->willReturn(1); |
| 244 | + $summit->method('getSummitRegistrationInvitationByEmail')->willReturn(null); |
| 245 | + |
| 246 | + $summit->method('getTicketTypesByAudience')->willReturnCallback( |
| 247 | + function (string $audience) use ($audienceAllTypes, $audienceWithoutInvitationTypes) { |
| 248 | + if ($audience === SummitTicketType::Audience_All) { |
| 249 | + return new ArrayCollection($audienceAllTypes); |
| 250 | + } |
| 251 | + if ($audience === SummitTicketType::Audience_Without_Invitation) { |
| 252 | + return new ArrayCollection($audienceWithoutInvitationTypes); |
| 253 | + } |
| 254 | + return new ArrayCollection(); |
| 255 | + } |
| 256 | + ); |
| 257 | + |
| 258 | + return $summit; |
| 259 | + } |
| 260 | + |
| 261 | + private function buildMockMember(string $email = 'user@test.com'): Member |
| 262 | + { |
| 263 | + $member = $this->createMock(Member::class); |
| 264 | + $member->method('getId')->willReturn(1); |
| 265 | + $member->method('getEmail')->willReturn($email); |
| 266 | + $member->method('getCompany')->willReturn(null); |
| 267 | + return $member; |
| 268 | + } |
| 269 | + |
| 270 | + private function buildMockTicketType(int $id, string $audience, bool $canSell = true): SummitTicketType |
| 271 | + { |
| 272 | + $tt = $this->createMock(SummitTicketType::class); |
| 273 | + $tt->method('getId')->willReturn($id); |
| 274 | + $tt->method('getAudience')->willReturn($audience); |
| 275 | + $tt->method('canSell')->willReturn($canSell); |
| 276 | + $tt->method('isSoldOut')->willReturn(!$canSell); |
| 277 | + $tt->method('isPromoCodeOnly')->willReturn($audience === SummitTicketType::Audience_With_Promo_Code); |
| 278 | + return $tt; |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * WithPromoCode ticket type + no promo code → NOT returned |
| 283 | + */ |
| 284 | + public function testWithPromoCodeAudienceNoPromoCodeNotReturned(): void |
| 285 | + { |
| 286 | + $summit = $this->buildMockSummit(); |
| 287 | + $member = $this->buildMockMember(); |
| 288 | + |
| 289 | + $strategy = new RegularPromoCodeTicketTypesStrategy($summit, $member, null); |
| 290 | + $result = $strategy->getTicketTypes(); |
| 291 | + |
| 292 | + // No WithPromoCode types should appear (none were in Audience_All or Audience_Without_Invitation) |
| 293 | + foreach ($result as $tt) { |
| 294 | + $this->assertNotEquals( |
| 295 | + SummitTicketType::Audience_With_Promo_Code, |
| 296 | + $tt->getAudience(), |
| 297 | + 'WithPromoCode ticket types should not be returned without a promo code' |
| 298 | + ); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * WithPromoCode ticket type + live domain-authorized promo code → IS returned |
| 304 | + */ |
| 305 | + public function testWithPromoCodeAudienceLiveDomainAuthorizedPromoCodeReturned(): void |
| 306 | + { |
| 307 | + $promoCodeTicket = $this->buildMockTicketType(10, SummitTicketType::Audience_With_Promo_Code); |
| 308 | + |
| 309 | + $promoCode = $this->createMock(SummitRegistrationPromoCode::class); |
| 310 | + $promoCode->method('getCode')->willReturn('DOMAIN-CODE'); |
| 311 | + $promoCode->method('isLive')->willReturn(true); |
| 312 | + $promoCode->method('getAllowedTicketTypes')->willReturn(new ArrayCollection([$promoCodeTicket])); |
| 313 | + $promoCode->method('canBeAppliedTo')->willReturn(true); |
| 314 | + $promoCode->method('validate')->willReturn(true); |
| 315 | + |
| 316 | + $summit = $this->buildMockSummit(); |
| 317 | + $member = $this->buildMockMember(); |
| 318 | + |
| 319 | + $strategy = new RegularPromoCodeTicketTypesStrategy($summit, $member, $promoCode); |
| 320 | + $result = $strategy->getTicketTypes(); |
| 321 | + |
| 322 | + $ids = array_map(fn($tt) => $tt->getId(), $result); |
| 323 | + $this->assertContains(10, $ids, 'WithPromoCode ticket type should be returned with a live promo code'); |
| 324 | + } |
| 325 | + |
| 326 | + /** |
| 327 | + * WithPromoCode ticket type + live generic promo code → IS returned (any type unlocks) |
| 328 | + */ |
| 329 | + public function testWithPromoCodeAudienceLiveGenericPromoCodeReturned(): void |
| 330 | + { |
| 331 | + $promoCodeTicket = $this->buildMockTicketType(20, SummitTicketType::Audience_With_Promo_Code); |
| 332 | + |
| 333 | + $promoCode = $this->createMock(SummitRegistrationPromoCode::class); |
| 334 | + $promoCode->method('getCode')->willReturn('GENERIC-CODE'); |
| 335 | + $promoCode->method('isLive')->willReturn(true); |
| 336 | + $promoCode->method('getAllowedTicketTypes')->willReturn(new ArrayCollection([$promoCodeTicket])); |
| 337 | + $promoCode->method('canBeAppliedTo')->willReturn(true); |
| 338 | + $promoCode->method('validate')->willReturn(true); |
| 339 | + |
| 340 | + $summit = $this->buildMockSummit(); |
| 341 | + $member = $this->buildMockMember(); |
| 342 | + |
| 343 | + $strategy = new RegularPromoCodeTicketTypesStrategy($summit, $member, $promoCode); |
| 344 | + $result = $strategy->getTicketTypes(); |
| 345 | + |
| 346 | + $ids = array_map(fn($tt) => $tt->getId(), $result); |
| 347 | + $this->assertContains(20, $ids, 'WithPromoCode ticket type should be returned with any live promo code'); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Audience_All ticket type + no promo code → IS returned (existing behavior regression test) |
| 352 | + */ |
| 353 | + public function testAudienceAllNoPromoCodeReturned(): void |
| 354 | + { |
| 355 | + $allTicket = $this->buildMockTicketType(30, SummitTicketType::Audience_All); |
| 356 | + $summit = $this->buildMockSummit([$allTicket]); |
| 357 | + $member = $this->buildMockMember(); |
| 358 | + |
| 359 | + $strategy = new RegularPromoCodeTicketTypesStrategy($summit, $member, null); |
| 360 | + $result = $strategy->getTicketTypes(); |
| 361 | + |
| 362 | + $ids = array_map(fn($tt) => $tt->getId(), $result); |
| 363 | + $this->assertContains(30, $ids, 'Audience_All ticket type should be returned without a promo code'); |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * Audience_All ticket type + promo code → IS returned with promo applied (existing behavior regression test) |
| 368 | + */ |
| 369 | + public function testAudienceAllWithPromoCodeReturnedWithPromo(): void |
| 370 | + { |
| 371 | + $allTicket = $this->buildMockTicketType(40, SummitTicketType::Audience_All); |
| 372 | + |
| 373 | + $promoCode = $this->createMock(SummitRegistrationPromoCode::class); |
| 374 | + $promoCode->method('getCode')->willReturn('PROMO-ALL'); |
| 375 | + $promoCode->method('isLive')->willReturn(true); |
| 376 | + $promoCode->method('getAllowedTicketTypes')->willReturn(new ArrayCollection()); |
| 377 | + $promoCode->method('canBeAppliedTo')->willReturn(true); |
| 378 | + $promoCode->method('validate')->willReturn(true); |
| 379 | + |
| 380 | + $summit = $this->buildMockSummit([$allTicket]); |
| 381 | + $member = $this->buildMockMember(); |
| 382 | + |
| 383 | + $strategy = new RegularPromoCodeTicketTypesStrategy($summit, $member, $promoCode); |
| 384 | + $result = $strategy->getTicketTypes(); |
| 385 | + |
| 386 | + $ids = array_map(fn($tt) => $tt->getId(), $result); |
| 387 | + $this->assertContains(40, $ids, 'Audience_All ticket type should be returned with a promo code'); |
| 388 | + } |
230 | 389 | } |
0 commit comments