Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gradinarufelix committed Sep 28, 2023
1 parent b4c8279 commit d29f356
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions Tests/Unit/ZoomApiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CodeQ\ZoomApi\Tests\Unit;

use CodeQ\ZoomApi\Domain\Model\ZoomApiAccessToken;
use CodeQ\ZoomApi\Domain\Service\ZoomApiAccessTokenFactory;
use CodeQ\ZoomApi\Domain\Service\ZoomApiService;
use CodeQ\ZoomApi\ZoomApiException;
use DateTime;
Expand All @@ -19,11 +21,15 @@

class ZoomApiServiceTest extends UnitTestCase
{
protected MockObject $cacheMock;
private LoggerInterface $systemLoggerMock;
protected ZoomApiAccessTokenFactory|MockObject $accessTokenFactoryMock;
protected VariableFrontend|MockObject $cacheMock;
private LoggerInterface|MockObject $systemLoggerMock;

protected function setUp(): void
{
$this->accessTokenFactoryMock = Mockery::mock(ZoomApiAccessTokenFactory::class, [
'createFromConfiguration' => new ZoomApiAccessToken('123456789', [])
]);
$this->cacheMock = $this->getMockBuilder(VariableFrontend::class)->disableOriginalConstructor()->getMock();
$this->systemLoggerMock = Mockery::mock(LoggerInterface::class);
}
Expand All @@ -37,15 +43,14 @@ public function it_can_fetch_data()
new Response(200, [], json_encode(['meetings' => ['meeting2'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);



$this->cacheMock->expects($this->once())
->method('get')
->willReturn(false);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();


Expand All @@ -58,14 +63,12 @@ public function it_can_fetch_data()
/** @test */
public function getRecordingsWithStringDatesAndValidCacheReturnsCachedData()
{
$client = new Client();

$this->cacheMock = $this->getMockBuilder(VariableFrontend::class)->disableOriginalConstructor()->getMock();
$this->cacheMock->expects($this->once())
->method('get')
->willReturn(['cached123']);

$service = $this->getService($client);
$service = $this->getService();
$service->initializeObject();


Expand All @@ -83,9 +86,8 @@ public function getRecordingsWithStringDatesEmptyCacheReturnsFetchedData()
new Response(200, [], json_encode(['meetings' => ['meeting2'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);

Expand All @@ -105,9 +107,8 @@ public function getRecordingsWithObjectDatesEmptyCacheReturnsFetchedData()
new Response(200, [], json_encode(['meetings' => ['meeting2'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);

Expand All @@ -129,9 +130,8 @@ public function getRecordingsWithObjectDatesAndCacheExceptionReturnsFetchedData(
new Response(200, [], json_encode(['meetings' => ['meeting4'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);
$this->cacheMock->method('set')->willThrowException(new Exception());
Expand All @@ -148,9 +148,7 @@ public function getRecordingsThrowsExceptionIfFromIsBiggerThanTo()
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The from date must be after the to date');

$client = new Client();

$service = $this->getService($client);
$service = $this->getService();
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);

Expand All @@ -161,14 +159,12 @@ public function getRecordingsThrowsExceptionIfFromIsBiggerThanTo()
/** @test */
public function upcomingMeetingsReturnsValidCachedData()
{
$client = new Client();

$this->cacheMock = $this->getMockBuilder(VariableFrontend::class)->disableOriginalConstructor()->getMock();
$this->cacheMock->expects($this->once())
->method('get')
->willReturn(['cached123']);

$service = $this->getService($client);
$service = $this->getService();
$service->initializeObject();


Expand All @@ -186,9 +182,8 @@ public function upcomingMeetingsWithEmptyCacheReturnsNewData()
new Response(200, [], json_encode(['meetings' => ['meeting2'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);

Expand All @@ -208,9 +203,8 @@ public function upcomingMeetingsWithCacheExceptionReturnsNewData()
new Response(200, [], json_encode(['meetings' => ['meeting2'], 'next_page_token' => ''])),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);
$this->cacheMock->method('set')->willThrowException(new Exception());
Expand All @@ -233,9 +227,8 @@ public function getUpcomingMeetingsWithEmptyResponseThrowsException()
new Response(200, [], ''),
])
);
$client = new Client(['handler' => $handlerStack]);

$service = $this->getService($client);
$service = $this->getService($handlerStack);
$service->initializeObject();
$this->cacheMock->method('get')->willReturn(false);

Expand All @@ -244,19 +237,16 @@ public function getUpcomingMeetingsWithEmptyResponseThrowsException()
}

/**
* @param \GuzzleHttp\Client $client
* @return MockObject
* @param Client $client
*
* @return ZoomApiService|MockObject
*/
private function getService(Client $client): MockObject
private function getService(HandlerStack $handlerStack = null): ZoomApiService|MockObject
{
$service = $this->getAccessibleMock(ZoomApiService::class, ['buildClient', 'getAccessToken'], [], '', false);
$service = $this->getAccessibleMock(ZoomApiService::class, ['buildClient'], [], '', false);
$client = new Client(['handler' => $handlerStack]);
$service->method('buildClient')->willReturn($client);
$service->method('getAccessToken')->willReturn('1234567890');
$this->inject(
$service,
'settings',
['auth' => ['accountId' => '1234567890', 'clientId' => '1234567890', 'clientSecret' => '1234567890']]
);
$this->inject($service, 'accessTokenFactory', $this->accessTokenFactoryMock);
$this->inject($service, 'requestsCache', $this->cacheMock);
$this->inject($service, 'systemLogger', $this->systemLoggerMock);

Expand Down

0 comments on commit d29f356

Please sign in to comment.