From 102d2a99012e36f125450c867ac33fc3247ed00a Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Thu, 29 Apr 2021 11:19:38 +0700 Subject: [PATCH 1/6] Unit test --- .../Http/Controllers/Exercise02Controller.php | 46 ---- app/Http/Controllers/Exercise02Controller.php | 91 ++++++++ app/Http/Controllers/Exercise03Controller.php | 0 app/Http/Controllers/Exercise08Controller.php | 46 ++++ phpunit.xml | 3 + .../Controllers/Exercise02ControllerTest.php | 53 +++++ .../Controllers/Exercise03ControllerTest.php | 212 ++++++++++++++++++ .../Controllers/Exercise08ControllerTest.php | 86 +++++++ 8 files changed, 491 insertions(+), 46 deletions(-) delete mode 100644 Modules/Exercise02/Http/Controllers/Exercise02Controller.php create mode 100644 app/Http/Controllers/Exercise02Controller.php create mode 100644 app/Http/Controllers/Exercise03Controller.php create mode 100644 app/Http/Controllers/Exercise08Controller.php create mode 100644 tests/Unit/Controllers/Exercise02ControllerTest.php create mode 100644 tests/Unit/Controllers/Exercise03ControllerTest.php create mode 100644 tests/Unit/Controllers/Exercise08ControllerTest.php diff --git a/Modules/Exercise02/Http/Controllers/Exercise02Controller.php b/Modules/Exercise02/Http/Controllers/Exercise02Controller.php deleted file mode 100644 index 995716d..0000000 --- a/Modules/Exercise02/Http/Controllers/Exercise02Controller.php +++ /dev/null @@ -1,46 +0,0 @@ -atmService = $atmService; - } - - public function index() - { - return view('exercise02::index', [ - 'normalFee' => $this->atmService::NORMAL_FEE, - 'noFee' => $this->atmService::NO_FEE, - 'timePeriod1' => $this->atmService::TIME_PERIOD_1, - 'timePeriod2' => $this->atmService::TIME_PERIOD_2, - 'timePeriod3' => $this->atmService::TIME_PERIOD_3, - ]); - } - - /** - * Display a listing of the resource - * - * @return Renderable - */ - public function takeATMFee(ATMRequest $request) - { - $inputs = $request->validated(); - $fee = $this->atmService->calculate($inputs['card_id']); - - return back() - ->withInput() - ->with('calculate', [ - 'fee' => $fee, - ]); - } -} diff --git a/app/Http/Controllers/Exercise02Controller.php b/app/Http/Controllers/Exercise02Controller.php new file mode 100644 index 0000000..0af1079 --- /dev/null +++ b/app/Http/Controllers/Exercise02Controller.php @@ -0,0 +1,91 @@ +isWeekend($date) || $this->isHoliday($date)) { + return self::NORMAL_FREE; + } + //rule3 + if ($this->isTimeDiscount($date, $time)) { + return self::FREE; + } + //rule4 + return self::NORMAL_FREE; + } + + /** + * Check is weekend + * + * @param $date + * @return bool + */ + public function isWeekend($date): bool + { + if (Carbon::createFromFormat('Y-m-d', $date)->isWeekend()) { + return true; + } + + return false; + } + + /** + * Check is holiday + * + * @param $date + * @return bool + */ + public function isHoliday($date): bool + { + $date = Carbon::createFromFormat('Y-m-d', $date); + if (in_array($date->format('d-m'), self::HOLIDAYS)) { + return true; + } + + return false; + } + + /** + * Check is time discount + * + * @param $date + * @param $time + * @return bool + */ + public function isTimeDiscount($date, $time): bool + { + $date = Carbon::createFromFormat('Y-m-d H:i', $date . " " . $time); + $time = $date->format('H:i'); + list($minTime, $maxTime) = self::TIME_PERIOD; + + if ($time >= $minTime && $time <= $maxTime) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Exercise03Controller.php b/app/Http/Controllers/Exercise03Controller.php new file mode 100644 index 0000000..e69de29 diff --git a/app/Http/Controllers/Exercise08Controller.php b/app/Http/Controllers/Exercise08Controller.php new file mode 100644 index 0000000..bc723c4 --- /dev/null +++ b/app/Http/Controllers/Exercise08Controller.php @@ -0,0 +1,46 @@ +dayOfWeek; + + if ($age < 0 || $age > 120) { + return "Error"; + } + + if ($age < 13) { + return 900; + } + + if ($dayOfWeek === Carbon::TUESDAY) { + return 1200; + } + + if ($dayOfWeek === Carbon::FRIDAY && $gender === self::FEMALE) { + return 1400; + } + + if ($age > 65) { + return 1600; + } + + return 1800; + } +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 77442f0..327cb31 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -40,6 +40,9 @@ ./Modules/Exercise10/Tests + + ./tests/Unit + diff --git a/tests/Unit/Controllers/Exercise02ControllerTest.php b/tests/Unit/Controllers/Exercise02ControllerTest.php new file mode 100644 index 0000000..e547705 --- /dev/null +++ b/tests/Unit/Controllers/Exercise02ControllerTest.php @@ -0,0 +1,53 @@ +exercise02Controller = new Controllers\Exercise02Controller(); + } + + // functions test with rule in decision table + + public function test_charge_ATM_with_rule_1() + { + $result = $this->exercise02Controller->chargeATM(true); + $this->assertEquals(0, $result); + } + + public function test_charge_ATM_with_rule_2() + { + $result = $this->exercise02Controller->chargeATM(false, '2021-05-01'); + $this->assertEquals(110, $result); + } + + public function test_charge_ATM_with_rule_3() + { + $result = $this->exercise02Controller->chargeATM(false, '2021-04-28', '14:25'); + $this->assertEquals(0, $result); + } + + public function test_charge_ATM_with_rule_4() + { + $result = $this->exercise02Controller->chargeATM(false, '2021-04-28', '07:00'); + $this->assertEquals(110, $result); + } + + public function test_is_holiday() + { + $result = $this->exercise02Controller->isHoliday('2021-05-01'); + $this->assertEquals(true, $result); + } +} \ No newline at end of file diff --git a/tests/Unit/Controllers/Exercise03ControllerTest.php b/tests/Unit/Controllers/Exercise03ControllerTest.php new file mode 100644 index 0000000..4c8f092 --- /dev/null +++ b/tests/Unit/Controllers/Exercise03ControllerTest.php @@ -0,0 +1,212 @@ +exercise03Controller = new Controllers\Exercise03Controller(); + } + + // functions test with rule in decision table + + public function test_calculate_discount_with_rule_1() + { + $products = [ + [ + 'id' => 1, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 2, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 3, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 4, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 5, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 6, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 7, + 'type' => 1, + 'name' => 'cravat' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(12, $result); + } + + public function test_calculate_discount_with_rule_2() + { + $products = [ + [ + 'id' => 1, + 'type' => 3, + 'name' => 'product1' + ], + [ + 'id' => 2, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 3, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 4, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 5, + 'type' => 3, + 'name' => 'product2' + ], + [ + 'id' => 6, + 'type' => 3, + 'name' => 'product3' + ], + [ + 'id' => 7, + 'type' => 3, + 'name' => 'product4' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(7, $result); + } + + public function test_calculate_discount_with_rule_3() + { + $products = [ + [ + 'id' => 1, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 2, + 'type' => 3, + 'name' => 'product1' + ], + [ + 'id' => 3, + 'type' => 3, + 'name' => 'product6' + ], + [ + 'id' => 4, + 'type' => 3, + 'name' => 'product5' + ], + [ + 'id' => 5, + 'type' => 3, + 'name' => 'product4' + ], + [ + 'id' => 6, + 'type' => 3, + 'name' => 'product2' + ], + [ + 'id' => 7, + 'type' => 3, + 'name' => 'product3' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(7, $result); + } + + public function test_calculate_discount_with_rule_5() + { + $products = [ + [ + 'id' => 1, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 2, + 'type' => 2, + 'name' => 'white shirt' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(5, $result); + } + + public function test_calculate_discount_with_rule_6() + { + $products = [ + [ + 'id' => 1, + 'type' => 2, + 'name' => 'white shirt' + ], + [ + 'id' => 2, + 'type' => 3, + 'name' => 'product1' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(0, $result); + } + + public function test_calculate_discount_with_rule_7() + { + $products = [ + [ + 'id' => 1, + 'type' => 1, + 'name' => 'cravat' + ], + [ + 'id' => 2, + 'type' => 3, + 'name' => 'product1' + ], + ]; + + $result = $this->exercise03Controller->calculateDiscount($products); + $this->assertEquals(0, $result); + } +} \ No newline at end of file diff --git a/tests/Unit/Controllers/Exercise08ControllerTest.php b/tests/Unit/Controllers/Exercise08ControllerTest.php new file mode 100644 index 0000000..4f38994 --- /dev/null +++ b/tests/Unit/Controllers/Exercise08ControllerTest.php @@ -0,0 +1,86 @@ +exercise08Controller = new Controllers\Exercise08Controller(); + } + + // functions test with rule in decision table + + public function test_calculate_discount_with_rule_1() + { + $age = 12; + $date = '2021-05-01'; + $gender = 'male'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(900, $result); + } + + public function test_calculate_discount_with_rule_2() + { + $age = 14; + $date = '2021-04-20'; + $gender = 'male'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(1200, $result); + } + + public function test_calculate_discount_with_rule_3() + { + $age = 14; + $date = '2021-04-30'; + $gender = 'female'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(1400, $result); + } + + public function test_calculate_discount_with_rule_4() + { + $age = 14; + $date = '2021-04-30'; + $gender = 'male'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(1800, $result); + } + + public function test_calculate_discount_with_rule_5() + { + $age = 66; + $date = '2021-04-30'; + $gender = 'male'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(1600, $result); + } + + public function test_calculate_discount_with_rule_6() + { + $age = 20; + $date = '2021-05-01'; + $gender = 'female'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals(1800, $result); + } + + public function test_calculate_discount_with_case_age_invalid() + { + $age = 150; + $date = '2021-05-01'; + $gender = 'female'; + $result = $this->exercise08Controller->calculatePrice($age, $date, $gender); + $this->assertEquals("Error", $result); + } +} \ No newline at end of file From 849c22f35ba06bb13523636a5dcb91a24d59dd42 Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Thu, 29 Apr 2021 11:32:19 +0700 Subject: [PATCH 2/6] Revert code --- app/Http/Controllers/Exercise02Controller.php | 99 +++++-------------- 1 file changed, 27 insertions(+), 72 deletions(-) diff --git a/app/Http/Controllers/Exercise02Controller.php b/app/Http/Controllers/Exercise02Controller.php index 0af1079..2010c53 100644 --- a/app/Http/Controllers/Exercise02Controller.php +++ b/app/Http/Controllers/Exercise02Controller.php @@ -1,91 +1,46 @@ isWeekend($date) || $this->isHoliday($date)) { - return self::NORMAL_FREE; - } - //rule3 - if ($this->isTimeDiscount($date, $time)) { - return self::FREE; - } - //rule4 - return self::NORMAL_FREE; - } - - /** - * Check is weekend - * - * @param $date - * @return bool - */ - public function isWeekend($date): bool + public function __construct(ATMService $atmService) { - if (Carbon::createFromFormat('Y-m-d', $date)->isWeekend()) { - return true; - } - - return false; + $this->atmService = $atmService; } - /** - * Check is holiday - * - * @param $date - * @return bool - */ - public function isHoliday($date): bool + public function index() { - $date = Carbon::createFromFormat('Y-m-d', $date); - if (in_array($date->format('d-m'), self::HOLIDAYS)) { - return true; - } - - return false; + return view('exercise02::index', [ + 'normalFee' => $this->atmService::NORMAL_FEE, + 'noFee' => $this->atmService::NO_FEE, + 'timePeriod1' => $this->atmService::TIME_PERIOD_1, + 'timePeriod2' => $this->atmService::TIME_PERIOD_2, + 'timePeriod3' => $this->atmService::TIME_PERIOD_3, + ]); } /** - * Check is time discount + * Display a listing of the resource * - * @param $date - * @param $time - * @return bool + * @return Renderable */ - public function isTimeDiscount($date, $time): bool + public function takeATMFee(ATMRequest $request) { - $date = Carbon::createFromFormat('Y-m-d H:i', $date . " " . $time); - $time = $date->format('H:i'); - list($minTime, $maxTime) = self::TIME_PERIOD; - - if ($time >= $minTime && $time <= $maxTime) { - return true; - } - - return false; + $inputs = $request->validated(); + $fee = $this->atmService->calculate($inputs['card_id']); + + return back() + ->withInput() + ->with('calculate', [ + 'fee' => $fee, + ]); } } \ No newline at end of file From 9b866a9c2d72ff49baf4f88483530cae336154a6 Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Thu, 29 Apr 2021 11:34:03 +0700 Subject: [PATCH 3/6] Revert --- app/Http/Controllers/Exercise02Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Exercise02Controller.php b/app/Http/Controllers/Exercise02Controller.php index 2010c53..995716d 100644 --- a/app/Http/Controllers/Exercise02Controller.php +++ b/app/Http/Controllers/Exercise02Controller.php @@ -43,4 +43,4 @@ public function takeATMFee(ATMRequest $request) 'fee' => $fee, ]); } -} \ No newline at end of file +} From fefcfcc1c1ad87fada8c2a5873012ab3c33cbea1 Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Thu, 29 Apr 2021 11:36:40 +0700 Subject: [PATCH 4/6] Update code test Ex2 --- app/Http/Controllers/Exercise02Controller.php | 101 +++++++++++++----- 1 file changed, 73 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Exercise02Controller.php b/app/Http/Controllers/Exercise02Controller.php index 995716d..35aff7e 100644 --- a/app/Http/Controllers/Exercise02Controller.php +++ b/app/Http/Controllers/Exercise02Controller.php @@ -1,46 +1,91 @@ isWeekend($date) || $this->isHoliday($date)) { + return self::NORMAL_FREE; + } + //rule3 + if ($this->isTimeDiscount($date, $time)) { + return self::FREE; + } + //rule4 + return self::NORMAL_FREE; + } + + /** + * Check is weekend + * + * @param $date + * @return bool + */ + public function isWeekend($date): bool { - $this->atmService = $atmService; + if (Carbon::createFromFormat('Y-m-d', $date)->isWeekend()) { + return true; + } + + return false; } - public function index() + /** + * Check is holiday + * + * @param $date + * @return bool + */ + public function isHoliday($date): bool { - return view('exercise02::index', [ - 'normalFee' => $this->atmService::NORMAL_FEE, - 'noFee' => $this->atmService::NO_FEE, - 'timePeriod1' => $this->atmService::TIME_PERIOD_1, - 'timePeriod2' => $this->atmService::TIME_PERIOD_2, - 'timePeriod3' => $this->atmService::TIME_PERIOD_3, - ]); + $date = Carbon::createFromFormat('Y-m-d', $date); + if (in_array($date->format('d-m'), self::HOLIDAYS)) { + return true; + } + + return false; } /** - * Display a listing of the resource + * Check is time discount * - * @return Renderable + * @param $date + * @param $time + * @return bool */ - public function takeATMFee(ATMRequest $request) + public function isTimeDiscount($date, $time): bool { - $inputs = $request->validated(); - $fee = $this->atmService->calculate($inputs['card_id']); - - return back() - ->withInput() - ->with('calculate', [ - 'fee' => $fee, - ]); + $date = Carbon::createFromFormat('Y-m-d H:i', $date . " " . $time); + $time = $date->format('H:i'); + list($minTime, $maxTime) = self::TIME_PERIOD; + + if ($time >= $minTime && $time <= $maxTime) { + return true; + } + + return false; } -} +} \ No newline at end of file From 53035a84ef566fb3bb837804c5b6e86a24873510 Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Thu, 29 Apr 2021 11:38:46 +0700 Subject: [PATCH 5/6] Revert deleted file --- .../Http/Controllers/Exercise02Controller.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Modules/Exercise02/Http/Controllers/Exercise02Controller.php diff --git a/Modules/Exercise02/Http/Controllers/Exercise02Controller.php b/Modules/Exercise02/Http/Controllers/Exercise02Controller.php new file mode 100644 index 0000000..995716d --- /dev/null +++ b/Modules/Exercise02/Http/Controllers/Exercise02Controller.php @@ -0,0 +1,46 @@ +atmService = $atmService; + } + + public function index() + { + return view('exercise02::index', [ + 'normalFee' => $this->atmService::NORMAL_FEE, + 'noFee' => $this->atmService::NO_FEE, + 'timePeriod1' => $this->atmService::TIME_PERIOD_1, + 'timePeriod2' => $this->atmService::TIME_PERIOD_2, + 'timePeriod3' => $this->atmService::TIME_PERIOD_3, + ]); + } + + /** + * Display a listing of the resource + * + * @return Renderable + */ + public function takeATMFee(ATMRequest $request) + { + $inputs = $request->validated(); + $fee = $this->atmService->calculate($inputs['card_id']); + + return back() + ->withInput() + ->with('calculate', [ + 'fee' => $fee, + ]); + } +} From 5c0c9ac70745d3fa28c3990e52cdfaf09db417de Mon Sep 17 00:00:00 2001 From: phongnq-2036 Date: Tue, 11 May 2021 09:38:20 +0700 Subject: [PATCH 6/6] Add UT for Ex2 --- app/Http/Controllers/Exercise03Controller.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/app/Http/Controllers/Exercise03Controller.php b/app/Http/Controllers/Exercise03Controller.php index e69de29..fdbf21e 100644 --- a/app/Http/Controllers/Exercise03Controller.php +++ b/app/Http/Controllers/Exercise03Controller.php @@ -0,0 +1,46 @@ +count(); + + if ($totalProduct) { + $hasCravat = $products->filter(function ($product) { + return $product['type'] == self::CRAVAT_TYPE; + })->isNotEmpty(); + + $hasWhiteShirt = $products->filter(function ($product) { + return $product['type'] === self::WHITE_SHIRT_TYPE; + })->isNotEmpty(); + + if ($totalProduct >= self::TOTAL_PRODUCT_TO_DISCOUNT) { + if ($hasCravat && $hasWhiteShirt) return self::DISCOUNT_TYPE_ONE; + if (!$hasCravat || !$hasWhiteShirt) return self::DISCOUNT_TYPE_TWO; + } else { + if ($hasCravat && $hasWhiteShirt) return self::DISCOUNT_TYPE_THREE; + if (!$hasCravat && !$hasWhiteShirt) return self::NO_DISCOUNT; + } + } + + return self::NO_DISCOUNT; + } +} \ No newline at end of file