AdapterTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Srmklive\PayPal\Tests\Unit;
  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. use Srmklive\PayPal\Tests\MockResponsePayloads;
  8. class AdapterTest extends TestCase
  9. {
  10. use MockClientClasses;
  11. use MockResponsePayloads;
  12. #[Test]
  13. public function it_can_be_instantiated(): void
  14. {
  15. $client = new PayPalClient($this->getMockCredentials());
  16. $this->assertInstanceOf(PayPalClient::class, $client);
  17. }
  18. #[Test]
  19. public function it_throws_exception_if_invalid_credentials_are_provided(): void
  20. {
  21. $this->expectException(\RuntimeException::class);
  22. $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->expectErrorMessage('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->getMockCredentials();
  30. $credentials['mode'] = '';
  31. $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->expectErrorMessage('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->getMockCredentials();
  39. $credentials['sandbox'] = [];
  40. $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->expectErrorMessage("{$item} missing from the provided configuration. Please add your application {$item}.");
  48. $credentials = $this->getMockCredentials();
  49. $credentials['sandbox'][$item] = '';
  50. $client = new PayPalClient($credentials);
  51. }
  52. #[Test]
  53. public function it_can_get_access_token(): void
  54. {
  55. $expectedResponse = $this->mockAccessTokenResponse();
  56. $expectedMethod = 'getAccessToken';
  57. $mockClient = $this->mock_client($expectedResponse, $expectedMethod, false);
  58. $mockClient->setApiCredentials($this->getMockCredentials());
  59. $this->assertEquals($expectedResponse, $mockClient->{$expectedMethod}());
  60. }
  61. }