Skip to content

Commit

Permalink
Split adapters into separate tests for better maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Jul 25, 2023
1 parent 81aba34 commit ddf6f6f
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 298 deletions.
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@
"scripts": {
"check": "./vendor/bin/phpstan analyse --level max src tests",
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
"format": "./vendor/bin/pint",
"test-unit": "./vendor/bin/phpunit --configuration phpunit.xml --testsuite unit --debug",
"test-e2e": "./vendor/bin/phpunit --configuration phpunit.xml --testsuite e2e --debug",
"test": [
"@test-unit",
"@test-e2e"
]
},
"autoload": {
"psr-4": {"Utopia\\Logger\\": "src/Logger"}
},
"autoload-dev": {
"psr-4": {
"Utopia\\Tests\\E2E\\":"tests/e2e",
"Utopia\\Tests\\Unit\\":"tests/unit"
}
},
"require": {
"php": ">=8.0"
},
Expand Down
272 changes: 162 additions & 110 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
stopOnFailure="false"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
<testsuite name="unit">
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="e2e">
<directory>./tests/e2e</directory>
</testsuite>
</testsuites>
</phpunit>
185 changes: 0 additions & 185 deletions tests/LoggerTest.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/e2e/Adapter/AppSignalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Utopia\Tests\E2E\Adapter;

use Utopia\Logger\Adapter\AppSignal;
use Utopia\Tests\E2E\AdapterBase;

class AppSignalTest extends AdapterBase
{
protected function setUp(): void
{
parent::setUp();
$appSignalKey = \getenv('TEST_APPSIGNAL_KEY');
$this->adapter = new AppSignal($appSignalKey ? $appSignalKey : '');
}
}
16 changes: 16 additions & 0 deletions tests/e2e/Adapter/LogOwlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Utopia\Tests\E2E\Adapter;

use Utopia\Logger\Adapter\LogOwl;
use Utopia\Tests\E2E\AdapterBase;

class LogOwlTest extends AdapterBase
{
protected function setUp(): void
{
parent::setUp();
$logOwlKey = \getenv('TEST_LOGOWL_KEY');
$this->adapter = new LogOwl($logOwlKey ? $logOwlKey : '');
}
}
17 changes: 17 additions & 0 deletions tests/e2e/Adapter/RaygunTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Utopia\Tests\E2E\Adapter;

use Utopia\Logger\Adapter\Raygun;
use Utopia\Tests\E2E\AdapterBase;

class RaygunTest extends AdapterBase
{
protected function setUp(): void
{
parent::setUp();
$raygunKey = \getenv('TEST_RAYGUN_KEY');
$this->adapter = new Raygun($raygunKey ? $raygunKey : '');
$this->expected = 202;
}
}
15 changes: 15 additions & 0 deletions tests/e2e/Adapter/SentryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Utopia\Tests\E2E\Adapter;

use Utopia\Logger\Adapter\Sentry;
use Utopia\Tests\E2E\AdapterBase;

class SentryTest extends AdapterBase
{
protected function setUp(): void
{
parent::setUp();
$this->adapter = new Sentry(\getenv('TEST_SENTRY_KEY').';'.\getenv('TEST_SENTRY_PROJECT_ID').';'.\getenv('TEST_SENTRY_HOST'));
}
}
61 changes: 61 additions & 0 deletions tests/e2e/AdapterBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Utopia\Tests\E2E;

use PHPUnit\Framework\TestCase;
use Utopia\Logger\Adapter;
use Utopia\Logger\Log;
use Utopia\Logger\Log\Breadcrumb;
use Utopia\Logger\Log\User;
use Utopia\Logger\Logger;

