Skip to content

Commit

Permalink
register parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyBel committed Sep 8, 2023
1 parent 88a29e0 commit 0e7a733
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpInvariant\BaseInvariant\BaseInvariant;
use PhpInvariant\Finish\FinishRuns;

class FinishCountInvariant extends BaseInvariant
class FinishRunsInvariant extends BaseInvariant
{
#[FinishRuns(2)]
public function checkInteger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpInvariant\BaseInvariant\BaseInvariant;
use PhpInvariant\Finish\FinishSeconds;

class FinishTimeInvariant extends BaseInvariant
class FinishSecondsInvariant extends BaseInvariant
{
#[FinishSeconds(1)]
public function checkInteger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function checkInteger()
{
$x = $this->provider->integer(50, 100)->get();


$this->assertTrue(is_integer($x));
$this->assertLessOrEqual($x, 100);
$this->assertGreaterOrEqual($x, 50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ public function checkString()
{
$str = $this->provider->string(5, 10)->unicode()->get();
$this->assertTrue(is_string($str));
$this->assertLessOrEqual(mb_strlen($str), 10);
$this->assertGreaterOrEqual(mb_strlen($str), 5);
}
}
10 changes: 0 additions & 10 deletions src/BaseInvariant/BaseInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,4 @@ public function configure(): self
return $this;
}


/**
* @return array<string>
*/
public function getArgs(): array
{
return [];
}


}
4 changes: 3 additions & 1 deletion src/CheckMethodRunner/CheckMethodGeneratorsCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpInvariant\BaseInvariant\Exception\PhpInvariantAssertException;
use PhpInvariant\CheckMethodRunner\Dto\ErrorRunResult;
use PhpInvariant\CheckMethodRunner\Dto\CheckMethodCallResult;
use PhpInvariant\Registrator\ParametersRegistrator;
use PHPUnit\Framework\ExpectationFailedException;
use ReflectionMethod;
use ReflectionException;
Expand All @@ -24,6 +25,7 @@ public function callMethod(BaseInvariant $checkClass, ReflectionMethod $checkMet
{

$result = new CheckMethodCallResult();
ParametersRegistrator::clear();

try {
/**
Expand All @@ -36,7 +38,7 @@ public function callMethod(BaseInvariant $checkClass, ReflectionMethod $checkMet
get_class($checkClass),
$checkMethod->getName(),
$exception->getMessage(),
$checkClass->getArgs()
ParametersRegistrator::get()
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/CheckMethodRunner/Condition/CountCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
public function run(BaseInvariant $checkClass, ReflectionMethod $checkMethod, FinishRuns $finishCondition): MethodRunResult
{
$result = new MethodRunResult();
for ($i = 0; $i < $finishCondition->getCount(); $i++) {
for ($i = 0; $i < $finishCondition->getRuns(); $i++) {
$this->methodRunner->callMethod($checkClass, $checkMethod, $result);
}
return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/CheckMethodRunner/Condition/TimeCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function run(BaseInvariant $checkClass, ReflectionMethod $checkMethod, Fi
{
$result = new MethodRunResult();
$start = time();
while (time() - $start < $finishCondition->getSecondsDelay()) {
while (time() - $start < $finishCondition->getSeconds()) {
$this->methodRunner->callMethod($checkClass, $checkMethod, $result);
}
return $result;
Expand Down
10 changes: 5 additions & 5 deletions src/Finish/FinishRuns.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
#[Attribute]
class FinishRuns implements FinishInterface
{
private int $count;
private int $runs;


public function __construct(int $count)
public function __construct(int $runs)
{
$this->count = $count;
$this->runs = $runs;
}


public function getCount(): int
public function getRuns(): int
{
return $this->count;
return $this->runs;
}
}
10 changes: 5 additions & 5 deletions src/Finish/FinishSeconds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
#[Attribute]
class FinishSeconds implements FinishInterface
{
private int $secondsDelay;
private int $seconds;


public function __construct(int $secondsDelay)
public function __construct(int $seconds)
{
$this->secondsDelay = $secondsDelay;
$this->seconds = $seconds;
}


public function getSecondsDelay(): int
public function getSeconds(): int
{
return $this->secondsDelay;
return $this->seconds;
}
}
5 changes: 2 additions & 3 deletions src/Generator/Generators/ArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class ArrayGenerator extends Random implements GeneratorInterface
class ArrayGenerator extends BaseGenerator
{
private int $count;
private GeneratorInterface $value;
Expand Down Expand Up @@ -34,6 +32,7 @@ public function get(): array
$data[] = $this->value->get();
}

$this->register($data);
return $data;

}
Expand Down
20 changes: 20 additions & 0 deletions src/Generator/Generators/BaseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;
use PhpInvariant\Registrator\ParametersRegistrator;

abstract class BaseGenerator extends Random implements GeneratorInterface
{
public function register(mixed $value): void
{
$trace = debug_backtrace();
$file = $trace[1]['file'];
if (str_ends_with($file, 'Invariant.php')) {
ParametersRegistrator::add($value);
}

}

}
9 changes: 5 additions & 4 deletions src/Generator/Generators/BooleanGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class BooleanGenerator extends Random implements GeneratorInterface
class BooleanGenerator extends BaseGenerator
{
public function get(): bool
{
return (bool)($this->getInt(0, 1));
$value = (bool)($this->getInt(0, 1));

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

}
5 changes: 2 additions & 3 deletions src/Generator/Generators/CombineGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class CombineGenerator extends Random implements GeneratorInterface
class CombineGenerator extends BaseGenerator
{
/**
* @var array<GeneratorInterface>
Expand All @@ -24,6 +22,7 @@ public function __construct(array $generators)
public function get(): mixed
{
$generator = $this->getArrayElement($this->generators);

return $generator->get();
}

Expand Down
13 changes: 10 additions & 3 deletions src/Generator/Generators/DateTimeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace PhpInvariant\Generator\Generators;

use DateTimeImmutable;
use PhpInvariant\Random\Random;

class DateTimeGenerator extends Random implements GeneratorInterface
class DateTimeGenerator extends BaseGenerator
{
private DateTimeImmutable $from;
private DateTimeImmutable $to;
Expand All @@ -19,8 +18,16 @@ public function __construct()

public function get(): DateTimeImmutable
{
$value = (new DateTimeImmutable())->setTimestamp(
$this->getInt(
$this->from->getTimestamp(),
$this->to->getTimestamp()
)
);

return (new DateTimeImmutable())->setTimestamp($this->getInt($this->from->getTimestamp(), $this->to->getTimestamp()));

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

public function from(DateTimeImmutable $from): self
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/Generators/ElementsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class ElementsGenerator extends Random implements GeneratorInterface
class ElementsGenerator extends BaseGenerator
{
/**
* @var array<mixed>
Expand Down Expand Up @@ -32,6 +30,7 @@ public function get(): array
$elements[] = $this->getArrayElement($this->data);
}

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

Expand Down
9 changes: 5 additions & 4 deletions src/Generator/Generators/FloatGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class FloatGenerator extends Random implements GeneratorInterface
class FloatGenerator extends BaseGenerator
{
private float $min;
private float $max;
Expand Down Expand Up @@ -32,7 +30,10 @@ public function get(): float

$realPart = $this->getInt(0, $realMax) / $denominator;

return round($integerPart + floatval($realPart), $this->decimals);
$value = round($integerPart + floatval($realPart), $this->decimals);

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


Expand Down
9 changes: 5 additions & 4 deletions src/Generator/Generators/IntegerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class IntegerGenerator extends Random implements GeneratorInterface
class IntegerGenerator extends BaseGenerator
{
private int $min;
private int $max;
Expand All @@ -19,7 +17,10 @@ public function __construct(int $min, int $max)

public function get(): int
{
return $this->getInt($this->min, $this->max);
$value = $this->getInt($this->min, $this->max);
$this->register($value);

return $value;
}


Expand Down
9 changes: 5 additions & 4 deletions src/Generator/Generators/StringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpInvariant\Generator\Generators;

use PhpInvariant\Random\Random;

class StringGenerator extends Random implements GeneratorInterface
class StringGenerator extends BaseGenerator
{
private int $minLength;
private int $maxLength;
Expand All @@ -29,9 +27,12 @@ public function get(): mixed

$text = '';
for ($i = 0; $i < $length; $i++) {
$text .= $this->getArrayElement($this->dictionary);
$element = $this->getArrayElement($this->dictionary);
//dump(mb_ord($element));
$text .= $element;
}

$this->register($text);
return $text;

}
Expand Down
32 changes: 32 additions & 0 deletions src/Registrator/ParametersRegistrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpInvariant\Registrator;

class ParametersRegistrator
{
/**
* @var array<mixed>
*/
private static array $parameters = [];

/**
* @return mixed[]
*/
public static function get(): array
{
return self::$parameters;
}

public static function add(mixed $parameter): void
{
self::$parameters[] = $parameter;
}

public static function clear(): void
{
self::$parameters = [];

}


}

0 comments on commit 0e7a733

Please sign in to comment.