InsightsTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 10
dl 0
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testCanSendAsyncRequest() 0 7 1
A testFulfillMessageInterface() 0 18 1
A provideInvalidEventTypes() 0 8 1
A testEventType() 0 9 1
A testFullInsightsUrlAreCalledFollowingRFC3986() 0 14 1
1
<?php
2
3
namespace NewRelic\Test;
4
5
use NewRelic;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\Psr7\Response;
9
use GuzzleHttp\HandlerStack;
10
use GuzzleHttp\Promise;
11
use GuzzleHttp\Middleware;
12
use DJStarCOM\NewRelic\Insights;
13
use DJStarCOM\NewRelic\Entity\Insights\EventCollection;
14
use DJStarCOM\NewRelic\Entity\Insights\Event;
15
use PHPUnit\Framework\TestCase;
16
17
class InsightsTest extends TestCase
18
{
19
    /**
20
     * @var Insights
21
     */
22
    private $newRelicInsights;
23
    private $requestContainer = [];
24
    private $handler;
25
26
    public function setUp()
27
    {
28
        $this->requestContainer = [];
29
        $history = Middleware::history($this->requestContainer);
30
        $mock = new MockHandler([new Response(200, [])]);
31
        $this->handler = HandlerStack::create($mock);
32
        $this->handler->push($history);
33
34
        $client = new Client([
35
            'handler' => $this->handler,
36
            'base_uri' => 'http://SomeEndpoint/'
37
        ]);
38
        $this->newRelicInsights = new Insights($client, 'Mum-Ha');
39
    }
40
41
    public function testCanSendAsyncRequest()
42
    {
43
        $promise = $this->newRelicInsights->sendEvent(new EventCollection());
44
45
        $response = $promise->wait();
46
        $this->assertEquals(200, $response->getStatusCode());
47
    }
48
49
    public function testFulfillMessageInterface()
50
    {
51
        $event = new Event();
52
        $event->eventType = 'Purchase';
53
        $event->account = 3;
54
        $event->amount = 259.54;
55
        $events = new EventCollection();
56
        $events->add($event);
57
58
        $promise = $this->newRelicInsights->sendEvent($events);
59
60
        $promise->wait();
61
        $request = $this->requestContainer[0]['request'];
62
        $this->assertEquals(
63
            '[{"eventType":"Purchase","account":3,"amount":259.54}]',
64
            $request->getBody()->getContents()
65
        );
66
    }
67
68
    public function provideInvalidEventTypes()
69
    {
70
        return [
71
            'no type defined' => [null],
72
            'type as integer' => [123],
73
            'empty type' => [''],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider provideInvalidEventTypes
79
     * @expectedException \Exception
80
     * @param string $type
81
     */
82
    public function testEventType($type)
83
    {
84
        $event = new Event();
85
        $event->eventType = $type;
86
        $events = new EventCollection();
87
        $events->add($event);
88
89
        $this->newRelicInsights->sendEvent($events);
90
    }
91
92
    public function testFullInsightsUrlAreCalledFollowingRFC3986()
93
    {
94
        $client = new Client([
95
            'handler' => $this->handler,
96
            'base_uri' => 'http://SomeEndpoint/base/path/'
97
        ]);
98
        $this->newRelicInsights = new Insights($client, 'Mum-Ha');
99
100
        $promise = $this->newRelicInsights->sendEvent(new EventCollection());
101
102
        $promise->wait();
103
        $lastRequestPath = $this->requestContainer[0]['request']->getUri()->getPath();
104
        $this->assertEquals('/base/path/events', $lastRequestPath);
105
    }
106
}
107