diff --git a/src/Debugger/Connection/ConnectionInterface.php b/src/Debugger/Connection/ConnectionInterface.php index 364fca70e2f3..f8a4113797eb 100644 --- a/src/Debugger/Connection/ConnectionInterface.php +++ b/src/Debugger/Connection/ConnectionInterface.php @@ -53,4 +53,11 @@ public function listBreakpoints(array $args = []); * @param array $args */ public function updateBreakpoint(array $args); + + /** + * Sets a breakpoint. + * + * @param array $args + */ + public function setBreakpoint(array $args); } diff --git a/src/Debugger/Connection/Rest.php b/src/Debugger/Connection/Rest.php index 256024ebbcec..33ffbe559591 100644 --- a/src/Debugger/Connection/Rest.php +++ b/src/Debugger/Connection/Rest.php @@ -86,4 +86,14 @@ public function updateBreakpoint(array $args) { return $this->send('controller.resources.debuggees.resources.breakpoints', 'update', $args); } + + /** + * Sets a breakpoint. + * + * @param array $args + */ + public function setBreakpoint(array $args) + { + return $this->send('debugger.resources.debuggees.resources.breakpoints', 'set', $args); + } } diff --git a/src/Debugger/Debuggee.php b/src/Debugger/Debuggee.php index d79dba00e6fe..041a5e0c30e3 100644 --- a/src/Debugger/Debuggee.php +++ b/src/Debugger/Debuggee.php @@ -280,16 +280,51 @@ public function breakpointsWithWaitToken(array $options = []) * @codingStandardsIgnoreEnd * * @param Breakpoint $breakpoint The modified breakpoint. + * @param array $options [optional] Configuration options. See + * {@see Google\Cloud\Core\RequestWrapper::__construct()} for + * configuration options which apply to all network requests. * @return void * @throws ServiceException */ - public function updateBreakpoint(Breakpoint $breakpoint) + public function updateBreakpoint(Breakpoint $breakpoint, array $options = []) { $this->connection->updateBreakpoint([ 'debuggeeId' => $this->id, 'id' => $breakpoint->id(), 'breakpoint' => $breakpoint - ]); + ] + $options); + } + + /** + * Set a breakpoint for this debuggee. + * + * Example: + * ``` + * $breakpoint = $debuggee->setBreakpoint('DebuggerClient.php', 10); + * ``` + * + * @codingStandardsIgnoreStart + * @see https://cloud.google.com/debugger/api/reference/rest/v2/debugger.debuggees.breakpoints/set Breakpoint set API documentation. + * @codingStandardsIgnoreEnd + * + * @param string $path Path to the source file. + * @param int $line Line within the source file. + * @param array $options [optional] Array of Breakpoint constructor arguments. See + * {@see Google\Cloud\Debugger\Breakpoint::__construct()} for + * configuration details. See + * {@see Google\Cloud\Core\RequestWrapper::__construct()} for + * configuration options which apply to all network requests. + */ + public function setBreakpoint($path, $line, array $options = []) + { + $resp = $this->connection->setBreakpoint([ + 'debuggeeId' => $this->id, + 'location' => [ + 'path' => $path, + 'line' => $line + ] + ] + $options); + return new Breakpoint($resp['breakpoint']); } /** @@ -305,13 +340,16 @@ public function updateBreakpoint(Breakpoint $breakpoint) * @codingStandardsIgnoreEnd * * @param Breakpoint[] $breakpoints The modified breakpoints. + * @param array $options [optional] Configuration options. See + * {@see Google\Cloud\Core\RequestWrapper::__construct()} for + * configuration options which apply to all network requests. * @return void * @throws ServiceException */ - public function updateBreakpointBatch(array $breakpoints) + public function updateBreakpointBatch(array $breakpoints, array $options = []) { foreach ($breakpoints as $breakpoint) { - $this->updateBreakpoint($breakpoint); + $this->updateBreakpoint($breakpoint, $options); } } diff --git a/tests/snippets/Debugger/DebuggeeTest.php b/tests/snippets/Debugger/DebuggeeTest.php index 3766ffed7495..086fde3c64a2 100644 --- a/tests/snippets/Debugger/DebuggeeTest.php +++ b/tests/snippets/Debugger/DebuggeeTest.php @@ -132,4 +132,15 @@ public function testUpdateBreakpointBatch() $snippet->addLocal('breakpoint2', $breakpoint2); $snippet->invoke('debuggee'); } + + public function testSetBreakpoint() + { + $this->connection->setBreakpoint(Argument::any())->willReturn(['breakpoint' => ['id' => 'breakpoint1']])->shouldBeCalled(); + $debuggee = new Debuggee($this->connection->reveal(), ['project' => 'project']); + $snippet = $this->snippetFromMethod(Debuggee::class, 'setBreakpoint'); + $snippet->addLocal('debuggee', $debuggee); + $resp = $snippet->invoke('breakpoint'); + $breakpoint = $resp->returnVal(); + $this->assertInstanceOf(Breakpoint::class, $breakpoint); + } } diff --git a/tests/system/Debugger/BasicTest.php b/tests/system/Debugger/BasicTest.php index ac16f2068c7a..a486311c10ce 100644 --- a/tests/system/Debugger/BasicTest.php +++ b/tests/system/Debugger/BasicTest.php @@ -72,15 +72,9 @@ public function testRegisterSetUseBreakpoint($transport) $this->assertNotEmpty($debuggee->id()); // Set a breakpoint - $client = new GapicClient(); - $breakpoint = new GapicBreakpoint(); - $location = new SourceLocation(); - $location->setPath('tests/system/Debugger/BasicTest.php'); - $location->setLine(__LINE__); - $breakpoint->setLocation($location); - $resp = $client->setBreakpoint($debuggee->id(), $breakpoint, 'google.com/php/v0.1'); - $bp = $resp->getBreakpoint(); - $this->assertNotEmpty($bp->getId()); + $breakpoint = $debuggee->setBreakpoint('tests/system/Debugger/BasicTest.php', __LINE__); + $this->assertInstanceOf(Breakpoint::class, $breakpoint); + $this->assertNotNull($breakpoint->location()); // Fetch the list of breakpoints $breakpoints = $debuggee->breakpoints(); @@ -91,7 +85,6 @@ public function testRegisterSetUseBreakpoint($transport) ); // fulfill a breakpoint - $breakpoint = $breakpoints[0]; $this->assertTrue($breakpoint->resolveLocation()); $breakpoint->finalize(); $debuggee->updateBreakpoint($breakpoint); diff --git a/tests/unit/Debugger/Connection/RestTest.php b/tests/unit/Debugger/Connection/RestTest.php index 4058c88d8498..223029cb95a3 100644 --- a/tests/unit/Debugger/Connection/RestTest.php +++ b/tests/unit/Debugger/Connection/RestTest.php @@ -75,7 +75,8 @@ public function methodProvider() ['listDebuggees'], ['registerDebuggee'], ['listBreakpoints'], - ['updateBreakpoint'] + ['updateBreakpoint'], + ['setBreakpoint'] ]; } } diff --git a/tests/unit/Debugger/DebuggeeTest.php b/tests/unit/Debugger/DebuggeeTest.php index c3822f111be8..304a8c17a730 100644 --- a/tests/unit/Debugger/DebuggeeTest.php +++ b/tests/unit/Debugger/DebuggeeTest.php @@ -102,6 +102,20 @@ public function testUpdatesBreakpoint() $debuggee->updateBreakpoint($breakpoint); } + public function testSetsBreakpoint() + { + $this->connection->setBreakpoint(Argument::that(function ($args) { + return $args['debuggeeId'] == 'debuggee1' && + $args['location']['path'] == '/path/to/file.php' && + $args['location']['line'] == 10; + }))->willReturn(['breakpoint' => ['id' => 'breakpoint1']]); + $debuggee = new Debuggee($this->connection->reveal(), ['id' => 'debuggee1', 'project' => 'project1']); + + $breakpoint = $debuggee->setBreakpoint('/path/to/file.php', 10); + $this->assertInstanceOf(Breakpoint::class, $breakpoint); + $this->assertEquals('breakpoint1', $breakpoint->id()); + } + // Debug agents should populate both sourceContexts and extSourceContexts. public function testProvidesDeprecatedSourceContext() {