Skip to content

Commit

Permalink
feat: add modify query
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed May 23, 2024
1 parent ed528f4 commit 18ef45c
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Query/AddressBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public function each(callable $callback): void
}, $callback, pageSize: $listLimit, offset: $listOffset);
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
$this->onOfficeService->requestApi(
OnOfficeAction::Modify,
OnOfficeResourceType::Address,
$id,
parameters: $this->modifies,
);

return true;
}

public function count(): int
{
$recordIds = $this->recordIds;
Expand Down
20 changes: 20 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ abstract class Builder
*/
public array $filters = [];

/**
* An array of modify parameters.
*/
public array $modifies = [];

/**
* The limit for the query.
*/
Expand Down Expand Up @@ -67,6 +72,19 @@ public function orderByDesc(string $column): self
return $this->orderBy($column, 'desc');
}

public function addModify(string|array $column, mixed $value = null): self
{
if (is_array($column)) {
$this->modifies = array_merge($this->modifies, $column);

return $this;
}

$this->modifies[$column] = $value;

return $this;
}

public function offset(int $value): self
{
$this->offset = max(0, $value);
Expand Down Expand Up @@ -125,4 +143,6 @@ abstract public function first(): array;
abstract public function find(int $id): array;

abstract public function each(callable $callback): void;

abstract public function modify(int $id): bool;
}
15 changes: 15 additions & 0 deletions src/Query/EstateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ public function each(callable $callback): void
);
}, $callback, pageSize: $listLimit, offset: $listOffset);
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
$this->onOfficeService->requestApi(
OnOfficeAction::Modify,
OnOfficeResourceType::Estate,
$id,
parameters: $this->modifies,
);

return true;
}
}
8 changes: 8 additions & 0 deletions src/Query/EstateFileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ public function each(callable $callback): void
);
}, $callback, pageSize: $this->limit, offset: $this->offset);
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
8 changes: 8 additions & 0 deletions src/Query/FieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ public function withModules(array|string $modules): self

return $this;
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
8 changes: 8 additions & 0 deletions src/Query/MarketplaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ public function each(callable $callback): void
{
throw new OnOfficeException('Method not implemented yet');
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
8 changes: 8 additions & 0 deletions src/Query/RegionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public function each(callable $callback): void
);
}, $callback, pageSize: $listLimit, offset: $listOffset);
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
8 changes: 8 additions & 0 deletions src/Query/RelationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,12 @@ public function relationType(OnOfficeRelationType $relationType): self

return $this;
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
13 changes: 13 additions & 0 deletions src/Query/Testing/BaseFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ public function each(callable $callback): void
$callback($records);
});
}

/**
* @throws Throwable
*/
public function modify(int $id): bool
{
$nextRequest = $this->fakeResponses->shift();
throw_if($nextRequest === null, new Exception('No more fake responses'));

return collect($nextRequest)
->flatten()
->first();
}
}
8 changes: 8 additions & 0 deletions src/Query/UserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,12 @@ public function each(callable $callback): void
);
}, $callback, pageSize: $listLimit, offset: $listOffset);
}

/**
* @throws OnOfficeException
*/
public function modify(int $id): bool
{
throw new OnOfficeException('Not implemented');
}
}
52 changes: 52 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function find(int $id): array
public function each(callable $callback): void
{
}

public function modify(int $id): bool
{
return true;
}
}

describe('select', function () {
Expand Down Expand Up @@ -69,6 +74,53 @@ public function each(callable $callback): void
});
});

describe('modifies', function () {
it('should return the builder instance', function () {
$builder = new Builder();

$result = $builder->addModify('Name', 'Foo');

expect($result)->toBeInstanceOf(Builder::class);
});

it('should add modifies parameters', function () {
$builder = new Builder();

$builder->addModify('Name', 'Foo');

expect($builder->modifies)->toBe(['Name' => 'Foo']);
});

it('should add multiple modifies parameters', function () {
$builder = new Builder();

$builder->addModify('Name', 'Foo');
$builder->addModify('ID', 1);

expect($builder->modifies)->toBe(['Name' => 'Foo', 'ID' => 1]);
});

it('should add multiple modifies parameters with the same key', function () {
$builder = new Builder();

$builder->addModify('Name', 'Foo');
$builder->addModify('Name', 'Bar');

expect($builder->modifies)->toBe(['Name' => 'Bar']);
});

it('should add multiple modifies parameters as array', function () {
$builder = new Builder();

$builder->addModify([
'Name' => 'Foo',
'ID' => 1,
]);

expect($builder->modifies)->toBe(['Name' => 'Foo', 'ID' => 1]);
});
});

describe('orderBy', function () {
it('should add the given column and direction to the orderBy property', function () {
$builder = new Builder();
Expand Down
40 changes: 40 additions & 0 deletions tests/Testing/EstateRepositoryFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,43 @@
->toThrow('No more fake responses');
});
});

describe('modify', function () {
it('can modify', function () {
EstateRepository::fake([
[
true,
],
]);

$result = EstateRepository::query()->modify(1);

expect($result)->toBeTrue();
});

it('can modify multiple times', function () {
EstateRepository::fake([
[
true,
],
], [
[
true,
],
]);

$result = EstateRepository::query()->modify(1);

expect($result)->toBeTrue();

$result = EstateRepository::query()->modify(2);

expect($result)->toBeTrue();
});

it('throws an exception when no more fake responses are available', function () {
EstateRepository::fake();

EstateRepository::query()->modify(1);
})->throws(Exception::class, 'No more fake responses');
});

0 comments on commit 18ef45c

Please sign in to comment.