Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: and email domain cannot contain more than 63 chars allow to configure the number of iterations via an env var diversify the tested combinations add randomize set
- Loading branch information
Showing
8 changed files
with
158 additions
and
4 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
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
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
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,43 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\BlackBox\Set; | ||
|
||
use Innmind\BlackBox\Set; | ||
|
||
/** | ||
* Use this set to prevent iterating over all possible combinations of a composite set | ||
* | ||
* It will allow to test more diverse combinations for a given set | ||
*/ | ||
final class Randomize implements Set | ||
{ | ||
private Set $set; | ||
private int $size; | ||
|
||
public function __construct(Set $set) | ||
{ | ||
$this->set = $set; | ||
$this->size = 100; | ||
} | ||
|
||
public function take(int $size): Set | ||
{ | ||
$self = clone $this; | ||
$self->size = $size; | ||
|
||
return $self; | ||
} | ||
|
||
public function filter(callable $predicate): Set | ||
{ | ||
return new self($this->set->filter($predicate)); | ||
} | ||
|
||
public function values(): \Generator | ||
{ | ||
for ($i = 0; $i < $this->size; $i++) { | ||
yield $this->set->values()->current(); | ||
} | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Tests\Innmind\BlackBox\Set; | ||
|
||
use Innmind\BlackBox\{ | ||
Set\Randomize, | ||
Set, | ||
Set\Value, | ||
}; | ||
|
||
class RandomizeTest extends TestCase | ||
{ | ||
public function testInterface() | ||
{ | ||
$this->assertInstanceOf( | ||
Set::class, | ||
new Randomize($this->createMock(Set::class)), | ||
); | ||
} | ||
|
||
public function testGenerate100ValuesByDefault() | ||
{ | ||
$set = new Randomize( | ||
Set\Elements::of(new \stdClass, 42), | ||
); | ||
|
||
$this->assertCount(100, $this->unwrap($set->values())); | ||
} | ||
|
||
public function testTake() | ||
{ | ||
$set1 = new Randomize( | ||
Set\Elements::of(new \stdClass, 42), | ||
); | ||
$set2 = $set1->take(50); | ||
|
||
$this->assertInstanceOf(Set::class, $set2); | ||
$this->assertNotSame($set2, $set1); | ||
$this->assertCount(100, $this->unwrap($set1->values())); | ||
$this->assertCount(50, $this->unwrap($set2->values())); | ||
} | ||
|
||
public function testFilter() | ||
{ | ||
$set1 = new Randomize( | ||
Set\Elements::of('foo', 42), | ||
); | ||
$set2 = $set1->filter(fn($v) => \is_int($v)); | ||
|
||
$this->assertInstanceOf(Set::class, $set2); | ||
$this->assertNotSame($set2, $set1); | ||
$this->assertCount(100, $this->unwrap($set1->values())); | ||
$this->assertCount(100, $this->unwrap($set2->values())); | ||
$this->assertCount( | ||
2, | ||
\array_unique($this->unwrap($set1->values())), | ||
); | ||
$this->assertCount( | ||
1, | ||
\array_unique($this->unwrap($set2->values())), | ||
); | ||
} | ||
|
||
public function testAlwaysTakeTheFirstValueGeneratedByTheUnderlyingSet() | ||
{ | ||
$set = new Randomize( | ||
$inner = $this->createMock(Set::class), | ||
); | ||
$expected = Value::immutable(new \stdClass); | ||
$inner | ||
->expects($this->exactly(100)) | ||
->method('values') | ||
->willReturn((fn() => yield $expected)()); | ||
|
||
foreach ($set->values() as $value) { | ||
$this->assertSame($expected, $value); | ||
} | ||
} | ||
} |