Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
add method addTags in CaseService
Browse files Browse the repository at this point in the history
  • Loading branch information
siufong-ho committed Jan 8, 2024
1 parent a90d638 commit 9a425a1
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ echo $caseCreated->name; // Display "This is a new case."
```

See [dotfile documentation](https://docs.dotfile.com/reference/case-create-one).
</details>

<details>

<summary>Add tags in an existing case</summary>

```php
use Dotfile\Model\Case\CaseTags;
use Dotfile\Model\Case\CaseTagsInput;

$input = new CaseTagsInput();
$input->caseId = '39cbd6d5-4da5-4d94-ae71-84895c5e552a';
$input->tags = ['A faire'];

$caseAddTags = $client->case->addTags($input); // Returns an instance of CaseTags

echo $caseAddTags->tags[0]['label']; // Display "A faire"
```

</details>

Expand Down Expand Up @@ -103,3 +121,11 @@ echo $individual->lastName; // Display "Parks"
See [dotfile documentation](https://docs.dotfile.com/reference/individual-create-one).

</details>

### Tests

To launch all the tests:

```php
make test
```
4 changes: 3 additions & 1 deletion src/Exception/DotfileApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace Dotfile\Exception;

class DotfileApiException extends \Exception {}
class DotfileApiException extends \Exception
{
}
18 changes: 18 additions & 0 deletions src/Model/Case/CaseTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Dotfile\Model\Case;

/**
* Response when a tag has been added to a case.
*
* @see https://docs.dotfile.com/reference/case-tag-create-case-tags
*/
class CaseTags
{
/**
* @var array<string, string>
*/
public array $tags = [];
}
23 changes: 23 additions & 0 deletions src/Model/Case/CaseTagsInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Dotfile\Model\Case;

/**
* Input used to add a tag for a case.
*
* @see https://docs.dotfile.com/reference/case-tag-create-case-tags
*/
class CaseTagsInput
{
/**
* Case id where the case is created.
*/
public string $caseId;

/**
* @var string[]
*/
public ?array $tags;
}
3 changes: 2 additions & 1 deletion src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ abstract class AbstractService
public function __construct(
protected HttpClientInterface $client,
protected SerializerInterface $serializer,
) {}
) {
}

/**
* @throws DotfileApiException
Expand Down
16 changes: 16 additions & 0 deletions src/Service/CaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Dotfile\Model\Case\CaseCreated;
use Dotfile\Model\Case\CaseCreateInput;
use Dotfile\Model\Case\CaseTags;
use Dotfile\Model\Case\CaseTagsInput;
use Dotfile\Model\Case\RiskLevel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
Expand Down Expand Up @@ -33,4 +35,18 @@ public function create(CaseCreateInput $input): CaseCreated

return $caseCreated;
}

public function addTags(CaseTagsInput $input, string $caseId): CaseTags
{
$body = $this->serializer->serialize($input, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);

$response = $this->client->request(Request::METHOD_POST, 'cases/'.$caseId.'/tags', [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $body,
]);

return $this->serializer->deserialize($this->getContent($response), CaseTags::class, 'json');
}
}
27 changes: 27 additions & 0 deletions src/Service/TagsCaseService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Dotfile\Service;

use Dotfile\Model\Case\CaseTags;
use Dotfile\Model\Case\CaseTagsInput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

class TagsCaseService extends AbstractService
{
public function create(CaseTagsInput $input, string $caseId): CaseTags
{
$body = $this->serializer->serialize($input, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);

$response = $this->client->request(Request::METHOD_POST, 'cases/'.$caseId.'/tags', [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $body,
]);

return $this->serializer->deserialize($this->getContent($response), CaseTags::class, 'json');
}
}
34 changes: 34 additions & 0 deletions tests/FunctionalTests/CaseAddTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Dotfile\Tests\FunctionalTests;

use Dotfile\DotfileClient;
use Dotfile\Model\Case\CaseTags;
use Dotfile\Model\Case\CaseTagsInput;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

class CaseAddTagsTest extends TestCase
{
public function testCaseCreateWithMinimumValidData(): void
{
$client = new DotfileClient(new MockHttpClient([
new MockResponse((string) file_get_contents(__DIR__.'/../fixtures/case_add_tags_with_minimal_data_response.json')),
]));

$input = new CaseTagsInput();
$input->caseId = '39cbd6d5-4da5-4d94-ae71-84895c5e552a';
$input->tags = ['A faire'];

$caseAddTags = $client->case->addTags($input, $input->caseId);

$this->assertInstanceOf(CaseTags::class, $caseAddTags);
$this->assertSame([
'id' => 'ea5c5935-2ec8-4519-a526-7ab25c71ac4e',
'label' => 'A faire',
], current($caseAddTags->tags));
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/case_add_tags_with_minimal_data_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tags": [
{
"id": "ea5c5935-2ec8-4519-a526-7ab25c71ac4e",
"label": "A faire"
}
]
}

0 comments on commit 9a425a1

Please sign in to comment.