Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from devqaly/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
Bruno Francisco authored Aug 30, 2023
2 parents d090b14 + 273b56e commit 29593e7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/DevqalyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class DevqalyClient

private LogEvent $logEvent;

public function __construct(?string $backendUrl, ?string $sourceIdentifier)
public function __construct(?string $backendUrl, ?string $sourceIdentifier, string $securityToken)
{
$this->databaseEvent = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier);
$this->logEvent = new LogEvent($backendUrl, $sourceIdentifier);
$this->databaseEvent = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier, $securityToken);
$this->logEvent = new LogEvent($backendUrl, $sourceIdentifier, $securityToken);
}

public function createDatabaseEventTransaction(string $sessionId, string $sessionSecret, array $data): void
Expand Down
6 changes: 5 additions & 1 deletion src/Events/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ class BaseEvent

private string $backendUrl;

public function __construct(?string $backendUrl, ?string $sourceIdentifier)
private string $securityToken;

public function __construct(?string $backendUrl, ?string $sourceIdentifier, string $securityToken)
{
$this->handle = curl_init($backendUrl);
$this->backendUrl = $backendUrl ?? 'https://api.devqaly.com/api';
$this->sourceIdentifier = $sourceIdentifier;
$this->securityToken = $securityToken;
}

public function setOption($name, $value): void
Expand Down Expand Up @@ -46,6 +49,7 @@ public function generatePayload(array $data, string $type): array
'type' => $type,
'source' => $this->sourceIdentifier,
'clientUtcEventCreatedAt' => (new \DateTime('now', new \DateTimeZone('utc')))->format('Y-m-d\TH:i:s.u\Z'),
'securityToken' => $this->securityToken
];
}

Expand Down
5 changes: 0 additions & 5 deletions src/Events/DatabaseTransactionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class DatabaseTransactionEvent extends BaseEvent implements Event
{
const EVENT_TYPE = 'App\Models\Session\Event\EventDatabaseTransaction';

public function __construct(?string $backendUrl, ?string $sourceIdentifier)
{
parent::__construct($backendUrl, $sourceIdentifier);
}

public function create(string $sessionId, string $sessionSecret, array $data): void
{
if (! isset($data['sql'])) {
Expand Down
19 changes: 13 additions & 6 deletions tests/src/Events/BaseEventClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
it('should return the correct endpoint when calling `getCreateEventEndpoint` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$sessionId = 'b79cb3ba-745e-5d9a-8903-4a02327a7e09';

Expand All @@ -18,8 +19,9 @@
it('should return correct payload when calling `generatePayload` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$clickEventPayload = ['positionX' => 500, 'positionY' => 500];
$eventType = 'click-event';
Expand All @@ -33,6 +35,7 @@
...$clickEventPayload,
'type' => $eventType,
'source' => $sourceIdentifier,
'securityToken' => $securityToken,
])
->and(DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $payload['clientUtcEventCreatedAt']) !== false)
->toBeTrue();
Expand All @@ -41,8 +44,9 @@
it('should allow to validate the session id when calling `validateSessionId` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';

Expand All @@ -56,8 +60,9 @@
it('should throw error exception when calling `validateSessionId` method with invalid session id', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$sessionId = '';

Expand All @@ -67,8 +72,9 @@
it('should allow to validate session secret when calling `validateSessionSecret` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

Expand All @@ -82,8 +88,9 @@
it('should throw error exception when calling `validateSessionSecret` method with invalid session secret', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new BaseEvent($backendUrl, $sourceIdentifier);
$client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken);

$sessionSecret = '';

Expand Down
6 changes: 4 additions & 2 deletions tests/src/Events/DatabaseTransactionEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier);
$client = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier, $securityToken);

$client->create($sessionId, $sessionSecret, []);
})->throws(\Error::class, '`sql` must be set to create a database transaction event in $data');
Expand All @@ -18,8 +19,9 @@
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = Mockery::mock(DatabaseTransactionEvent::class, [$backendUrl, $sourceIdentifier])->makePartial();
$client = Mockery::mock(DatabaseTransactionEvent::class, [$backendUrl, $sourceIdentifier, $securityToken])->makePartial();

$endpoint = 'https://something.com';

Expand Down
9 changes: 6 additions & 3 deletions tests/src/Events/LogEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new LogEvent($backendUrl, $sourceIdentifier);
$client = new LogEvent($backendUrl, $sourceIdentifier, $securityToken);

$client->create($sessionId, $sessionSecret, ['log' => 'some log']);
})->throws(\Error::class, '`level` must be set to create a log event in $data');
Expand All @@ -18,8 +19,9 @@
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = new LogEvent($backendUrl, $sourceIdentifier);
$client = new LogEvent($backendUrl, $sourceIdentifier, $securityToken);

$client->create($sessionId, $sessionSecret, ['level' => LogEvent::LOG_LEVEL_ALERT]);
})->throws(\Error::class, '`log` must be set to create a log event in $data');
Expand All @@ -29,8 +31,9 @@
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';
$securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i';

$client = Mockery::mock(LogEvent::class, [$backendUrl, $sourceIdentifier])->makePartial();
$client = Mockery::mock(LogEvent::class, [$backendUrl, $sourceIdentifier, $securityToken])->makePartial();

$endpoint = 'https://something.com';

Expand Down

0 comments on commit 29593e7

Please sign in to comment.