From 447ea83bf077e707fc3e34101b4530088ef5094c Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sun, 25 Feb 2024 18:20:20 +0100 Subject: [PATCH] Switch to only have solve() instead of __constructor() + solve() --- .../practice/two-bucket/.meta/example.php | 6 +-- exercises/practice/two-bucket/TwoBucket.php | 7 +--- .../practice/two-bucket/TwoBucketTest.php | 38 +++++++++---------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php index f039063c..b9ed88f0 100644 --- a/exercises/practice/two-bucket/.meta/example.php +++ b/exercises/practice/two-bucket/.meta/example.php @@ -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)]; @@ -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; diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index d8d2fc27..7bd58607 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -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__)); } diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php index 4651c1b5..63b2ea82 100644 --- a/exercises/practice/two-bucket/TwoBucketTest.php +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -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 */ @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -111,8 +112,8 @@ 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'); } /** @@ -120,8 +121,7 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void */ 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); @@ -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); @@ -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'); } }