Skip to content

Commit

Permalink
Tests & Travis & sentry/sdk ^3.0 (#4)
Browse files Browse the repository at this point in the history
* Added tests, Makefile and Travis CI definition.
* `setUser` is called only for logged user.
* Fix other types of log value, than string.
* Library `sentry/sdk` was bumped to `3.0`
  • Loading branch information
martenb authored Oct 22, 2020
1 parent 90905a5 commit a0019f3
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
tests/cases/**/output
tests/tmp
vendor
coverage.xml
composer.lock
52 changes: 52 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
language: php
php:
- 7.2
- 7.3
- 7.4

before_install:
# Turn off XDebug
- phpenv config-rm xdebug.ini || return 0

install:
# Composer
- travis_retry composer install --no-interaction --no-progress --prefer-dist

script:
# Tests
- composer test

after_failure:
# Print *.actual content
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done

jobs:
include:
- env: title="Lowest Dependencies 7.2"
php: 7.2
install:
- travis_retry composer update --no-interaction --no-progress --prefer-dist --prefer-lowest
script:
- composer test

- stage: cs
php: 7.4
script:
- composer cs

# - stage: Test Coverage
# if: branch = master AND type = push
# script:
# - composer coverage
# after_script:
# - wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.2.0/php-coveralls.phar
# - php php-coveralls.phar --verbose --config .coveralls.yml
#
# allow_failures:
# - stage: Test Coverage

sudo: false

cache:
directories:
- $HOME/.composer/cache
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ vendor/autoload.php:
composer install

sniff: vendor/autoload.php
vendor/bin/phpcs --standard=PSR2 src -n --ignore=src/errbit-php
composer cs

sniff-fix: vendor/autoload.php
composer cs-fix

test: vendor/autoload.php
composer test

coverage: vendor/autoload.php
composer coverage
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,30 @@
],
"type": "library",
"require": {
"ext-iconv":"*",
"php": "^7.1",
"sentry/sdk": "^2.1",
"tracy/tracy": "~2.4",
"nette/di": "^2.4 || ^3.0",
"php": "^7.2",
"nette/di": "^2.4.7 || ^3.0",
"nette/http": "^2.4.7 || ^3.0",
"nette/security": "^2.4 || ^3.0",
"nette/http": "^2.4 || ^3.0"
"nette/utils": "^2.4.5 || ^3.0",
"sentry/sdk": "^3.0",
"tracy/tracy": "^2.4"
},
"require-dev": {
"ninjify/nunjuck": "^0.3",
"squizlabs/php_codesniffer": "~3.5"
},
"autoload": {
"psr-4": {
"Rootpd\\NetteSentry\\": "src"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"cs": "phpcs --standard=PSR2 --extensions=php,phpt src tests --ignore=tests/tmp",
"cs-fix": "phpcbf --standard=PSR2 --extensions=php,phpt src tests --ignore=tests/tmp",
"test": "tester -C tests",
"coverage": "tester -p phpdbg -C --coverage coverage.xml --coverage-src src tests"
}
}
6 changes: 4 additions & 2 deletions src/DI/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rootpd\NetteSentry\DI;

use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
use Rootpd\NetteSentry\SentryLogger;
use Tracy\Debugger;
use Tracy\ILogger;

Expand Down Expand Up @@ -35,7 +37,7 @@ public function loadConfiguration()

$this->getContainerBuilder()
->addDefinition($this->prefix('logger'))
->setFactory(\Rootpd\NetteSentry\SentryLogger::class)
->setFactory(SentryLogger::class, [Debugger::$logDirectory])
->addSetup(
'register',
[
Expand Down Expand Up @@ -75,7 +77,7 @@ public function beforeCompile()
}
}

