Skip to content

Commit

Permalink
Added Debug strategy and accompanying test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Jun 7, 2017
1 parent d4f9b40 commit 1642443
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Expand Up @@ -22,9 +22,15 @@ Contents
1. [Strategies](#strategies)
1. [Practical example](#practical-example)
1. [Strategy reference](#strategy-reference)

[Fetchers](#fetchers)

1. [Copy](#copy)
1. [CopyContext](#copycontext)
1. [CopyKey](#copykey)

[Augmenters](#augmenters)

1. [Callback](#callback)
1. [Collection](#collection)
1. [Context](#context)
Expand All @@ -42,6 +48,10 @@ Contents
1. [Type](#type)
1. [Unique](#unique)
1. [Walk](#walk)

[Others](#others)

1. [Debug](#debug)
1. [Requirements](#requirements)
1. [Limitations](#limitations)
1. [Testing](#testing)
Expand Down Expand Up @@ -307,6 +317,10 @@ The following strategies ship with Mapper and provide a suite of commonly used f
- [Unique](#unique) – Creates a collection of unique values by removing duplicates.
- [Walk](#walk) – Walks a nested structure to the specified element in the same manner as `Copy`.

#### Others

- [Debug](#debug) – Debugs a mapping by breaking the debugger wherever this strategy is inserted.

### Copy

Copy copies a portion of the input data with support for nested structures.
Expand Down Expand Up @@ -843,7 +857,7 @@ Creates a collection of unique values by removing duplicates.
Unique(Strategy|Mapping|array|mixed $collection)
```

1. `$collection` – Expression the maps to an array.
1. `$collection` – Expression that maps to an array.

#### Example

Expand Down Expand Up @@ -886,6 +900,20 @@ Walk(Strategy|Mapping|array|mixed $expression, array|string $path)

> 123
### Debug

Debugs a mapping by breaking the debugger wherever this strategy is inserted. The specified expression will be mapped immediately before triggering the breakpoint. The debugger should see the current data, context and mapped expression.

Currently only the [Xdebug][Xdebug] debugger is supported.

#### Signature

```php
Debug(Strategy|Mapping|array|mixed $expression)
```

1. `$expression` – Expression to delegate to `Mapper`.

Requirements
------------

Expand Down Expand Up @@ -916,3 +944,5 @@ in this document can be found in `DocumentationTest`.
[Coverage image]: https://coveralls.io/repos/ScriptFUSION/Mapper/badge.svg "Test coverage"
[Style]: https://styleci.io/repos/59734709
[Style image]: https://styleci.io/repos/59734709/shield?style=flat "Code style"

[Xdebug]: https://xdebug.org
30 changes: 30 additions & 0 deletions src/Strategy/Debug.php
@@ -0,0 +1,30 @@
<?php
namespace ScriptFUSION\Mapper\Strategy;

/**
* Debugs a mapping by breaking the debugger wherever this strategy is inserted.
*/
final class Debug extends Delegate
{
public function __construct($expression = null)
{
parent::__construct($expression);
}

public function __invoke($data, $context = null)
{
$mapped = parent::__invoke($data, $context);

self::debug($data, $context, $mapped);

return $mapped;
}

// Although all these parameters are unused, it is helpful to have relevant data in the current stack frame.
private static function debug($data, $context, $mapped)
{
if (function_exists('xdebug_break')) {
xdebug_break();
}
}
}
50 changes: 50 additions & 0 deletions test/Integration/Mapper/Strategy/DebugTest.php
@@ -0,0 +1,50 @@
<?php
namespace ScriptFUSIONTest\Integration\Mapper\Strategy {

use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Copy;
use ScriptFUSION\Mapper\Strategy\Debug;

final class DebugTest extends \PHPUnit_Framework_TestCase
{
public static $debugged;

protected function setUp()
{
self::$debugged = false;
}

/**
* Tests that expressions are delegated to Mapper.
*/
public function testDelegation()
{
$debug = (new Debug(new Copy(0)))->setMapper(new Mapper);

self::assertSame($record = 'foo', $debug([$record]));
}

/**
* Tests that the Xdebug breakpoint is called.
*/
public function testXdebug()
{
$debug = (new Debug)->setMapper(new Mapper);

$debug([]);

self::assertTrue(self::$debugged);
}
}
}

// Mock debugging functions.
namespace ScriptFUSION\Mapper\Strategy {

use ScriptFUSIONTest\Integration\Mapper\Strategy\DebugTest;

function xdebug_break()
{
DebugTest::$debugged = true;
}
}

0 comments on commit 1642443

Please sign in to comment.