abstract class AdapterBase extends TestCase
{
protected ?Log $log = null;

protected ?Adapter $adapter = null;

protected int $expected = 200;

protected function setUp(): void
{
// Prepare log
$this->log = new Log();
$this->log->setAction('controller.database.deleteDocument');
$this->log->setEnvironment('production');
$this->log->setNamespace('api');
$this->log->setServer('digitalocean-us-001');
$this->log->setType(Log::TYPE_ERROR);
$this->log->setVersion('0.11.5');
$this->log->setMessage('Document efgh5678 not found');
$this->log->setUser(new User('efgh5678'));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_DEBUG, 'http', 'DELETE /api/v1/database/abcd1234/efgh5678', \microtime(true) - 500));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_DEBUG, 'auth', 'Using API key', \microtime(true) - 400));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_INFO, 'auth', 'Authenticated with * Using API Key', \microtime(true) - 350));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_INFO, 'database', 'Found collection abcd1234', \microtime(true) - 300));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_DEBUG, 'database', 'Permission for collection abcd1234 met', \microtime(true) - 200));
$this->log->addBreadcrumb(new Breadcrumb(Log::TYPE_ERROR, 'database', 'Missing document when searching by ID!', \microtime(true)));
$this->log->addTag('sdk', 'Flutter');
$this->log->addTag('sdkVersion', '0.0.1');
$this->log->addTag('authMode', 'default');
$this->log->addTag('authMethod', 'cookie');
$this->log->addTag('authProvider', 'MagicLink');
$this->log->addExtra('urgent', false);
$this->log->addExtra('isExpected', true);
$this->log->addExtra('file', '/User/example/server/src/server/server.js');
$this->log->addExtra('line', '15');
}

/**
* @throws \Throwable
*/
public function testAdapter(): void
{
if (empty($this->log) || empty($this->adapter)) {
throw new \Exception('Log or adapter not set');
}
$logger = new Logger($this->adapter);
$response = $logger->addLog($this->log);
$this->assertEquals($this->expected, $response);
}
}
40 changes: 40 additions & 0 deletions tests/unit/Log/BreadcrumbTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Utopia\Tests\Unit\Log;

use PHPUnit\Framework\TestCase;
use Utopia\Logger\Log;
use Utopia\Logger\Log\Breadcrumb;

class BreadcrumbTest extends TestCase
{
/**
* @throws \Throwable
*/
public function testLogBreadcrumb(): void
{
$timestamp = \microtime(true);
$breadcrumb = new Breadcrumb(Log::TYPE_DEBUG, 'http', 'POST /user', $timestamp);

self::assertEquals(Log::TYPE_DEBUG, $breadcrumb->getType());
self::assertEquals('http', $breadcrumb->getCategory());
self::assertEquals('POST /user', $breadcrumb->getMessage());
self::assertEquals($timestamp, $breadcrumb->getTimestamp());

$breadcrumb = new Breadcrumb(Log::TYPE_INFO, 'http', 'POST /user', $timestamp);
self::assertEquals(Log::TYPE_INFO, $breadcrumb->getType());
$breadcrumb = new Breadcrumb(Log::TYPE_VERBOSE, 'http', 'POST /user', $timestamp);
self::assertEquals(Log::TYPE_VERBOSE, $breadcrumb->getType());
$breadcrumb = new Breadcrumb(Log::TYPE_ERROR, 'http', 'POST /user', $timestamp);
self::assertEquals(Log::TYPE_ERROR, $breadcrumb->getType());
$breadcrumb = new Breadcrumb(Log::TYPE_WARNING, 'http', 'POST /user', $timestamp);
self::assertEquals(Log::TYPE_WARNING, $breadcrumb->getType());

// Assert FAILS
self::expectException(\ArgumentCountError::class);
$breadcrumb = new Breadcrumb(); // @phpstan-ignore-line
$breadcrumb = new Breadcrumb(Log::TYPE_DEBUG); // @phpstan-ignore-line
$breadcrumb = new Breadcrumb(Log::TYPE_DEBUG, 'http'); // @phpstan-ignore-line
$breadcrumb = new Breadcrumb(Log::TYPE_DEBUG, 'http', 'POST /user'); // @phpstan-ignore-line
}
}
Loading

0 comments on commit ddf6f6f

Please sign in to comment.