Skip to content

Commit

Permalink
Merge pull request #126 from MaxGoryunov/109
Browse files Browse the repository at this point in the history
109
  • Loading branch information
MaxGoryunov committed Sep 29, 2021
2 parents b970844 + e271676 commit 328dfed
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
20 changes: 20 additions & 0 deletions fakes/Repetition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MaxGoryunov\SavingIterator\Fakes;

/**
* Some repeating process.
* @template T result type
*/
interface Repetition
{
/**
* Returns an array of results.
* Number of values in the result array is the same as $count.
*
* @param int $count
* @phpstan-return T[]
* @return mixed[]
*/
public function times(int $count): array;
}
58 changes: 58 additions & 0 deletions fakes/RepetitionEnvelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace MaxGoryunov\SavingIterator\Fakes;

use Closure;

/**
* Repeats some process several times and returns its result.
* @todo #109:30min Add Rewinding repetition or repetition which converts
* iterator to array for rewind tests and use it in iterator tests.
* @template X subject type
* @template Y result type
* @implements Repetition<Y>
*/
abstract class RepetitionEnvelope implements Repetition
{
/**
* 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[]
*/
final public function times(int $count): array
{
return array_map(
$this->context,
array_fill_keys(range(0, $count - 1), $this->subject)
);
}
}
60 changes: 60 additions & 0 deletions tests/fakes/RepetitionEnvelopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace MaxGoryunov\SavingIterator\Tests\Fakes;

use ArrayIterator;
use Iterator;
use MaxGoryunov\SavingIterator\Fakes\RepetitionEnvelope;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* @covers MaxGoryunov\SavingIterator\Fakes\RepetitionEnvelope
*/
final class RepetitionEnvelopeTest extends TestCase
{
/**
* @small
*
* @return void
*/
public function testReturnsExactAmountOfResults(): void
{
$times = 3;
$this->assertCount(
$times,
$this->getMockForAbstractClass(
RepetitionEnvelope::class,
[
new stdClass(),
fn (stdClass $std) => $std->name = "Jane"
]
)
->times($times)
);
}

/**
* @small
*
* @return void
*/
public function testReturnsActualResults(): void
{
$source = new ArrayIterator([4, 76, 28, 83, 95, 9, 27]);
$this->assertEquals(
[
iterator_to_array($source),
iterator_to_array($source)
],
$this->getMockForAbstractClass(
RepetitionEnvelope::class,
[
$source,
fn (Iterator $source): array => iterator_to_array($source)
]
)
->times(2)
);
}
}
1 change: 1 addition & 0 deletions tests/src/SavingIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InfiniteIterator;
use Iterator;
use LimitIterator;
use MaxGoryunov\SavingIterator\Fakes\Repeat;
use MaxGoryunov\SavingIterator\Fakes\The;
use MaxGoryunov\SavingIterator\Src\ArrayAddingIterator;
use MaxGoryunov\SavingIterator\Src\BsCount;
Expand Down

0 comments on commit 328dfed

Please sign in to comment.