Skip to content

Commit

Permalink
Add test casettes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joppe de Cuyper committed Apr 9, 2023
1 parent 60cd90f commit ea8ae4c
Show file tree
Hide file tree
Showing 12 changed files with 608 additions and 13 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Tests\\ClientTest::testThrowCommunicationException":3,"Tests\\ClientTest::testThrowValidationError":4,"Tests\\Endpoints\\LogTest::testCreateLog":4,"Tests\\Endpoints\\LogTest::testCreateLogWithTags":4,"Tests\\Endpoints\\LogTest::testCreateImportedLog":4,"Tests\\Endpoints\\InsightTest::testCreateInsight":4,"Tests\\Endpoints\\InsightTest::testMutateInsight":4},"times":{"Tests\\ClientTest::testThrowCommunicationException":4.29,"Tests\\ClientTest::testThrowValidationError":0.003,"Tests\\Endpoints\\LogTest::testCreateLog":0,"Tests\\Endpoints\\LogTest::testCreateLogWithTags":0,"Tests\\Endpoints\\LogTest::testCreateImportedLog":0,"Tests\\Endpoints\\InsightTest::testCreateInsight":0,"Tests\\Endpoints\\InsightTest::testMutateInsight":0}}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ you want to use because it's an implementation detail of your application.
### Configuration

```php
new JoppeDc\LogsnagPhpSdk\Client("https://api.logsnag.com/v1/", "your_secret_key");
new JoppeDc\LogsnagPhpSdk\Client("your_secret_key");
```

