From 6f9a73d127db684449d7089928ff1ebcffe207aa Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Wed, 14 Mar 2018 20:25:14 +0100 Subject: [PATCH 1/6] add php runtime information --- lib/Raven/Client.php | 2 ++ test/Raven/Tests/ClientTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/Raven/Client.php b/lib/Raven/Client.php index f9887b213..1c855ee15 100644 --- a/lib/Raven/Client.php +++ b/lib/Raven/Client.php @@ -884,6 +884,8 @@ public function capture($data, $stack = null, $vars = null) if (empty($data['site'])) { unset($data['site']); } + $data['contexts']['runtime'] = array('version' => phpversion(), 'name' => 'php'); + if (!$this->breadcrumbs->is_empty()) { $data['breadcrumbs'] = $this->breadcrumbs->fetch(); diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index b8fb70bf9..8719bd293 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1035,6 +1035,22 @@ public function testCaptureMessageWithUserContext() ), $event['user']); } + /** + * @covers Raven_Client::capture + */ + public function testRuntimeContext() + { + $client = new Dummy_Raven_Client(); + + $client->captureMessage('test'); + $events = $client->getSentEvents(); + $this->assertEquals(phpversion(), $events[0]['contexts']['runtime']['version']); + $event = array_pop($events); + $this->assertEquals('php', $event['contexts']['runtime']['name']); + } + + + /** * @covers Raven_Client::captureMessage */ From 56405bd52b192278e9eb46344b15da58c7eacc97 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Wed, 14 Mar 2018 20:33:21 +0100 Subject: [PATCH 2/6] cs --- test/Raven/Tests/ClientTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index 8719bd293..369546fb1 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1049,8 +1049,6 @@ public function testRuntimeContext() $this->assertEquals('php', $event['contexts']['runtime']['name']); } - - /** * @covers Raven_Client::captureMessage */ From d4cba3326d49049591c85830ebb01a3acceabacb Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Wed, 14 Mar 2018 20:49:22 +0100 Subject: [PATCH 3/6] cs fixes --- lib/Raven/Client.php | 2 +- test/Raven/Tests/ClientTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Raven/Client.php b/lib/Raven/Client.php index 1c855ee15..49d897ea9 100644 --- a/lib/Raven/Client.php +++ b/lib/Raven/Client.php @@ -884,8 +884,8 @@ public function capture($data, $stack = null, $vars = null) if (empty($data['site'])) { unset($data['site']); } - $data['contexts']['runtime'] = array('version' => phpversion(), 'name' => 'php'); + $data['contexts']['runtime'] = array('version' => PHP_VERSION, 'name' => 'php'); if (!$this->breadcrumbs->is_empty()) { $data['breadcrumbs'] = $this->breadcrumbs->fetch(); diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index 369546fb1..d029200e8 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1044,7 +1044,7 @@ public function testRuntimeContext() $client->captureMessage('test'); $events = $client->getSentEvents(); - $this->assertEquals(phpversion(), $events[0]['contexts']['runtime']['version']); + $this->assertEquals(PHP_VERSION, $events[0]['contexts']['runtime']['version']); $event = array_pop($events); $this->assertEquals('php', $event['contexts']['runtime']['name']); } From 59909d7c1c2ceb08c3327f3508c5d1be4528dc5a Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Wed, 14 Mar 2018 22:05:12 +0100 Subject: [PATCH 4/6] add more tests --- test/Raven/Tests/ClientTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index d029200e8..792a58de0 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1049,6 +1049,34 @@ public function testRuntimeContext() $this->assertEquals('php', $event['contexts']['runtime']['name']); } + /** + * @covers Raven_Client::capture + */ + public function testRuntimeOnCustomContext() + { + $client = new Dummy_Raven_Client(); + + $data = array('contexts' => array( + 'mine' => array( + 'line' => 1216, + 'stack' => array( + 1, array( + 'foo' => 'bar', + 'level4' => array(array('level5', 'level5 a'), 2), + ), 3 + ), + ), + )); + + $client->captureMessage('test', array(), $data); + + $events = $client->getSentEvents(); + $this->assertEquals(PHP_VERSION, $events[0]['contexts']['runtime']['version']); + $event = array_pop($events); + $this->assertEquals('php', $event['contexts']['runtime']['name']); + $this->assertEquals(1216, $event['contexts']['mine']['line']); + } + /** * @covers Raven_Client::captureMessage */ From 06b2e904f0ad83ff969688601e4565b3ffc8bc90 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Thu, 15 Mar 2018 08:16:02 +0100 Subject: [PATCH 5/6] also support user provided runtime context --- lib/Raven/Client.php | 4 +++- test/Raven/Tests/ClientTest.php | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/Raven/Client.php b/lib/Raven/Client.php index 49d897ea9..93cfc164b 100644 --- a/lib/Raven/Client.php +++ b/lib/Raven/Client.php @@ -885,7 +885,9 @@ public function capture($data, $stack = null, $vars = null) unset($data['site']); } - $data['contexts']['runtime'] = array('version' => PHP_VERSION, 'name' => 'php'); + $existing_runtime_context = isset($data['contexts']['runtime']) ? $data['contexts']['runtime'] : []; + $runtime_context = array('version' => PHP_VERSION, 'name' => 'php'); + $data['contexts']['runtime'] = array_merge($existing_runtime_context, $runtime_context); if (!$this->breadcrumbs->is_empty()) { $data['breadcrumbs'] = $this->breadcrumbs->fetch(); diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index 792a58de0..542cdbe41 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1044,8 +1044,8 @@ public function testRuntimeContext() $client->captureMessage('test'); $events = $client->getSentEvents(); - $this->assertEquals(PHP_VERSION, $events[0]['contexts']['runtime']['version']); $event = array_pop($events); + $this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']); $this->assertEquals('php', $event['contexts']['runtime']['name']); } @@ -1071,12 +1071,40 @@ public function testRuntimeOnCustomContext() $client->captureMessage('test', array(), $data); $events = $client->getSentEvents(); - $this->assertEquals(PHP_VERSION, $events[0]['contexts']['runtime']['version']); $event = array_pop($events); + $this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']); $this->assertEquals('php', $event['contexts']['runtime']['name']); $this->assertEquals(1216, $event['contexts']['mine']['line']); } + /** + * @covers Raven_Client::capture + */ + public function testRuntimeOnExistingRuntimeContext() + { + $client = new Dummy_Raven_Client(); + + $data = array('contexts' => array( + 'runtime' => array( + 'line' => 1216, + 'stack' => array( + 1, array( + 'foo' => 'bar', + 'level4' => array(array('level5', 'level5 a'), 2), + ), 3 + ), + ), + )); + + $client->captureMessage('test', array(), $data); + + $events = $client->getSentEvents(); + $event = array_pop($events); + $this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']); + $this->assertEquals('php', $event['contexts']['runtime']['name']); + $this->assertEquals(1216, $event['contexts']['runtime']['line']); + } + /** * @covers Raven_Client::captureMessage */ From 737b03078a84c55b7d21fa82e7ffe38f4c247a54 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Thu, 15 Mar 2018 10:31:55 +0100 Subject: [PATCH 6/6] address feedback: * fix 5.3 array() vs [] * change array_merge order so [runtime][name|version] is overrideable * add test for overriden runtime --- lib/Raven/Client.php | 4 ++-- test/Raven/Tests/ClientTest.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Raven/Client.php b/lib/Raven/Client.php index 93cfc164b..d547760f0 100644 --- a/lib/Raven/Client.php +++ b/lib/Raven/Client.php @@ -885,9 +885,9 @@ public function capture($data, $stack = null, $vars = null) unset($data['site']); } - $existing_runtime_context = isset($data['contexts']['runtime']) ? $data['contexts']['runtime'] : []; + $existing_runtime_context = isset($data['contexts']['runtime']) ? $data['contexts']['runtime'] : array(); $runtime_context = array('version' => PHP_VERSION, 'name' => 'php'); - $data['contexts']['runtime'] = array_merge($existing_runtime_context, $runtime_context); + $data['contexts']['runtime'] = array_merge($runtime_context, $existing_runtime_context); if (!$this->breadcrumbs->is_empty()) { $data['breadcrumbs'] = $this->breadcrumbs->fetch(); diff --git a/test/Raven/Tests/ClientTest.php b/test/Raven/Tests/ClientTest.php index 542cdbe41..95e114f58 100644 --- a/test/Raven/Tests/ClientTest.php +++ b/test/Raven/Tests/ClientTest.php @@ -1077,6 +1077,28 @@ public function testRuntimeOnCustomContext() $this->assertEquals(1216, $event['contexts']['mine']['line']); } + /** + * @covers Raven_Client::capture + */ + public function testRuntimeOnOverrideRuntimeItself() + { + $client = new Dummy_Raven_Client(); + + $data = array('contexts' => array( + 'runtime' => array( + 'name' => 'sentry', + 'version' => '0.1.1-alpha.1' + ), + )); + + $client->captureMessage('test', array(), $data); + + $events = $client->getSentEvents(); + $event = array_pop($events); + $this->assertEquals('0.1.1-alpha.1', $event['contexts']['runtime']['version']); + $this->assertEquals('sentry', $event['contexts']['runtime']['name']); + } + /** * @covers Raven_Client::capture */