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
1 parent
564623f
commit d49bc88
Showing
2 changed files
with
91 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,56 @@ | ||
<?php | ||
|
||
namespace MaxGoryunov\SavingIterator\Fakes; | ||
|
||
use Closure; | ||
|
||
/** | ||
* Repeats some process several times and returns its result. | ||
* @template X subject type | ||
* @template Y result type | ||
*/ | ||
final class Repeat | ||
{ | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @phpstan-param T $subject | ||
* @phpstan-param Closure(T): Y $context | ||
* @param mixed $subject element to be processed. | ||
* @param Closure $context context for the element. | ||
*/ | ||
public function __construct( | ||
/** | ||
* Element to be processed. | ||
* | ||
* @phpstan-var X | ||
* @var mixed | ||
*/ | ||
private mixed $subject, | ||
|
||
/** | ||
* Context for the element. | ||
* | ||
* @phpstan-var Closure(X): Y | ||
* @var Closure | ||
*/ | ||
private Closure $context | ||
) { | ||
} | ||
|
||
/** | ||
* Returns several results of applying context to subject. | ||
* | ||
* @param int $count | ||
* @phpstan-return Y[] | ||
* @return mixed[] | ||
*/ | ||
public function times(int $count): array | ||
{ | ||
return array_map( | ||
$this->context, | ||
array_fill(0, $count, $this->subject) | ||
); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace MaxGoryunov\SavingIterator\Tests\Fakes; | ||
|
||
use MaxGoryunov\SavingIterator\Fakes\Repeat; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
/** | ||
* @covers MaxGoryunov\SavingIterator\Fakes\Repeat | ||
*/ | ||
final class RepeatTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::times | ||
* | ||
* @small | ||
* | ||
* @return void | ||
*/ | ||
public function testReturnsExactAmountOfResults(): void | ||
{ | ||
$times = 3; | ||
$this->assertCount( | ||
$times, | ||
(new Repeat( | ||
new stdClass(), | ||
fn (stdClass $std) => $std->name = "Jane" | ||
)) | ||
->times(3) | ||
); | ||
} | ||
} |