Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,51 @@ Podporované verze PHP: 8.2, 8.3, 8.4, 8.5.

## Implementace
```
$client = new Client(
$accountUrl,
$companyCode,
$restApiUserName,
$restApiPassword,
$evidenceName,
$disableSelfSignedCertificate,
$authSessionId,
);
use EcomailFlexibee\Client;
use EcomailFlexibee\Config;

$client = new Client(new Config(
$accountUrl,
$companyCode,
$restApiUserName,
$restApiPassword,
$evidenceName,
$verifySSLCertificate,
$authSessionId,
$debugMode,
));
```
`$disableSelfSignedCertificate - Vypnutí self signed certifikátu`
`$verifySSLCertificate` - ověřování SSL certifikátu serveru (`false` vypne ověření, např. u self-signed certifikátu)

`$authSessionId` - hodnota autentizačního id pro Flexibee (volitelné)

`$debugMode` - při chybě 403 vrátí odeslané hlavičky požadavku (volitelné)

`$authSessionId - Hodnota authentikačního id pro Flexibee`
### Přechod z verze 1.x
Konstruktor `Client` nově přijímá `Config` (a volitelně `HttpClient`) místo jednotlivých hodnot:
```
// 1.x
$client = new Client($url, $company, $user, $password, $evidence, false, null);
// 2.0
$client = new Client(new Config($url, $company, $user, $password, $evidence, false, null));
```

## Vygenerování autorizačního tokenu
```
$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null);
$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null));
$tokens = $client->getAuthAndRefreshToken();
```

## Informace o firmách či firmě
```
$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null);
$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null));
$companies = $client->getCompanies();
$company = $client->getCompany();
```

