Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #8

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ node_modules
*.DS_Store
/.idea
.remote-sync.json
/bin
/Packages
composer.lock
.phpunit.result.cache
Tests/Reports
34 changes: 23 additions & 11 deletions Classes/Domain/Service/ZoomApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ public function initializeObject(): void
$zoomApiClientId = $this->settings['auth']['clientId'];
$zoomApiClientSecret = $this->settings['auth']['clientSecret'];
if (!$zoomApiAccountId || !$zoomApiClientId || !$zoomApiClientSecret) {
throw new ZoomApiException('Please set a Zoom Account ID, Client ID and Secret for CodeQ.ZoomApi to be able to authenticate.');
throw new ZoomApiException('Please set a Zoom Account ID, Client ID and Secret for CodeQ.ZoomApi to be able to authenticate.', 1695830249149);
}

$accessToken = $this->getAccessToken($zoomApiAccountId, $zoomApiClientId, $zoomApiClientSecret);

$this->client = (new Client([
$this->client = $this->buildClient($accessToken);
}

protected function buildClient(string $accessToken): Client
{
return (new Client([
'base_uri' => 'https://api.zoom.us/v2/',
'headers' => [
'Authorization' => "Bearer $accessToken",
Expand Down Expand Up @@ -110,29 +115,30 @@ public function getUpcomingMeetings(bool $skipCache = false): array
*/
public function getRecordings(DateTime|string $from, DateTime|string $to, bool $skipCache = false): array
{
$cacheEntryIdentifier = sprintf('recordings_%s_%s', is_string($from) ? $from : $from->format('Y-m-d'), is_string($to) ? $to : $to->format('Y-m-d'));

$recordings = $this->getCacheEntry($cacheEntryIdentifier);
if (!$skipCache && $recordings !== false) {
return $recordings;
}

if (is_string($from)) {
$from = new DateTimeImmutable($from);
} else {
$from = DateTimeImmutable::createFromMutable($from);
}
assert($from instanceof DateTimeImmutable);
gradinarufelix marked this conversation as resolved.
Show resolved Hide resolved

if (is_string($to)) {
$to = new DateTimeImmutable($to);
} else {
$to = DateTimeImmutable::createFromMutable($to);
}
assert($to instanceof DateTimeImmutable);
gradinarufelix marked this conversation as resolved.
Show resolved Hide resolved

if ($from > $to) {
throw new InvalidArgumentException('The from date must be after the to date');
}

$cacheEntryIdentifier = sprintf('recordings_%s_%s', $from->format('Y-m-d'), $to->format('Y-m-d'));
$recordings = $this->getCacheEntry($cacheEntryIdentifier);
gradinarufelix marked this conversation as resolved.
Show resolved Hide resolved
if (!$skipCache && $recordings !== false) {
return $recordings;
}

$recordings = $this->fetchDataForDateRange($from, $to);

try {
Expand Down Expand Up @@ -236,7 +242,13 @@ private function fetchPaginatedData(string $uri, string $paginatedDataKey): arra
throw new ZoomApiException(sprintf('Could not fetch Zoom paginated data for data key "%s", returned status "%s"', $paginatedDataKey, $response->getStatusCode()), 1695239983421);
Copy link
Contributor

@krsriq krsriq Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is still suspiciously not covered by tests - and has a bug (guess you can find it on your own ;) ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sollte jetzt gefixt sein, was meinst du? ;-)

Ich habe jetzt auch noch PHPStan hinzugefügt und Codesniffer

}
$bodyContents = $response->getBody()->getContents();
return json_decode($bodyContents, true);
$bodyContentsArray = json_decode($bodyContents, true);

if ($bodyContentsArray === null || !array_key_exists($paginatedDataKey, $bodyContentsArray)) {
throw new ZoomApiException(sprintf('Could not fetch Zoom paginated data for data key "%s", returned empty response', $paginatedDataKey), 1695828849253);
}

return $bodyContentsArray;
}

private function dateDifferenceIsBiggerThanOneMonth(DateTimeImmutable $from, DateTimeImmutable $to): bool
Expand All @@ -254,7 +266,7 @@ private function dateDifferenceIsBiggerThanOneMonth(DateTimeImmutable $from, Dat
* @return string|null
* @throws GuzzleException|ZoomApiException
*/
private function getAccessToken(string $accountId, string $zoomApiClientId, string $zoomApiClientSecret): ?string
protected function getAccessToken(string $accountId, string $zoomApiClientId, string $zoomApiClientSecret): ?string
{
$client = new Client([
'base_uri' => 'https://zoom.us/',
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ CodeQ_ZoomApi_Requests:
```

Of course, you can also switch to a different cache backend at your convenience.


## Testing

To run the unit tests, execute the following commands:

1. `composer install`
2. `composer test`
107 changes: 107 additions & 0 deletions Tests/Unit/ZoomApiHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace CodeQ\ZoomApi\Tests\Unit;

use CodeQ\ZoomApi\Domain\Service\ZoomApiService;
use CodeQ\ZoomApi\Eel\ZoomApiHelper;
use Mockery;
use Neos\Flow\Tests\UnitTestCase;
use Psr\Log\LoggerInterface;

class ZoomApiHelperTest extends UnitTestCase
{
/**
* @var ZoomApiHelper
*/
protected $zoomApiHelper;
private ZoomApiService $zoomApiServiceMock;
private LoggerInterface $systemLoggerMock;

protected function setUp(): void
{

$this->zoomApiHelper = new ZoomApiHelper();

$this->systemLoggerMock = Mockery::mock(LoggerInterface::class);
$this->inject(
$this->zoomApiHelper,
'systemLogger',
$this->systemLoggerMock
);

$this->zoomApiServiceMock = $this->createMock(ZoomApiService::class);
$this->inject(
$this->zoomApiHelper,
'zoomApiService',
$this->zoomApiServiceMock
);
}

/** @test * */
public function canGetRecordings(): void
{
$this->zoomApiServiceMock
->expects($this->once())
->method('getRecordings')
->with('2021-01-01', '2021-01-02')
->willReturn([]);


$result = $this->zoomApiHelper->getRecordings('2021-01-01', '2021-01-02');


$this->assertSame([], $result);
}

/** @test * */
public function canHandleGetRecordingsException(): void
{
$this->zoomApiServiceMock
->expects($this->once())
->method('getRecordings')
->with('2021-01-01', '2021-01-02')
->willThrowException(new \Exception('Test Exception'));

$this->systemLoggerMock->shouldReceive('error')->once();


$result = $this->zoomApiHelper->getRecordings('2021-01-01', '2021-01-02');


$this->assertFalse($result);
}

/** @test * */
public function canGetUpcomingMeetings(): void
{
$this->zoomApiServiceMock
->expects($this->once())
->method('getUpcomingMeetings')
->with()
->willReturn([]);


$result = $this->zoomApiHelper->getUpcomingMeetings();


$this->assertSame([], $result);
}

/** @test * */
public function canHandleGetUpcomingMeetingsException(): void
{
$this->zoomApiServiceMock
->expects($this->once())
->method('getUpcomingMeetings')
->with()
->willThrowException(new \Exception('Test Exception'));

$this->systemLoggerMock->shouldReceive('error')->once();


$result = $this->zoomApiHelper->getUpcomingMeetings();


$this->assertFalse($result);
}
}
Loading