|
| 1 | +<?php namespace Tests\Unit\Services; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Services\Model\SummitPromoCodeService; |
| 16 | +use App\Services\Utils\ILockManagerService; |
| 17 | +use libs\utils\ITransactionService; |
| 18 | +use Mockery; |
| 19 | +use models\main\ICompanyRepository; |
| 20 | +use models\main\IMemberRepository; |
| 21 | +use models\main\ITagRepository; |
| 22 | +use models\main\Member; |
| 23 | +use models\summit\DomainAuthorizedSummitRegistrationPromoCode; |
| 24 | +use models\summit\ISpeakerRepository; |
| 25 | +use models\summit\ISummitAttendeeTicketRepository; |
| 26 | +use models\summit\ISummitRegistrationPromoCodeRepository; |
| 27 | +use models\summit\ISummitRepository; |
| 28 | +use models\summit\Summit; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | + |
| 31 | +/** |
| 32 | + * Class SummitPromoCodeServiceDiscoveryTest |
| 33 | + * |
| 34 | + * Regression unit tests for {@see SummitPromoCodeService::discoverPromoCodes} |
| 35 | + * filtering logic. Focuses on the global-exhaustion guard added to match |
| 36 | + * checkout's validate() behavior. |
| 37 | + * |
| 38 | + * @package Tests\Unit\Services |
| 39 | + */ |
| 40 | +class SummitPromoCodeServiceDiscoveryTest extends TestCase |
| 41 | +{ |
| 42 | + protected function tearDown(): void |
| 43 | + { |
| 44 | + Mockery::close(); |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + private function buildService(ISummitRegistrationPromoCodeRepository $repository): SummitPromoCodeService |
| 49 | + { |
| 50 | + return new SummitPromoCodeService( |
| 51 | + Mockery::mock(IMemberRepository::class), |
| 52 | + Mockery::mock(ICompanyRepository::class), |
| 53 | + Mockery::mock(ISpeakerRepository::class), |
| 54 | + Mockery::mock(ISummitRepository::class), |
| 55 | + Mockery::mock(ISummitAttendeeTicketRepository::class), |
| 56 | + Mockery::mock(ITagRepository::class), |
| 57 | + $repository, |
| 58 | + Mockery::mock(ITransactionService::class), |
| 59 | + Mockery::mock(ILockManagerService::class) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Regression: a finite domain-authorized code whose quantity_used has |
| 65 | + * reached quantity_available must not appear in discovery. Previously, |
| 66 | + * the repository filter used isLive() (dates only), so globally |
| 67 | + * exhausted codes leaked through — frontend auto-apply would then hit |
| 68 | + * a hard checkout failure. |
| 69 | + */ |
| 70 | + public function testDiscoverExcludesGloballyExhaustedDomainAuthorizedCode(): void |
| 71 | + { |
| 72 | + $exhausted = Mockery::mock(DomainAuthorizedSummitRegistrationPromoCode::class); |
| 73 | + $exhausted->shouldReceive('getCode')->andReturn('GLOBAL_EXHAUSTED'); |
| 74 | + $exhausted->shouldReceive('hasQuantityAvailable')->andReturn(false); |
| 75 | + // getQuantityPerAccount should never be reached if the global-exhaustion |
| 76 | + // guard is in place — but define it defensively so a regression would |
| 77 | + // surface as a quota check, not an uncaught Mockery error. |
| 78 | + $exhausted->shouldReceive('getQuantityPerAccount')->andReturn(0); |
| 79 | + $exhausted->shouldReceive('setRemainingQuantityPerAccount')->andReturn(null); |
| 80 | + |
| 81 | + $summit = Mockery::mock(Summit::class); |
| 82 | + $member = Mockery::mock(Member::class); |
| 83 | + $member->shouldReceive('getEmail')->andReturn('new-buyer@acme.com'); |
| 84 | + $member->shouldReceive('getId')->andReturn(99); |
| 85 | + |
| 86 | + $repository = Mockery::mock(ISummitRegistrationPromoCodeRepository::class); |
| 87 | + // Repository filter is isLive()-only, so it would pass the exhausted |
| 88 | + // code through — simulate that by returning it. |
| 89 | + $repository->shouldReceive('getDiscoverableByEmailForSummit') |
| 90 | + ->with($summit, 'new-buyer@acme.com') |
| 91 | + ->andReturn([$exhausted]); |
| 92 | + |
| 93 | + $service = $this->buildService($repository); |
| 94 | + $result = $service->discoverPromoCodes($summit, $member); |
| 95 | + |
| 96 | + $this->assertSame([], $result, |
| 97 | + 'Globally exhausted domain-authorized code must not appear in discovery'); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A healthy domain-authorized code (has global quantity, unlimited quota) |
| 102 | + * passes through. Guards against over-filtering: the exhaustion guard must |
| 103 | + * not drop valid codes. |
| 104 | + */ |
| 105 | + public function testDiscoverReturnsHealthyDomainAuthorizedCode(): void |
| 106 | + { |
| 107 | + $healthy = Mockery::mock(DomainAuthorizedSummitRegistrationPromoCode::class); |
| 108 | + $healthy->shouldReceive('getCode')->andReturn('HEALTHY'); |
| 109 | + $healthy->shouldReceive('hasQuantityAvailable')->andReturn(true); |
| 110 | + $healthy->shouldReceive('getQuantityPerAccount')->andReturn(0); |
| 111 | + $healthy->shouldReceive('setRemainingQuantityPerAccount')->with(null)->once(); |
| 112 | + |
| 113 | + $summit = Mockery::mock(Summit::class); |
| 114 | + $member = Mockery::mock(Member::class); |
| 115 | + $member->shouldReceive('getEmail')->andReturn('buyer@acme.com'); |
| 116 | + $member->shouldReceive('getId')->andReturn(42); |
| 117 | + |
| 118 | + $repository = Mockery::mock(ISummitRegistrationPromoCodeRepository::class); |
| 119 | + $repository->shouldReceive('getDiscoverableByEmailForSummit') |
| 120 | + ->with($summit, 'buyer@acme.com') |
| 121 | + ->andReturn([$healthy]); |
| 122 | + |
| 123 | + $service = $this->buildService($repository); |
| 124 | + $result = $service->discoverPromoCodes($summit, $member); |
| 125 | + |
| 126 | + $this->assertCount(1, $result); |
| 127 | + $this->assertSame('HEALTHY', $result[0]->getCode()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Mixed case: exhausted code is dropped while a healthy sibling survives. |
| 132 | + * This proves the guard uses per-code `continue`, not a scalar short-circuit. |
| 133 | + */ |
| 134 | + public function testDiscoverMixedHealthyAndExhaustedCodes(): void |
| 135 | + { |
| 136 | + $exhausted = Mockery::mock(DomainAuthorizedSummitRegistrationPromoCode::class); |
| 137 | + $exhausted->shouldReceive('getCode')->andReturn('EXHAUSTED'); |
| 138 | + $exhausted->shouldReceive('hasQuantityAvailable')->andReturn(false); |
| 139 | + $exhausted->shouldReceive('getQuantityPerAccount')->andReturn(0); |
| 140 | + $exhausted->shouldReceive('setRemainingQuantityPerAccount')->andReturn(null); |
| 141 | + |
| 142 | + $healthy = Mockery::mock(DomainAuthorizedSummitRegistrationPromoCode::class); |
| 143 | + $healthy->shouldReceive('getCode')->andReturn('HEALTHY'); |
| 144 | + $healthy->shouldReceive('hasQuantityAvailable')->andReturn(true); |
| 145 | + $healthy->shouldReceive('getQuantityPerAccount')->andReturn(0); |
| 146 | + $healthy->shouldReceive('setRemainingQuantityPerAccount')->with(null)->once(); |
| 147 | + |
| 148 | + $summit = Mockery::mock(Summit::class); |
| 149 | + $member = Mockery::mock(Member::class); |
| 150 | + $member->shouldReceive('getEmail')->andReturn('buyer@acme.com'); |
| 151 | + $member->shouldReceive('getId')->andReturn(7); |
| 152 | + |
| 153 | + $repository = Mockery::mock(ISummitRegistrationPromoCodeRepository::class); |
| 154 | + $repository->shouldReceive('getDiscoverableByEmailForSummit') |
| 155 | + ->with($summit, 'buyer@acme.com') |
| 156 | + ->andReturn([$exhausted, $healthy]); |
| 157 | + |
| 158 | + $service = $this->buildService($repository); |
| 159 | + $result = $service->discoverPromoCodes($summit, $member); |
| 160 | + |
| 161 | + $this->assertCount(1, $result); |
| 162 | + $this->assertSame('HEALTHY', $result[0]->getCode()); |
| 163 | + } |
| 164 | +} |
0 commit comments