Skip to content

Commit

Permalink
feat: add create call to search criteria repo
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Nov 11, 2024
1 parent 2fe013d commit 9c5e14d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum OnOfficeResourceType: string
case FileRelation = 'fileRelation';
case Impressum = 'impressum';
case Activity = 'agentslog';
case SearchCriteria = 'searchcriterias';
case GetSearchCriteria = 'searchcriterias';
case SearchCriteria = 'searchcriteria';
case ActionTypes = 'actionkindtypes';
case Relation = 'relation';
case Search = 'search';
Expand Down
33 changes: 32 additions & 1 deletion src/Query/SearchCriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ class SearchCriteriaBuilder extends Builder
{
private string $mode = 'internal';

private int $addressId;

/**
* @throws Throwable<OnOfficeException>
*/
public function find(int|array $id): ?array
{
$request = new OnOfficeRequest(
OnOfficeAction::Get,
OnOfficeResourceType::SearchCriteria,
OnOfficeResourceType::GetSearchCriteria,
parameters: [
OnOfficeService::MODE => $this->mode,
OnOfficeService::IDS => Arr::wrap($id),
Expand All @@ -35,10 +37,39 @@ public function find(int|array $id): ?array
->json('response.results.0.data.records.0', []);
}

/**
* @throws Throwable<OnOfficeException>
*/
public function create(array $data): array
{
if (! isset($this->addressId)) {
throw new OnOfficeException('Address ID is required to create a search criteria');
}

$request = new OnOfficeRequest(
OnOfficeAction::Create,
OnOfficeResourceType::SearchCriteria,
parameters: [
'addressid' => $this->addressId,
OnOfficeService::DATA => $data,
],
);

return $this->requestApi($request)
->json('response.results.0.data.records.0');
}

public function mode(string $mode): self
{
$this->mode = $mode;

return $this;
}

public function addressId(int $addressId): self
{
$this->addressId = $addressId;

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/Repositories/SearchCriteriaRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Http;
use Innobrain\OnOfficeAdapter\Facades\SearchCriteriaRepository;
use Innobrain\OnOfficeAdapter\Facades\Testing\RecordFactories\SearchCriteriaFactory;
use Innobrain\OnOfficeAdapter\Tests\Stubs\CreateSearchCriteriaResponse;
use Innobrain\OnOfficeAdapter\Tests\Stubs\GetSearchCriteriaResponse;

describe('fake responses', function () {
Expand Down Expand Up @@ -44,4 +45,25 @@

SearchCriteriaRepository::assertSentCount(1);
});

test('create', function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([
CreateSearchCriteriaResponse::make(),
]),
]);

SearchCriteriaRepository::record();

$response = SearchCriteriaRepository::query()->find(1);

expect($response)->toBe([
'id' => 25,
'type' => 'searchCriteria',
'elements' => [],
]);

SearchCriteriaRepository::assertSentCount(1);
});
});
54 changes: 54 additions & 0 deletions tests/Stubs/CreateSearchCriteriaResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Tests\Stubs;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Facades\Http;

class CreateSearchCriteriaResponse
{
public static function make(array $data = []): PromiseInterface
{
return Http::response(self::getBody($data));
}

private static function getBody(array $data): array
{
return array_merge_recursive([
'status' => [
'code' => 200,
'errorcode' => 0,
'message' => 'OK',
],
'response' => [
'results' => [
[
'actionid' => 'urn:onoffice-de-ns:smart:2.5:smartml:action:create',
'resourceid' => '',
'resourcetype' => 'searchcriteria',
'cacheable' => false,
'identifier' => '',
'data' => [
'meta' => [
'cntabsolute' => null,
],
'records' => [
[
'id' => 25,
'type' => 'searchCriteria',
'elements' => [],
],
],
],
'status' => [
'errorcode' => 0,
'message' => 'OK',
],
],
],
],
], $data);
}
}

0 comments on commit 9c5e14d

Please sign in to comment.