## Vytvoření či editace záznamu
```
$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null);
$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null));
$evidenceData['kod'] = 'prvnizaznam'
$evidenceData['nazev'] = 'První kontaktní adresa'
$evidenceItemId = $client->save($evidenceData, null, $dryRun, $uriParameters);
Expand Down
15 changes: 1 addition & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,10 @@ class Client

protected HttpClient $httpClient;

protected Config $config;

private readonly ResponseHydrator $responseHydrator;

public function __construct(
string $url,
string $company,
string $user,
string $password,
string $evidence,
bool $verifySSLCertificate,
?string $authSessionId = null,
bool $debugMode = false,
?HttpClient $httpClient = null,
)
public function __construct(protected Config $config, ?HttpClient $httpClient = null)
{
$this->config = new Config($url, $company, $user, $password, $evidence, $verifySSLCertificate, $authSessionId, $debugMode);
$this->queryBuilder = new UrlBuilder($this->config);
$this->responseHydrator = new ResponseHydrator($this->config);
$this->httpClient = $httpClient ?? new HttpClient();
Expand Down
12 changes: 1 addition & 11 deletions src/CompanyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,7 @@ class CompanyClient extends Client

public function __construct(Config $config, ?HttpClient $httpClient = null)
{
parent::__construct(
$config->getUrl(),
$config->getCompany(),
$config->getUser(),
$config->getPassword(),
self::EVIDENCE,
$config->verifySSLCertificate(),
$config->getAuthSessionId(),
$config->debugMode(),
$httpClient,
);
parent::__construct($config->withEvidence(self::EVIDENCE), $httpClient);
}

public function backup(): Response
Expand Down
14 changes: 14 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ public function debugMode(): bool
return $this->debugMode;
}

public function withEvidence(string $evidence): self
{
return new self(
$this->url,
$this->company,
$this->user,
$this->password,
$evidence,
$this->verifySSLCertificate,
$this->authSessionId,
$this->debugMode,
);
}

}
2 changes: 1 addition & 1 deletion tests/ClientOfflineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testCountInEvidenceReturnsTheRowCount(): void

private function createClient(HttpClient $httpClient): Client
{
return new Client('https://demo.flexibee.eu', 'demo', 'user', 'pass', 'adresar', verifySSLCertificate: true, httpClient: $httpClient);
return new Client(new ClientConfig('https://demo.flexibee.eu', 'demo', 'user', 'pass', 'adresar', verifySSLCertificate: true), $httpClient);
}

/**
Expand Down
28 changes: 17 additions & 11 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace EcomailFlexibeeTest;

use EcomailFlexibee\Client;
use EcomailFlexibee\Config as ClientConfig;
use EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization;
use EcomailFlexibee\Exception\EcomailFlexibeeNoEvidenceResult;
use EcomailFlexibee\Exception\EcomailFlexibeeRequestFail;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testGetLoginFormUrl(): void

public function testInvalidAuthorization(): void
{
$client = new Client(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false);
$client = new Client(new ClientConfig(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false));
$this->expectException(EcomailFlexibeeInvalidAuthorization::class);
$client->findById($this->faker->numberBetween());
}
Expand All @@ -63,15 +64,15 @@ public function testGetAuthTokenAndMakeSuccessCallWithSessionAuthId(): void
Assert::assertArrayHasKey('refreshToken', $authToken);
Assert::assertArrayHasKey('authSessionId', $authToken);
Assert::assertArrayHasKey('csrfToken', $authToken);
$client = new Client(
$client = new Client(new ClientConfig(
Config::HOST,
Config::COMPANY,
'xxx',
'xxx',
Config::EVIDENCE,
verifySSLCertificate: false,
authSessionId: $this->getString($authToken, 'authSessionId'),
);
));
Assert::assertNotEmpty($client->allInEvidence());
}

Expand All @@ -82,7 +83,7 @@ public function testCount(): void

public function testInvalidGetAuthToken(): void
{
$client = new Client(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false);
$client = new Client(new ClientConfig(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false));
$flexibeeResponse = $client->getAuthAndRefreshToken();
Assert::assertFalse($flexibeeResponse->isSuccess());
$data = $flexibeeResponse->getData();
Expand Down Expand Up @@ -155,7 +156,7 @@ public function testEvidenceGetOnlyCustomFields(): void
*/
public function testCrudOperations(string $evidence, array $evidenceData, array $expectedDataAfterUpdate): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, $evidence, verifySSLCertificate: false);
$client = $this->createClient($evidence);
$addressBookId = $this->getInt($this->getFirstItem($client->save($evidenceData, id: null)->getData()), 'id');
$client->save($expectedDataAfterUpdate, $addressBookId);
$addressBookRefreshed = $this->getFirstItem($client->getById($addressBookId)->getData());
Expand Down Expand Up @@ -226,7 +227,7 @@ public function testAllAndChunkInEvidence(): void

public function testSearchInEvidence(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false);
$client = $this->createClient('faktura-vydana');
$result = $client->searchInEvidence('(kod neq \'JAN\')', []);
Assert::assertTrue(count($result) > 0);
$this->expectException(EcomailFlexibeeRequestFail::class);
Expand All @@ -235,7 +236,7 @@ public function testSearchInEvidence(): void

public function testSearchInEvidencePaginated(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false);
$client = $this->createClient('faktura-vydana');
$result = $client->searchInEvidencePaginated('(kod neq \'JAN\')', []);
Assert::assertIsInt($result['row_count']);
Assert::assertTrue($result['row_count'] > 0);
Expand All @@ -247,15 +248,15 @@ public function testSearchInEvidencePaginated(): void

public function testFindLastInEvidence(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false);
$client = $this->createClient('faktura-vydana');
$result = $client->findLastInEvidence(fullDetail: true);
Assert::assertIsArray($result->getData()['faktura-vydana']);
Assert::assertCount(1, $result->getData()['faktura-vydana']);
}

public function testSearchInEvidenceWithInvalidUrl(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false);
$client = $this->createClient('faktura-vydana');
$this->expectException(EcomailFlexibeeRequestFail::class);
$client->searchInEvidence(' bla.json', []);
}
Expand Down Expand Up @@ -288,7 +289,7 @@ public function testMakeCustomRequest(): void

public function testUnknownCompany(): void
{
$client = new Client(Config::HOST, 'xxx', Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false);
$client = new Client(new ClientConfig(Config::HOST, 'xxx', Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false));
$this->expectException(EcomailFlexibeeRequestFail::class);
$client->save([], id: null);
}
Expand Down Expand Up @@ -319,7 +320,12 @@ protected function setUp(): void
parent::setUp();

$this->faker = Factory::create();
$this->client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, Config::EVIDENCE, verifySSLCertificate: false);
$this->client = $this->createClient(Config::EVIDENCE);
}

private function createClient(string $evidence): Client
{
return new Client(new ClientConfig(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, $evidence, verifySSLCertificate: false));
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types = 1);

namespace EcomailFlexibeeTest;

use EcomailFlexibee\Config as ClientConfig;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

final class ConfigTest extends TestCase
{

public function testWithEvidenceReturnsCopyWithOnlyTheEvidenceChanged(): void
{
$config = new ClientConfig(
'https://demo.flexibee.eu',
'demo',
'user',
'pass',
'adresar',
verifySSLCertificate: true,
authSessionId: 'session',
debugMode: true,
);

$companyConfig = $config->withEvidence('c');

Assert::assertEquals(
new ClientConfig(
'https://demo.flexibee.eu',
'demo',
'user',
'pass',
'c',
verifySSLCertificate: true,
authSessionId: 'session',
debugMode: true,
),
$companyConfig,
);
Assert::assertSame('adresar', $config->getEvidence());
}

}
Loading