Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
victorstanciu committed Sep 26, 2018
1 parent c00ed22 commit 7a3d222
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.idea
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 slashtrace
Copyright (c) 2018 Victor Stanciu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SlashTrace - Awesome error handler - Raygun handler

This is the [Raygun](https://raygun.com/) handler for [SlashTrace](https://github.com/slashtrace/slashtrace).
Use it to send your errors and exceptions to your Raygun account.

## Usage

1. Install using Composer:

```
composer require slashtrace/slashtrace-raygun
```

2. Hook it into SlashTrace:

```PHP
use SlashTrace\SlashTrace;
use SlashTrace\Raygun\RaygunHandler;

$handler = new RaygunHandler("Your Raygun API key");

$slashtrace = new SlashTrace();
$slashtrace->addHandler($handler);
```

Alternatively, you can pass in a pre-configured Raygun client when you instantiate the handler:

```
$raygun = new Raygun4php\RaygunClient("Your Raygun API key");
$handler = new RaygunHandler($raygun);
$slashtrace->addHandler($handler);
```

Read the [SlashTrace](https://github.com/slashtrace/slashtrace) docs to see how to capture errors and exceptions, and how to attach additional data to your events.
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "slashtrace/slashtrace-raygun",
"description": "Raygun handler for SlashTrace",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Victor Stanciu",
"email": "[email protected]"
}
],
"require": {
"php": ">= 5.6",
"slashtrace/slashtrace": "^1.0",
"mindscape/raygun4php": "1.*"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
"SlashTrace\\Raygun\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SlashTrace\\Raygun\\Tests\\": "tests/"
}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
processIsolation="false"
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false">
<testsuites>
<testsuite>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
73 changes: 73 additions & 0 deletions src/RaygunHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace SlashTrace\Raygun;

use SlashTrace\Context\User;
use SlashTrace\EventHandler\EventHandler;
use SlashTrace\EventHandler\EventHandlerException;

use Raygun4php\RaygunClient;
use Exception;

class RaygunHandler implements EventHandler
{
/** @var RaygunClient */
private $raygun;

public function __construct($raygun)
{
$this->raygun = $raygun instanceof RaygunClient ? $raygun : new RaygunClient($raygun);
}

/**
* @param Exception $exception
* @return int
* @throws EventHandlerException
*/
public function handleException($exception)
{
try {
$this->raygun->SendException($exception);
} catch (Exception $e) {
throw new EventHandlerException($e->getMessage(), $e->getCode(), $e);
}
return EventHandler::SIGNAL_CONTINUE;
}

/**
* @param User $user
* @return void
*/
public function setUser(User $user)
{
$this->raygun->SetUser((string) $user->getId() ?: $user->getEmail(), null, $user->getName(), $user->getEmail());
}

/**
* @param string $title
* @param array $data
* @return void
*/
public function recordBreadcrumb($title, array $data = [])
{
// Breadcrumbs are currently not supported by the Raygun PHP SDK
}

/**
* @param string $release
* @return void
*/
public function setRelease($release)
{
$this->raygun->SetVersion($release);
}

/**
* @param string $path
* @return void
*/
public function setApplicationPath($path)
{
// Local application path is currently not supported by the Raygun PHP SDK
}
}
96 changes: 96 additions & 0 deletions tests/RaygunHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace SlashTrace\Raygun\Tests;

use SlashTrace\Context\User;
use SlashTrace\EventHandler\EventHandlerException;
use SlashTrace\Raygun\RaygunHandler;

use PHPUnit\Framework\TestCase;
use Raygun4php\RaygunClient;

use Exception;

class RaygunHandlerTest extends TestCase
{
public function testExceptionIsPassedToRaygunClient()
{
$exception = new Exception();

$raygun = $this->createMock(RaygunClient::class);
$raygun->expects($this->once())
->method("SendException")
->with($exception);

$handler = new RaygunHandler($raygun);
$handler->handleException($exception);
}

public function testRaygunExceptionsAreHandled()
{
$originalException = new Exception();
$raygunException = new Exception();

$raygun = $this->createMock(RaygunClient::class);
$raygun->expects($this->once())
->method("SendException")
->with($originalException)
->willThrowException($raygunException);

$handler = new RaygunHandler($raygun);
try {
$handler->handleException($originalException);
$this->fail("Expected exception: " . EventHandlerException::class);
} catch (EventHandlerException $e) {
$this->assertSame($raygunException, $e->getPrevious());
}
}

public function testUserIsPassedToRaygunClient()
{
$user = new User();
$user->setId(12345);
$user->setEmail("[email protected]");
$user->setName("Philip J. Fry");

$raygun = $this->createMock(RaygunClient::class);
$raygun->expects($this->once())
->method("SetUser")
->with(
$user->getId(),
null,
$user->getName(),
$user->getEmail()
);

$handler = new RaygunHandler($raygun);
$handler->setUser($user);
}

public function testPartialUserData()
{
$user = new User();
$user->setEmail("[email protected]");

$raygun = $this->createMock(RaygunClient::class);
$raygun->expects($this->once())
->method("SetUser")
->with($user->getEmail(), null, null, $user->getEmail());

$handler = new RaygunHandler($raygun);
$handler->setUser($user);
}

public function testReleaseIsPassedToSentryClient()
{
$release = "1.0.0";

$raygun = $this->createMock(RaygunClient::class);
$raygun->expects($this->once())
->method("SetVersion")
->with($release);

$handler = new RaygunHandler($raygun);
$handler->setRelease($release);
}
}
13 changes: 13 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$autoload_paths = [
__DIR__ . "/../vendor/autoload.php",
__DIR__ . "/../../../autoload.php",
];

foreach ($autoload_paths as $path) {
if (file_exists($path)) {
require_once $path;
return;
}
}

0 comments on commit 7a3d222

Please sign in to comment.