public function testConvertsBackAndForthMongoDatesWithoutLosingMillisecondPrecision() { $this->forAll(Generator\choose(0, 1500 * 1000 * 1000))->then(function ($milliseconds) { $moment = new Moment($milliseconds); $this->assertEquals($moment, MongoDate::toMoment(MongoDate::from($moment))); }); }
public function testConcatenationMaintainsLength() { $this->forAll(Generator\tuple(Generator\elements("A", "B", "C"), Generator\choose(0, 9)))->then(function ($tuple) { $letter = $tuple[0]; $cipher = $tuple[1]; $this->assertEquals(2, strlen($letter . $cipher), "{$letter}{$cipher} is not a 2-char string"); }); }
public function testShrinkingRespectsAntecedents() { $this->forAll(Generator\choose(0, 20))->when(function ($number) { return $number > 10; })->then(function ($number) { $this->assertTrue($number % 29 == 0, "The number {$number} is not multiple of 29"); }); }
public function testSumOfTwoIntegersFromBoundedRangesIsCommutative() { $this->forAll(Generator\choose(-1000, 430), Generator\choose(230, -30000))->then(function ($first, $second) { $x = $first + $second; $y = $second + $first; $this->assertEquals($x, $y, "Sum between {$first} and {$second} should be commutative"); }); }
public function testFromDayOfYearFactoryMethodRespectsDistanceBetweenDays() { $this->forAll(Generator\choose(2000, 2020), Generator\choose(0, 364), Generator\choose(0, 364))->then(function ($year, $dayOfYear, $anotherDayOfYear) { $day = fromZeroBasedDayOfYear($year, $dayOfYear); $anotherDay = fromZeroBasedDayOfYear($year, $anotherDayOfYear); $this->assertEquals(abs($dayOfYear - $anotherDayOfYear) * 86400, abs($day->getTimestamp() - $anotherDay->getTimestamp()), "Days of the year {$year}: {$dayOfYear}, {$anotherDayOfYear}" . PHP_EOL . "{$day->format(DateTime::ISO8601)}, {$anotherDay->format(DateTime::ISO8601)}"); }); }
public function testPassesBecauseOfTheArtificiallyLowMinimumEvaluationRatio() { $this->minimumEvaluationRatio(0.01)->forAll(Generator\choose(0, 100))->when(function ($n) { return $n > 90; })->then(function ($number) { $this->assertTrue($number * 2 > 90 * 2); }); }
public function testAssociativeArraysGeneratedOnStandardKeys() { $this->forAll(Generator\associative(['letter' => Generator\elements("A", "B", "C"), 'cipher' => Generator\choose(0, 9)]))->then(function ($array) { $this->assertEquals(2, count($array)); $letter = $array['letter']; $this->assertInternalType('string', $letter); $cipher = $array['cipher']; $this->assertInternalType('integer', $cipher); }); }
public function testNotWithstandingCrashesJobsAreEventuallyPerformed() { $this->limitTo(100)->forAll(Generator\bind(Generator\choose(1, 4), function ($workers) { return Generator\tuple(Generator\constant($workers), Generator\seq(Generator\oneOf(Generator\map(function ($durationAndTag) { list($duration, $tag) = $durationAndTag; return ['enqueueJob', $duration, $tag]; }, Generator\tuple(Generator\nat(), Generator\elements(['generic', 'fast-lane']))), Generator\map(function ($workerIndex) { return ['restartWorkerGracefully', $workerIndex]; }, Generator\choose(0, $workers - 1)), Generator\map(function ($workerIndex) { return ['restartWorkerByKilling', $workerIndex]; }, Generator\choose(0, $workers - 1)), Generator\constant('restartRecruiterGracefully'), Generator\constant('restartRecruiterByKilling'), Generator\map(function ($milliseconds) { return ['sleep', $milliseconds]; }, Generator\choose(1, 1000))))); }))->hook(Listener\log('/tmp/recruiter-test-iterations.log'))->hook(Listener\collectFrequencies())->disableShrinking()->then(function ($tuple) { list($workers, $actions) = $tuple; $this->clean(); $this->start($workers); foreach ($actions as $action) { $this->logAction($action); if (is_array($action)) { $arguments = $action; $method = array_shift($arguments); call_user_func_array([$this, $method], $arguments); } else { $this->{$action}(); } } $estimatedTime = max(count($actions) * 4, 60); Timeout::inSeconds($estimatedTime, function () { return "all {$this->jobs} jobs to be performed. Now is " . date('c') . " Logs: " . $this->files(); })->until(function () { return $this->jobRepository->countArchived() === $this->jobs; }); $at = T\now(); $statistics = $this->recruiter->statistics($tag = null, $at); $this->assertInvariantsOnStatistics($statistics); // TODO: remove duplication $statisticsByTag = []; $cumulativeThroughput = 0; foreach (['generic', 'fast-lane'] as $tag) { $statisticsByTag[$tag] = $this->recruiter->statistics($tag, $at); $this->assertInvariantsOnStatistics($statisticsByTag[$tag]); $cumulativeThroughput += $statisticsByTag[$tag]['throughput']['value']; } var_dump($statistics, $statisticsByTag); // TODO: add tolerance $this->assertEquals($statistics['throughput']['value'], $cumulativeThroughput); }); }
public function testDoesNotPerformTheCheckTooManyTimes() { $this->forAll(Generator\date(), Generator\choose(10, 30), Generator\seq(Generator\choose(1, 60)))->then(function ($startingDate, $period, $deltas) { $clock = new SettableClock($startingDate); $check = PeriodicalCheck::every($period, $clock); $this->counter = 0; $check->onFire(function () { $this->counter++; }); $check->__invoke(); foreach ($deltas as $delta) { $clock->advance($delta); $check->__invoke(); } $totalInterval = array_sum($deltas); $maximumNumberOfCalls = ceil($totalInterval / $period); $actualNumberOfCallsExcludingTheFirst = $this->counter - 1; $this->assertLessThanOrEqual($maximumNumberOfCalls, $actualNumberOfCallsExcludingTheFirst); }); }
public function testSuchThatShrinkingRespectsTheConditionButTriesToSkipOverTheNotAllowedSet() { $this->forAll(Generator\suchThat(function ($n) { return $n != 42; }, Generator\choose(0, 1000)))->then($this->numberIsBiggerThan(100)); }
/** * The current implementation shows no problem as PHPUnit prefers to show * the exception from the test method than the one from teardown * when both fail. */ public function testWhenFailingWillNaturallyHaveALowEvaluationRatioSoWeDontWantThatErrorToObscureTheTrueOne() { $this->forAll(Generator\choose(0, 1000))->when($this->greaterThan(100))->then(function ($number) { $this->assertTrue($number <= 100, "\$number should be less or equal to 100, but it is {$number}"); }); }
public function testNaturalNumbersMagnitude() { $this->forAll(Generator\choose(0, 1000))->then(function ($number) { $this->assertTrue($number < 42, "{$number} is not less than 42 apparently"); }); }
public function testFromOneDayOfYearFactoryMethodRespectsDistanceBetweenDays() { $this->forAll(Generator\choose(2000, 2020), Generator\choose(1, 365), Generator\choose(1, 365))->then(function ($year, $dayOfYear, $anotherDayOfYear) { $day = UTCDateTime::fromOneBasedDayOfYear($year, $dayOfYear); $anotherDay = UTCDateTime::fromOneBasedDayOfYear($year, $anotherDayOfYear); $this->assertEquals(abs($dayOfYear - $anotherDayOfYear) * 86400, abs($day->differenceInSeconds($anotherDay)), "Days of the year {$year}: {$dayOfYear}, {$anotherDayOfYear}" . PHP_EOL . "{$day->toIso8601()}, {$anotherDay->toIso8601()}"); }); }
public function testAlwaysFails() { $this->forAll(Generator\frequency([8, Generator\choose(1, 100)], [4, Generator\choose(100, 200)], [4, Generator\choose(200, 300)]))->then(function ($element) { $this->assertEquals(0, $element); }); }