Skip to content

Commit 6e909dc

Browse files
committed
Fix UpdateClientCommand options
1 parent 6d65806 commit 6e909dc

2 files changed

Lines changed: 96 additions & 33 deletions

File tree

Command/UpdateClientCommand.php

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,30 @@ protected function configure(): void
3939
->addOption(
4040
'redirect-uri',
4141
null,
42-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
43-
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
44-
[]
42+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
43+
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs. Use it without value to remove existing values.',
44+
[0]
4545
)
4646
->addOption(
4747
'grant-type',
4848
null,
49-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
50-
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types.',
51-
[]
49+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
50+
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types. Use it without value to remove existing values.',
51+
[0]
5252
)
5353
->addOption(
5454
'scope',
5555
null,
56-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
57-
'Sets allowed scope for client. Use this option multiple times to set multiple scopes.',
58-
[]
56+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
57+
'Sets allowed scope for client. Use this option multiple times to set multiple scopes. Use it without value to remove existing values.',
58+
[0]
5959
)
6060
->addOption(
61-
'deactivated',
61+
'active',
6262
null,
63-
InputOption::VALUE_NONE,
64-
'If provided, it will deactivate the given client.'
63+
InputOption::VALUE_REQUIRED,
64+
'Client active state, 1 for active, 0 for inactive',
65+
null
6566
)
6667
->addArgument(
6768
'identifier',
@@ -90,26 +91,69 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9091

9192
private function updateClientFromInput(Client $client, InputInterface $input): Client
9293
{
93-
$client->setActive(!$input->getOption('deactivated'));
94-
95-
$redirectUris = array_map(
96-
static function (string $redirectUri): RedirectUri { return new RedirectUri($redirectUri); },
97-
$input->getOption('redirect-uri')
98-
);
99-
$client->setRedirectUris(...$redirectUris);
100-
101-
$grants = array_map(
102-
static function (string $grant): Grant { return new Grant($grant); },
103-
$input->getOption('grant-type')
104-
);
105-
$client->setGrants(...$grants);
106-
107-
$scopes = array_map(
108-
static function (string $scope): Scope { return new Scope($scope); },
109-
$input->getOption('scope')
110-
);
111-
$client->setScopes(...$scopes);
94+
$active = $input->getOption('active');
95+
96+
if (null !== $active) {
97+
$client->setActive((bool) $active);
98+
}
99+
100+
$redirectUrisArray = $this->getNullableOption($input, 'redirect-uri');
101+
102+
if (null !== $redirectUrisArray) {
103+
$redirectUris = array_map(
104+
static function (string $redirectUri): RedirectUri {
105+
return new RedirectUri($redirectUri);
106+
},
107+
$redirectUrisArray
108+
);
109+
$client->setRedirectUris(...$redirectUris);
110+
}
111+
112+
$grantsArray = $this->getNullableOption($input, 'grant-type');
113+
114+
if (null !== $grantsArray) {
115+
$grants = array_map(
116+
static function (string $grant): Grant {
117+
return new Grant($grant);
118+
},
119+
$grantsArray
120+
);
121+
$client->setGrants(...$grants);
122+
}
123+
124+
$scopesArray = $this->getNullableOption($input, 'scope');
125+
126+
if (null !== $scopesArray) {
127+
$scopes = array_map(
128+
static function (string $scope): Scope {
129+
return new Scope($scope);
130+
},
131+
$scopesArray
132+
);
133+
$client->setScopes(...$scopes);
134+
}
112135

113136
return $client;
114137
}
138+
139+
private function getNullableOption(InputInterface $input, string $name): ?array
140+
{
141+
$value = $input->getOption($name);
142+
143+
if (
144+
\array_key_exists(0, $value)
145+
&& 0 === $value[0] //if user has entered some value it will always be string so it is fine to rely on 0
146+
) {
147+
return null;
148+
}
149+
150+
if (
151+
\array_key_exists(0, $value)
152+
&& null === $value[0] //when option has mode InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY and no value is sent, option will have value [null]
153+
) {
154+
return [];
155+
}
156+
157+
return $value;
158+
}
115159
}

Tests/Acceptance/UpdateClientCommandTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testUpdateScopes(): void
6464
$this->assertCount(2, $client->getScopes());
6565
}
6666

67-
public function testDeactivate(): void
67+
public function testSetClientActive(): void
6868
{
6969
$client = $this->fakeAClient('foobar');
7070
$this->getClientManager()->save($client);
@@ -75,7 +75,26 @@ public function testDeactivate(): void
7575
$commandTester->execute([
7676
'command' => $command->getName(),
7777
'identifier' => $client->getIdentifier(),
78-
'--deactivated' => true,
78+
'--active' => 1,
79+
]);
80+
$output = $commandTester->getDisplay();
81+
$this->assertStringContainsString('Given oAuth2 client updated successfully', $output);
82+
$updatedClient = $this->getClientManager()->find($client->getIdentifier());
83+
$this->assertFalse($updatedClient->isActive());
84+
}
85+
86+
public function testSetClientInactive(): void
87+
{
88+
$client = $this->fakeAClient('foobar');
89+
$this->getClientManager()->save($client);
90+
$this->assertTrue($client->isActive());
91+
92+
$command = $this->application->find('trikoder:oauth2:update-client');
93+
$commandTester = new CommandTester($command);
94+
$commandTester->execute([
95+
'command' => $command->getName(),
96+
'identifier' => $client->getIdentifier(),
97+
'--active' => 0,
7998
]);
8099
$output = $commandTester->getDisplay();
81100
$this->assertStringContainsString('Given oAuth2 client updated successfully', $output);

0 commit comments

Comments
 (0)