1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\NewRelic; |
4
|
|
|
|
5
|
|
|
use DJStarCOM\NewRelic\Config\TransactionConfig; |
6
|
|
|
use DJStarCOM\NewRelic\Stub\Foo; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class TransactionTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
private $transaction; |
12
|
|
|
public static $transactionName; |
13
|
|
|
public static $applicationNameStarted; |
14
|
|
|
public static $customParameters; |
15
|
|
|
public static $endTransaction; |
16
|
|
|
public static $exceptionMessage; |
17
|
|
|
public static $exception; |
18
|
|
|
public static $applicationName; |
19
|
|
|
public static $extensionAvailable; |
20
|
|
|
public static $transactionsCount; |
21
|
|
|
public static $isBackground; |
22
|
|
|
|
23
|
|
|
private $config; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->config = new TransactionConfig(); |
28
|
|
|
$this->config->applicationName = 'RTG'; |
29
|
|
|
$this->config->transactionName = 'Track'; |
30
|
|
|
self::$extensionAvailable = true; |
31
|
|
|
$this->transaction = new Transaction(new Foo(), $this->config); |
32
|
|
|
self::$endTransaction = false; |
33
|
|
|
self::$transactionsCount = 0; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testHasAbilityToSetApplicationName() |
37
|
|
|
{ |
38
|
|
|
$this->transaction->bar(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->assertEquals('RTG', self::$applicationName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCanSetBackgroundJob() |
44
|
|
|
{ |
45
|
|
|
$this->transaction->bar(); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->assertEquals(true, self::$isBackground); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testStartATransaction() |
51
|
|
|
{ |
52
|
|
|
$this->transaction->bar(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->assertEquals('RTG', self::$applicationNameStarted); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCanSetTransactionName() |
58
|
|
|
{ |
59
|
|
|
$this->transaction->bar(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->assertEquals('Track', self::$transactionName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCanAddComplexArgumentsToNewRelic() |
65
|
|
|
{ |
66
|
|
|
$complexArgument = [ |
67
|
|
|
0 => 'simple', |
68
|
|
|
1 => ['array' => 'simple'], |
69
|
|
|
2 => ['string' => 'simple', 'named array' => ['json'], 'object' => new \stdClass()] |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$this->transaction->bar($complexArgument[0], $complexArgument[1], $complexArgument[2]); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->assertEquals([ |
75
|
|
|
'0' => 'simple', |
76
|
|
|
'array' => 'simple', |
77
|
|
|
'string' => 'simple', |
78
|
|
|
'named array' => '["json"]', |
79
|
|
|
'object' => '{}' |
80
|
|
|
], self::$customParameters); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testArgumentAreCorrectedPassedToObject() |
84
|
|
|
{ |
85
|
|
|
$argument = 'NRI'; |
86
|
|
|
|
87
|
|
|
$returnedArgument = $this->transaction->bar($argument); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->assertEquals($argument, $returnedArgument); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @expectedException \InvalidArgumentException |
94
|
|
|
* @expectedExceptionMessage NRI |
95
|
|
|
*/ |
96
|
|
|
public function testExceptionStillTheSame() |
97
|
|
|
{ |
98
|
|
|
$expectedException = new \InvalidArgumentException('NRI'); |
99
|
|
|
|
100
|
|
|
$this->transaction->fooThrowThisException($expectedException); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testExceptionIsRecordedOnNewRelic() |
104
|
|
|
{ |
105
|
|
|
$expectedException = new \InvalidArgumentException('NRI'); |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
$this->transaction->fooThrowThisException($expectedException); |
|
|
|
|
109
|
|
|
} catch (\Exception $exception) { |
110
|
|
|
//Ignoring exceptions |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->assertNotEmpty(self::$exceptionMessage); |
114
|
|
|
$this->assertEquals($expectedException, self::$exception); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testEndTransactionAndSendToNewRelicWhenAnExceptionHappen() |
118
|
|
|
{ |
119
|
|
|
$expectedException = new \InvalidArgumentException('NRI'); |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$this->transaction->fooThrowThisException($expectedException); |
|
|
|
|
123
|
|
|
} catch (\Exception $exception) { |
124
|
|
|
//Ignoring exceptions |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->assertTrue(self::$endTransaction); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testEndTransactionAndSendToNewRelic() |
131
|
|
|
{ |
132
|
|
|
$this->transaction->bar(); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$this->assertTrue(self::$endTransaction); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
|
public function testNonObject() |
141
|
|
|
{ |
142
|
|
|
new Transaction('NRI', new TransactionConfig()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @expectedException \DJStarCOM\NewRelic\Exception\NotLoadedNewRelicExtensionException |
147
|
|
|
*/ |
148
|
|
|
public function testExceptionIfExtensionIsNotLoaded() |
149
|
|
|
{ |
150
|
|
|
self::$extensionAvailable = false; |
151
|
|
|
|
152
|
|
|
new Transaction(new \StdClass, new TransactionConfig()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testTransactioIsStartedOnlyForDesiredMethodCall() |
156
|
|
|
{ |
157
|
|
|
$this->config->monitoredMethodName = 'foo'; |
158
|
|
|
|
159
|
|
|
$this->transaction->bar(); |
|
|
|
|
160
|
|
|
$this->transaction->foo(); |
|
|
|
|
161
|
|
|
|
162
|
|
|
$this->assertEquals(1, self::$transactionsCount); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
function newrelic_start_transaction($appName) |
167
|
|
|
{ |
168
|
|
|
TransactionTest::$applicationNameStarted = $appName; |
169
|
|
|
TransactionTest::$transactionsCount++; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function newrelic_name_transaction($transactionName) |
173
|
|
|
{ |
174
|
|
|
TransactionTest::$transactionName = $transactionName; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function newrelic_add_custom_parameter($key, $value) |
178
|
|
|
{ |
179
|
|
|
TransactionTest::$customParameters[$key] = $value; |
180
|
|
|
return true; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
function newrelic_end_transaction() |
184
|
|
|
{ |
185
|
|
|
TransactionTest::$endTransaction = true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
function newrelic_notice_error($exceptionMessage, \Exception $exception = null) |
189
|
|
|
{ |
190
|
|
|
TransactionTest::$exceptionMessage = $exceptionMessage; |
191
|
|
|
TransactionTest::$exception = $exception; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function extension_loaded($extension) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
return TransactionTest::$extensionAvailable; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
function newrelic_set_appname($appName) |
200
|
|
|
{ |
201
|
|
|
TransactionTest::$applicationName = $appName; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
function newrelic_background_job($isBackground) |
205
|
|
|
{ |
206
|
|
|
TransactionTest::$isBackground = $isBackground; |
207
|
|
|
} |
208
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: