Skip to content

Commit

Permalink
ShuffleGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyBel committed Sep 8, 2023
1 parent 0e79bcd commit a236a24
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 44 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@ composer require --dev sergey-bel/phpinvariant
See [examples](https://github.com/SergeyBel/phpinvariant/tree/main/invariants/examples)


## Provider
Provider is a main class to generate random data

This example will generate a alphabetic string with length between 5 and 10 (inclusive)
```php
$this->provider
->string(5, 10)
->alphabetic()
->get();
```

## Finish conditions
Finish conditions are used to determine when to end the check execution
Finish condition is specified by method attribute

## Example

```php
Expand All @@ -55,6 +40,23 @@ class IntegerInvariant extends BaseInvariant
}
}
```

## Provider
Provider is a main class to generate random data

This example will generate a alphabetic string with length between 5 and 10 (inclusive)
```php
$this->provider
->string(5, 10)
->alphabetic()
->get();
```

## Finish conditions
Finish conditions are used to determine when to end the check execution
Finish condition is specified by method attribute


## Command Line Options
`--path`
Specifies directory with Check classes
Expand Down
3 changes: 2 additions & 1 deletion invariants/examples/generators/Arrays/ArrayInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

class ArrayInvariant extends BaseInvariant
{
#[FinishRuns(5)]
#[FinishRuns(2)]
public function checkArray()
{
$elements = $this->provider->array(3, $this->provider->string(5, 10)->ascii())->get();

$this->assertCount($elements, 3);
foreach ($elements as $element) {
$this->assertTrue(is_string($element));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class AssociativeArrayInvariant extends BaseInvariant
{
#[FinishRuns(5)]
#[FinishRuns(2)]
public function checkAssociativeArray()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ElementsInvariant extends BaseInvariant
public function checkElements()
{
$elements = $this->provider->elements([10, 20, 30, 40], 2)->get();

$this->assertCount($elements, 2);
foreach ($elements as $element) {
$this->assertTrue(in_array($element, [10, 20, 30, 40]));
Expand Down
22 changes: 22 additions & 0 deletions invariants/examples/generators/Arrays/ShuffleInvariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace PhpInvariant\Invariants\examples\generators\Arrays;

use PhpInvariant\BaseInvariant\BaseInvariant;
use PhpInvariant\Finish\FinishRuns;

class ShuffleInvariant extends BaseInvariant
{
#[FinishRuns(2)]
public function checkShuffle()
{
$data = $this->provider->shuffle(['a', 'b', 'c'])->get();

$this->assertCount($data, 3);
$this->assertInArray('a', $data);
$this->assertInArray('b', $data);
$this->assertInArray('c', $data);

}

}
3 changes: 2 additions & 1 deletion invariants/examples/generators/Boolean/BooleanInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

class BooleanInvariant extends BaseInvariant
{
#[FinishRuns(5)]
#[FinishRuns(2)]
public function checkFloat()
{
$x = $this->provider->boolean()->get();

$this->assertTrue(is_bool($x));
}
}
4 changes: 2 additions & 2 deletions invariants/examples/generators/Combine/CombineInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class CombineInvariant extends BaseInvariant
{
#[FinishRuns(5)]
public function checkFromArray()
#[FinishRuns(2)]
public function checkCombine()
{
$value = $this->provider->combine(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

class DateTimeInvariant extends BaseInvariant
{
#[FinishRuns(5)]
#[FinishRuns(2)]
public function checkDateTime()
{
$time = $this->provider->datetime()->get();

$this->assertLessOrEqual($time->getTimestamp(), time());
}
}
1 change: 1 addition & 0 deletions invariants/examples/generators/Float/FloatInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class FloatInvariant extends BaseInvariant
public function checkFloat()
{
$x = $this->provider->float(1, 10)->decimals(2)->get();

$this->assertTrue(is_float($x));
$this->assertLessOrEqual($x, 10);
$this->assertGreaterOrEqual($x, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use PhpInvariant\BaseInvariant\BaseInvariant;
use PhpInvariant\Finish\FinishRuns;

class PrintableStringInvariant extends BaseInvariant
class AlphanumericStringInvariant extends BaseInvariant
{
#[FinishRuns(2)]
public function checkString()
{
$str = $this->provider
->string(5, 10)
->alphabetic()
->alphanumeric()
->get();

$this->assertTrue(is_string($str));
$this->assertLessOrEqual(strlen($str), 10);
$this->assertGreaterOrEqual(strlen($str), 5);
Expand Down
1 change: 1 addition & 0 deletions invariants/examples/generators/String/StringInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class StringInvariant extends BaseInvariant
public function checkString()
{
$str = $this->provider->string(5, 10)->dictionary(['a', 'b', 'c'])->get();

$this->assertTrue(is_string($str));
$this->assertLessOrEqual(strlen($str), 10);
$this->assertGreaterOrEqual(strlen($str), 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class UnicodeStringInvariant extends BaseInvariant
public function checkString()
{
$str = $this->provider->string(5, 10)->unicode()->get();

$this->assertTrue(is_string($str));
}
}
2 changes: 1 addition & 1 deletion src/Generator/Generators/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

abstract class BaseGenerator extends Random implements GeneratorInterface
{
public function register(mixed $value): void
protected function register(mixed $value): void
{
$trace = debug_backtrace();
$file = $trace[1]['file'];
Expand Down
30 changes: 30 additions & 0 deletions src/Generator/Generators/ShuffleGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PhpInvariant\Generator\Generators;

class ShuffleGenerator extends BaseGenerator
{
/**
* @var array<mixed>
*/
private array $data;

/**
* @param mixed[] $data
*/
public function __construct(array $data)
{
$this->data = $data;
}


public function get(): mixed
{
$shuffledData = $this->shuffle($this->data);

$this->register($shuffledData);
return $shuffledData;
}


}
44 changes: 29 additions & 15 deletions src/Generator/Generators/StringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

class StringGenerator extends BaseGenerator
{
private const ASCII = 'ascii';
private const UNICODE = 'unicode';

private int $minLength;
private int $maxLength;

private ?string $preDefine = null;

/**
* @var array<string>
*/
Expand All @@ -26,10 +31,21 @@ public function get(): mixed
$length = $this->getInt($this->minLength, $this->maxLength);

$text = '';
for ($i = 0; $i < $length; $i++) {
$element = $this->getArrayElement($this->dictionary);
//dump(mb_ord($element));
$text .= $element;
if (count($this->dictionary) > 0) {
for ($i = 0; $i < $length; $i++) {
$element = $this->getArrayElement($this->dictionary);
$text .= $element;
}
} elseif ($this->preDefine === self::ASCII) {
for ($i = 0; $i < $length; $i++) {
$char = $this->getInt(0, 255);
$text .= chr($char);
}
} elseif ($this->preDefine === self::UNICODE) {
for ($i = 0; $i < $length; $i++) {
$char = $this->getInt(0, 0xffff);
$text .= mb_chr($char);
}
}

$this->register($text);
Expand All @@ -50,25 +66,23 @@ public function dictionary(array $dictionary): self

public function ascii(): self
{
$dictionary = [];
for ($c = 0; $c <= 255; $c++) {
$dictionary[] = chr($c);
}

return $this->dictionary($dictionary);
$this->preDefine = self::ASCII;
return $this;
}

public function alphabetic(): self
{
return $this->dictionary(array_merge(range('A', 'Z'), range('a', 'z')));
}

public function alphanumeric(): self
{
return $this->dictionary(array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9')));
}

public function unicode(): self
{
$dictionary = [];
for ($i = 0; $i < 0xffff; $i++) {
$dictionary[] = mb_chr($i);
}
return $this->dictionary($dictionary);
$this->preDefine = self::UNICODE;
return $this;
}
}
9 changes: 9 additions & 0 deletions src/Generator/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpInvariant\Generator\Generators\FloatGenerator;
use PhpInvariant\Generator\Generators\GeneratorInterface;
use PhpInvariant\Generator\Generators\IntegerGenerator;
use PhpInvariant\Generator\Generators\ShuffleGenerator;
use PhpInvariant\Generator\Generators\StringGenerator;

class Provider
Expand Down Expand Up @@ -58,4 +59,12 @@ public function combine(array $generators): CombineGenerator
{
return new CombineGenerator($generators);
}

/**
* @param array<mixed> $data
*/
public function shuffle(array $data): ShuffleGenerator
{
return new ShuffleGenerator($data);
}
}
15 changes: 13 additions & 2 deletions src/Random/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@

class Random
{
public function getInt(int $min, int $max): int
protected function getInt(int $min, int $max): int
{
return random_int($min, $max);
}

/**
* @param array<mixed> $data
*/
public function getArrayElement(array $data): mixed
protected function getArrayElement(array $data): mixed
{
$keys = array_keys($data);
$key = $keys[$this->getInt(0, count($keys) - 1)];
return $data[$key];
}

/**
* @param array<mixed> $data
* @return array<mixed>
*/
protected function shuffle(array $data): array
{
shuffle($data);
return $data;

}
}
4 changes: 1 addition & 3 deletions src/Runner/ConfigurationApplyer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
namespace PhpInvariant\Runner;

use PhpInvariant\Progress\ProgressFactory;
use PhpInvariant\Random\Random;
use PhpInvariant\Random\SeedSetter;
use PhpInvariant\Runner\Dto\ActualConfiguration;
use PhpInvariant\Runner\Dto\RunnerConfiguration;

class ConfigurationApplyer
{
public function __construct(
private Random $random,
private ProgressFactory $progressFactory,
private SeedSetter $seedSetter
) {
}
public function applyConfiguration(RunnerConfiguration $configuration): ActualConfiguration
{
$seed = $configuration->seed ?? $this->random->getInt(1, 1000000);
$seed = $configuration->seed ?? random_int(1, 1000000);
$this->seedSetter->setSeed($seed);
$result = new ActualConfiguration($seed, $this->progressFactory->getProgress($configuration));
return $result;
Expand Down

0 comments on commit a236a24

Please sign in to comment.