### Usage
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"friendsofphp/php-cs-fixer": "^3.16",
"guzzlehttp/guzzle": "^7.5",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"php-vcr/php-vcr": "^1.6",
"php-vcr/phpunit-testlistener-vcr": "^3.1"
},
"license": "MIT",
"autoload": {
Expand Down
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap = "vendor/autoload.php">

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/php-vcr/phpunit-testlistener-vcr/src/VCRTestListener.php" />
</listeners>

</phpunit>
5 changes: 3 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class Client
{
public const API_URL = 'https://api.logsnag.com/v1';

use HandlesLog;
use HandlesInsight;

Expand All @@ -24,14 +26,13 @@ class Client
private Insight $insight;

public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactory = null,
array $clientAgents = [],
StreamFactoryInterface $streamFactory = null
) {
$this->http = new LogSnagClient($url, $apiKey, $httpClient, $requestFactory, $clientAgents, $streamFactory);
$this->http = new LogSnagClient(self::API_URL, $apiKey, $httpClient, $requestFactory, $clientAgents, $streamFactory);

$this->log = new Log($this->http);
$this->insight = new Insight($this->http);
Expand Down
12 changes: 7 additions & 5 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@

use JoppeDc\LogsnagPhpSdk\Client;
use JoppeDc\LogsnagPhpSdk\Contracts\LogPayload;
use JoppeDc\LogsnagPhpSdk\Exceptions\CommunicationException;
use PHPUnit\Framework\TestCase;
use JoppeDc\LogsnagPhpSdk\Exceptions\ApiException;

class ClientTest extends TestCase
{
public function testThrowCommunicationException(): void
/**
* @vcr client_test_throw_validation_error
*/
public function testThrowValidationError(): void
{
$client = new Client('https://doesntexist:1234');
$client = new Client('testkey');

$this->expectException(CommunicationException::class);
$this->expectException(ApiException::class);
$client->createLog(new LogPayload('', '', ''));
}
}
6 changes: 6 additions & 0 deletions tests/Endpoints/InsightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class InsightTest extends TestCase
{
/**
* @vcr insight_test
*/
public function testCreateInsight(): void
{
$payload = new InsightPayload(
Expand All @@ -25,6 +28,9 @@ public function testCreateInsight(): void
$this->assertEquals('5', $response['value']);
}

/**
* @vcr insight_test
*/
public function testMutateInsight(): void
{
$payload = new InsightPayload(
Expand Down
11 changes: 10 additions & 1 deletion tests/Endpoints/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

final class LogTest extends TestCase
{
/**
* @vcr log_test
*/
public function testCreateLog(): void
{
$payload = new LogPayload(
Expand All @@ -29,6 +32,9 @@ public function testCreateLog(): void
$this->assertEquals('😀', $response['icon']);
}

/**
* @vcr log_test
*/
public function testCreateLogWithTags(): void
{
$payload = new LogPayload(
Expand All @@ -44,6 +50,9 @@ public function testCreateLogWithTags(): void
$this->assertEquals(['tag' => 'tag value'], $response['tags']);
}

/**
* @vcr log_test
*/
public function testCreateImportedLog(): void
{
$payload = new LogPayload(
Expand All @@ -52,7 +61,7 @@ public function testCreateImportedLog(): void
'test-imported-event'
);

$payload->setTimestamp((new \DateTime('-6 months'))->getTimestamp());
$payload->setTimestamp((new \DateTime('1-1-2023'))->getTimestamp());

$response = $this->client->createLog($payload);

Expand Down
17 changes: 14 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@

use JoppeDc\LogsnagPhpSdk\Client;
use PHPUnit\Framework\TestCase as BaseTestCase;
use VCR\Event\Event;
use VCR\VCR;
use VCR\VCREvents;

abstract class TestCase extends BaseTestCase
{
protected Client $client;
protected string $host;

protected function setUp(): void
{
parent::setUp();
VCR::getEventDispatcher()->addListener(VCREvents::VCR_BEFORE_RECORD, array($this, 'cleanRequest'));

$this->host = getenv('LOGSNAG_URL');
$this->client = new Client($this->host, getenv('LOGSNAG_API_KEY'));
// Disable this line if you want to record new cassettes
VCR::getEventDispatcher()->addListener(VCREvents::VCR_BEFORE_PLAYBACK, array($this, 'cleanRequest'));

$this->client = new Client(getenv('LOGSNAG_API_KEY'));
}

public function cleanRequest(Event $event): void
{
$request = $event->getRequest();
$request->removeHeader('Authorization');
}
}
138 changes: 138 additions & 0 deletions tests/fixtures/client_test_throw_validation_error
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

-
request:
method: POST
url: 'https://api.logsnag.com/v1/log'
headers:
Host: api.logsnag.com
Expect: ''
Accept-Encoding: ''
User-Agent: 'LogSnag PHP (v1.0.0)'
Content-Type: application/json
Authorization: 'Bearer testkey'
Accept: ''
body: '{"notify":true,"parser":"text"}'
response:
status:
code: 400
message: 'Bad Request'
headers:
Date: 'Sun, 09 Apr 2023 10:18:35 GMT'
Content-Type: application/json
Content-Length: '284'
Connection: keep-alive
Access-Control-Allow-Origin: '*'
Report-To: '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=ZoA0WYBAB1nQzfAXKYyAvtgNqLpZ8php%2FUKdDFDS37EfFDr8Civ5Wf76wGO0g9d1Y1tvnSnuvaIamPH3cpGX9uXJ42PbduWYlT%2F7KPr%2BZdWeL4GuH3OVUJX20%2BH5OrfajcsjRyNxAl8PZUIDvIE%3D"}],"group":"cf-nel","max_age":604800}'
NEL: '{"success_fraction":0,"report_to":"cf-nel","max_age":604800}'
Server: cloudflare
CF-RAY: 7b5201c10c7ab9b4-BRU
alt-svc: 'h3=":443"; ma=86400, h3-29=":443"; ma=86400'
body: '{"message":"Validation Error","validation":{"body":[{"path":"project","type":"invalid_type","message":"Project name is required"},{"path":"channel","type":"invalid_type","message":"Channel name is required"},{"path":"event","type":"invalid_type","message":"Event name is required"}]}}'
curl_info:
url: 'https://api.logsnag.com/v1/log'
content_type: application/json
http_code: 400
header_size: 621
request_size: 196
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.279923
namelookup_time: 0.004179
connect_time: 0.209366
pretransfer_time: 0.244768
size_upload: 31.0
size_download: 284.0
speed_download: 1014.0
speed_upload: 110.0
download_content_length: 284.0
upload_content_length: 31.0
starttransfer_time: 0.279804
redirect_time: 0.0
redirect_url: ''
primary_ip: '2606:4700:3032::ac43:b4a1'
certinfo: { }
primary_port: 443
local_ip: '2a02:1810:a466:5100:2d06:afbf:bf4b:d2ed'
local_port: 61597
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 454090
connect_time_us: 209366
namelookup_time_us: 4179
pretransfer_time_us: 244768
redirect_time_us: 0
starttransfer_time_us: 279804
total_time_us: 279923
effective_method: POST
index: 0
-
request:
method: POST
url: 'https://api.logsnag.com/v1/log'
headers:
Host: api.logsnag.com
Expect: ''
Accept-Encoding: ''
User-Agent: 'LogSnag PHP (v1.0.0)'
Content-Type: application/json
Accept: ''
body: '{"notify":true,"parser":"text"}'
response:
status:
code: 400
message: 'Bad Request'
headers:
Date: 'Sun, 09 Apr 2023 10:25:19 GMT'
Content-Type: application/json
Content-Length: '166'
Connection: keep-alive
Access-Control-Allow-Origin: '*'
Report-To: '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=4vV8fD%2BzICEi1v5v%2F%2FM1DCDKTuxztukGZuXVb%2F3bS3HlZgkimgsqBJDDdAqyQfVJ8GKNuGWkPBbv%2BCUbEFNhKYNpfq%2B0epFxH6Haz7eo8zHD8rf%2FlaBVAouHkMnZ72kDhgThxBtrHmxoPKfNCPU%3D"}],"group":"cf-nel","max_age":604800}'
NEL: '{"success_fraction":0,"report_to":"cf-nel","max_age":604800}'
Server: cloudflare
CF-RAY: 7b520ba18e4cd488-BRU
alt-svc: 'h3=":443"; ma=86400, h3-29=":443"; ma=86400'
body: '{"message":"Validation Error","validation":{"headers":[{"path":"authorization","type":"invalid_type","message":"Authorization header must be a valid Bearer token"}]}}'
curl_info:
url: 'https://api.logsnag.com/v1/log'
content_type: application/json
http_code: 400
header_size: 627
request_size: 165
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.27186
namelookup_time: 0.003642
connect_time: 0.203801
pretransfer_time: 0.240871
size_upload: 31.0
size_download: 166.0
speed_download: 610.0
speed_upload: 114.0
download_content_length: 166.0
upload_content_length: 31.0
starttransfer_time: 0.271837
redirect_time: 0.0
redirect_url: ''
primary_ip: '2606:4700:3032::ac43:b4a1'
certinfo: { }
primary_port: 443
local_ip: '2a02:1810:a466:5100:2d06:afbf:bf4b:d2ed'
local_port: 61743
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 444636
connect_time_us: 203801
namelookup_time_us: 3642
pretransfer_time_us: 240871
redirect_time_us: 0
starttransfer_time_us: 271837
total_time_us: 271860
effective_method: POST
index: 0

0 comments on commit ea8ae4c

Please sign in to comment.