Skip to content

Commit

Permalink
Merge pull request #22 from daylerees/develop
Browse files Browse the repository at this point in the history
PHP 7 Version
  • Loading branch information
daylerees authored Mar 14, 2018
2 parents e4d88a8 + beed3c4 commit 3143ea6
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 166 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php
php:
- '5.5'
- '5.6'
- '7.0'
- hhvm
- nightly
- '7.1'
- '7.2'
- 'nightly'
matrix:
allow_failures:
- php: nightly
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
}
],
"require": {
"php": "^5.5 || ^7.0",
"ircmaxell/random-lib": "^1.2"
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": ">=4.8",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^2.5",
"jakub-onderka/php-parallel-lint": "^0.9.2",
"phing/phing": "^2.13"
Expand Down
23 changes: 3 additions & 20 deletions src/Chances/StandardChance.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
<?php
namespace Scientist\Chances;

use RandomLib\Factory;
use RandomLib\Generator;

class StandardChance implements Chance
{
private $generator;

private $percentage = 100;

/**
* StandardChance constructor.
* @param Generator|null $generator
*/
public function __construct(Generator $generator = null)
{
if ($generator === null) {
$factory = new Factory;
$generator = $factory->getLowStrengthGenerator();
}
$this->generator = $generator;
}

/**
* Determine whether or not the experiment should run
*/
Expand All @@ -32,8 +14,8 @@ public function shouldRun()
return false;
}

$random = $this->generator
->generateInt(0, 100);
$random = random_int(0, 100);

return $random <= $this->percentage;
}

Expand All @@ -52,6 +34,7 @@ public function getPercentage()
public function setPercentage($percentage)
{
$this->percentage = $percentage;

return $this;
}
}
4 changes: 2 additions & 2 deletions src/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Scientist;

use Exception;
use Throwable;

/**
* Class Executor
Expand Down Expand Up @@ -102,7 +102,7 @@ protected function executeMutedCallback()
{
try {
$this->result->setValue(call_user_func_array($this->callback, $this->params));
} catch (Exception $exception) {
} catch (Throwable $exception) {
$this->result->setException($exception);
$this->result->setValue(null);
}
Expand Down
120 changes: 2 additions & 118 deletions tests/Chances/StandardChanceTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
<?php
namespace Scientist\Chances;

use RandomLib\Generator;

class StandardChanceTest extends \PHPUnit_Framework_TestCase
class StandardChanceTest extends \PHPUnit\Framework\TestCase
{
/**
* @var StandardChance
*/
private $chance;

/**
* @var Generator
*/
private $generator;

public function setUp()
{
$this->generator = $this->getMockGenerator();
$this->chance = new StandardChance($this->generator);
$this->chance = new StandardChance();
}

public function test_that_standard_chance_is_an_instance_of_chance()
Expand All @@ -27,39 +19,6 @@ public function test_that_standard_chance_is_an_instance_of_chance()
$this->assertInstanceOf('\Scientist\Chances\Chance', $chance);
}

public function test_that_a_random_number_generator_is_created_upon_instantiation()
{
$chance = new StandardChance();
$reflection = new \ReflectionClass($chance);
$property = $reflection->getProperty('generator');
$property->setAccessible(true);

$this->assertInstanceOf('\RandomLib\Generator', $property->getValue($chance));
}

public function test_that_it_takes_a_custom_random_number_generator_in_the_constructor()
{
$reflection = new \ReflectionClass($this->chance);
$property = $reflection->getProperty('generator');
$property->setAccessible(true);

$this->assertSame($this->generator, $property->getValue($this->chance));
}

/**
* @dataProvider percentageDataProvider
*/
public function test_that_should_run_returns_true_when_the_chance_is_100($random)
{
$this->generator
->expects($this->once())
->method('generateInt')
->with(0, 100)
->willReturn($random);

$this->assertTrue($this->chance->shouldRun());
}

public function test_that_the_default_percentage_is_100()
{
$this->assertEquals(100, $this->chance->getPercentage());
Expand All @@ -78,79 +37,4 @@ public function test_that_set_percentage_returns_the_chance_object_for_chaining(
$percentage = rand(1, 100);
$this->assertSame($this->chance, $this->chance->setPercentage($percentage));
}

public function test_that_should_run_always_returns_false_when_percentage_is_zero()
{
$this->generator
->expects($this->never())
->method('generateInt');
$this->chance
->setPercentage(0);
$this->assertFalse($this->chance->shouldRun());
}

/**
* @dataProvider nonZeroPercentageDataProvider
* @param integer $percentage Percentage of the time to run
*/
public function test_that_it_returns_true_when_percentage_is_greater_than_the_generated_number($percentage)
{
$this->generator
->expects($this->once())
->method('generateInt')
->with(0, 100)
->willReturn($percentage - 1);

$this->chance
->setPercentage($percentage);

$this->assertTrue($this->chance->shouldRun());
}

/**
* @dataProvider nonZeroPercentageDataProvider
* @param integer $percentage Percentage of the time to run
*/
public function test_that_it_returns_false_when_percentage_is_less_than_the_generated_number($percentage)
{
$this->generator
->expects($this->once())
->method('generateInt')
->with(0, 100)
->willReturn($percentage + 1);

$this->chance
->setPercentage($percentage);

$this->assertFalse($this->chance->shouldRun());
}

/**
* @return array
*/
public function nonZeroPercentageDataProvider()
{
$percentages = $this->percentageDataProvider();
array_shift($percentages);
return $percentages;
}

/**
* Data provider to cover all 100 percentage values
* @return array
*/
public function percentageDataProvider()
{
return array_map(function ($value) {
return [$value];
}, range(0, 100));
}

public function getMockGenerator()
{
return $this->getMockBuilder('\RandomLib\Generator')
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();
}
}
4 changes: 2 additions & 2 deletions tests/ExperimentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Scientist\Laboratory;
use Scientist\Matchers\StandardMatcher;

