Skip to content

Commit

Permalink
Switch to only have solve() instead of __constructor() + solve()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Feb 25, 2024
1 parent 72d3903 commit 447ea83
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
6 changes: 1 addition & 5 deletions exercises/practice/two-bucket/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TwoBucket
private int $goal;
private array $buckets;

public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket)
public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket): Solution
{
$this->goal = $goal;
$this->buckets = [new Bucket('one', $sizeBucketOne), new Bucket('two', $sizeBucketTwo)];
Expand All @@ -39,10 +39,6 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s
}

$this->validate();
}

public function solve(): Solution
{
$this->first()->empty();
$this->second()->empty();
$moves = 0;
Expand Down
7 changes: 1 addition & 6 deletions exercises/practice/two-bucket/TwoBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@

class TwoBucket
{
public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket)
{
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
}

public function solve()
public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket)
{
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
}
Expand Down
38 changes: 18 additions & 20 deletions exercises/practice/two-bucket/TwoBucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

class TwoBucketTest extends PHPUnit\Framework\TestCase
{
private TwoBucket $twoBucket;

public static function setUpBeforeClass(): void
{
require_once 'TwoBucket.php';
}

protected function setUp(): void
{
$this->twoBucket = new TwoBucket();
}

/**
* uuid: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661
*/
Expand All @@ -18,8 +25,7 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck
$buckTwo = 5;
$goal = 1;
$starterBuck = 'one';
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck);

$this->assertEquals(4, $solution->numberOfActions);
$this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -35,8 +41,7 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck
$buckTwo = 5;
$goal = 1;
$starterBuck = 'two';
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck);

$this->assertEquals(8, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -52,8 +57,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc
$buckTwo = 11;
$goal = 2;
$starterBuck = 'one';
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck);

$this->assertEquals(14, $solution->numberOfActions);
$this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -69,8 +73,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc
$buckTwo = 11;
$goal = 2;
$starterBuck = 'two';
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck);

$this->assertEquals(18, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -82,8 +85,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc
*/
public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartWithBucketTwo(): void
{
$twoBucket = new TwoBucket(1, 3, 3, 'two');
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve(1, 3, 3, 'two');

$this->assertEquals(1, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -95,8 +97,7 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW
*/
public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBucketOneAndEndWithBucketTwo(): void
{
$twoBucket = new TwoBucket(2, 3, 3, 'one');
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve(2, 3, 3, 'one');

$this->assertEquals(2, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -111,17 +112,16 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void
$buckOne = 6;
$buckTwo = 15;
$this->expectException(Exception::class);
$twoBucket = new TwoBucket($buckOne, $buckTwo, 5, 'one');
$twoBucket->solve();

$this->twoBucket->solve($buckOne, $buckTwo, 5, 'one');
}

/**
* uuid: aac38b7a-77f4-4d62-9b91-8846d533b054
*/
public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWithBucketTwo(): void
{
$twoBucket = new TwoBucket(6, 15, 9, 'one');
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve(6, 15, 9, 'one');

$this->assertEquals(10, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -137,8 +137,7 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void
$buckTwo = 15;
$goal = 9;
$starterBuck = 'one';
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$solution = $twoBucket->solve();
$solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck);

$this->assertEquals(10, $solution->numberOfActions);
$this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters);
Expand All @@ -151,7 +150,6 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void
public function testGoalLargerThanBothBucketsIsImpossible(): void
{
$this->expectException(Exception::class);
$twoBucket = new TwoBucket(5, 7, 8, 'one');
$twoBucket->solve();
$this->twoBucket->solve(5, 7, 8, 'one');
}
}

0 comments on commit 447ea83

Please sign in to comment.