*/ final class SingleOverflowPerScenarioValidatorTest extends ConstraintValidatorTestCase { private BucketRepository&Stub $bucketRepository; protected function createValidator(): ConstraintValidatorInterface { $this->bucketRepository = $this->createStub(BucketRepository::class); return new SingleOverflowPerScenarioValidator($this->bucketRepository); } public function testOverflowBucketIsValidWhenRepositoryReportsNoOtherOverflowBuckets(): void { $this->bucketRepository ->method('countOverflowBucketsForScenario') ->willReturn(0); $bucket = $this->makeBucket(BucketType::OVERFLOW); $this->validator->validate($bucket, new SingleOverflowPerScenario()); $this->assertNoViolation(); } public function testOverflowBucketIsInvalidWhenRepositoryReportsAnExistingOverflowBucket(): void { $this->bucketRepository ->method('countOverflowBucketsForScenario') ->willReturn(1); $bucket = $this->makeBucket(BucketType::OVERFLOW); $this->validator->validate($bucket, new SingleOverflowPerScenario()); $this->buildViolation('A scenario can only have one overflow bucket.') ->atPath('property.path.type') ->setCode(SingleOverflowPerScenario::TOO_MANY_OVERFLOW_ERROR) ->assertRaised(); } public function testNeedBucketIsValid(): void { $bucket = $this->makeBucket(BucketType::NEED); $this->validator->validate($bucket, new SingleOverflowPerScenario()); $this->assertNoViolation(); } public function testWantBucketIsValid(): void { $bucket = $this->makeBucket(BucketType::WANT); $this->validator->validate($bucket, new SingleOverflowPerScenario()); $this->assertNoViolation(); } private function makeBucket(BucketType $type): Bucket { $scenario = new Scenario(); $scenario->setName('Household Budget'); $bucket = new Bucket(); $bucket->setScenario($scenario); $bucket->setType($type); $bucket->setName('Test Bucket'); $bucket->setPriority(1); $bucket->setSortOrder(0); $bucket->setAllocationType( BucketType::OVERFLOW === $type ? BucketAllocationType::UNLIMITED : BucketAllocationType::FIXED_LIMIT, ); $bucket->setAllocationValue(BucketType::OVERFLOW === $type ? null : 50000); $bucket->setStartingAmount(0); $bucket->setBufferMultiplier('0.00'); return $bucket; } }