From d345456dc01f29c87381ee6903664aab1f8c9ecf Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Fri, 16 Aug 2019 14:14:12 -0400 Subject: [PATCH] fix: Prevent setting null debuggee ID (#2252) * fix: Prevent setting null debuggee ID * add test coverage, reorder constructor property settings --- Debugger/src/Debuggee.php | 19 ++++++++++++------- Debugger/tests/Unit/DebuggeeTest.php | 13 ++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Debugger/src/Debuggee.php b/Debugger/src/Debuggee.php index f77dfbce653a..9aa963b82715 100644 --- a/Debugger/src/Debuggee.php +++ b/Debugger/src/Debuggee.php @@ -149,18 +149,19 @@ public function __construct(ConnectionInterface $connection, array $info = []) 'extSourceContexts' => [], 'uniquifier' => null, 'description' => null, - 'labels' => [] + 'labels' => [], + 'project' => null ]; $this->id = $info['id']; - $this->project = $info['project']; - $this->uniquifier = $info['uniquifier']; - $this->description = $info['description']; - $this->status = $info['status']; - $this->agentVersion = $info['agentVersion']; $this->isInactive = $info['isInactive']; + $this->agentVersion = $info['agentVersion']; + $this->status = $info['status']; $this->extSourceContexts = $info['extSourceContexts']; + $this->uniquifier = $info['uniquifier']; + $this->description = $info['description']; $this->labels = $info['labels']; + $this->project = $info['project']; } /** @@ -367,7 +368,6 @@ public function updateBreakpointBatch(array $breakpoints, array $options = []) public function info() { $info = [ - 'id' => $this->id, 'project' => $this->project, 'uniquifier' => $this->uniquifier, 'description' => $this->description, @@ -384,6 +384,11 @@ public function info() }, $this->extSourceContexts) ]; + // Do not include the ID unless it is set. + if ($this->id) { + $info['id'] = $this->id; + } + if ($this->status instanceof StatusMessage) { $info['status'] = $this->status->info(); } elseif ($this->status) { diff --git a/Debugger/tests/Unit/DebuggeeTest.php b/Debugger/tests/Unit/DebuggeeTest.php index d23ad1e270ec..4e97eb8b4e9c 100644 --- a/Debugger/tests/Unit/DebuggeeTest.php +++ b/Debugger/tests/Unit/DebuggeeTest.php @@ -139,7 +139,7 @@ public function testProvidesDeprecatedSourceContext() public function testRegisterSetsDebuggeeId() { $this->connection->registerDebuggee(Argument::that(function ($args) { - return $args['debuggee']['id'] == null; + return !isset($args['debuggee']['id']); }), Argument::any())->willReturn([ 'debuggee' => [ 'id' => 'debuggee1' @@ -167,4 +167,15 @@ public function testDebuggeeExtendedSourceContextSerialization() $this->assertArrayHasKey('sourceContexts', $info); $this->assertCount(1, $info['sourceContexts']); } + + public function testDebuggeeOmitsId() + { + $debuggee = new Debuggee($this->connection->reveal(), []); + $this->assertArrayNotHasKey('id', $debuggee->info()); + + $debuggee = new Debuggee($this->connection->reveal(), [ + 'id' => 'foo' + ]); + $this->assertArrayHasKey('id', $debuggee->info()); + } }