From c1b06944bb5305173d2124fd13fbca0adf2be22e Mon Sep 17 00:00:00 2001 From: wickedOne Date: Fri, 20 Dec 2024 09:26:54 +0100 Subject: [PATCH] optional query - make query key and query tag optional to match all keys --- README.md | 6 ++++ src/Command/AbstractPhraseKeyCommand.php | 6 ---- src/Service/PhraseTagService.php | 34 ++++++++++++------- .../Command/AbstractPhraseKeyCommandTest.php | 12 ------- tests/Unit/Service/PhraseTagServiceTest.php | 14 +++++--- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 4140b58..42c9f3a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/Command/AbstractPhraseKeyCommand.php b/src/Command/AbstractPhraseKeyCommand.php index ec24995..dfef568 100644 --- a/src/Command/AbstractPhraseKeyCommand.php +++ b/src/Command/AbstractPhraseKeyCommand.php @@ -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('no query parameters provided'); - - return Command::FAILURE; - } - if (true === $input->getOption('dry-run')) { return $this->list($output, $key, $tags); } diff --git a/src/Service/PhraseTagService.php b/src/Service/PhraseTagService.php index df1ffd7..fbb165b 100644 --- a/src/Service/PhraseTagService.php +++ b/src/Service/PhraseTagService.php @@ -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()) { @@ -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([ + '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()) { @@ -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([ + '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()) { diff --git a/tests/Unit/Command/AbstractPhraseKeyCommandTest.php b/tests/Unit/Command/AbstractPhraseKeyCommandTest.php index 47aa5a7..b9368e8 100644 --- a/tests/Unit/Command/AbstractPhraseKeyCommandTest.php +++ b/tests/Unit/Command/AbstractPhraseKeyCommandTest.php @@ -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(); diff --git a/tests/Unit/Service/PhraseTagServiceTest.php b/tests/Unit/Service/PhraseTagServiceTest.php index a2b58be..955ecd0 100644 --- a/tests/Unit/Service/PhraseTagServiceTest.php +++ b/tests/Unit/Service/PhraseTagServiceTest.php @@ -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); @@ -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']); @@ -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']); @@ -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