NewRelicMiddlewareTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandle() 0 30 1
A testGetTransactionName() 0 11 1
1
<?php
2
3
namespace ALehdet\ContentApi\Tests\Unit\GraphQL\Http;
4
5
use Digia\JsonHelpers\JsonEncoder;
6
use Digia\Lumen\GraphQL\Http\Middleware\NewRelicMiddleware;
7
use Digia\Lumen\GraphQL\Models\GraphQLError;
8
use Digia\Lumen\GraphQL\Tests\MiddlewareTestCase;
9
use Illuminate\Http\Request;
10
use Intouch\Newrelic\Newrelic;
11
12
/**
13
 * Class NewRelicMiddlewareTest
14
 * @package ALehdet\ContentApi\Tests\Unit\GraphQL\Http
15
 */
16
class NewRelicMiddlewareTest extends MiddlewareTestCase
17
{
18
19
    /**
20
     * Tests that the transaction name is correctly determined
21
     */
22
    public function testGetTransactionName()
23
    {
24
        // Create a request
25
        $request = new Request();
26
        $request->merge([
27
            'operationName' => 'foo',
28
        ]);
29
30
        /** @var NewRelicMiddleware $middleware */
31
        $middleware = $this->app->make(NewRelicMiddleware::class);
32
        $this->assertEquals('GraphQLController@foo', $middleware->getTransactionName($request));
33
    }
34
35
    /**
36
     * Tests that GraphQL errors are properly reported to New Relic
37
     */
38
    public function testHandle()
39
    {
40
        // Mock a NewRelic instance
41
        /** @var \PHPUnit_Framework_MockObject_MockObject|Newrelic $newRelic */
42
        $newRelic = $this->getMockBuilder(Newrelic::class)
43
                         ->setMethods(['noticeError'])
44
                         ->getMock();
45
46
        $graphQLError = new GraphQLError('Some query', ['foo' => 'bar'], [new \Exception()]);
47
48
        $newRelic->expects($this->once())
49
                 ->method('noticeError')
50
                 ->with(JsonEncoder::encode([
51
                     'query'     => $graphQLError->getQuery(),
52
                     'variables' => $graphQLError->getVariables(),
53
                     'message'   => '',
54
                 ]), $graphQLError->getExceptions()[0]);
55
56
        // Create a request with errors
57
        $request = new Request();
58
        $request->attributes->set(NewRelicMiddleware::ATTRIBUTE_ERROR, $graphQLError);
59
60
        $middleware = new NewRelicMiddleware($newRelic);
61
        $this->assertMiddlewarePasses($middleware, $request);
62
63
        // Check again with no errors - nothing should get reported
64
        $newRelic->expects($this->never())
65
                 ->method('noticeError');
66
67
        $this->assertMiddlewarePasses($middleware);
68
    }
69
70
}
71