Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional query #58

Merged
merged 2 commits into from
Dec 20, 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
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
'strict_comparison' => true,
'strict_param' => true,
'fopen_flags' => array('b_mode' => true),
'native_function_invocation' => false,
))
->setFinder(
(new PhpCsFixer\Finder())
Expand Down
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
10 changes: 2 additions & 8 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 All @@ -77,7 +71,7 @@ private function list(OutputInterface $output, ?string $key, array $tags): int
try {
$result = $this->tagService->list($key, $tags);
} catch (ProviderException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));

return Command::FAILURE;
}
Expand All @@ -91,7 +85,7 @@ private function list(OutputInterface $output, ?string $key, array $tags): int
$output->writeln('<info>your query would match the following keys (sample):</info>');

foreach ($result as $item) {
$output->writeln(sprintf('<comment>></comment> <info>%s</info>', $item));
$output->writeln(\sprintf('<comment>></comment> <info>%s</info>', $item));
}

return Command::SUCCESS;
Expand Down
4 changes: 2 additions & 2 deletions src/Command/PhraseKeyTagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
try {
$keys = $this->tagService->tag($queryKey, $queryTag, $tag);
} catch (ProviderException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));

return Command::FAILURE;
}

$output->writeln(sprintf('<info>successfully tagged %d keys with "%s"</info>', $keys, implode(', ', $tag)));
$output->writeln(\sprintf('<info>successfully tagged %d keys with "%s"</info>', $keys, implode(', ', $tag)));

return Command::SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Command/PhraseKeyUntagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
try {
$keys = $this->tagService->untag($queryKey, $queryTag, $tag);
} catch (ProviderException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));

return Command::FAILURE;
}

$output->writeln(sprintf('<info>successfully untagged %d keys with "%s"</info>', $keys, implode(', ', $tag)));
$output->writeln(\sprintf('<info>successfully untagged %d keys with "%s"</info>', $keys, implode(', ', $tag)));

return Command::SUCCESS;
}
Expand Down
44 changes: 26 additions & 18 deletions src/Service/PhraseTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@
*/
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()) {
throw new ProviderException(sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
throw new ProviderException(\sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
}

/** @var array{name: string} $arr */
Expand All @@ -59,26 +61,29 @@
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()) {
throw new ProviderException(sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
throw new ProviderException(\sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
}

/** @var array{records_affected: string} $arr */
$arr = $response->toArray();
$records = $arr['records_affected'];

$this->logger->info(sprintf('tagged %d keys matching "%s" with tag(s) "%s"', $records, $query, implode(', ', $addTags)));
$this->logger->info(\sprintf('tagged %d keys matching "%s" with tag(s) "%s"', $records, $query, implode(', ', $addTags)));

return (int) $records;
}
Expand All @@ -90,26 +95,29 @@
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()) {
throw new ProviderException(sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
throw new ProviderException(\sprintf('phrase replied with an error (%d): "%s"', $statusCode, $response->getContent(false)), $response);
}

/** @var array{records_affected: string} $arr */
$arr = $response->toArray();
$records = $arr['records_affected'];

$this->logger->info(sprintf('untagged %d keys matching "%s" with tag(s) "%s"', $records, $query, implode(', ', $removeTags)));
$this->logger->info(\sprintf('untagged %d keys matching "%s" with tag(s) "%s"', $records, $query, implode(', ', $removeTags)));

return (int) $records;
}
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
18 changes: 12 additions & 6 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 @@ -103,11 +103,11 @@ public function testTag(?string $key, array $tags, array $newTags): void
$this->getLogger()
->expects(self::once())
->method('info')
->with(sprintf('tagged 10 keys matching "%s" with tag(s) "%s"', $this->query($key, $tags), implode(', ', $newTags)));
->with(\sprintf('tagged 10 keys matching "%s" with tag(s) "%s"', $this->query($key, $tags), implode(', ', $newTags)));

$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 @@ -158,11 +158,11 @@ public function testUnTag(?string $key, array $tags, array $newTags): void
$this->getLogger()
->expects(self::once())
->method('info')
->with(sprintf('untagged 10 keys matching "%s" with tag(s) "%s"', $this->query($key, $tags), implode(', ', $newTags)));
->with(\sprintf('untagged 10 keys matching "%s" with tag(s) "%s"', $this->query($key, $tags), implode(', ', $newTags)));

$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
Loading