class ExperimentTest extends PHPUnit_Framework_TestCase
class ExperimentTest extends \PHPUnit\Framework\TestCase
{
public function test_that_a_new_experiment_can_be_created()
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public function test_that_multiple_trial_callbacks_can_be_defined()

public function test_that_a_chance_variable_can_be_set()
{
$chance = $this->getMock('\Scientist\Chances\Chance');
$chance = $this->createMock('\Scientist\Chances\Chance');
$e = new Experiment('test experiment', new Laboratory);
$e->chance($chance);
$this->assertEquals($chance, $e->getChance());
Expand Down
2 changes: 1 addition & 1 deletion tests/InternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Scientist\Experiment;
use Scientist\Laboratory;

class InternTest extends PHPUnit_Framework_TestCase
class InternTest extends \PHPUnit\Framework\TestCase
{
public function test_that_intern_can_be_created()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Journals/JournalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Scientist\Experiment;
use Scientist\Journals\StandardJournal;

class JournalTest extends PHPUnit_Framework_TestCase
class JournalTest extends \PHPUnit\Framework\TestCase
{
public function test_that_journals_can_be_created()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/LaboratoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Scientist\Report;
use Scientist\Laboratory;

class LaboratoryTest extends PHPUnit_Framework_TestCase
class LaboratoryTest extends \PHPUnit\Framework\TestCase
{
public function test_laboratory_can_be_created()
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function test_laboratory_can_fetch_report_for_experiment_with_no_journals

public function test_that_exceptions_are_thrown_within_control()
{
$this->setExpectedException(Exception::class);
$this->expectException(Exception::class);

$v = (new Laboratory)
->experiment('test experiment')
Expand Down
31 changes: 24 additions & 7 deletions tests/MachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
use Scientist\Result;
use Scientist\Machine;

class MachineTest extends PHPUnit_Framework_TestCase
class MachineTest extends \PHPUnit\Framework\TestCase
{
public function test_that_machine_can_be_created()
{
new Machine(function () {});
$m = new Machine(function () {});

$this->assertInstanceOf(Machine::class, $m);
}

public function test_that_machine_can_receive_parameters()
{
new Machine(function () {}, [1, 2, 3]);
$m = new Machine(function () {}, [1, 2, 3]);

$this->assertInstanceOf(Machine::class, $m);
}

public function test_that_machine_can_receive_mutable_state()
{
new Machine(function () {}, [1, 2, 3], true);
$m = new Machine(function () {}, [1, 2, 3], true);

$this->assertInstanceOf(Machine::class, $m);
}

public function test_that_machine_can_produce_a_result()
Expand Down Expand Up @@ -47,14 +53,25 @@ public function test_that_exceptions_can_be_thrown_by_machine_callback_execution
{
$m = new Machine(function () { throw new Exception('foo'); });

$this->setExpectedException(Exception::class);
$this->expectException(Exception::class);

$m->execute();
}

public function test_that_machine_can_mute_exceptions_from_callback()
public static function getErrorData()
{
return [
[new Exception()],
[new Error()],
];
}

/**
* @dataProvider getErrorData
*/
public function test_that_machine_can_mute_exceptions_from_callback($exception)
{
$m = new Machine(function () { throw new Exception('foo'); }, [], true);
$m = new Machine(function () use ($exception) { throw $exception; }, [], true);

$this->assertEquals(null, $m->execute()->getValue());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Matchers/ClosureMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Scientist\Matchers\ClosureMatcher;

class ClosureMatcherTest extends PHPUnit_Framework_TestCase
class ClosureMatcherTest extends \PHPUnit\Framework\TestCase
{
private function getClosure()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Matchers/StandardMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Scientist\Matchers\StandardMatcher;

class StandardMatcherTest extends PHPUnit_Framework_TestCase
class StandardMatcherTest extends \PHPUnit\Framework\TestCase
{
public function test_that_standard_matcher_can_be_created()
{
Expand Down
6 changes: 4 additions & 2 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
use Scientist\Result;
use Scientist\Report;

class ReportTest extends PHPUnit_Framework_TestCase
class ReportTest extends \PHPUnit\Framework\TestCase
{
public function test_that_report_can_be_created()
{
$r = new Result;
new Report('foo', $r, []);
$rep = new Report('foo', $r, []);

$this->assertInstanceOf(Report::class, $rep);
}

public function test_that_report_can_hold_experiment_name()
Expand Down
Loading

0 comments on commit 3143ea6

Please sign in to comment.