-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Debug strategy and accompanying test.
- Loading branch information
Paul
committed
Jun 7, 2017
1 parent
d4f9b40
commit 361843e
Showing
3 changed files
with
126 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |