AdapterExperienceContextTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Feature;
  3. namespace Srmklive\PayPal\Tests\Feature;
  4. use Carbon\Carbon;
  5. use PHPUnit\Framework\Attributes\Test;
  6. use PHPUnit\Framework\TestCase;
  7. use Srmklive\PayPal\Services\PayPal as PayPalClient;
  8. use Srmklive\PayPal\Tests\MockClientClasses;
  9. use Srmklive\PayPal\Tests\MockRequestPayloads;
  10. use Srmklive\PayPal\Tests\MockResponsePayloads;
  11. class AdapterExperienceContextTest extends TestCase
  12. {
  13. use MockClientClasses;
  14. use MockRequestPayloads;
  15. use MockResponsePayloads;
  16. /** @var string */
  17. protected static string $access_token = '';
  18. /** @var PayPalClient */
  19. protected PayPalClient $client;
  20. protected function setUp(): void
  21. {
  22. $this->client = new PayPalClient($this->getApiCredentials());
  23. $this->client->setClient(
  24. $this->mock_http_client(
  25. $this->mockAccessTokenResponse()
  26. )
  27. );
  28. $response = $this->client->getAccessToken();
  29. self::$access_token = $response['access_token'];
  30. parent::setUp();
  31. }
  32. #[Test]
  33. public function it_can_set_payment_experience_context_before_performing_api_call(): void
  34. {
  35. $this->client->setAccessToken([
  36. 'access_token' => self::$access_token,
  37. 'token_type' => 'Bearer',
  38. ]);
  39. $start_date = Carbon::now()->addDay()->toDateString();
  40. $this->client = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  41. ->setBrandName('Test Brand')
  42. ->addProductById('PROD-XYAB12ABSB7868434')
  43. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
  44. $this->client->setClient(
  45. $this->mock_http_client(
  46. $this->mockCreateSubscriptionResponse()
  47. )
  48. );
  49. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  50. $this->assertNotEmpty($response);
  51. $this->assertArrayHasKey('id', $response);
  52. $this->assertArrayHasKey('plan_id', $response);
  53. }
  54. }