public function testSamplesFairlyWithDefaultCallback() { $mockReq = $this->getMockBuilder('WebRequest')->disableOriginalConstructor()->getMock(); $mockReq->expects($this->any())->method('getIP')->will($this->returnCallback(function () { return mt_rand(); })); $mockReq->expects($this->any())->method('getHeader')->will($this->returnCallback(function () { return mt_rand(); })); \RequestContext::getMain()->setRequest($mockReq); $config = $this->config('test', 3); $samples = 3000; $expected = $samples / $config['test']['sampleRate']; $expectedPerBucket = $expected / count($config['test']['buckets']); $allowedError = 0.25; $buckets = array(); for ($i = 0; $i < $samples; ++$i) { $ut = new UserTesting($config); if ($ut->isParticipatingIn('test')) { $bucket = $ut->getBucket('test'); if (isset($buckets[$bucket])) { $buckets[$bucket]++; } else { $buckets[$bucket] = 1; } } } unset($buckets['']); $participants = array_sum($buckets); $this->assertGreaterThan($expected * (1 - $allowedError), $participants); $this->assertLessThan($expected * (1 + $allowedError), $participants); foreach ($buckets as $bucket => $participants) { $this->assertGreaterThan($expectedPerBucket * (1 - $allowedError), $participants); $this->assertLessThan($expectedPerBucket * (1 + $allowedError), $participants); } }