Add Tags endpoint
[PREMIUM-213]
This commit is contained in:
54
mailpoet/lib/API/JSON/v1/Tags.php
Normal file
54
mailpoet/lib/API/JSON/v1/Tags.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\Entities\TagEntity;
|
||||
use MailPoet\InvalidStateException;
|
||||
use MailPoet\Tags\TagRepository;
|
||||
|
||||
class Tags extends APIEndpoint {
|
||||
|
||||
private $repository;
|
||||
|
||||
public function __construct(
|
||||
TagRepository $repository
|
||||
) {
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function save($data = []) {
|
||||
if (!isset($data['name'])) {
|
||||
throw InvalidStateException::create()->withError(
|
||||
'tag_without_name',
|
||||
__('A tag needs to have a name.', 'mailpoet')
|
||||
);
|
||||
}
|
||||
|
||||
$data['name'] = sanitize_text_field(wp_unslash($data['name']));
|
||||
$data['description'] = isset($data['description']) ? sanitize_text_field(wp_unslash($data['description'])) : '';
|
||||
|
||||
return $this->successResponse(
|
||||
$this->mapTagEntity($this->repository->createOrUpdate($data))
|
||||
);
|
||||
}
|
||||
|
||||
public function listing() {
|
||||
return $this->successResponse(
|
||||
array_map(
|
||||
[$this, 'mapTagEntity'],
|
||||
$this->repository->findAll()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function mapTagEntity(TagEntity $tag): array {
|
||||
return [
|
||||
'id' => $tag->getId(),
|
||||
'name' => $tag->getName(),
|
||||
'description' => $tag->getDescription(),
|
||||
'created' => $tag->getCreatedAt(),
|
||||
'updated' => $tag->getUpdatedAt(),
|
||||
];
|
||||
}
|
||||
}
|
@@ -73,6 +73,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Forms::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\ImportExport::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Mailer::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Tags::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Newsletters::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\NewsletterLinks::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\NewsletterTemplates::class)->setPublic(true);
|
||||
|
58
mailpoet/tests/integration/API/JSON/v1/TagsTest.php
Normal file
58
mailpoet/tests/integration/API/JSON/v1/TagsTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Test\API\JSON\v1;
|
||||
|
||||
use MailPoet\API\JSON\v1\Tags;
|
||||
use MailPoet\Entities\TagEntity;
|
||||
use MailPoet\InvalidStateException;
|
||||
use MailPoet\Tags\TagRepository;
|
||||
|
||||
class TagsTest extends \MailPoetTest {
|
||||
|
||||
/** @var TagRepository */
|
||||
private $repository;
|
||||
|
||||
/** @var Tags */
|
||||
private $testee;
|
||||
|
||||
public function _before() {
|
||||
parent::_before(); // TODO: Change the autogenerated stub
|
||||
|
||||
$this->repository = $this->diContainer->get(TagRepository::class);
|
||||
$this->testee = $this->diContainer->get(Tags::class);
|
||||
}
|
||||
|
||||
public function testItListsAllTags() {
|
||||
$count = count($this->repository->findAll());
|
||||
$this->assertSame(0, $count);
|
||||
do {
|
||||
$this->repository->persist(new TagEntity(sprintf("Tag %d", $count + 1)));
|
||||
$this->repository->flush();
|
||||
$count = count($this->repository->findAll());
|
||||
} while ($count < 10);
|
||||
|
||||
$response = $this->testee->listing();
|
||||
$this->assertCount(10, $response->data);
|
||||
}
|
||||
|
||||
public function testItCreatesTags() {
|
||||
|
||||
$entity = $this->repository->findOneBy(['name' => 'test']);
|
||||
$this->assertNull($entity);
|
||||
$response = $this->testee->save(['name' => 'test']);
|
||||
$entity = $this->repository->findOneBy(['name' => 'test']);
|
||||
$this->assertNotNull($entity);
|
||||
$this->assertSame('test', $response->data['name']);
|
||||
}
|
||||
|
||||
public function testItCanNotCreateTagsWithoutNames() {
|
||||
|
||||
$this->expectException(InvalidStateException::class);
|
||||
$this->testee->save([]);
|
||||
}
|
||||
|
||||
public function _after() {
|
||||
parent::_after(); // TODO: Change the autogenerated stub
|
||||
$this->repository->truncate();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user