Skip to content

Commit

Permalink
optional query
Browse files Browse the repository at this point in the history
- make query key and query tag optional to match all keys
  • Loading branch information
wickedOne committed Dec 20, 2024
1 parent 0a5dd81 commit c1b0694
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ this command helps you to batch tag keys in phrase by querying for existing tags
you can search for multiple tags at once and a broad search on key name using the `*` wildcard.
keep in mind the query is an AND query, meaning the keys have to match all criteria.

> [!TIP]
> if you want to match **all** keys, simply omit the query-key (`-k`) and query-tag (`-t`) options

**example**:

```bash
Expand All @@ -100,6 +103,9 @@ this command helps you to batch remove tags from keys in phrase by querying for
you can search for multiple tags at once and a broad search on key name using the `*` wildcard.
keep in mind the query is an AND query, meaning the keys have to match all criteria.

> [!TIP]
> if you want to match **all** keys, simply omit the query-key (`-k`) and query-tag (`-t`) options

**example**:

```bash
Expand Down
6 changes: 0 additions & 6 deletions src/Command/AbstractPhraseKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string[] $tags */
$tags = $input->getOption('query-tag');

if (null === $key && [] === $tags) {
$output->writeln('<error>no query parameters provided</error>');

return Command::FAILURE;
}

if (true === $input->getOption('dry-run')) {
return $this->list($output, $key, $tags);
}
Expand Down
34 changes: 21 additions & 13 deletions src/Service/PhraseTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public function __construct(
*/
public function list(?string $key, array $tags): array
{
$query = array_filter([
'page' => '1',
'per_page' => '100',
'q' => $this->createQuery($key, $tags),
], static fn (string $value): bool => '' !== $value);

$response = $this->httpClient->request('GET', 'keys', [
'query' => [
'page' => '1',
'per_page' => '100',
'q' => $this->createQuery($key, $tags),
],
'query' => $query,
]);

if (200 !== $statusCode = $response->getStatusCode()) {
Expand All @@ -59,15 +61,18 @@ public function list(?string $key, array $tags): array
public function tag(?string $key, array $tags, array $addTags): int
{
$query = $this->createQuery($key, $tags);

$body = array_filter([

Check warning on line 65 in src/Service/PhraseTagService.php

View workflow job for this annotation

GitHub Actions / mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "UnwrapArrayFilter": @@ @@ public function tag(?string $key, array $tags, array $addTags): int { $query = $this->createQuery($key, $tags); - $body = array_filter(['q' => $query, 'tags' => implode(',', $addTags)], static fn(string $value): bool => '' !== $value); + $body = ['q' => $query, 'tags' => implode(',', $addTags)]; $response = $this->httpClient->request('PATCH', 'keys/tag', ['query' => ['page' => '1', 'per_page' => '100'], 'body' => $body]); if (200 !== $statusCode = $response->getStatusCode()) { throw new ProviderException(sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
'q' => $query,
'tags' => implode(',', $addTags),
], static fn (string $value): bool => '' !== $value);

$response = $this->httpClient->request('PATCH', 'keys/tag', [
'query' => [
'page' => '1',
'per_page' => '100',
],
'body' => [
'q' => $query,
'tags' => implode(',', $addTags),
],
'body' => $body,
]);

if (200 !== $statusCode = $response->getStatusCode()) {
Expand All @@ -90,15 +95,18 @@ public function tag(?string $key, array $tags, array $addTags): int
public function untag(?string $key, array $tags, array $removeTags): int
{
$query = $this->createQuery($key, $tags);

$body = array_filter([

Check warning on line 99 in src/Service/PhraseTagService.php

View workflow job for this annotation

GitHub Actions / mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "UnwrapArrayFilter": @@ @@ public function untag(?string $key, array $tags, array $removeTags): int { $query = $this->createQuery($key, $tags); - $body = array_filter(['q' => $query, 'tags' => implode(',', $removeTags)], static fn(string $value): bool => '' !== $value); + $body = ['q' => $query, 'tags' => implode(',', $removeTags)]; $response = $this->httpClient->request('PATCH', 'keys/untag', ['query' => ['page' => '1', 'per_page' => '100'], 'body' => $body]); if (200 !== $statusCode = $response->getStatusCode()) { throw new ProviderException(sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
'q' => $query,
'tags' => implode(',', $removeTags),
], static fn (string $value): bool => '' !== $value);

$response = $this->httpClient->request('PATCH', 'keys/untag', [
'query' => [
'page' => '1',
'per_page' => '100',
],
'body' => [
'q' => $query,
'tags' => implode(',', $removeTags),
],
'body' => $body,
]);

if (200 !== $statusCode = $response->getStatusCode()) {
Expand Down
12 changes: 0 additions & 12 deletions tests/Unit/Command/AbstractPhraseKeyCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ public function testListProviderException(): void
$this->assertSame('something went wrong', trim($commandTester->getDisplay()));
}

public function testInputFailure(): void
{
$commandTester = $this->createCommandTester();
$commandTester->execute([
'command' => PhraseKeyTagCommand::getDefaultName(),
'-t' => [],
]);

$this->assertSame(Command::FAILURE, $commandTester->getStatusCode());
$this->assertSame('no query parameters provided', trim($commandTester->getDisplay()));
}

public function testInputFailureNoDryRun(): void
{
$commandTester = $this->createCommandTester();
Expand Down
14 changes: 10 additions & 4 deletions tests/Unit/Service/PhraseTagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function testList(?string $key, array $tags, string $responseContent): vo
{
$responses = [
'list keys' => function (string $method, string $url) use ($key, $tags, $responseContent): ResponseInterface {
$parts = [
$parts = array_filter([
'page' => '1',
'per_page' => '100',
'q' => $this->query($key, $tags),
];
], static fn (string $value): bool => '' !== $value);

$queryString = $this->mergeQueryString(null, $parts, true);
$this->assertSame('GET', $method);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testTag(?string $key, array $tags, array $newTags): void

$responses = [
'tag keys' => function (string $method, string $url, array $options = []) use ($key, $tags, $newTags): ResponseInterface {
$body = ['q' => $this->query($key, $tags), 'tags' => implode(',', $newTags)];
$body = array_filter(['q' => $this->query($key, $tags), 'tags' => implode(',', $newTags)], static fn (string $value): bool => '' !== $value);
$this->assertSame('PATCH', $method);
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys/tag?page=1&per_page=100', $url);
$this->assertSame(http_build_query($body), $options['body']);
Expand Down Expand Up @@ -162,7 +162,7 @@ public function testUnTag(?string $key, array $tags, array $newTags): void

$responses = [
'untag keys' => function (string $method, string $url, array $options = []) use ($key, $tags, $newTags): ResponseInterface {
$body = ['q' => $this->query($key, $tags), 'tags' => implode(',', $newTags)];
$body = array_filter(['q' => $this->query($key, $tags), 'tags' => implode(',', $newTags)], static fn (string $value): bool => '' !== $value);
$this->assertSame('PATCH', $method);
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys/untag?page=1&per_page=100', $url);
$this->assertSame(http_build_query($body), $options['body']);
Expand Down Expand Up @@ -268,6 +268,12 @@ public static function listProvider(): \Generator
'tags' => ['tag-one', 'tag-two'],
'responseContent' => $content,
];

yield 'no key no tags' => [
'key' => null,
'tags' => [],
'responseContent' => $content,
];
}

private function createTagService(?MockHttpClient $httpClient = null): PhraseTagService
Expand Down

0 comments on commit c1b0694

Please sign in to comment.