-
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.
Merge pull request #126 from MaxGoryunov/109
109
- Loading branch information
Showing
4 changed files
with
139 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,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; | ||
} |
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,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) | ||
); | ||
} | ||
} |
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,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) | ||
); | ||
} | ||
} |
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