This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from devqaly/feature/add-tests
Feature/add tests
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |