-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/* | ||
* This file is part of the jojo1981/data-resolver package | ||
* | ||
* Copyright (c) 2020 Joost Nijhuis <jnijhuis81@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed in the root of the source code | ||
*/ | ||
namespace Jojo1981\DataResolver\Builder\Extractor; | ||
|
||
use Jojo1981\DataResolver\Builder\ExtractorBuilderInterface; | ||
use Jojo1981\DataResolver\Extractor\CallbackExtractor; | ||
use Jojo1981\DataResolver\Extractor\ExtractorInterface; | ||
|
||
/** | ||
* @package Jojo1981\DataResolver\Builder\Extractor | ||
*/ | ||
class CallbackExtractorBuilder implements ExtractorBuilderInterface | ||
{ | ||
/** @var callable */ | ||
private $callback; | ||
|
||
/** | ||
* @param callable $callback | ||
*/ | ||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* @return CallbackExtractor | ||
*/ | ||
public function build(): ExtractorInterface | ||
{ | ||
return new CallbackExtractor($this->callback); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
/* | ||
* This file is part of the jojo1981/data-resolver package | ||
* | ||
* Copyright (c) 2020 Joost Nijhuis <jnijhuis81@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed in the root of the source code | ||
*/ | ||
namespace Jojo1981\DataResolver\Extractor; | ||
|
||
use Jojo1981\DataResolver\Resolver\Context; | ||
use function call_user_func; | ||
|
||
/** | ||
* @package Jojo1981\DataResolver\Extractor | ||
*/ | ||
class CallbackExtractor implements ExtractorInterface | ||
{ | ||
/** @var callable */ | ||
private $callback; | ||
|
||
/** | ||
* @param callable $callback | ||
*/ | ||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* @param Context $context | ||
* @return mixed | ||
*/ | ||
public function extract(Context $context) | ||
{ | ||
return call_user_func($this->callback, $context->copy()->getData()); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
/* | ||
* This file is part of the jojo1981/data-resolver package | ||
* | ||
* Copyright (c) 2020 Joost Nijhuis <jnijhuis81@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed in the root of the source code | ||
*/ | ||
namespace tests\Jojo1981\DataResolver\Extractor; | ||
|
||
use Jojo1981\DataResolver\Extractor\CallbackExtractor; | ||
use Jojo1981\DataResolver\Resolver\Context; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Exception\Doubler\ClassNotFoundException; | ||
use Prophecy\Exception\Doubler\DoubleException; | ||
use Prophecy\Exception\Doubler\InterfaceNotFoundException; | ||
use Prophecy\Exception\Prophecy\ObjectProphecyException; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use SebastianBergmann\RecursionContext\InvalidArgumentException; | ||
|
||
/** | ||
* @package tests\Jojo1981\DataResolver\Extractor | ||
*/ | ||
class CallbackExtractorTest extends TestCase | ||
{ | ||
/** @var ObjectProphecy|Context */ | ||
private $originalContext; | ||
|
||
/** @var ObjectProphecy|Context */ | ||
private $copiedContext; | ||
|
||
/** | ||
* @throws DoubleException | ||
* @throws InterfaceNotFoundException | ||
* @throws ClassNotFoundException | ||
* @return void | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->originalContext = $this->prophesize(Context::class); | ||
$this->copiedContext = $this->prophesize(Context::class); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
* | ||
* @throws InvalidArgumentException | ||
* @throws ObjectProphecyException | ||
* @throws ExpectationFailedException | ||
* @return void | ||
*/ | ||
public function extractShouldReturnTheResultFromTheCallback(): void | ||
{ | ||
$this->originalContext->copy()->willReturn($this->copiedContext)->shouldBeCalled(); | ||
$this->originalContext->getData()->shouldNotBeCalled(); | ||
$this->copiedContext->getData()->willReturn('my-data')->shouldBeCalledOnce(); | ||
|
||
$invokerCounter = 0; | ||
$callback = function (string $item) use (&$invokerCounter): string { | ||
$this->assertEquals('my-data', $item); | ||
$invokerCounter++; | ||
|
||
return 'newResult'; | ||
}; | ||
$this->assertEquals('newResult', (new CallbackExtractor($callback))->extract($this->originalContext->reveal())); | ||
$this->assertEquals(1, $invokerCounter); | ||
} | ||
} |