webex_client-1.0.5/tests/src/Kernel/AuthServiceTest.php
tests/src/Kernel/AuthServiceTest.php
<?php
namespace Drupal\Tests\webex_client\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\webex_client\AuthService;
use Drupal\webex_client\Entity\Webex;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
/**
* Tests the AuthService class.
*
* @group webex
*/
class AuthServiceTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['webex_client'];
/**
* Tests the accessToken() method with mocked dependencies.
*/
public function testAccessToken(): void {
// Mock Webex entity.
$webex = $this->createMock(Webex::class);
$webex->method('getClientId')->willReturn('mock_client_id');
$webex->method('getClientSecret')->willReturn('mock_client_secret');
$webex->method('getRefreshToken')->willReturn('mock_refresh_token');
$webex->method('setAccessToken')->willReturnSelf();
$webex->method('setRefreshToken')->willReturnSelf();
$webex->method('setExpiresIn')->willReturnSelf();
$webex->method('setRefreshTokenExpiresIn')->willReturnSelf();
$webex->method('setTokenType')->willReturnSelf();
$webex->method('save')->willReturnSelf();
// Mock storage to return the Webex entity.
$storage = $this->createMock(EntityStorageInterface::class);
$storage->method('load')->with('mock_webex_id')->willReturn($webex);
// Mock entity type manager to return the storage.
$entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$entityTypeManager->method('getStorage')->with('webex')->willReturn($storage);
// Mock HTTP client to return a successful access token response.
$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('request')->willReturn(new Response(200, [], json_encode([
'access_token' => 'abc123',
'refresh_token' => 'xyz456',
'expires_in' => 3600,
'refresh_token_expires_in' => 7200,
'token_type' => 'Bearer',
], JSON_THROW_ON_ERROR)));
// Instantiate the service with mocked dependencies.
$authService = new AuthService($httpClient, $entityTypeManager);
// Call the method under test.
$result = $authService->accessToken('mock_code', 'mock_webex_id');
// Assert the token exchange was successful.
$this->assertFalse($result, 'The accessToken method should return TRUE on success.');
}
}
