diff --git a/.travis.yml b/.travis.yml index bb0ec678..f7258a07 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ matrix: env: WITH_CS=true - php: 7 # PHPUnit 6.x - php: 7.1 # PHPUnit 7.x + - php: 7.2 # PHPUnit 7.x - php: hhvm env: SKIP_LINT_TEST_CASES=1 sudo: required diff --git a/docs/limits.rst b/docs/limits.rst index a2a8bc41..79f4396a 100644 --- a/docs/limits.rst +++ b/docs/limits.rst @@ -6,10 +6,10 @@ Time and iterations By default Eris extracts a sample of 100 values for each ``forAll()`` call, and runs the ``then()`` callback over each of them. -For tests which take very long to run, it is possible to either limit the number of elements in the sample, or to specify a time limit the test should not exceed. For this purpose, the ``limitTo()`` method accepts either: +For tests which take very long to run, it is possible to limit the number of elements in the sample, or to specify a time limit the test should not exceed. For this purpose, the you can use the following annotations: -* an integer requesting a fixed number of iterations; -* a `DateInterval`_ object from the standard PHP library. +* `@eris-repeat {number}` with an integer requesting a fixed number of iterations; +* `@eris-duration {definition}` with a `DateInterval`_ compatible definition. .. literalinclude:: ../examples/LimitToTest.php :language: php diff --git a/docs/listeners.rst b/docs/listeners.rst index ee7a44b2..60372025 100644 --- a/docs/listeners.rst +++ b/docs/listeners.rst @@ -97,7 +97,7 @@ It is not advised to rely on this format for parsing, being it only oriented to Minimum Evaluations --- -The ``minimumEvaluations($ratio)`` API method instantiates and wires in a Listener that checks that at least ``$ratio`` of the total number of inputs being generated is actually evaluated. This Listener is only needed in case of an aggressive use of ``when()``. +The ``@eris-ratio {ratio}`` annotation instantiates and wires in a Listener that checks that at least ``ratio``% of the total number of inputs being generated is actually evaluated. This Listener is only needed in case of an aggressive use of ``when()``. Management of this Listener is provided through this method instead of explicitly adding a Listener object, as there is a default Listener instantiated with a threshold of 0.5 that has to be replaced in case a new minimum is chosen. diff --git a/docs/shrinking.rst b/docs/shrinking.rst index c2c9bfe0..219e7655 100644 --- a/docs/shrinking.rst +++ b/docs/shrinking.rst @@ -105,7 +105,7 @@ The shrinking for this test will not run for more than 2 seconds (although the t 1) ShrinkingTimeLimitTest::testLengthPreservation RuntimeException: Eris has reached the time limit for shrinking (2s elapsed of 2s), here it is presenting the simplest failure case. - If you can afford to spend more time to find a simpler failing input, increase it with $this->shrinkingTimeLimit($seconds). + If you can afford to spend more time to find a simpler failing input, increase it with the annotation '@eris-shrink {seconds}'. /home/giorgio/code/eris/src/Eris/Shrinker/Random.php:71 /home/giorgio/code/eris/src/Eris/Quantifier/ForAll.php:128 diff --git a/examples/CharacterTest.php b/examples/CharacterTest.php index 7a66b1b9..7939fbae 100644 --- a/examples/CharacterTest.php +++ b/examples/CharacterTest.php @@ -27,10 +27,12 @@ public function testLengthOfPrintableAsciiCharacters() }); } + /** + * @eris-ratio 10 + */ public function testMultiplePrintableCharacters() { $this - ->minimumEvaluationRatio(0.1) ->forAll( Generator\char(['basic-latin']), Generator\char(['basic-latin']) diff --git a/examples/LimitToTest.php b/examples/LimitToTest.php index 5689b8f8..9297ac30 100644 --- a/examples/LimitToTest.php +++ b/examples/LimitToTest.php @@ -6,10 +6,12 @@ class LimitToTest extends PHPUnit_Framework_TestCase { use TestTrait; + /** + * @eris-repeat 5 + */ public function testNumberOfIterationsCanBeConfigured() { - $this->limitTo(5) - ->forAll( + $this->forAll( Generator\int() ) ->then(function ($value) { @@ -33,11 +35,13 @@ public function testTimeIntervalToRunForCanBeConfiguredButItNeedsToProduceAtLeas } */ + /** + * @eris-ratio 0 + * @eris-duration PT2S + */ public function testTimeIntervalToRunForCanBeConfiguredAndAVeryLowNumberOfIterationsCanBeIgnored() { - $this->minimumEvaluationRatio(0.0) - ->limitTo(new DateInterval("PT2S")) - ->forAll( + $this->forAll( Generator\int() ) ->then(function ($value) { diff --git a/examples/LogFileTest.php b/examples/LogFileTest.php index 726e87af..fcca1490 100644 --- a/examples/LogFileTest.php +++ b/examples/LogFileTest.php @@ -13,7 +13,7 @@ public function testWritingIterationsOnALogFile() ->forAll( Generator\int() ) - ->hook(Listener\log('/tmp/eris-log-file-test.log')) + ->hook(Listener\log(sys_get_temp_dir().'/eris-log-file-test.log')) ->then(function ($number) { $this->assertInternalType('integer', $number); }); @@ -25,7 +25,7 @@ public function testLogOfFailuresAndShrinking() ->forAll( Generator\int() ) - ->hook(Listener\log('/tmp/eris-log-file-shrinking.log')) + ->hook(Listener\log(sys_get_temp_dir().'/eris-log-file-shrinking.log')) ->then(function ($number) { $this->assertLessThanOrEqual(42, $number); }); diff --git a/examples/MinimumEvaluationsTest.php b/examples/MinimumEvaluationsTest.php index 7056f163..2eea3879 100644 --- a/examples/MinimumEvaluationsTest.php +++ b/examples/MinimumEvaluationsTest.php @@ -19,10 +19,12 @@ public function testFailsBecauseOfTheLowEvaluationRatio() }); } + /** + * @eris-ratio 1 + */ public function testPassesBecauseOfTheArtificiallyLowMinimumEvaluationRatio() { $this - ->minimumEvaluationRatio(0.01) ->forAll( Generator\choose(0, 100) ) diff --git a/examples/RandConfigurationTest.php b/examples/RandConfigurationTest.php index 30887fa7..4dfb66a9 100644 --- a/examples/RandConfigurationTest.php +++ b/examples/RandConfigurationTest.php @@ -7,10 +7,12 @@ class RandConfigurationTest extends PHPUnit_Framework_TestCase { use TestTrait; + /** + * @eris-method rand + */ public function testUsingTheDefaultRandFunction() { $this - ->withRand('rand') ->forAll( Generator\int() ) @@ -18,10 +20,12 @@ public function testUsingTheDefaultRandFunction() ->then($this->isInteger()); } + /** + * @eris-method mt_rand + */ public function testUsingTheDefaultMtRandFunction() { $this - ->withRand('mt_rand') ->forAll( Generator\int() ) diff --git a/examples/SequenceTest.php b/examples/SequenceTest.php index 203330e6..ae4f72fa 100644 --- a/examples/SequenceTest.php +++ b/examples/SequenceTest.php @@ -1,6 +1,5 @@ shrinkingTimeLimit(2) ->forAll( Generator\string(), Generator\string() diff --git a/examples/StringTest.php b/examples/StringTest.php index 70777920..c2fafc43 100644 --- a/examples/StringTest.php +++ b/examples/StringTest.php @@ -36,7 +36,7 @@ public function testLengthPreservation() Generator\string(), Generator\string() ) - ->hook(Listener\log('/tmp/eris-string-shrinking.log')) + ->hook(Listener\log(sys_get_temp_dir().'/eris-string-shrinking.log')) ->then(function ($first, $second) { $result = string_concatenation($first, $second); $this->assertEquals( diff --git a/examples/SuchThatTest.php b/examples/SuchThatTest.php index aa172047..0523626c 100644 --- a/examples/SuchThatTest.php +++ b/examples/SuchThatTest.php @@ -55,7 +55,7 @@ public function testSuchThatAcceptsPHPUnitConstraints() ) ) ) - ->hook(Listener\log('/tmp/eris-such-that.log')) + ->hook(Listener\log(sys_get_temp_dir().'/eris-such-that.log')) ->then($this->allNumbersAreBiggerThan(42)); } diff --git a/src/Facade.php b/src/Facade.php index 38708fe4..2d857ac4 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -10,4 +10,13 @@ public function __construct() $this->erisSetupBeforeClass(); $this->erisSetup(); } + + /** + * sadly this facade has no option to retrieve annotations of testcases + * @return array + */ + protected function getAnnotations() + { + return array(); + } } diff --git a/src/Generator.php b/src/Generator.php index 0b776e0c..526cd288 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -10,10 +10,10 @@ interface Generator { /** * @param int The generation size - * @param callable a rand() function + * @param Random\RandomRange * @return GeneratedValueSingle */ - public function __invoke($size, $rand); + public function __invoke($size, Random\RandomRange $rand); /** * The conditions for terminating are either: diff --git a/src/Generator/AssociativeArrayGenerator.php b/src/Generator/AssociativeArrayGenerator.php index 1189ef7c..68cc7178 100644 --- a/src/Generator/AssociativeArrayGenerator.php +++ b/src/Generator/AssociativeArrayGenerator.php @@ -22,7 +22,7 @@ public function __construct(array $generators) $this->tupleGenerator = new TupleGenerator(array_values($generators)); } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $tuple = $this->tupleGenerator->__invoke($size, $rand); return $this->mapToAssociativeArray($tuple); diff --git a/src/Generator/BindGenerator.php b/src/Generator/BindGenerator.php index 70d9bc82..50e01a9a 100644 --- a/src/Generator/BindGenerator.php +++ b/src/Generator/BindGenerator.php @@ -22,7 +22,7 @@ public function __construct($innerGenerator, $outerGeneratorFactory) $this->outerGeneratorFactory = $outerGeneratorFactory; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $innerGeneratorValue = $this->innerGenerator->__invoke($size, $rand); $outerGenerator = call_user_func($this->outerGeneratorFactory, $innerGeneratorValue->unbox()); diff --git a/src/Generator/BooleanGenerator.php b/src/Generator/BooleanGenerator.php index 45e3827b..e196c79c 100644 --- a/src/Generator/BooleanGenerator.php +++ b/src/Generator/BooleanGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; function bool() { @@ -11,10 +10,10 @@ function bool() class BooleanGenerator implements Generator { - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { $booleanValues = [true, false]; - $randomIndex = $rand(0, count($booleanValues) - 1); + $randomIndex = $rand->rand(0, count($booleanValues) - 1); return GeneratedValueSingle::fromJustValue($booleanValues[$randomIndex], 'boolean'); } diff --git a/src/Generator/CharacterGenerator.php b/src/Generator/CharacterGenerator.php index f4e78916..43b7bd2b 100644 --- a/src/Generator/CharacterGenerator.php +++ b/src/Generator/CharacterGenerator.php @@ -49,14 +49,13 @@ public function __construct($lowerLimit, $upperLimit) $this->shrinkingProgression = ArithmeticProgression::discrete($this->lowerLimit); } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { - return GeneratedValueSingle::fromJustValue(chr($rand($this->lowerLimit, $this->upperLimit)), 'character'); + return GeneratedValueSingle::fromJustValue(chr($rand->rand($this->lowerLimit, $this->upperLimit)), 'character'); } public function shrink(GeneratedValueSingle $element) { - $codePoint = ord($element->unbox()); $shrinkedValue = chr($this->shrinkingProgression->next(ord($element->unbox()))); return GeneratedValueSingle::fromJustValue($shrinkedValue, 'character'); } diff --git a/src/Generator/ChooseGenerator.php b/src/Generator/ChooseGenerator.php index e9a0aca9..8538aae6 100644 --- a/src/Generator/ChooseGenerator.php +++ b/src/Generator/ChooseGenerator.php @@ -3,7 +3,6 @@ use Eris\Generator; use InvalidArgumentException; -use DomainException; if (!defined('ERIS_PHP_INT_MIN')) { define('ERIS_PHP_INT_MIN', ~PHP_INT_MAX); @@ -42,9 +41,9 @@ public function __construct($x, $y) ); } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { - $value = $rand($this->lowerLimit, $this->upperLimit); + $value = $rand->rand($this->lowerLimit, $this->upperLimit); return GeneratedValueSingle::fromJustValue($value, 'choose'); } diff --git a/src/Generator/ConstantGenerator.php b/src/Generator/ConstantGenerator.php index 9b373443..da4055e7 100644 --- a/src/Generator/ConstantGenerator.php +++ b/src/Generator/ConstantGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; /** * @param mixed $value the only value to generate @@ -27,7 +26,7 @@ public function __construct($value) $this->value = $value; } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { return GeneratedValueSingle::fromJustValue($this->value, 'constant'); } diff --git a/src/Generator/DateGenerator.php b/src/Generator/DateGenerator.php index dc8194cb..a77e21fc 100644 --- a/src/Generator/DateGenerator.php +++ b/src/Generator/DateGenerator.php @@ -3,7 +3,6 @@ use Eris\Generator; use DateTime; -use DomainException; function date($lowerLimit = null, $upperLimit = null) { @@ -42,9 +41,9 @@ public function __construct(DateTime $lowerLimit, DateTime $upperLimit) $this->intervalInSeconds = $upperLimit->getTimestamp() - $lowerLimit->getTimestamp(); } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { - $generatedOffset = $rand(0, $this->intervalInSeconds); + $generatedOffset = $rand->rand(0, $this->intervalInSeconds); return GeneratedValueSingle::fromJustValue( $this->fromOffset($generatedOffset), 'date' diff --git a/src/Generator/ElementsGenerator.php b/src/Generator/ElementsGenerator.php index 14c58756..9d3ddecf 100644 --- a/src/Generator/ElementsGenerator.php +++ b/src/Generator/ElementsGenerator.php @@ -29,9 +29,9 @@ private function __construct($domain) $this->domain = $domain; } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { - $index = $rand(0, count($this->domain) - 1); + $index = $rand->rand(0, count($this->domain) - 1); return GeneratedValueSingle::fromJustValue($this->domain[$index], 'elements'); } diff --git a/src/Generator/FloatGenerator.php b/src/Generator/FloatGenerator.php index 1d366353..72d28871 100644 --- a/src/Generator/FloatGenerator.php +++ b/src/Generator/FloatGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; function float() { @@ -15,13 +14,13 @@ public function __construct() { } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { - $denominator = $rand(1, $size) ?: 1; + $denominator = $rand->rand(1, $size) ?: 1; - $value = (float) $rand(0, $size) / (float) $denominator; + $value = (float) $rand->rand(0, $size) / (float) $denominator; - $signedValue = $rand(0, 1) === 0 + $signedValue = $rand->rand(0, 1) === 0 ? $value : $value * (-1); return GeneratedValueSingle::fromJustValue($signedValue, 'float'); diff --git a/src/Generator/FrequencyGenerator.php b/src/Generator/FrequencyGenerator.php index 7a01931b..17c57de8 100644 --- a/src/Generator/FrequencyGenerator.php +++ b/src/Generator/FrequencyGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; use InvalidArgumentException; /** @@ -16,7 +15,6 @@ function frequency(/*$frequencyAndGenerator, $frequencyAndGenerator, ...*/) class FrequencyGenerator implements Generator { private $generators; - private $frequencies; public function __construct(array $generatorsWithFrequency) { @@ -43,7 +41,7 @@ function ($generators, $generatorWithFrequency) { ); } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { list($index, $generator) = $this->pickFrom($this->generators, $rand); $originalValue = $generator->__invoke($size, $rand); @@ -77,11 +75,11 @@ public function shrink(GeneratedValueSingle $element) /** * @return array two elements: index and Generator object */ - private function pickFrom($generators, $rand) + private function pickFrom($generators, \Eris\Random\RandomRange $rand) { $acc = 0; $frequencies = $this->frequenciesFrom($generators); - $random = $rand(1, array_sum($frequencies)); + $random = $rand->rand(1, array_sum($frequencies)); foreach ($generators as $index => $generator) { $acc += $generator['frequency']; if ($random <= $acc) { diff --git a/src/Generator/GeneratedValueOptions.php b/src/Generator/GeneratedValueOptions.php index be7a4478..da726495 100644 --- a/src/Generator/GeneratedValueOptions.php +++ b/src/Generator/GeneratedValueOptions.php @@ -1,148 +1,140 @@ - of its value, - * which should be the type parameter of all the contained GeneratedValueSingle - * instances. - * - * Mainly used in shrinking, to support multiple options as possibilities - * for shrinking a GeneratedValueSingle. - * - * This class tends to delegate operations to its last() elements for - * backwards compatibility. So it can be used in context where a single - * value is expected. The last of the options is usually the more conservative - * in shrinking, for example subtracting 1 for the IntegerGenerator. - */ -class GeneratedValueOptions implements GeneratedValue -{ - private $generatedValues; - - public function __construct(array $generatedValues) - { - $this->generatedValues = $generatedValues; - } - - public static function mostPessimisticChoice(GeneratedValue $value) - { - if ($value instanceof GeneratedValueOptions) { - return $value->last(); - } else { - return $value; - } - } - - public function first() - { - return $this->generatedValues[0]; - } - - public function last() - { - if (count($this->generatedValues) == 0) { - throw new LogicException("This GeneratedValueOptions is empty"); - } - return $this->generatedValues[count($this->generatedValues) - 1]; - } - - public function map(callable $callable, $generatorName) - { - return new self(array_map( - function ($value) use ($callable, $generatorName) { - return $value->map($callable, $generatorName); - }, - $this->generatedValues - )); - } - - public function derivedIn($generatorName) - { - throw new RuntimeException("GeneratedValueOptions::derivedIn() is needed, uncomment it"); - /* - * Not sure this is needed. - return $this->map( - function ($value) { return $value; }, - $generatorName - ); - */ - } - - /** - * @return self - */ - public function add(GeneratedValueSingle $value) - { - return new self(array_merge( - $this->generatedValues, - [$value] - )); - } - - public function remove(GeneratedValueSingle $value) - { - $generatedValues = $this->generatedValues; - $index = array_search($value, $generatedValues); - if ($index !== false) { - unset($generatedValues[$index]); - } - return new self(array_values($generatedValues)); - } - - /** - * @override - */ - public function unbox() - { - return $this->last()->unbox(); - } - - /** - * @override - */ - public function input() - { - return $this->last()->input(); - } - - /** - * @override - */ - public function __toString() - { - return var_export($this, true); - } - - /** - * @override - * @return string - */ - public function generatorName() - { - return $this->last()->generatorName(); - } - - public function getIterator() - { - return new ArrayIterator($this->generatedValues); - } - - public function count() - { - return count($this->generatedValues); - } - - public function cartesianProduct($generatedValueOptions, callable $merge) - { - $options = []; - foreach ($this as $firstPart) { - foreach ($generatedValueOptions as $secondPart) { - $options[] = $firstPart->merge($secondPart, $merge); - } - } - return new self($options); - } -} + of its value, + * which should be the type parameter of all the contained GeneratedValueSingle + * instances. + * + * Mainly used in shrinking, to support multiple options as possibilities + * for shrinking a GeneratedValueSingle. + * + * This class tends to delegate operations to its last() elements for + * backwards compatibility. So it can be used in context where a single + * value is expected. The last of the options is usually the more conservative + * in shrinking, for example subtracting 1 for the IntegerGenerator. + */ +class GeneratedValueOptions implements GeneratedValue +{ + private $generatedValues; + + public function __construct(array $generatedValues) + { + $this->generatedValues = $generatedValues; + } + + public static function mostPessimisticChoice(GeneratedValue $value) + { + if ($value instanceof GeneratedValueOptions) { + return $value->last(); + } else { + return $value; + } + } + + public function first() + { + return $this->generatedValues[0]; + } + + public function last() + { + if (count($this->generatedValues) == 0) { + throw new LogicException("This GeneratedValueOptions is empty"); + } + return $this->generatedValues[count($this->generatedValues) - 1]; + } + + public function map(callable $callable, $generatorName) + { + return new self(array_map( + function ($value) use ($callable, $generatorName) { + return $value->map($callable, $generatorName); + }, + $this->generatedValues + )); + } + + public function derivedIn($generatorName) + { + throw new RuntimeException("GeneratedValueOptions::derivedIn() is needed, uncomment it"); + } + + /** + * @return self + */ + public function add(GeneratedValueSingle $value) + { + return new self(array_merge( + $this->generatedValues, + [$value] + )); + } + + public function remove(GeneratedValueSingle $value) + { + $generatedValues = $this->generatedValues; + $index = array_search($value, $generatedValues); + if ($index !== false) { + unset($generatedValues[$index]); + } + return new self(array_values($generatedValues)); + } + + /** + * @override + */ + public function unbox() + { + return $this->last()->unbox(); + } + + /** + * @override + */ + public function input() + { + return $this->last()->input(); + } + + /** + * @override + */ + public function __toString() + { + return var_export($this, true); + } + + /** + * @override + * @return string + */ + public function generatorName() + { + return $this->last()->generatorName(); + } + + public function getIterator() + { + return new ArrayIterator($this->generatedValues); + } + + public function count() + { + return count($this->generatedValues); + } + + public function cartesianProduct($generatedValueOptions, callable $merge) + { + $options = []; + foreach ($this as $firstPart) { + foreach ($generatedValueOptions as $secondPart) { + $options[] = $firstPart->merge($secondPart, $merge); + } + } + return new self($options); + } +} diff --git a/src/Generator/IntegerGenerator.php b/src/Generator/IntegerGenerator.php index e0bc1b73..33c8da2e 100644 --- a/src/Generator/IntegerGenerator.php +++ b/src/Generator/IntegerGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; /** * Generates a positive or negative integer (with absolute value bounded by @@ -61,12 +60,12 @@ public function __construct(callable $mapFn = null) } } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { - $value = $rand(0, $size); + $value = $rand->rand(0, $size); $mapFn = $this->mapFn; - $result = $rand(0, 1) === 0 + $result = $rand->rand(0, 1) === 0 ? $mapFn($value) : $mapFn($value * (-1)); return GeneratedValueSingle::fromJustValue( diff --git a/src/Generator/MapGenerator.php b/src/Generator/MapGenerator.php index 70df70e9..4a5fc86f 100644 --- a/src/Generator/MapGenerator.php +++ b/src/Generator/MapGenerator.php @@ -20,7 +20,7 @@ public function __construct(callable $map, $generator) $this->generator = $generator; } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { $input = $this->generator->__invoke($_size, $rand); return $input->map( diff --git a/src/Generator/NamesGenerator.php b/src/Generator/NamesGenerator.php index 020121de..bb038afc 100644 --- a/src/Generator/NamesGenerator.php +++ b/src/Generator/NamesGenerator.php @@ -32,7 +32,7 @@ public function __construct(array $list) $this->list = $list; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $candidateNames = $this->filterDataSet( $this->lengthLessThanOrEqualTo($size) @@ -40,7 +40,7 @@ public function __invoke($size, $rand) if (!$candidateNames) { return GeneratedValueSingle::fromJustValue('', 'names'); } - $index = $rand(0, count($candidateNames) - 1); + $index = $rand->rand(0, count($candidateNames) - 1); return GeneratedValueSingle::fromJustValue($candidateNames[$index], 'names'); } diff --git a/src/Generator/OneOfGenerator.php b/src/Generator/OneOfGenerator.php index b0351b4a..94e1bdf0 100644 --- a/src/Generator/OneOfGenerator.php +++ b/src/Generator/OneOfGenerator.php @@ -20,7 +20,7 @@ public function __construct($generators) $this->generator = new FrequencyGenerator($this->allWithSameFrequency($generators)); } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { return $this->generator->__invoke($size, $rand); } diff --git a/src/Generator/RegexGenerator.php b/src/Generator/RegexGenerator.php index 4a59b5b0..de619240 100644 --- a/src/Generator/RegexGenerator.php +++ b/src/Generator/RegexGenerator.php @@ -33,10 +33,10 @@ public function __construct($expression) $this->expression = $expression; } - public function __invoke($_size, $rand) + public function __invoke($_size, \Eris\Random\RandomRange $rand) { $lexer = new Lexer($this->expression); - $gen = new SimpleRandom($rand()); + $gen = new SimpleRandom($rand->rand()); $result = null; $parser = new Parser($lexer, new Scope(), new Scope()); diff --git a/src/Generator/SequenceGenerator.php b/src/Generator/SequenceGenerator.php index f0e0206b..74de4922 100644 --- a/src/Generator/SequenceGenerator.php +++ b/src/Generator/SequenceGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; function seq(Generator $singleElementGenerator) { @@ -22,9 +21,9 @@ public function __construct(Generator $singleElementGenerator) $this->singleElementGenerator = $singleElementGenerator; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { - $sequenceLength = $rand(0, $size); + $sequenceLength = $rand->rand(0, $size); return $this->vector($sequenceLength)->__invoke($size, $rand); } diff --git a/src/Generator/SetGenerator.php b/src/Generator/SetGenerator.php index 0120e2fc..b2dd3188 100644 --- a/src/Generator/SetGenerator.php +++ b/src/Generator/SetGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; /** * @param Generator $singleElementGenerator @@ -22,7 +21,7 @@ public function __construct(Generator $singleElementGenerator) $this->singleElementGenerator = $singleElementGenerator; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $setSize = rand(0, $size); $set = []; diff --git a/src/Generator/StringGenerator.php b/src/Generator/StringGenerator.php index 92cfb7a4..2897f32d 100644 --- a/src/Generator/StringGenerator.php +++ b/src/Generator/StringGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; function string() { @@ -11,13 +10,13 @@ function string() class StringGenerator implements Generator { - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { - $length = $rand(0, $size); + $length = $rand->rand(0, $size); $built = ''; for ($i = 0; $i < $length; $i++) { - $built .= chr($rand(33, 126)); + $built .= chr($rand->rand(33, 126)); } return GeneratedValueSingle::fromJustValue($built, 'string'); } diff --git a/src/Generator/SubsetGenerator.php b/src/Generator/SubsetGenerator.php index a7aefdba..573ae055 100644 --- a/src/Generator/SubsetGenerator.php +++ b/src/Generator/SubsetGenerator.php @@ -24,11 +24,11 @@ public function __construct(array $universe) $this->universe = $universe; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $relativeSize = $size / ForAll::DEFAULT_MAX_SIZE; $maximumSubsetIndex = floor(pow(2, count($this->universe)) * $relativeSize); - $subsetIndex = $rand(0, $maximumSubsetIndex); + $subsetIndex = $rand->rand(0, $maximumSubsetIndex); $binaryDescription = str_pad(decbin($subsetIndex), count($this->universe), "0", STR_PAD_LEFT); $subset = []; for ($i = 0; $i < strlen($binaryDescription); $i++) { diff --git a/src/Generator/SuchThatGenerator.php b/src/Generator/SuchThatGenerator.php index 2353bb98..7e677242 100644 --- a/src/Generator/SuchThatGenerator.php +++ b/src/Generator/SuchThatGenerator.php @@ -43,7 +43,7 @@ public function __construct($filter, $generator, $maximumAttempts = 100) $this->maximumAttempts = $maximumAttempts; } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $value = $this->generator->__invoke($size, $rand); $attempts = 0; diff --git a/src/Generator/TupleGenerator.php b/src/Generator/TupleGenerator.php index 7d4763d5..c3788e22 100644 --- a/src/Generator/TupleGenerator.php +++ b/src/Generator/TupleGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; /** * One Generator for each member of the Tuple: @@ -25,7 +24,6 @@ function tuple() class TupleGenerator implements Generator { private $generators; - private $size; private $numberOfGenerators; public function __construct(array $generators) @@ -34,7 +32,7 @@ public function __construct(array $generators) $this->numberOfGenerators = count($generators); } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { $input = array_map( function ($generator) use ($size, $rand) { diff --git a/src/Generator/VectorGenerator.php b/src/Generator/VectorGenerator.php index c9678c0f..a05d5e34 100644 --- a/src/Generator/VectorGenerator.php +++ b/src/Generator/VectorGenerator.php @@ -2,7 +2,6 @@ namespace Eris\Generator; use Eris\Generator; -use DomainException; function vector($size, Generator $elementsGenerator) { @@ -24,7 +23,7 @@ public function __construct($size, Generator $generator) $this->elementsGeneratorClass = get_class($generator); } - public function __invoke($size, $rand) + public function __invoke($size, \Eris\Random\RandomRange $rand) { return $this->generator->__invoke($size, $rand); } diff --git a/src/Quantifier/ForAll.php b/src/Quantifier/ForAll.php index d12bc2e0..2911a702 100644 --- a/src/Quantifier/ForAll.php +++ b/src/Quantifier/ForAll.php @@ -29,10 +29,14 @@ class ForAll private $terminationConditions = []; private $listeners = []; private $shrinkerFactoryMethod; + + /** + * @var RandomRange + */ private $rand; private $shrinkingEnabled = true; - public function __construct(array $generators, $iterations, $shrinkerFactory, $shrinkerFactoryMethod, $rand) + public function __construct(array $generators, $iterations, $shrinkerFactory, $shrinkerFactoryMethod, \Eris\Random\RandomRange $rand) { $this->generators = $this->generatorsFrom($generators); $this->iterations = $iterations; @@ -167,8 +171,7 @@ public function __invoke(callable $assertion) } } catch (Exception $e) { $redTestException = $e; - $wrap = (bool) getenv('ERIS_ORIGINAL_INPUT'); - if ($wrap) { + if ((bool) getenv('ERIS_ORIGINAL_INPUT')) { $message = "Original input: " . var_export($values, true) . PHP_EOL . "Possibly shrinked input follows." . PHP_EOL; throw new RuntimeException($message, -1, $e); diff --git a/src/Random/MersenneTwister.php b/src/Random/MersenneTwister.php index cdb91328..2a478d84 100644 --- a/src/Random/MersenneTwister.php +++ b/src/Random/MersenneTwister.php @@ -43,27 +43,27 @@ public function seed($seed) $this->mt[$i] = ($this->f * ( $this->mt[$i - 1] ^ (($this->mt[$i - 1] >> ($this->w - 2)) & 0b11) ) + $i) & $this->wMask; - assert('$this->mt[$i] <= $this->wMask'); + assert($this->mt[$i] <= $this->wMask); } - assert('count($this->mt) === 624'); + assert(count($this->mt) === 624); return $this; } public function extractNumber() { - assert('$this->index <= $this->n'); + assert($this->index <= $this->n); if ($this->index >= $this->n) { $this->twist(); } $y = $this->mt[$this->index]; $y = $y ^ (($y >> $this->u) & $this->d); - assert('$y <= 0xffffffff'); + assert($y <= 0xffffffff); $y = $y ^ (($y << $this->s) & $this->b); - assert('$y <= 0xffffffff'); + assert($y <= 0xffffffff); $y = $y ^ (($y << $this->t) & $this->c); - assert('$y <= 0xffffffff'); + assert($y <= 0xffffffff); $y = $y ^ ($y >> $this->l); - assert('$y <= 0xffffffff'); + assert($y <= 0xffffffff); $this->index = $this->index + 1; return $y & $this->wMask; } @@ -78,9 +78,9 @@ private function twist() for ($i = 0; $i <= $this->n - 1; $i++) { $x = ($this->mt[$i] & $this->upperMask) + (($this->mt[($i+1) % $this->n]) & $this->lowerMask); - assert('$x <= 0xffffffff'); + assert($x <= 0xffffffff); $xA = $x >> 1; - assert('$xA <= 0x7fffffff'); + assert($xA <= 0x7fffffff); if (($x % 2) != 0) { $xA = $xA ^ $this->a; } diff --git a/src/Random/MtRandSource.php b/src/Random/MtRandSource.php new file mode 100644 index 00000000..ad50c44c --- /dev/null +++ b/src/Random/MtRandSource.php @@ -0,0 +1,33 @@ +max()); + } + + /** + * @return integer + */ + public function max() + { + return mt_getrandmax(); + } + + /** + * @param integer $seed + * @return self + */ + public function seed($seed) + { + mt_srand($seed); + return $this; + } +} diff --git a/src/Random/RandSource.php b/src/Random/RandSource.php new file mode 100644 index 00000000..16e5ac84 --- /dev/null +++ b/src/Random/RandSource.php @@ -0,0 +1,33 @@ +max()); + } + + /** + * @return integer + */ + public function max() + { + return getrandmax(); + } + + /** + * @param integer $seed + * @return self + */ + public function seed($seed) + { + srand($seed); + return $this; + } +} diff --git a/src/Random/RandomRange.php b/src/Random/RandomRange.php index 2658d798..e9b2880d 100644 --- a/src/Random/RandomRange.php +++ b/src/Random/RandomRange.php @@ -43,6 +43,9 @@ public function rand($lower = null, $upper = null) return $this->source->extractNumber(); } + if ($lower > $upper) { + list($lower, $upper) = [$upper, $lower]; + } $delta = $upper - $lower; $divisor = ($this->source->max()) / ($delta + 1); diff --git a/src/Shrinker/Multiple.php b/src/Shrinker/Multiple.php index b495f31a..92d70952 100644 --- a/src/Shrinker/Multiple.php +++ b/src/Shrinker/Multiple.php @@ -71,7 +71,7 @@ public function from(GeneratedValueSingle $elements, $exception) if ($this->timeLimit->hasBeenReached()) { throw new \RuntimeException( "Eris has reached the time limit for shrinking ($this->timeLimit), here it is presenting the simplest failure case." . PHP_EOL - . "If you can afford to spend more time to find a simpler failing input, increase it with \$this->shrinkingTimeLimit(\$seconds).", + . "If you can afford to spend more time to find a simpler failing input, increase it with the annotation \'@eris-shrink {seconds}\'.", -1, $exception ); diff --git a/src/TestTrait.php b/src/TestTrait.php index 2b292ea9..32431ef3 100644 --- a/src/TestTrait.php +++ b/src/TestTrait.php @@ -1,9 +1,15 @@ seedingRandomNumberGeneration(); - $this->minimumEvaluationRatio(0.5); + $this->seed = intval(getenv('ERIS_SEED') ?: (microtime(true)*1000000)); + if ($this->seed < 0) { + $this->seed *= -1; + } + $this->listeners = array_filter( + $this->listeners, + function ($listener) { + return !($listener instanceof MinimumEvaluations); + } + ); + $tags = $this->getAnnotations();//from TestCase of PHPunit + $this->withRand($this->getAnnotationValue($tags, 'eris-method', 'rand', 'strval')); + $this->iterations = $this->getAnnotationValue($tags, 'eris-repeat', 100, 'intval'); + $this->shrinkingTimeLimit = $this->getAnnotationValue($tags, 'eris-shrink', null, 'intval'); + $this->listeners[] = MinimumEvaluations::ratio($this->getAnnotationValue($tags, 'eris-ratio', 50, 'floatval')/100); + $duration = $this->getAnnotationValue($tags, 'eris-duration', false, 'strval'); + if ($duration) { + $terminationCondition = new TimeBasedTerminationCondition('time', new DateInterval($duration)); + $this->listeners[] = $terminationCondition; + $this->terminationConditions[] = $terminationCondition; + } } /** - * @after + * @param array $annotations + * @param string $key + * @param mixed $default + * @return mixed */ - public function erisTeardown() + private function getAnnotationValue(array $annotations, $key, $default, $cast) { - $this->dumpSeedForReproducing(); - } - - private function seedingRandomNumberGeneration() - { - if ($seed = getenv('ERIS_SEED')) { - $this->seed = $seed; - } else { - $this->seed = (int) (microtime(true)*1000000); - } + $annotation = $this->getAnnotation($annotations, $key); + return isset($annotation[0])?$cast($annotation[0]):$default; } /** - * Maybe: we could add --filter options to the command here, - * since now the original command is printed. + * @param array $annotations + * @param string $key + * @return array */ - private function dumpSeedForReproducing() + private function getAnnotation(array $annotations, $key) { - if ($this->hasFailed()) { - global $argv; - $command = PHPUnitCommand::fromSeedAndName($this->seed, $this->toString()); - echo PHP_EOL; - echo "Reproduce with:", PHP_EOL; - echo $command, PHP_EOL; + if (isset($annotations['method'][$key])) { + return $annotations['method'][$key]; } + return isset($annotations['class'][$key])?$annotations['class'][$key]:[]; } /** - * @param float from 0.0 to 1.0 - * @return self + * @after */ - protected function minimumEvaluationRatio($ratio) - { - $this->filterOutListenersOfClass('Eris\\Listener\\MinimumEvaluations'); - $this->listeners[] = Listener\MinimumEvaluations::ratio($ratio); - return $this; - } - - private function filterOutListenersOfClass($className) + public function erisTeardown() { - $this->listeners = array_filter( - $this->listeners, - function ($listener) use ($className) { - return !($listener instanceof $className); - } - ); + $this->dumpSeedForReproducing(); } /** - * @param integer|DateInterval - * @return self + * Maybe: we could add --filter options to the command here, + * since now the original command is printed. */ - protected function limitTo($limit) + private function dumpSeedForReproducing() { - if ($limit instanceof DateInterval) { - $interval = $limit; - $terminationCondition = new Quantifier\TimeBasedTerminationCondition('time', $interval); - $this->listeners[] = $terminationCondition; - $this->terminationConditions[] = $terminationCondition; - } elseif (is_integer($limit)) { - $this->iterations = $limit; - } else { - throw new InvalidArgumentException("The limit " . var_export($limit, true) . " is not valid. Please pass an integer or DateInterval."); + if (!$this->hasFailed()) { + return; } - return $this; - } - - /** - * The maximum time to spend trying to shrink the input after a failed test. - * The default is no limit. - * - * @param integer in seconds - * @return self - */ - protected function shrinkingTimeLimit($shrinkingTimeLimit) - { - $this->shrinkingTimeLimit = $shrinkingTimeLimit; - return $this; + $command = PHPUnitCommand::fromSeedAndName($this->seed, $this->toString()); + echo PHP_EOL."Reproduce with:".PHP_EOL.$command.PHP_EOL; } /** @@ -130,44 +118,33 @@ protected function shrinkingTimeLimit($shrinkingTimeLimit) */ protected function withRand($randFunction) { - // TODO: invert and wrap rand, srand into objects? - if ($randFunction instanceof \Eris\Random\RandomRange) { - $this->randFunction = function ($lower = null, $upper = null) use ($randFunction) { - return $randFunction->rand($lower, $upper); - }; - $this->seedFunction = function ($seed) use ($randFunction) { - return $randFunction->seed($seed); - }; + if ($randFunction === 'mt_rand') { + $this->randFunction = new RandomRange(new MtRandSource()); + return $this; } - if (is_callable($randFunction)) { - switch ($randFunction) { - case 'rand': - $seedFunction = 'srand'; - break; - case 'mt_rand': - $seedFunction = 'mt_srand'; - break; - default: - throw new BadMethodCallException("When specifying random generators different from the standard ones, you must also pass a \$seedFunction callable that will be called to seed it."); - } + if ($randFunction === 'rand') { + $this->randFunction = new RandomRange(new RandSource()); + return $this; + } + if ($randFunction instanceof RandomRange) { $this->randFunction = $randFunction; - $this->seedFunction = $seedFunction; + return $this; } - return $this; + throw new BadMethodCallException("When specifying random generators different from the standard ones, you must also pass a \$seedFunction callable that will be called to seed it."); } /** * forAll($generator1, $generator2, ...) - * @return Quantifier\ForAll + * @return ForAll */ public function forAll() { - call_user_func($this->seedFunction, $this->seed); + $this->randFunction->seed($this->seed); $generators = func_get_args(); - $quantifier = new Quantifier\ForAll( + $quantifier = new ForAll( $generators, $this->iterations, - new Shrinker\ShrinkerFactory([ + new ShrinkerFactory([ 'timeLimit' => $this->shrinkingTimeLimit, ]), $this->shrinkerFactoryMethod, diff --git a/test/ExampleEnd2EndTest.php b/test/ExampleEnd2EndTest.php index 593613c0..089bc8a7 100644 --- a/test/ExampleEnd2EndTest.php +++ b/test/ExampleEnd2EndTest.php @@ -274,13 +274,15 @@ private function runExample($testFile) { $this->testFile = $testFile; $examplesDir = realpath(__DIR__ . '/../examples'); - $samplesTestCase = $examplesDir . '/' . $testFile; + $samplesTestCase = $examplesDir . DIRECTORY_SEPARATOR . $testFile; $logFile = tempnam(sys_get_temp_dir(), 'phpunit_log_'); $environmentVariables = []; foreach ($this->environment as $name => $value) { - $environmentVariables[] .= "$name=$value"; + $var = "$name=$value"; + $environmentVariables[] = DIRECTORY_SEPARATOR==='\\' ? "set $var && " : $var; } - $phpunitCommand = implode(" ", $environmentVariables) . " vendor/bin/phpunit --log-junit $logFile $samplesTestCase"; + $bin = "vendor".DIRECTORY_SEPARATOR."bin".DIRECTORY_SEPARATOR."phpunit"; + $phpunitCommand = implode(" ", $environmentVariables) . " $bin --log-junit $logFile $samplesTestCase"; exec($phpunitCommand, $output); $contentsOfXmlLog = file_get_contents($logFile); if (!$contentsOfXmlLog) { diff --git a/test/Generator/AssociativeArrayGeneratorTest.php b/test/Generator/AssociativeArrayGeneratorTest.php index a1dc9002..3471e0c1 100644 --- a/test/Generator/AssociativeArrayGeneratorTest.php +++ b/test/Generator/AssociativeArrayGeneratorTest.php @@ -9,7 +9,7 @@ protected function setUp() $this->cipherGenerator = ElementsGenerator::fromArray([0, 1, 2]); $this->smallIntegerGenerator = new ChooseGenerator(0, 100); $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testConstructWithAnAssociativeArrayOfGenerators() diff --git a/test/Generator/BindGeneratorTest.php b/test/Generator/BindGeneratorTest.php index fc1f7312..fac1f188 100644 --- a/test/Generator/BindGeneratorTest.php +++ b/test/Generator/BindGeneratorTest.php @@ -6,7 +6,7 @@ class BindGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGeneratesAGeneratedValueObject() diff --git a/test/Generator/BooleanGeneratorTest.php b/test/Generator/BooleanGeneratorTest.php index b6e40a05..ba103e4b 100644 --- a/test/Generator/BooleanGeneratorTest.php +++ b/test/Generator/BooleanGeneratorTest.php @@ -5,7 +5,7 @@ class BooleanGeneratorTest extends \PHPUnit_Framework_TestCase { public function setUp() { - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testRandomlyPicksTrueOrFalse() diff --git a/test/Generator/CharacterGeneratorTest.php b/test/Generator/CharacterGeneratorTest.php index 9d511287..d85c8d25 100644 --- a/test/Generator/CharacterGeneratorTest.php +++ b/test/Generator/CharacterGeneratorTest.php @@ -6,7 +6,7 @@ class CharacterGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 0; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testBasicAsciiCharacterGenerators() diff --git a/test/Generator/ChooseGeneratorTest.php b/test/Generator/ChooseGeneratorTest.php index deae0b3c..def11728 100644 --- a/test/Generator/ChooseGeneratorTest.php +++ b/test/Generator/ChooseGeneratorTest.php @@ -6,7 +6,7 @@ class ChooseGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 0; // ignored by this kind of generator - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testPicksRandomlyAnIntegerAmongBoundaries() diff --git a/test/Generator/ConstantGeneratorTest.php b/test/Generator/ConstantGeneratorTest.php index afb5df17..ed4641ff 100644 --- a/test/Generator/ConstantGeneratorTest.php +++ b/test/Generator/ConstantGeneratorTest.php @@ -6,7 +6,7 @@ class ConstantGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 0; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testPicksAlwaysTheValue() diff --git a/test/Generator/DateGeneratorTest.php b/test/Generator/DateGeneratorTest.php index 30eebcbe..4ea26e6f 100644 --- a/test/Generator/DateGeneratorTest.php +++ b/test/Generator/DateGeneratorTest.php @@ -8,7 +8,7 @@ class DateGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGenerateDateTimeObjectsInTheGivenInterval() diff --git a/test/Generator/ElementsGeneratorTest.php b/test/Generator/ElementsGeneratorTest.php index 47f98f19..829e3d79 100644 --- a/test/Generator/ElementsGeneratorTest.php +++ b/test/Generator/ElementsGeneratorTest.php @@ -6,7 +6,7 @@ class ElementsGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGeneratesOnlyArgumentsInsideTheGivenArray() diff --git a/test/Generator/FloatGeneratorTest.php b/test/Generator/FloatGeneratorTest.php index b0c371a3..c214b799 100644 --- a/test/Generator/FloatGeneratorTest.php +++ b/test/Generator/FloatGeneratorTest.php @@ -6,7 +6,7 @@ class FloatGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 300; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testPicksUniformelyPositiveAndNegativeFloatNumbers() diff --git a/test/Generator/FrequencyGeneratorTest.php b/test/Generator/FrequencyGeneratorTest.php index f6f48af3..97269082 100644 --- a/test/Generator/FrequencyGeneratorTest.php +++ b/test/Generator/FrequencyGeneratorTest.php @@ -6,7 +6,7 @@ class FrequencyGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testEqualProbability() diff --git a/test/Generator/IntegerGeneratorTest.php b/test/Generator/IntegerGeneratorTest.php index 09f8abc4..88a17c5b 100644 --- a/test/Generator/IntegerGeneratorTest.php +++ b/test/Generator/IntegerGeneratorTest.php @@ -6,7 +6,7 @@ class IntegerGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testPicksRandomlyAnInteger() diff --git a/test/Generator/MapGeneratorTest.php b/test/Generator/MapGeneratorTest.php index 79885e0c..fed8df73 100644 --- a/test/Generator/MapGeneratorTest.php +++ b/test/Generator/MapGeneratorTest.php @@ -6,7 +6,7 @@ class MapGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGeneratesAGeneratedValueObject() diff --git a/test/Generator/NamesGeneratorTest.php b/test/Generator/NamesGeneratorTest.php index b024c699..55fce511 100644 --- a/test/Generator/NamesGeneratorTest.php +++ b/test/Generator/NamesGeneratorTest.php @@ -5,7 +5,7 @@ class NamesGeneratorTest extends \PHPUnit_Framework_TestCase { public function setUp() { - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testItRespectsTheGenerationSize() diff --git a/test/Generator/OneOfGeneratorTest.php b/test/Generator/OneOfGeneratorTest.php index cab4edf3..9dccb485 100644 --- a/test/Generator/OneOfGeneratorTest.php +++ b/test/Generator/OneOfGeneratorTest.php @@ -7,7 +7,7 @@ protected function setUp() { $this->singleElementGenerator = new ChooseGenerator(0, 100); $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testConstructWithAnArrayOfGenerators() diff --git a/test/Generator/RegexGeneratorTest.php b/test/Generator/RegexGeneratorTest.php index 3eb1fee0..b184c1c4 100644 --- a/test/Generator/RegexGeneratorTest.php +++ b/test/Generator/RegexGeneratorTest.php @@ -18,7 +18,7 @@ public static function supportedRegexes() protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } /** diff --git a/test/Generator/SequenceGeneratorTest.php b/test/Generator/SequenceGeneratorTest.php index 678ed235..b3188c14 100644 --- a/test/Generator/SequenceGeneratorTest.php +++ b/test/Generator/SequenceGeneratorTest.php @@ -7,7 +7,7 @@ protected function setUp() { $this->size = 100; $this->singleElementGenerator = new ChooseGenerator(10, 100); - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testRespectsGenerationSize() diff --git a/test/Generator/SetGeneratorTest.php b/test/Generator/SetGeneratorTest.php index 859f325e..96b297ae 100644 --- a/test/Generator/SetGeneratorTest.php +++ b/test/Generator/SetGeneratorTest.php @@ -7,7 +7,7 @@ protected function setUp() { $this->size = 100; $this->singleElementGenerator = new ChooseGenerator(10, 100); - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testRespectsGenerationSize() diff --git a/test/Generator/StringGeneratorTest.php b/test/Generator/StringGeneratorTest.php index 47acb39b..51689d0a 100644 --- a/test/Generator/StringGeneratorTest.php +++ b/test/Generator/StringGeneratorTest.php @@ -5,7 +5,7 @@ class StringGeneratorTest extends \PHPUnit_Framework_TestCase { public function setUp() { - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testRandomlyPicksLengthAndCharacters() diff --git a/test/Generator/SubsetGeneratorTest.php b/test/Generator/SubsetGeneratorTest.php index abd7e367..1708e29b 100644 --- a/test/Generator/SubsetGeneratorTest.php +++ b/test/Generator/SubsetGeneratorTest.php @@ -10,7 +10,7 @@ protected function setUp() $this->universe = ['a', 'b', 'c', 'd', 'e']; $this->generator = new SubsetGenerator($this->universe); $this->size = 100; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testScalesGenerationSizeToTouchAllPossibleSubsets() diff --git a/test/Generator/SuchThatGeneratorTest.php b/test/Generator/SuchThatGeneratorTest.php index 0558f4c4..1a0a05fe 100644 --- a/test/Generator/SuchThatGeneratorTest.php +++ b/test/Generator/SuchThatGeneratorTest.php @@ -6,7 +6,7 @@ class SuchThatGeneratorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGeneratesAGeneratedValueObject() diff --git a/test/Generator/TupleGeneratorTest.php b/test/Generator/TupleGeneratorTest.php index 20d6c77f..63e38120 100644 --- a/test/Generator/TupleGeneratorTest.php +++ b/test/Generator/TupleGeneratorTest.php @@ -7,7 +7,7 @@ protected function setUp() { $this->generatorForSingleElement = new ChooseGenerator(0, 100); $this->size = 10; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } private function assertInSingleElementGenerator($value) diff --git a/test/Generator/VectorGeneratorTest.php b/test/Generator/VectorGeneratorTest.php index 6129a05d..49b6b8d3 100644 --- a/test/Generator/VectorGeneratorTest.php +++ b/test/Generator/VectorGeneratorTest.php @@ -13,7 +13,7 @@ protected function setUp() $acc = $acc + $item; return $acc; }; - $this->rand = 'rand'; + $this->rand = new \Eris\Random\RandomRange(new \Eris\Random\RandSource()); } public function testGeneratesVectorWithGivenSizeAndElementsFromGivenGenerator() diff --git a/test/Listener/LogTest.php b/test/Listener/LogTest.php index cd743c8a..7c52cd2a 100644 --- a/test/Listener/LogTest.php +++ b/test/Listener/LogTest.php @@ -1,22 +1,24 @@ file = '/tmp/eris-log-unit-test.log'; + $this->timezone = date_default_timezone_get(); + $this->file = sys_get_temp_dir().'/eris-log-unit-test.log'; $this->time = function () { return 1300000000; }; $this->log = new Log($this->file, $this->time, 1234); + date_default_timezone_set('UTC'); } public function tearDown() { $this->log->endPropertyVerification(null, null); + date_default_timezone_set($this->timezone); } public function testWritesALineForEachIterationShowingItsIndex()