|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Unit\Messaging\Validator\Constraint; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Messaging\Model\Template; |
| 8 | +use PhpList\Core\Domain\Messaging\Repository\TemplateRepository; |
| 9 | +use PhpList\RestBundle\Messaging\Validator\Constraint\UniqueTemplateTitle; |
| 10 | +use PhpList\RestBundle\Messaging\Validator\Constraint\UniqueTemplateTitleValidator; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
| 14 | +use Symfony\Component\Validator\Constraint; |
| 15 | +use Symfony\Component\Validator\Context\ExecutionContextInterface; |
| 16 | +use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 17 | +use Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 18 | + |
| 19 | +class UniqueTemplateTitleValidatorTest extends TestCase |
| 20 | +{ |
| 21 | + private TemplateRepository&MockObject $templateRepository; |
| 22 | + private UniqueTemplateTitleValidator $validator; |
| 23 | + private ExecutionContextInterface&MockObject $context; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $this->templateRepository = $this->createMock(TemplateRepository::class); |
| 28 | + $this->context = $this->createMock(ExecutionContextInterface::class); |
| 29 | + |
| 30 | + $this->validator = new UniqueTemplateTitleValidator($this->templateRepository); |
| 31 | + $this->validator->initialize($this->context); |
| 32 | + } |
| 33 | + |
| 34 | + public function testValidateSkipsNull(): void |
| 35 | + { |
| 36 | + $this->templateRepository->expects($this->never())->method('findOneBy'); |
| 37 | + $this->validator->validate(null, new UniqueTemplateTitle()); |
| 38 | + $this->assertTrue(true); |
| 39 | + } |
| 40 | + |
| 41 | + public function testValidateSkipsEmptyString(): void |
| 42 | + { |
| 43 | + $this->templateRepository->expects($this->never())->method('findOneBy'); |
| 44 | + $this->validator->validate('', new UniqueTemplateTitle()); |
| 45 | + $this->assertTrue(true); |
| 46 | + } |
| 47 | + |
| 48 | + public function testValidateThrowsUnexpectedTypeException(): void |
| 49 | + { |
| 50 | + $this->expectException(UnexpectedTypeException::class); |
| 51 | + $this->validator->validate('title', $this->createMock(Constraint::class)); |
| 52 | + } |
| 53 | + |
| 54 | + public function testValidateThrowsUnexpectedValueException(): void |
| 55 | + { |
| 56 | + $this->expectException(UnexpectedValueException::class); |
| 57 | + $this->validator->validate(123, new UniqueTemplateTitle()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testValidateThrowsConflictHttpExceptionIfTemplateTitleExists(): void |
| 61 | + { |
| 62 | + $existingTemplate = $this->createMock(Template::class); |
| 63 | + |
| 64 | + $this->templateRepository |
| 65 | + ->expects($this->once()) |
| 66 | + ->method('findOneBy') |
| 67 | + ->with(['title' => 'Newsletter Template']) |
| 68 | + ->willReturn($existingTemplate); |
| 69 | + |
| 70 | + $this->expectException(ConflictHttpException::class); |
| 71 | + $this->expectExceptionMessage('Template title already exists.'); |
| 72 | + |
| 73 | + $this->validator->validate('Newsletter Template', new UniqueTemplateTitle()); |
| 74 | + } |
| 75 | + |
| 76 | + public function testValidatePassesIfTemplateTitleIsUnique(): void |
| 77 | + { |
| 78 | + $this->templateRepository |
| 79 | + ->expects($this->once()) |
| 80 | + ->method('findOneBy') |
| 81 | + ->with(['title' => 'Unique Template']) |
| 82 | + ->willReturn(null); |
| 83 | + |
| 84 | + $this->validator->validate('Unique Template', new UniqueTemplateTitle()); |
| 85 | + $this->assertTrue(true); |
| 86 | + } |
| 87 | + |
| 88 | + public function testValidateSkipsConflictForSameTemplateOnUpdate(): void |
| 89 | + { |
| 90 | + $existingTemplate = $this->createConfiguredMock(Template::class, ['getId' => 10]); |
| 91 | + |
| 92 | + $this->templateRepository |
| 93 | + ->expects($this->once()) |
| 94 | + ->method('findOneBy') |
| 95 | + ->with(['title' => 'Existing Title']) |
| 96 | + ->willReturn($existingTemplate); |
| 97 | + |
| 98 | + $dto = new class { |
| 99 | + public int $templateId = 10; |
| 100 | + }; |
| 101 | + |
| 102 | + $this->context |
| 103 | + ->method('getObject') |
| 104 | + ->willReturn($dto); |
| 105 | + |
| 106 | + $this->validator->validate('Existing Title', new UniqueTemplateTitle()); |
| 107 | + $this->assertTrue(true); |
| 108 | + } |
| 109 | +} |
0 commit comments