Skip to content

Commit

Permalink
Merge pull request #1 from rapidwebltd/analysis-8KWlee
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
DivineOmega authored Mar 21, 2018
2 parents 15de490 + 0045490 commit fd7db8f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Bucket
{
public $url;

public function __construct($url)
public function __construct($url)
{
$this->url = $url;
}
}
}
6 changes: 2 additions & 4 deletions src/BucketManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace RapidWeb\BucketTesting;

use RapidWeb\BucketTesting\Bucket;
use RapidWeb\BucketTesting\WeightedBucketSelector;
use Exception;

class BucketManager
{
private $weightedBuckets = [];

public function add(Bucket $bucket)
public function add(Bucket $bucket)
{
$this->weightedBuckets[] = new WeightedBucket($bucket);

Expand Down Expand Up @@ -56,4 +54,4 @@ public function redirect()
header('location: '.$bucket->url);
die;
}
}
}
7 changes: 3 additions & 4 deletions src/WeightedBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

namespace RapidWeb\BucketTesting;

use RapidWeb\BucketTesting\Bucket;
use Exception;

class WeightedBucket
{
public $weight = 1;
public $bucket;

public function __construct(Bucket $bucket)
public function __construct(Bucket $bucket)
{
$this->bucket = $bucket;
}

public function setWeight($weight)
{
if (!is_integer($weight) || $weight < 1) {
if (!is_int($weight) || $weight < 1) {
throw new Exception('Weight must be specified as a positive integer.');
}

$this->weight = $weight;
}
}
}
17 changes: 8 additions & 9 deletions src/WeightedBucketSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class WeightedBucketSelector
{
private $weightedBuckets;

public function __construct(array $weightedBuckets)
{
$this->weightedBuckets = $weightedBuckets;
Expand All @@ -24,8 +24,8 @@ public function getRandomBucket()
return $this->weightedBuckets[$index];
}

private function getRandomWeightedIndex() {

private function getRandomWeightedIndex()
{
$indexToWeightArray = $this->getIndexToWeightArray();

$rand = mt_rand(1, (int) array_sum($indexToWeightArray));
Expand All @@ -40,15 +40,14 @@ private function getRandomWeightedIndex() {
throw new Exception('Error retrieving random weighted index during bucket selection process.');
}

private function getIndexToWeightArray() {
private function getIndexToWeightArray()
{
$indexToWeightArray = [];

foreach($this->weightedBuckets as $index => $weightedBucket) {
foreach ($this->weightedBuckets as $index => $weightedBucket) {
$indexToWeightArray[$index] = $weightedBucket->weight;
}

return $indexToWeightArray;

}
}
}
}
9 changes: 3 additions & 6 deletions tests/Integration/WeightsTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

use PHPUnit\Framework\TestCase;
use RapidWeb\BucketTesting\BucketManager;
use RapidWeb\BucketTesting\Bucket;
use RapidWeb\BucketTesting\BucketManager;

final class WeightsTest extends TestCase
{

public function testWeights()
{
// Create a new bucket manager
$bucketManager = new BucketManager;
$bucketManager = new BucketManager();

// Add buckets, with URLs and optional weights
$bucketManager->add(new Bucket('https://google.co.uk/'))->withWeight(25);
Expand All @@ -19,7 +18,7 @@ public function testWeights()
// For testing, get thousands of random buckets and count how often the URLs appear
$urlCount = [];

for ($i=0; $i < 100000; $i++) {
for ($i = 0; $i < 100000; $i++) {
$bucket = $bucketManager->getRandomBucket();

if (!isset($urlCount[$bucket->url])) {
Expand All @@ -34,7 +33,5 @@ public function testWeights()
$this->assertLessThan(25500, $urlCount['https://google.co.uk/']);
$this->assertGreaterThan(74500, $urlCount['https://php.net/']);
$this->assertLessThan(75500, $urlCount['https://php.net/']);

}

}
8 changes: 3 additions & 5 deletions tests/Unit/ExceptionsTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<?php

use PHPUnit\Framework\TestCase;
use RapidWeb\BucketTesting\Bucket;
use RapidWeb\BucketTesting\BucketManager;
use RapidWeb\BucketTesting\WeightedBucket;
use RapidWeb\BucketTesting\Bucket;

final class ExceptionsTest extends TestCase
{

public function testGetRandomBucketWhenNoBucketsAreAdded()
{
$this->expectException(Exception::class);
(new BucketManager)->getRandomBucket();
(new BucketManager())->getRandomBucket();
}

public function testRedirectWhenNoBucketsAreAdded()
{
$this->expectException(Exception::class);
(new BucketManager)->redirect();
(new BucketManager())->redirect();
}

public function testCreatingWeightedBucketWithStringWeight()
Expand All @@ -43,5 +42,4 @@ public function testCreatingWeightedBucketWithZeroWeight()
$this->expectException(Exception::class);
(new WeightedBucket(new Bucket('https://php.net/')))->setWeight(0);
}

}

0 comments on commit fd7db8f

Please sign in to comment.