diff --git a/tests/RefodevClientClassTest.php b/tests/DevqalyClientClassTest.php similarity index 100% rename from tests/RefodevClientClassTest.php rename to tests/DevqalyClientClassTest.php diff --git a/tests/src/Events/BaseEventClassTest.php b/tests/src/Events/BaseEventClassTest.php new file mode 100644 index 0000000..cbf5d0b --- /dev/null +++ b/tests/src/Events/BaseEventClassTest.php @@ -0,0 +1,91 @@ +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'); diff --git a/tests/src/Events/DatabaseTransactionEventTest.php b/tests/src/Events/DatabaseTransactionEventTest.php new file mode 100644 index 0000000..b0602fb --- /dev/null +++ b/tests/src/Events/DatabaseTransactionEventTest.php @@ -0,0 +1,41 @@ +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); +}); diff --git a/tests/src/Events/LogEventTest.php b/tests/src/Events/LogEventTest.php new file mode 100644 index 0000000..91206bf --- /dev/null +++ b/tests/src/Events/LogEventTest.php @@ -0,0 +1,52 @@ +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); +});