buckets/tests/Validator/SingleOverflowPerScenarioValidatorTest.php

98 lines
3 KiB
PHP
Raw Normal View History

<?php
namespace App\Tests\Validator;
use App\Entity\Bucket;
use App\Entity\Scenario;
use App\Enum\BucketAllocationType;
use App\Enum\BucketType;
use App\Repository\BucketRepository;
use App\Validator\SingleOverflowPerScenario;
use App\Validator\SingleOverflowPerScenarioValidator;
use PHPUnit\Framework\MockObject\Stub;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @extends ConstraintValidatorTestCase<SingleOverflowPerScenarioValidator>
*/
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;
}
}