From c488e9fde03491abe4a1bb842eb4f3ac3a5c25b3 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sun, 29 Dec 2024 23:32:29 +0100 Subject: [PATCH] add subscription process tests --- src/Resources/SubscriptionProcess.php | 13 +- .../Resources/SubscriptionProcessTest.php | 147 ++++++++++++++++++ 2 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 tests/integration/Resources/SubscriptionProcessTest.php diff --git a/src/Resources/SubscriptionProcess.php b/src/Resources/SubscriptionProcess.php index 5c67e9b..e63d567 100644 --- a/src/Resources/SubscriptionProcess.php +++ b/src/Resources/SubscriptionProcess.php @@ -18,46 +18,45 @@ namespace D3\KlicktippPhpClient\Resources; use D3\KlicktippPhpClient\Entities\Subscription as SubscriptionEntity; use D3\KlicktippPhpClient\Entities\SubscriptionList; use D3\KlicktippPhpClient\Exceptions\BaseException; -use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\RequestOptions; class SubscriptionProcess extends Model { /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function index(): SubscriptionList { $data = $this->connection->requestAndParse( 'GET', - 'list' + 'list.json' ); return new SubscriptionList($data); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function get(string $listId): SubscriptionEntity { $data = $this->connection->requestAndParse( 'GET', - 'list/'.urlencode(trim($listId)) + 'list/'.urlencode(trim($listId)).'.json' ); return new SubscriptionEntity($data); } /** - * @throws BaseException|GuzzleException + * @throws BaseException */ public function redirect(string $listId, string $email): string { return current( $this->connection->requestAndParse( 'POST', - 'list/redirect', + 'list/redirect.json', [ RequestOptions::FORM_PARAMS => [ 'listid' => trim($listId), diff --git a/tests/integration/Resources/SubscriptionProcessTest.php b/tests/integration/Resources/SubscriptionProcessTest.php new file mode 100644 index 0000000..b75d4c8 --- /dev/null +++ b/tests/integration/Resources/SubscriptionProcessTest.php @@ -0,0 +1,147 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\KlicktippPhpClient\tests\integration\Resources; + +use D3\KlicktippPhpClient\Entities\Subscription; +use D3\KlicktippPhpClient\Entities\SubscriptionList; +use D3\KlicktippPhpClient\Exceptions\BaseException; +use D3\KlicktippPhpClient\Resources\SubscriptionProcess; +use D3\KlicktippPhpClient\Resources\Tag; +use D3\KlicktippPhpClient\tests\integration\IntegrationTestCase; +use Generator; +use GuzzleHttp\Psr7\Response; +use Psr\Http\Message\ResponseInterface; +use ReflectionException; + +/** + * @coversNothing + */ +class SubscriptionProcessTest extends IntegrationTestCase +{ + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\SubscriptionProcess::index + * @dataProvider indexDataProvider + */ + public function testIndex(ResponseInterface $response, ?SubscriptionList $expected, bool $expectException = false) + { + $sut = new SubscriptionProcess($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'index', + ) + ); + } + + public static function indexDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '{ + "470370": "name 1", + "470371": "name 2" + }'), new SubscriptionList([ + "470370" => "name 1", + "470371" => "name 2", + ])]; + yield 'wrong request type' => [new Response(409, [], '["A list with the name already exists."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\SubscriptionProcess::get + * @dataProvider getDataProvider + */ + public function testGet(ResponseInterface $response, ?Subscription $expected, bool $expectException = false) + { + $sut = new SubscriptionProcess($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'get', + ['470370'] + ) + ); + } + + public static function getDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '{ + "listid": "470370", + "name": "name 1", + "pendingurl": "", + "thankyouurl": "", + "usesingleoptin": false, + "resendconfirmationemail": false, + "usechangeemail": false + }'), new Subscription([ + "listid" => "470370", + "name" => "name 1", + "pendingurl" => "", + "thankyouurl" => "", + "usesingleoptin" => false, + "resendconfirmationemail" => false, + "usechangeemail" => false, + ])]; + yield 'unknown id' => [new Response(404, [], '["Kein Opt-In-Prozess mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\KlicktippPhpClient\Resources\SubscriptionProcess::redirect + * @dataProvider redirectDataProvider + */ + public function testRedirect(ResponseInterface $response, ?string $expected, bool $expectException = false) + { + $sut = new SubscriptionProcess($this->getConnectionMock($response)); + + if ($expectException) { + $this->expectException(BaseException::class); + } + + $this->assertEquals( + $expected, + $this->callMethod( + $sut, + 'redirect', + ['470370', 'mymail@mydomain.com'] + ) + ); + } + + public static function redirectDataProvider(): Generator + { + yield 'success' => [new Response(200, [], '["https:\/\/klick.marketron.io\/thankyou\/p9i3z8e6ab4xx5uvzzpzd768"]'), 'https://klick.marketron.io/thankyou/p9i3z8e6ab4xx5uvzzpzd768']; + yield 'missing or empty list id' => [new Response(404, [], '["Kein Opt-In-Prozess mit dieser ID."]'), null, true]; + yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; + } +}