public function afterCompile(\Nette\PhpGenerator\ClassType $class)
public function afterCompile(ClassType $class)
{
if (!$this->enabled) {
return;
Expand Down
29 changes: 14 additions & 15 deletions src/SentryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
use Nette\Http\Session;
use Nette\Security\IIdentity;
use Nette\Security\User;
use Sentry\ClientBuilder;
use Sentry\Integration\RequestIntegration;
use Sentry\Severity;
use Sentry\State\Hub;
use Sentry\State\Scope;
use Throwable;
use Tracy\Debugger;
use Tracy\Dumper;
use Tracy\ILogger;
use Tracy\Logger;
use function Sentry\captureException;
use function Sentry\captureMessage;
use function Sentry\configureScope;
use function Sentry\init;

class SentryLogger extends Logger
{
Expand All @@ -34,27 +36,24 @@ class SentryLogger extends Logger

public function register(string $dsn, string $environment)
{
$options = new \Sentry\Options([
init([
'dsn' => $dsn,
'environment' => $environment,
'default_integrations' => false,
'integrations' => [
new RequestIntegration(),
],
]);

$options->setIntegrations([
new RequestIntegration($options),
]);

$builder = new ClientBuilder($options);
$client = $builder->getClient();
Hub::setCurrent(new Hub($client));

$this->email = & Debugger::$email;
$this->directory = Debugger::$logDirectory;
}

public function setUser(User $user)
{
$this->identity = $user->getIdentity();
if ($user->isLoggedIn()) {
$this->identity = $user->getIdentity();
}
}

public function setUserFields(array $userFields)
Expand Down Expand Up @@ -89,7 +88,7 @@ public function log($value, $priority = ILogger::INFO)
return $response;
}

configureScope(function (\Sentry\State\Scope $scope) use ($severity) {
configureScope(function (Scope $scope) use ($severity) {
if (!$severity) {
return;
}
Expand All @@ -114,10 +113,10 @@ public function log($value, $priority = ILogger::INFO)
}
});

if ($value instanceof \Throwable) {
if ($value instanceof Throwable) {
captureException($value);
} else {
captureMessage($value);
captureMessage(is_string($value) ? $value : Dumper::toText($value));
}

return $response;
Expand Down
10 changes: 10 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

use Ninjify\Nunjuck\Environment;

require __DIR__ . '/../vendor/autoload.php';

// Configure environment
Environment::setupTester();
Environment::setupTimezone();
Environment::setupVariables(__DIR__);
99 changes: 99 additions & 0 deletions tests/cases/DI/Extension.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php declare(strict_types = 1);

use Nette\Bridges\HttpDI\HttpExtension;
use Nette\Bridges\HttpDI\SessionExtension;
use Nette\Bridges\SecurityDI\SecurityExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\Http\Session;
use Nette\Security\Identity;
use Nette\Security\IIdentity;
use Nette\Security\User;
use Rootpd\NetteSentry\DI\SentryExtension;
use Rootpd\NetteSentry\SentryLogger;
use Tester\Assert;

require __DIR__ . '/../../bootstrap.php';

// simple config
test(function (): void {
$config = [
'dsn' => 'https://[email protected]/3',
];

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler) use ($config): void {
$compiler->addExtension('sentry', new SentryExtension())
->addConfig([
'sentry' => $config,
]);
}, 1);

/** @var Container $container */
$container = new $class();

/** @var SentryLogger $logger */
$logger = $container->getService('sentry.logger');

Assert::type(SentryLogger::class, $logger);

Assert::with($logger, function () use ($config): void {
Assert::null($this->session);

Assert::null($this->identity);

Assert::same([], $this->userFields);

Assert::same([], $this->priorityMapping);
});
});

// complex config
test(function (): void {
$config = [
'dsn' => 'https://[email protected]/3',
'environment' => 'test',
'user_fields' => [
'email',
],
'priority_mapping' => [
'mypriority' => 'warning',
]
];

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler) use ($config): void {
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('security', new SecurityExtension());
$compiler->addExtension('session', new SessionExtension());

$compiler->addExtension('sentry', new SentryExtension())
->addConfig([
'sentry' => $config,
]);
}, 2);

/** @var Container $container */
$container = new $class();

$user = $container->getByType(User::class);
$identity = new Identity(1, null, ['email' => '']);
$user->login($identity);

/** @var SentryLogger $logger */
$logger = $container->getService('sentry.logger');

Assert::type(SentryLogger::class, $logger);

Assert::with($logger, function () use ($config, $identity): void {
Assert::type(Session::class, $this->session);

Assert::type(IIdentity::class, $this->identity);
Assert::same($identity, $this->identity);

Assert::same($config['user_fields'], $this->userFields);

Assert::same($config['priority_mapping'], $this->priorityMapping);
});
});

0 comments on commit a0019f3

Please sign in to comment.