Skip to content

Commit

Permalink
Merge pull request #116 from IonBazan/feature/session-id-check
Browse files Browse the repository at this point in the history
Add session ID check to each API call
  • Loading branch information
johnzuk authored Nov 3, 2022
2 parents be9fee6 + f5db89a commit 2c1ba02
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
php: ['8.0', '8.1', '8.2']
php: ['8.0', '8.1', '8.2', '8.3']

steps:
- name: Checkout Code
Expand Down
20 changes: 20 additions & 0 deletions src/GusApi/GusApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static function createWithApiClient(string $userKey, GusApiClient $apiCli

public function getSessionId(): string
{
$this->ensureSession();

return $this->sessionId;
}

Expand All @@ -75,6 +77,10 @@ public function logout(): bool

public function isLogged(): bool
{
if (!isset($this->sessionId)) {
return false;
}

return (bool) $this->getSessionStatus();
}

Expand All @@ -83,6 +89,7 @@ public function isLogged(): bool
*/
public function dataStatus(): DateTimeImmutable
{
$this->ensureSession();
$result = $this->apiClient->getValue(
new GetValue(ParamName::STATUS_DATE_STATE),
$this->sessionId
Expand Down Expand Up @@ -244,6 +251,7 @@ public function getByregons14(array $regons): array
*/
public function getFullReport(SearchReport $searchReport, string $reportName): array
{
$this->ensureSession();
$result = $this->apiClient->getFullReport(
new GetFullReport(
ReportRegonNumberMapper::getRegonNumberByReportName($searchReport, $reportName),
Expand All @@ -260,6 +268,7 @@ public function getFullReport(SearchReport $searchReport, string $reportName): a
*/
public function getBulkReport(DateTimeImmutable $date, string $reportName): array
{
$this->ensureSession();
if (!\in_array($reportName, BulkReportTypes::REPORTS, true)) {
throw new InvalidReportTypeException(sprintf('Invalid report type: "%s", use one of allowed type: (%s)', $reportName, implode(', ', BulkReportTypes::REPORTS)));
}
Expand All @@ -275,6 +284,7 @@ public function getBulkReport(DateTimeImmutable $date, string $reportName): arra
*/
public function getMessageCode(): int
{
$this->ensureSession();
$result = $this->apiClient->getValue(
new GetValue(ParamName::MESSAGE_CODE),
$this->sessionId
Expand All @@ -288,13 +298,15 @@ public function getMessageCode(): int
*/
public function getMessage(): string
{
$this->ensureSession();
$result = $this->apiClient->getValue(new GetValue(ParamName::MESSAGE), $this->sessionId);

return $result->getGetValueResult();
}

public function getSessionStatus(): int
{
$this->ensureSession();
$response = $this->apiClient->getValue(
new GetValue(ParamName::SESSION_STATUS),
$this->sessionId
Expand All @@ -317,8 +329,16 @@ private function checkIdentifiersCount(array $identifiers): void
*/
private function search(SearchData $searchData): array
{
$this->ensureSession();
$result = $this->apiClient->searchData($searchData, $this->sessionId);

return array_map(static fn (SearchResponseCompanyData $company): SearchReport => new SearchReport($company), $result->getDaneSzukajResult());
}

private function ensureSession(): void
{
if (!isset($this->sessionId)) {
throw new \BadMethodCallException('Session is not started. Call login() first.');
}
}
}
23 changes: 22 additions & 1 deletion tests/GusApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GusApi\Tests;

use BadMethodCallException;
use DateTimeImmutable;
use DateTimeZone;
use GusApi\Client\GusApiClient;
Expand Down Expand Up @@ -50,7 +51,14 @@ public function testLoginWillSetSessionId(): void
$this->loginApiWithSessionId('12sessionid21');
$this->api->login();

self::assertSame($this->api->getSessionId(), '12sessionid21');
self::assertSame('12sessionid21', $this->api->getSessionId());
}

public function testGetSessionIdFailsWhenNotLoggedIn(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Session is not started. Call login() first.');
$this->api->getSessionId();
}

public function testLoginWillThrowExceptionWhenResponseIsEmpty(): void
Expand Down Expand Up @@ -83,6 +91,11 @@ public function testIsLogged(): void
self::assertTrue($this->api->isLogged());
}

public function testIsLoggedReturnsFalseWhenSessionIdIsEmpty(): void
{
self::assertFalse($this->api->isLogged());
}

public function testGetDataStatus(): void
{
$this->expectGetValueCall('StanDanych', '31-12-2014');
Expand Down Expand Up @@ -247,6 +260,7 @@ public function testGetFullReport(): void

public function testGetBulkReportWillThrowExceptionWhenInvalidReportTypeProvided(): void
{
$this->loginApiWithSessionId('12sessionid21');
$this->expectException(InvalidReportTypeException::class);
$this->api->getBulkReport(new DateTimeImmutable(), 'asdf');
}
Expand Down Expand Up @@ -276,6 +290,13 @@ public function testGetMessage(): void
self::assertSame('Invalid Test', $this->api->getMessage());
}

public function testItThrowsBadMethodCallExceptionWhenLoginWasNotCalled(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Session is not started. Call login() first.');
$this->api->getMessageCode();
}

public function testGetSessionStatus(): void
{
$this->expectGetValueCall('StatusSesji', '1');
Expand Down

0 comments on commit 2c1ba02

Please sign in to comment.