Skip to content

Commit ef86452

Browse files
committed
ISSUE-345: delete template endpoint
1 parent 68465fc commit ef86452

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/Controller/TemplateController.php

+50
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,54 @@ public function createTemplates(Request $request): JsonResponse
242242
Response::HTTP_CREATED
243243
);
244244
}
245+
246+
#[Route('/{templateId}', name: 'delete_template', methods: ['DELETE'])]
247+
#[OA\Delete(
248+
path: 'templates/{templateId}',
249+
description: 'Deletes template by id.',
250+
summary: 'Deletes a template.',
251+
tags: ['templates'],
252+
parameters: [
253+
new OA\Parameter(
254+
name: 'session',
255+
description: 'Session ID',
256+
in: 'header',
257+
required: true,
258+
schema: new OA\Schema(type: 'string')
259+
),
260+
new OA\Parameter(
261+
name: 'templateId',
262+
description: 'Template ID',
263+
in: 'path',
264+
required: true,
265+
schema: new OA\Schema(type: 'string')
266+
)
267+
],
268+
responses: [
269+
new OA\Response(
270+
response: Response::HTTP_NO_CONTENT,
271+
description: 'Success'
272+
),
273+
new OA\Response(
274+
response: 403,
275+
description: 'Failure',
276+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
277+
),
278+
new OA\Response(
279+
response: 404,
280+
description: 'Failure',
281+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
282+
)
283+
]
284+
)]
285+
public function delete(
286+
Request $request,
287+
#[MapEntity(mapping: ['templateId' => 'id'])] Template $template
288+
): JsonResponse {
289+
$this->requireAuthentication($request);
290+
291+
$this->templateManager->delete($template);
292+
293+
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
294+
}
245295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)