Skip to content

Commit

Permalink
#109: added Repeat class
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGoryunov committed Sep 21, 2021
1 parent 564623f commit d49bc88
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
56 changes: 56 additions & 0 deletions fakes/Repeat.php
@@ -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)
);
}
}
35 changes: 35 additions & 0 deletions tests/fakes/RepeatTest.php
@@ -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)
);
}
}

0 comments on commit d49bc88

Please sign in to comment.