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 #3 from devqaly/feature/add-tests
Browse files Browse the repository at this point in the history
Feature/add tests
  • Loading branch information
Bruno Francisco authored Aug 15, 2023
2 parents 13703e9 + eb1c963 commit d090b14
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
File renamed without changes.
91 changes: 91 additions & 0 deletions tests/src/Events/BaseEventClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

use Devqaly\DevqalyClient\Events\BaseEvent;

it('should return the correct endpoint when calling `getCreateEventEndpoint` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

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

$createEndpoint = $client->getCreateEventEndpoint($sessionId);

expect($createEndpoint)->toBe(sprintf('%s/sessions/%s/events', $backendUrl, $sessionId));
});

it('should return correct payload when calling `generatePayload` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

$clickEventPayload = ['positionX' => 500, 'positionY' => 500];
$eventType = 'click-event';

$payload = $client->generatePayload($clickEventPayload, $eventType);

expect($payload)
->toBeArray()
->and($payload)
->toMatchArray([
...$clickEventPayload,
'type' => $eventType,
'source' => $sourceIdentifier,
])
->and(DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $payload['clientUtcEventCreatedAt']) !== false)
->toBeTrue();
});

it('should allow to validate the session id when calling `validateSessionId` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

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

$client->validateSessionId($sessionId);

// I have added this here just to assert something.
// As far as I researched, pest doesn't have something like PHP Unit's @doesNotPerformAssertions
expect(1)->toBe(1);
});

it('should throw error exception when calling `validateSessionId` method with invalid session id', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

$sessionId = '';

$client->validateSessionId($sessionId);
})->throws(\Error::class, '$sessionId must be set and not empty');

it('should allow to validate session secret when calling `validateSessionSecret` method', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

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

$client->validateSessionSecret($sessionSecret);

// I have added this here just to assert something.
// As far as I researched, pest doesn't have something like PHP Unit's @doesNotPerformAssertions
expect(1)->toBe(1);
});

it('should throw error exception when calling `validateSessionSecret` method with invalid session secret', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';

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

$sessionSecret = '';

$client->validateSessionSecret($sessionSecret);
})->throws(\Error::class, '$sessionSecret must be set and not empty');
41 changes: 41 additions & 0 deletions tests/src/Events/DatabaseTransactionEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Devqaly\DevqalyClient\Events\DatabaseTransactionEvent;

it('should throw an error when passing empty sql', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

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

$client->create($sessionId, $sessionSecret, []);
})->throws(\Error::class, '`sql` must be set to create a database transaction event in $data');

it('should execute curl request when calling `create` method and close', function() {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

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

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

$baseData = ['sql' => 'select * from users'];

$client->shouldReceive('validateSessionId')->with($sessionId)->once();
$client->shouldReceive('validateSessionSecret')->with($sessionSecret)->once();
$client->shouldReceive('getCreateEventEndpoint')->with($sessionId)->once()->andReturn($endpoint);

$client->shouldReceive('setOption')->times(1)->with(CURLOPT_URL, $endpoint);
$client->shouldReceive('setOption')->times(1)->with(CURLOPT_RETURNTRANSFER, true);
$client->shouldReceive('setOption')->times(1);
$client->shouldReceive('setOption')->times(1)->with(CURLOPT_HTTPHEADER, ['x-devqaly-session-secret-token: '.$sessionSecret]);

$client->shouldReceive('execute')->withNoArgs()->once();
$client->shouldReceive('close')->withNoArgs()->once();

$client->create($sessionId, $sessionSecret, $baseData);
});
52 changes: 52 additions & 0 deletions tests/src/Events/LogEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Devqaly\DevqalyClient\Events\LogEvent;

it('should throw an error when passing empty `level`', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

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

$client->create($sessionId, $sessionSecret, ['log' => 'some log']);
})->throws(\Error::class, '`level` must be set to create a log event in $data');

it('should throw an error when passing empty `log`', function () {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

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

$client->create($sessionId, $sessionSecret, ['level' => LogEvent::LOG_LEVEL_ALERT]);
})->throws(\Error::class, '`log` must be set to create a log event in $data');

it('should execute curl request when calling `create` method and close', function() {
$backendUrl = 'https://devqaly.test/api';
$sourceIdentifier = 'microservice-x';
$sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423';
$sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423';

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

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

$baseData = ['level' => LogEvent::LOG_LEVEL_ALERT, 'log' => 'something'];

$client->shouldReceive('validateSessionId')->with($sessionId)->once();
$client->shouldReceive('validateSessionSecret')->with($sessionSecret)->once();
$client->shouldReceive('getCreateEventEndpoint')->with($sessionId)->once()->andReturn($endpoint);

$client->shouldReceive('setOption')->times(1)->with(CURLOPT_URL, $endpoint);
$client->shouldReceive('setOption')->times(1)->with(CURLOPT_RETURNTRANSFER, true);
$client->shouldReceive('setOption')->times(1);
$client->shouldReceive('setOption')->times(1)->with(CURLOPT_HTTPHEADER, ['x-devqaly-session-secret-token: '.$sessionSecret]);

$client->shouldReceive('execute')->withNoArgs()->once();
$client->shouldReceive('close')->withNoArgs()->once();

$client->create($sessionId, $sessionSecret, $baseData);
});

0 comments on commit d090b14

Please sign in to comment.