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

Add update case feature #11

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ See [dotfile documentation](https://docs.dotfile.com/reference/case-create-one).

<details>

<summary>Update a case</summary>

```php
use Dotfile\Model\Case\CaseUpdated;
use Dotfile\Model\Case\CaseUpdateInput;

$input = new CaseUpdateInput();
$input->name = 'This is an update for the case.';

$caseId = '39cbd6d5-4da5-4d94-ae71-84895c5e552a';

$caseUpdated = $client->case->update($caseId, $input); // Returns an instance of CaseUpdated

echo $caseUpdated->name; // Display "This is an update for the case."
```

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

<details>

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

```php
Expand Down
51 changes: 51 additions & 0 deletions src/Model/Case/CaseUpdateInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Dotfile\Model\Case;

/**
* Input used to create a case.
*
* @see https://docs.dotfile.com/reference/case-create-one
*/
class CaseUpdateInput
{
/**
* Name for the case (at most 180 characters).
*/
public string $name;

/**
* Optional status for this case. Defaults to open.
*/
public ?CaseStatus $status = null;

/**
* Optional external id for this case. You can use that to reference an entity of your system. Must be unique if set.
*/
public ?string $externalId = null;

/**
* Optional template id to attach template to this case.
*/
public ?string $templateId = null;

/**
* Optional metadata in a format of key (string) / value (string) for this case.
*
* @see https://docs.dotfile.com/reference/cases-guide#metadata
*
* @var array<string, string>
*/
public ?array $metadata = null;

/**
* Optional custom properties in a format of key (string) / value (string|boolean|[string]|null) for this case.
*
* @see https://docs.dotfile.com/reference/cases-guide#custom-properties
*
* @var array<string, string|boolean|string[]|null>
*/
public ?array $customProperties = null;
}
51 changes: 51 additions & 0 deletions src/Model/Case/CaseUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Dotfile\Model\Case;

/**
* Response when a case have been created.
*
* @see https://docs.dotfile.com/reference/case-create-one
*/
class CaseUpdated
{
public string $id;
public string $name;
public CaseStatus $status;
public ?string $externalId = null;
public ?string $templateId = null;

/**
* @var CaseFlag[]
*/
public array $flags;
/**
* @var string[]
*/
public array $tags;
public ?Risk $risk = null;

/**
* Optional metadata in a format of key (string) / value (string) for this case.
*
* @see https://docs.dotfile.com/reference/cases-guide#metadata
*
* @var array<string, string>|null
*/
public ?array $metadata = null;

/**
* Optional custom properties in a format of key (string) / value (string|boolean|[string]|null) for this case.
*
* @see https://docs.dotfile.com/reference/cases-guide#custom-properties
*
* @var array<string, string|boolean|string[]|null>
*/
public ?array $customProperties = null;

public \DateTimeImmutable $createdAt;
public \DateTimeImmutable $updatedAt;
public \DateTimeImmutable $lastActivityAt;
}
24 changes: 24 additions & 0 deletions src/Service/CaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Dotfile\Model\Case\CaseCreated;
use Dotfile\Model\Case\CaseCreateInput;
use Dotfile\Model\Case\CaseTags;
use Dotfile\Model\Case\CaseUpdated;
use Dotfile\Model\Case\CaseUpdateInput;
use Dotfile\Model\Case\RiskLevel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
Expand Down Expand Up @@ -37,6 +39,28 @@ public function create(CaseCreateInput $input): CaseCreated
return $caseCreated;
}

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

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

$caseUpdated = $this->serializer->deserialize($this->getContent($response), CaseUpdated::class, 'json');

if (null !== $caseUpdated->risk) {
if (RiskLevel::NotDefined === $caseUpdated->risk->level) {
$caseUpdated->risk = null;
}
}

return $caseUpdated;
}

public function getTags(string $caseId): CaseTags
{
$response = $this->client->request(Request::METHOD_GET, 'cases/'.$caseId.'/tags', [
Expand Down
45 changes: 45 additions & 0 deletions tests/FunctionalTests/CaseUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Dotfile\Tests\FunctionalTests;

use Dotfile\DotfileClient;
use Dotfile\Model\Case\CaseStatus;
use Dotfile\Model\Case\CaseUpdated;
use Dotfile\Model\Case\CaseUpdateInput;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

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

$input = new CaseUpdateInput();
$input->name = 'Update case informations from tests';
$input->status = CaseStatus::Rejected;
$input->externalId = '0101596';
$input->metadata = ['metadata_key1' => 'metadata_value1'];

$caseUpdated = $client->case->update('fd69915d-4f0e-4003-a052-4db596cf090a', $input);

$this->assertInstanceOf(CaseUpdated::class, $caseUpdated);
$this->assertSame('Update case informations from tests', $caseUpdated->name);
$this->assertSame(CaseStatus::Rejected, $caseUpdated->status);
$this->assertSame('0101596', $caseUpdated->externalId);
$this->assertNull($caseUpdated->templateId);
$this->assertSame([], $caseUpdated->flags);
$this->assertSame([], $caseUpdated->tags);
$this->assertNull($caseUpdated->risk);
$this->assertSame(['metadata_key1' => 'metadata_value1'], $caseUpdated->metadata);
$this->assertSame(['a_custom_property' => null], $caseUpdated->customProperties);
$this->assertInstanceOf(\DateTimeImmutable::class, $caseUpdated->createdAt);
$this->assertInstanceOf(\DateTimeImmutable::class, $caseUpdated->updatedAt);
$this->assertInstanceOf(\DateTimeImmutable::class, $caseUpdated->lastActivityAt);
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/case_update_with_minimal_data_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "fd69915d-4f0e-4003-a052-4db596cf090a",
"name": "Update case informations from tests",
"status": "rejected",
"external_id": "0101596",
"template_id": null,
"flags": [],
"tags": [],
"risk": {
"level": "not_defined"
},
"custom_properties": {
"a_custom_property": null
},
"metadata": {
"metadata_key1": "metadata_value1"
},
"created_at": "2024-01-09T13:06:29.105Z",
"updated_at": "2024-01-09T13:09:50.122Z",
"last_activity_at": "2024-01-09T13:09:50.123Z"
}