From 35e95666779e3eafe5523edba5748b2755744a3f Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 11:58:49 +0200 Subject: [PATCH 1/7] Enhancement: Ensure Recipient --- tests/Client/GroupsClientTest.php | 64 +++++++++++++------------------ 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index a2cca0f..515a450 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -51,52 +51,40 @@ public function testBuildApiUrl(): void $this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); } - public function testBuildCurlPostFields(): void + /** + * @dataProvider buildCurlPostFieldsProvider + */ + public function testBuildCurlPostFields(array $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key $recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22'); // using dummy user key - // testing various "actions" below - - $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); - - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); - - $client = new GroupsClient($group, GroupsClient::ACTION_REMOVE_USER); - - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); + $client = new GroupsClient($group, $action); - $client = new GroupsClient($group, GroupsClient::ACTION_ENABLE_USER); - - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], + $this->assertSame( + $expected, $client->buildCurlPostFields($recipient), ); + } - $client = new GroupsClient($group, GroupsClient::ACTION_DISABLE_USER); - - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); + /** + * @return iterable, string}> + */ + public static function buildCurlPostFieldsProvider(): iterable + { + $base = [ + 'token' => 'cccc3333CCCC3333dddd4444DDDD44', + 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', + ]; + + foreach ([ + GroupsClient::ACTION_ADD_USER, + GroupsClient::ACTION_REMOVE_USER, + GroupsClient::ACTION_ENABLE_USER, + GroupsClient::ACTION_DISABLE_USER, + ] as $action) { + yield [$base, $action]; + } } } From 8131d4886eba5b8b03f6e3d4a9210cca3d9daf48 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 12:06:58 +0200 Subject: [PATCH 2/7] - --- tests/Client/GroupsClientTest.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index 515a450..e6f9a1d 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -34,21 +34,34 @@ public function testCanBeConstructed(): void $this->assertInstanceOf(GroupsClient::class, $client); } - public function testBuildApiUrl(): void + /** + * @dataProvider buildApiUrlProvider + */ + public function testBuildApiUrl(string $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key - // testing various "actions" below + $client = new GroupsClient($group, $action); + $this->assertSame($expected, $client->buildApiUrl()); + } - $client = new GroupsClient($group, GroupsClient::ACTION_RETRIEVE_GROUP); - $this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + public static function buildApiUrlProvider(): iterable + { + yield [ + 'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_RETRIEVE_GROUP, + ]; - $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); - $this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + yield [ + 'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_ADD_USER, + ]; - $client = new GroupsClient($group, GroupsClient::ACTION_LIST_GROUPS); - $this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + yield [ + 'https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_LIST_GROUPS, + ]; } /** From 1dbc1bd86ceea96a768004c1d990265608bae711 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 12:07:25 +0200 Subject: [PATCH 3/7] - --- tests/Client/GroupsClientTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index e6f9a1d..b501793 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -46,6 +46,9 @@ public function testBuildApiUrl(string $expected, string $action): void $this->assertSame($expected, $client->buildApiUrl()); } + /** + * @return iterable + */ public static function buildApiUrlProvider(): iterable { yield [ From 8278425a3b586a08652b0cdfb92fb45bb6374f6c Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 12:19:36 +0200 Subject: [PATCH 4/7] - --- src/Client/GroupsClient.php | 62 ++++++++++++------------------- tests/Client/GroupsClientTest.php | 31 ++++++++++++++++ 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/Client/GroupsClient.php b/src/Client/GroupsClient.php index 96ea459..287469e 100644 --- a/src/Client/GroupsClient.php +++ b/src/Client/GroupsClient.php @@ -35,32 +35,23 @@ class GroupsClient extends Client implements ClientInterface public const ACTION_LIST_GROUPS = 'list'; /** - * @param string $action Action that client performs + * @param self::ACTION_* $action Action that client performs */ public function __construct( private readonly Group $group, private readonly string $action, ) { - if (!$this->isActionValid($action)) { - throw new InvalidArgumentException('Action argument provided to construct method is invalid.'); - } + $this->validateAction($action); } public function buildApiUrl(): string { - if ($this->action === self::ACTION_CREATE_GROUP) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json'; - } - - if ($this->action === self::ACTION_LIST_GROUPS) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json?token='.$this->group->getApplication()->getToken(); - } - - if ($this->action === self::ACTION_RETRIEVE_GROUP) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'.json?token='.$this->group->getApplication()->getToken(); - } - - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'/'.$this->action.'.json?token='.$this->group->getApplication()->getToken(); + return match ($this->action) { + self::ACTION_CREATE_GROUP => sprintf('%s/%s/groups.json', Curl::API_BASE_URL, Curl::API_VERSION), + self::ACTION_LIST_GROUPS => sprintf('%s/%s/groups.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getApplication()->getToken()), + self::ACTION_RETRIEVE_GROUP => sprintf('%s/%s/groups/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->group->getApplication()->getToken()), + default => sprintf('%s/%s/groups/%s/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->action, $this->group->getApplication()->getToken()), + }; } /** @@ -72,28 +63,25 @@ public function buildCurlPostFields(?Recipient $recipient = null): array 'token' => $this->group->getApplication()->getToken(), ]; - if ( - $this->action === self::ACTION_ADD_USER - || $this->action === self::ACTION_REMOVE_USER - || $this->action === self::ACTION_DISABLE_USER - || $this->action === self::ACTION_ENABLE_USER - ) { + if (in_array($this->action, [self::ACTION_ADD_USER, self::ACTION_REMOVE_USER, self::ACTION_DISABLE_USER, self::ACTION_ENABLE_USER], true)) { + if (!$recipient instanceof Recipient) { + throw new \LogicException('Recipient object must be provided for this action.'); + } + $curlPostFields['user'] = $recipient->getUserKey(); - } - if ($this->action === self::ACTION_ADD_USER) { - if (!empty($recipient->getDevice())) { - $curlPostFields['device'] = $recipient->getDevice()[0]; - } + if ($this->action === self::ACTION_ADD_USER) { + if (!empty($recipient->getDevice())) { + $curlPostFields['device'] = $recipient->getDevice()[0]; + } - if (null !== $recipient->getMemo()) { - $curlPostFields['memo'] = $recipient->getMemo(); + if (null !== $recipient->getMemo()) { + $curlPostFields['memo'] = $recipient->getMemo(); + } } } - if ($this->action === self::ACTION_RENAME_GROUP - || $this->action === self::ACTION_CREATE_GROUP - ) { + if (in_array($this->action, [self::ACTION_RENAME_GROUP, self::ACTION_CREATE_GROUP], true)) { $curlPostFields['name'] = $this->group->getName(); } @@ -103,14 +91,12 @@ public function buildCurlPostFields(?Recipient $recipient = null): array /** * Checks if action that was provided into construct is valid. */ - private function isActionValid(string $action): bool + private function validateAction(string $action): void { $oClass = new \ReflectionClass(self::class); - if (\in_array($action, $oClass->getConstants(), true)) { - return true; + if (!\in_array($action, $oClass->getConstants(), true)) { + throw new InvalidArgumentException('Action argument provided to construct method is invalid.'); } - - return false; } } diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index b501793..678d4f9 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -68,6 +68,8 @@ public static function buildApiUrlProvider(): iterable } /** + * @param array $expected + * * @dataProvider buildCurlPostFieldsProvider */ public function testBuildCurlPostFields(array $expected, string $action): void @@ -103,4 +105,33 @@ public static function buildCurlPostFieldsProvider(): iterable yield [$base, $action]; } } + + /** + * @dataProvider actionsNeedRecipientProvider + */ + public function testBuildCurlPostFieldsThrowsExceptionWhenRecipientIsNotProvided(string $action): void + { + $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token + $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key + + $client = new GroupsClient($group, $action); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Recipient object must be provided for this action.'); + + $client->buildCurlPostFields(); + } + + /** + * @return iterable> + */ + public static function actionsNeedRecipientProvider(): iterable + { + return [ + [GroupsClient::ACTION_ADD_USER], + [GroupsClient::ACTION_REMOVE_USER], + [GroupsClient::ACTION_ENABLE_USER], + [GroupsClient::ACTION_DISABLE_USER], + ]; + } } From cd2fd447b6bfdf28663964991b2e660476b5c9ca Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 12:19:43 +0200 Subject: [PATCH 5/7] - --- src/Client/GroupsClient.php | 4 ++-- tests/Client/GroupsClientTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Client/GroupsClient.php b/src/Client/GroupsClient.php index 287469e..06d7f18 100644 --- a/src/Client/GroupsClient.php +++ b/src/Client/GroupsClient.php @@ -63,7 +63,7 @@ public function buildCurlPostFields(?Recipient $recipient = null): array 'token' => $this->group->getApplication()->getToken(), ]; - if (in_array($this->action, [self::ACTION_ADD_USER, self::ACTION_REMOVE_USER, self::ACTION_DISABLE_USER, self::ACTION_ENABLE_USER], true)) { + if (\in_array($this->action, [self::ACTION_ADD_USER, self::ACTION_REMOVE_USER, self::ACTION_DISABLE_USER, self::ACTION_ENABLE_USER], true)) { if (!$recipient instanceof Recipient) { throw new \LogicException('Recipient object must be provided for this action.'); } @@ -81,7 +81,7 @@ public function buildCurlPostFields(?Recipient $recipient = null): array } } - if (in_array($this->action, [self::ACTION_RENAME_GROUP, self::ACTION_CREATE_GROUP], true)) { + if (\in_array($this->action, [self::ACTION_RENAME_GROUP, self::ACTION_CREATE_GROUP], true)) { $curlPostFields['name'] = $this->group->getName(); } diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index 678d4f9..b256489 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -68,9 +68,9 @@ public static function buildApiUrlProvider(): iterable } /** - * @param array $expected - * * @dataProvider buildCurlPostFieldsProvider + * + * @param array $expected */ public function testBuildCurlPostFields(array $expected, string $action): void { From 98f8a19c2783fb633f77348c8cbdb793fb1acf81 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 30 Aug 2024 11:58:49 +0200 Subject: [PATCH 6/7] Enhancement: Ensure Recipient --- src/Client/GroupsClient.php | 62 ++++++--------- tests/Client/GroupsClientTest.php | 121 +++++++++++++++++++----------- 2 files changed, 102 insertions(+), 81 deletions(-) diff --git a/src/Client/GroupsClient.php b/src/Client/GroupsClient.php index 96ea459..06d7f18 100644 --- a/src/Client/GroupsClient.php +++ b/src/Client/GroupsClient.php @@ -35,32 +35,23 @@ class GroupsClient extends Client implements ClientInterface public const ACTION_LIST_GROUPS = 'list'; /** - * @param string $action Action that client performs + * @param self::ACTION_* $action Action that client performs */ public function __construct( private readonly Group $group, private readonly string $action, ) { - if (!$this->isActionValid($action)) { - throw new InvalidArgumentException('Action argument provided to construct method is invalid.'); - } + $this->validateAction($action); } public function buildApiUrl(): string { - if ($this->action === self::ACTION_CREATE_GROUP) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json'; - } - - if ($this->action === self::ACTION_LIST_GROUPS) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups.json?token='.$this->group->getApplication()->getToken(); - } - - if ($this->action === self::ACTION_RETRIEVE_GROUP) { - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'.json?token='.$this->group->getApplication()->getToken(); - } - - return Curl::API_BASE_URL.'/'.Curl::API_VERSION.'/groups/'.$this->group->getKey().'/'.$this->action.'.json?token='.$this->group->getApplication()->getToken(); + return match ($this->action) { + self::ACTION_CREATE_GROUP => sprintf('%s/%s/groups.json', Curl::API_BASE_URL, Curl::API_VERSION), + self::ACTION_LIST_GROUPS => sprintf('%s/%s/groups.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getApplication()->getToken()), + self::ACTION_RETRIEVE_GROUP => sprintf('%s/%s/groups/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->group->getApplication()->getToken()), + default => sprintf('%s/%s/groups/%s/%s.json?token=%s', Curl::API_BASE_URL, Curl::API_VERSION, $this->group->getKey(), $this->action, $this->group->getApplication()->getToken()), + }; } /** @@ -72,28 +63,25 @@ public function buildCurlPostFields(?Recipient $recipient = null): array 'token' => $this->group->getApplication()->getToken(), ]; - if ( - $this->action === self::ACTION_ADD_USER - || $this->action === self::ACTION_REMOVE_USER - || $this->action === self::ACTION_DISABLE_USER - || $this->action === self::ACTION_ENABLE_USER - ) { + if (\in_array($this->action, [self::ACTION_ADD_USER, self::ACTION_REMOVE_USER, self::ACTION_DISABLE_USER, self::ACTION_ENABLE_USER], true)) { + if (!$recipient instanceof Recipient) { + throw new \LogicException('Recipient object must be provided for this action.'); + } + $curlPostFields['user'] = $recipient->getUserKey(); - } - if ($this->action === self::ACTION_ADD_USER) { - if (!empty($recipient->getDevice())) { - $curlPostFields['device'] = $recipient->getDevice()[0]; - } + if ($this->action === self::ACTION_ADD_USER) { + if (!empty($recipient->getDevice())) { + $curlPostFields['device'] = $recipient->getDevice()[0]; + } - if (null !== $recipient->getMemo()) { - $curlPostFields['memo'] = $recipient->getMemo(); + if (null !== $recipient->getMemo()) { + $curlPostFields['memo'] = $recipient->getMemo(); + } } } - if ($this->action === self::ACTION_RENAME_GROUP - || $this->action === self::ACTION_CREATE_GROUP - ) { + if (\in_array($this->action, [self::ACTION_RENAME_GROUP, self::ACTION_CREATE_GROUP], true)) { $curlPostFields['name'] = $this->group->getName(); } @@ -103,14 +91,12 @@ public function buildCurlPostFields(?Recipient $recipient = null): array /** * Checks if action that was provided into construct is valid. */ - private function isActionValid(string $action): bool + private function validateAction(string $action): void { $oClass = new \ReflectionClass(self::class); - if (\in_array($action, $oClass->getConstants(), true)) { - return true; + if (!\in_array($action, $oClass->getConstants(), true)) { + throw new InvalidArgumentException('Action argument provided to construct method is invalid.'); } - - return false; } } diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index a2cca0f..b256489 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -34,69 +34,104 @@ public function testCanBeConstructed(): void $this->assertInstanceOf(GroupsClient::class, $client); } - public function testBuildApiUrl(): void + /** + * @dataProvider buildApiUrlProvider + */ + public function testBuildApiUrl(string $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key - // testing various "actions" below - - $client = new GroupsClient($group, GroupsClient::ACTION_RETRIEVE_GROUP); - $this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); - - $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); - $this->assertEquals('https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + $client = new GroupsClient($group, $action); + $this->assertSame($expected, $client->buildApiUrl()); + } - $client = new GroupsClient($group, GroupsClient::ACTION_LIST_GROUPS); - $this->assertSame('https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', $client->buildApiUrl()); + /** + * @return iterable + */ + public static function buildApiUrlProvider(): iterable + { + yield [ + 'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_RETRIEVE_GROUP, + ]; + + yield [ + 'https://api.pushover.net/1/groups/eeee5555EEEE5555ffff6666FFFF66/add_user.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_ADD_USER, + ]; + + yield [ + 'https://api.pushover.net/1/groups.json?token=cccc3333CCCC3333dddd4444DDDD44', + GroupsClient::ACTION_LIST_GROUPS, + ]; } - public function testBuildCurlPostFields(): void + /** + * @dataProvider buildCurlPostFieldsProvider + * + * @param array $expected + */ + public function testBuildCurlPostFields(array $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key $recipient = new Recipient('aaaa1111AAAA1111bbbb2222BBBB22'); // using dummy user key - // testing various "actions" below + $client = new GroupsClient($group, $action); - $client = new GroupsClient($group, GroupsClient::ACTION_ADD_USER); - - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], + $this->assertSame( + $expected, $client->buildCurlPostFields($recipient), ); + } - $client = new GroupsClient($group, GroupsClient::ACTION_REMOVE_USER); + /** + * @return iterable, string}> + */ + public static function buildCurlPostFieldsProvider(): iterable + { + $base = [ + 'token' => 'cccc3333CCCC3333dddd4444DDDD44', + 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', + ]; + + foreach ([ + GroupsClient::ACTION_ADD_USER, + GroupsClient::ACTION_REMOVE_USER, + GroupsClient::ACTION_ENABLE_USER, + GroupsClient::ACTION_DISABLE_USER, + ] as $action) { + yield [$base, $action]; + } + } - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); + /** + * @dataProvider actionsNeedRecipientProvider + */ + public function testBuildCurlPostFieldsThrowsExceptionWhenRecipientIsNotProvided(string $action): void + { + $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token + $group = new Group('eeee5555EEEE5555ffff6666FFFF66', $application); // using dummy group key - $client = new GroupsClient($group, GroupsClient::ACTION_ENABLE_USER); + $client = new GroupsClient($group, $action); - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Recipient object must be provided for this action.'); - $client = new GroupsClient($group, GroupsClient::ACTION_DISABLE_USER); + $client->buildCurlPostFields(); + } - $this->assertEquals( - [ - 'token' => 'cccc3333CCCC3333dddd4444DDDD44', - 'user' => 'aaaa1111AAAA1111bbbb2222BBBB22', - ], - $client->buildCurlPostFields($recipient), - ); + /** + * @return iterable> + */ + public static function actionsNeedRecipientProvider(): iterable + { + return [ + [GroupsClient::ACTION_ADD_USER], + [GroupsClient::ACTION_REMOVE_USER], + [GroupsClient::ACTION_ENABLE_USER], + [GroupsClient::ACTION_DISABLE_USER], + ]; } } From 0594e7b388a1cdf0d4391d32fb06ad6ebecb5e7c Mon Sep 17 00:00:00 2001 From: Serhiy Lunak Date: Mon, 2 Sep 2024 22:07:21 +0100 Subject: [PATCH 7/7] Run rector --- tests/Client/GroupsClientTest.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/Client/GroupsClientTest.php b/tests/Client/GroupsClientTest.php index b256489..4084de0 100644 --- a/tests/Client/GroupsClientTest.php +++ b/tests/Client/GroupsClientTest.php @@ -13,6 +13,7 @@ namespace Client; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Serhiy\Pushover\Api\Groups\Group; use Serhiy\Pushover\Application; @@ -34,9 +35,7 @@ public function testCanBeConstructed(): void $this->assertInstanceOf(GroupsClient::class, $client); } - /** - * @dataProvider buildApiUrlProvider - */ + #[DataProvider('buildApiUrlProvider')] public function testBuildApiUrl(string $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token @@ -68,10 +67,9 @@ public static function buildApiUrlProvider(): iterable } /** - * @dataProvider buildCurlPostFieldsProvider - * * @param array $expected */ + #[DataProvider('buildCurlPostFieldsProvider')] public function testBuildCurlPostFields(array $expected, string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token @@ -106,9 +104,7 @@ public static function buildCurlPostFieldsProvider(): iterable } } - /** - * @dataProvider actionsNeedRecipientProvider - */ + #[DataProvider('actionsNeedRecipientProvider')] public function testBuildCurlPostFieldsThrowsExceptionWhenRecipientIsNotProvided(string $action): void { $application = new Application('cccc3333CCCC3333dddd4444DDDD44'); // using dummy token