|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Unit\Service\Manager; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use PhpList\Core\Domain\Model\Messaging\Template; |
| 9 | +use PhpList\Core\Domain\Repository\Messaging\TemplateRepository; |
| 10 | +use PhpList\RestBundle\Entity\Request\CreateTemplateRequest; |
| 11 | +use PhpList\RestBundle\Service\Manager\TemplateImageManager; |
| 12 | +use PhpList\RestBundle\Service\Manager\TemplateManager; |
| 13 | +use PhpList\RestBundle\Validator\TemplateImageValidator; |
| 14 | +use PhpList\RestBundle\Validator\TemplateLinkValidator; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class TemplateManagerTest extends TestCase |
| 19 | +{ |
| 20 | + private TemplateRepository&MockObject $templateRepository; |
| 21 | + private TemplateImageManager&MockObject $templateImageManager; |
| 22 | + private TemplateLinkValidator&MockObject $templateLinkValidator; |
| 23 | + private TemplateImageValidator&MockObject $templateImageValidator; |
| 24 | + private TemplateManager $manager; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->templateRepository = $this->createMock(TemplateRepository::class); |
| 29 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 30 | + $this->templateImageManager = $this->createMock(TemplateImageManager::class); |
| 31 | + $this->templateLinkValidator = $this->createMock(TemplateLinkValidator::class); |
| 32 | + $this->templateImageValidator = $this->createMock(TemplateImageValidator::class); |
| 33 | + |
| 34 | + $this->manager = new TemplateManager( |
| 35 | + $this->templateRepository, |
| 36 | + $entityManager, |
| 37 | + $this->templateImageManager, |
| 38 | + $this->templateLinkValidator, |
| 39 | + $this->templateImageValidator |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testCreateTemplateSuccessfully(): void |
| 44 | + { |
| 45 | + $request = new CreateTemplateRequest(); |
| 46 | + $request->title = 'Test Template'; |
| 47 | + $request->content = '<html><body>Content</body></html>'; |
| 48 | + $request->text = 'Plain text'; |
| 49 | + $request->checkLinks = true; |
| 50 | + $request->checkImages = false; |
| 51 | + $request->checkExternalImages = false; |
| 52 | + $request->file = null; |
| 53 | + |
| 54 | + $this->templateLinkValidator->expects($this->once()) |
| 55 | + ->method('validate') |
| 56 | + ->with($request->content, $this->anything()); |
| 57 | + |
| 58 | + $this->templateImageManager->expects($this->once()) |
| 59 | + ->method('extractAllImages') |
| 60 | + ->with($request->content) |
| 61 | + ->willReturn([]); |
| 62 | + |
| 63 | + $this->templateImageValidator->expects($this->once()) |
| 64 | + ->method('validate') |
| 65 | + ->with([], $this->anything()); |
| 66 | + |
| 67 | + $this->templateRepository->expects($this->once()) |
| 68 | + ->method('save') |
| 69 | + ->with($this->isInstanceOf(Template::class)); |
| 70 | + |
| 71 | + $this->templateImageManager->expects($this->once()) |
| 72 | + ->method('createImagesFromImagePaths') |
| 73 | + ->with([], $this->isInstanceOf(Template::class)); |
| 74 | + |
| 75 | + $template = $this->manager->create($request); |
| 76 | + |
| 77 | + $this->assertSame('Test Template', $template->getTitle()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testDeleteTemplate(): void |
| 81 | + { |
| 82 | + $template = $this->createMock(Template::class); |
| 83 | + |
| 84 | + $this->templateRepository->expects($this->once()) |
| 85 | + ->method('remove') |
| 86 | + ->with($template); |
| 87 | + |
| 88 | + $this->manager->delete($template); |
| 89 | + } |
| 90 | +} |
0 commit comments