AdapterConfigTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Srmklive\PayPal\Tests\Feature;
  3. use PHPUnit\Framework\Attributes\Test;
  4. use PHPUnit\Framework\TestCase;
  5. use Srmklive\PayPal\Services\PayPal as PayPalClient;
  6. use Srmklive\PayPal\Tests\MockClientClasses;
  7. class AdapterConfigTest extends TestCase
  8. {
  9. use MockClientClasses;
  10. /** @var PayPalClient */
  11. protected PayPalClient $client;
  12. protected function setUp(): void
  13. {
  14. $this->client = new PayPalClient($this->getApiCredentials());
  15. parent::setUp();
  16. }
  17. #[Test]
  18. public function it_throws_exception_if_invalid_credentials_are_provided(): void
  19. {
  20. $this->expectException(\RuntimeException::class);
  21. $this->expectExceptionMessage('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');
  22. $this->client = new PayPalClient([]);
  23. }
  24. #[Test]
  25. public function it_throws_exception_if_invalid_mode_is_provided(): void
  26. {
  27. $this->expectException(\RuntimeException::class);
  28. $this->expectExceptionMessage('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');
  29. $credentials = $this->getApiCredentials();
  30. $credentials['mode'] = '';
  31. $this->client = new PayPalClient($credentials);
  32. }
  33. #[Test]
  34. public function it_throws_exception_if_empty_credentials_are_provided(): void
  35. {
  36. $this->expectException(\RuntimeException::class);
  37. $this->expectExceptionMessage('Invalid configuration provided. Please provide valid configuration for PayPal API. You can also refer to the documentation at https://srmklive.github.io/laravel-paypal/docs.html to setup correct configuration.');
  38. $credentials = $this->getApiCredentials();
  39. $credentials['sandbox'] = [];
  40. $this->client = new PayPalClient($credentials);
  41. }
  42. #[Test]
  43. public function it_throws_exception_if_credentials_items_are_not_provided(): void
  44. {
  45. $item = 'client_id';
  46. $this->expectException(\RuntimeException::class);
  47. $this->expectExceptionMessage("{$item} missing from the provided configuration. Please add your application {$item}.");
  48. $credentials = $this->getApiCredentials();
  49. $credentials['sandbox'][$item] = '';
  50. $client = new PayPalClient($credentials);
  51. }
  52. #[Test]
  53. public function it_can_take_valid_credentials_and_return_the_client_instance(): void
  54. {
  55. $this->assertInstanceOf(PayPalClient::class, $this->client);
  56. }
  57. #[Test]
  58. public function it_throws_exception_if_invalid_credentials_are_provided_through_method(): void
  59. {
  60. $this->expectException(\RuntimeException::class);
  61. $this->client->setApiCredentials([]);
  62. }
  63. #[Test]
  64. public function it_returns_the_client_instance_if_valid_credentials_are_provided_through_method(): void
  65. {
  66. $this->client->setApiCredentials($this->getApiCredentials());
  67. $this->assertInstanceOf(PayPalClient::class, $this->client);
  68. }
  69. #[Test]
  70. public function it_throws_exception_if_invalid_currency_is_set(): void
  71. {
  72. $this->expectException(\RuntimeException::class);
  73. $this->client->setCurrency('PKR');
  74. $this->assertNotEquals('PKR', $this->client->getCurrency());
  75. }
  76. #[Test]
  77. public function it_can_set_a_valid_currency(): void
  78. {
  79. $this->client->setCurrency('EUR');
  80. $this->assertNotEmpty($this->client->getCurrency());
  81. $this->assertEquals('EUR', $this->client->getCurrency());
  82. }
  83. #[Test]
  84. public function it_can_set_a_request_header(): void
  85. {
  86. $this->client->setRequestHeader('Prefer', 'return=representation');
  87. $this->assertNotEmpty($this->client->getRequestHeader('Prefer'));
  88. $this->assertEquals($this->client->getRequestHeader('Prefer'), 'return=representation');
  89. }
  90. #[Test]
  91. public function it_can_set_multiple_request_headers(): void
  92. {
  93. $this->client->setRequestHeaders([
  94. 'PayPal-Request-Id' => 'some-request-id',
  95. 'PayPal-Partner-Attribution-Id' => 'some-attribution-id',
  96. ]);
  97. $this->assertNotEmpty($this->client->getRequestHeader('PayPal-Request-Id'));
  98. $this->assertEquals($this->client->getRequestHeader('PayPal-Partner-Attribution-Id'), 'some-attribution-id');
  99. }
  100. #[Test]
  101. public function it_throws_exception_if_options_header_not_set(): void
  102. {
  103. $this->expectException(\RuntimeException::class);
  104. $this->expectExceptionCode('0');
  105. $this->expectExceptionMessage('Options header is not set.');
  106. $this->client->getRequestHeader('Prefer');
  107. }
  108. }