Skip to content

Commit 361843e

Browse files
author
Paul
committed
Added Debug strategy and accompanying test.
1 parent d4f9b40 commit 361843e

File tree

3 files changed

+126
-22
lines changed

3 files changed

+126
-22
lines changed

README.md

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ Contents
2222
1. [Strategies](#strategies)
2323
1. [Practical example](#practical-example)
2424
1. [Strategy reference](#strategy-reference)
25-
1. [Copy](#copy)
26-
1. [CopyContext](#copycontext)
27-
1. [CopyKey](#copykey)
28-
1. [Callback](#callback)
29-
1. [Collection](#collection)
30-
1. [Context](#context)
31-
1. [Either](#either)
32-
1. [Filter](#filter)
33-
1. [Flatten](#flatten)
34-
1. [IfElse](#ifelse)
35-
1. [IfExists](#ifexists)
36-
1. [Join](#join)
37-
1. [Merge](#merge)
38-
1. [TakeFirst](#takefirst)
39-
1. [ToList](#tolist)
40-
1. [Translate](#translate)
41-
1. [TryCatch](#trycatch)
42-
1. [Type](#type)
43-
1. [Unique](#unique)
44-
1. [Walk](#walk)
25+
1. [Fetchers](#fetchers)
26+
1. [Copy](#copy)
27+
1. [CopyContext](#copycontext)
28+
1. [CopyKey](#copykey)
29+
1. [Augmenters](#augmenters)
30+
1. [Callback](#callback)
31+
1. [Collection](#collection)
32+
1. [Context](#context)
33+
1. [Either](#either)
34+
1. [Filter](#filter)
35+
1. [Flatten](#flatten)
36+
1. [IfElse](#ifelse)
37+
1. [IfExists](#ifexists)
38+
1. [Join](#join)
39+
1. [Merge](#merge)
40+
1. [TakeFirst](#takefirst)
41+
1. [ToList](#tolist)
42+
1. [Translate](#translate)
43+
1. [TryCatch](#trycatch)
44+
1. [Type](#type)
45+
1. [Unique](#unique)
46+
1. [Walk](#walk)
47+
1. [Others](#others)
48+
1. [Debug](#debug)
4549
1. [Requirements](#requirements)
4650
1. [Limitations](#limitations)
4751
1. [Testing](#testing)
@@ -307,6 +311,10 @@ The following strategies ship with Mapper and provide a suite of commonly used f
307311
- [Unique](#unique) – Creates a collection of unique values by removing duplicates.
308312
- [Walk](#walk) – Walks a nested structure to the specified element in the same manner as `Copy`.
309313

314+
#### Others
315+
316+
- [Debug](#debug) – Debugs a mapping by breaking the debugger wherever this strategy is inserted.
317+
310318
### Copy
311319

312320
Copy copies a portion of the input data with support for nested structures.
@@ -329,7 +337,7 @@ $data = [
329337
'bar' => 123,
330338
],
331339
];
332-
340+
333341
(new Mapper)->map($data, new Copy('foo'));
334342
```
335343

@@ -843,7 +851,7 @@ Creates a collection of unique values by removing duplicates.
843851
Unique(Strategy|Mapping|array|mixed $collection)
844852
```
845853

846-
1. `$collection` – Expression the maps to an array.
854+
1. `$collection` – Expression that maps to an array.
847855

848856
#### Example
849857

@@ -886,6 +894,20 @@ Walk(Strategy|Mapping|array|mixed $expression, array|string $path)
886894

887895
> 123
888896
897+
### Debug
898+
899+
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.
900+
901+
Currently only the [Xdebug][Xdebug] debugger is supported.
902+
903+
#### Signature
904+
905+
```php
906+
Debug(Strategy|Mapping|array|mixed $expression)
907+
```
908+
909+
1. `$expression` – Expression to delegate to `Mapper`.
910+
889911
Requirements
890912
------------
891913

@@ -916,3 +938,5 @@ in this document can be found in `DocumentationTest`.
916938
[Coverage image]: https://coveralls.io/repos/ScriptFUSION/Mapper/badge.svg "Test coverage"
917939
[Style]: https://styleci.io/repos/59734709
918940
[Style image]: https://styleci.io/repos/59734709/shield?style=flat "Code style"
941+
942+
[Xdebug]: https://xdebug.org

src/Strategy/Debug.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace ScriptFUSION\Mapper\Strategy;
3+
4+
/**
5+
* Debugs a mapping by breaking the debugger wherever this strategy is inserted.
6+
*/
7+
final class Debug extends Delegate
8+
{
9+
public function __construct($expression = null)
10+
{
11+
parent::__construct($expression);
12+
}
13+
14+
public function __invoke($data, $context = null)
15+
{
16+
$mapped = parent::__invoke($data, $context);
17+
18+
self::debug($data, $context, $mapped);
19+
20+
return $mapped;
21+
}
22+
23+
// Although all these parameters are unused, it is helpful to have relevant data in the current stack frame.
24+
private static function debug($data, $context, $mapped)
25+
{
26+
if (function_exists('xdebug_break')) {
27+
xdebug_break();
28+
}
29+
}
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace ScriptFUSIONTest\Integration\Mapper\Strategy {
3+
4+
use ScriptFUSION\Mapper\Mapper;
5+
use ScriptFUSION\Mapper\Strategy\Copy;
6+
use ScriptFUSION\Mapper\Strategy\Debug;
7+
8+
final class DebugTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public static $debugged;
11+
12+
protected function setUp()
13+
{
14+
self::$debugged = false;
15+
}
16+
17+
/**
18+
* Tests that expressions are delegated to Mapper.
19+
*/
20+
public function testDelegation()
21+
{
22+
$debug = (new Debug(new Copy(0)))->setMapper(new Mapper);
23+
24+
self::assertSame($record = 'foo', $debug([$record]));
25+
}
26+
27+
/**
28+
* Tests that the Xdebug breakpoint is called.
29+
*/
30+
public function testXdebug()
31+
{
32+
$debug = (new Debug)->setMapper(new Mapper);
33+
34+
$debug([]);
35+
36+
self::assertTrue(self::$debugged);
37+
}
38+
}
39+
}
40+
41+
// Mock debugging functions.
42+
namespace ScriptFUSION\Mapper\Strategy {
43+
44+
use ScriptFUSIONTest\Integration\Mapper\Strategy\DebugTest;
45+
46+
function xdebug_break()
47+
{
48+
DebugTest::$debugged = true;
49+
}
50+
}

0 commit comments

Comments
 (0)