Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions Modules/Exercise02/Http/Controllers/Exercise02Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Modules\Exercise02\Http\Controllers;

use Modules\Exercise02\Http\Requests\ATMRequest;
use Modules\Exercise02\Services\ATMService;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Routing\Controller;
use Modules\Exercise02\Http\Requests\ATMRequest;
use Modules\Exercise02\Services\ATMService;

class Exercise02Controller extends Controller
{
Expand All @@ -19,11 +19,11 @@ public function __construct(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,
'normalFee' => ATMService::NORMAL_FEE,
'noFee' => ATMService::NO_FEE,
'timePeriod1' => ATMService::TIME_PERIOD_1,
'timePeriod2' => ATMService::TIME_PERIOD_2,
'timePeriod3' => ATMService::TIME_PERIOD_3,
]);
}

Expand All @@ -37,8 +37,7 @@ public function takeATMFee(ATMRequest $request)
$inputs = $request->validated();
$fee = $this->atmService->calculate($inputs['card_id']);

return back()
->withInput()
return back()->withInput()
->with('calculate', [
'fee' => $fee,
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Modules\Exercise02\Tests\Feature\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Modules\Exercise02\Http\Controllers\Exercise02Controller;
use Modules\Exercise02\Http\Requests\ATMRequest;
use Modules\Exercise02\Models\ATM;
use Modules\Exercise02\Repositories\ATMRepository;
use Modules\Exercise02\Services\ATMService;
use Tests\SetupDatabaseTrait;
use Tests\TestCase;

class Exercise02ControllerTest extends TestCase
{
use SetupDatabaseTrait;

/**
* @var Exercise02Controller
*/
protected $controller;

/**
* @var ATMService
*/
protected $atmService;

protected function setUp(): void
{
parent::setup();
$this->atmService = new ATMService(new ATMRepository(new ATM()));
$this->controller = new Exercise02Controller($this->atmService);
}

public function test_index_return_view()
{
$response = $this->controller->index();

$this->assertInstanceOf(View::class, $response);
$this->assertEquals('exercise02::index', $response->getName());
$this->assertEquals([
'normalFee' => ATMService::NORMAL_FEE,
'noFee' => ATMService::NO_FEE,
'timePeriod1' => ATMService::TIME_PERIOD_1,
'timePeriod2' => ATMService::TIME_PERIOD_2,
'timePeriod3' => ATMService::TIME_PERIOD_3,
], $response->getData());
}

public function test_take_atm_fee()
{
$request = \Mockery::mock(ATMRequest::class);
$card = ATM::factory()->isVip()->create()->fresh();
$request->shouldReceive('validated')->andReturn([
'card_id' => $card->card_id,
]);
$correctAnswer = ['fee' => 0];
$response = $this->controller->takeATMFee($request);

$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals($correctAnswer, $response->getSession()->all()['calculate']);
}
}
144 changes: 144 additions & 0 deletions Modules/Exercise02/Tests/Unit/Services/ATMServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Modules\Exercise02\Tests\Unit\Services;

use Carbon\Carbon;
use InvalidArgumentException;
use Modules\Exercise02\Models\ATM;
use Modules\Exercise02\Repositories\ATMRepository;
use Modules\Exercise02\Services\ATMService;
use Tests\SetupDatabaseTrait;
use Tests\TestCase;

class ATMServiceTest extends TestCase
{
use SetupDatabaseTrait;

protected $atmService;
protected $vipCard;
protected $notVipCard;

public function setUp(): void
{
parent::setUp();
$atmRepository = new ATMRepository(new ATM());
$this->atmService = new ATMService($atmRepository);
$this->vipCard = ATM::factory()->isVip()->create()->fresh();
$this->notVipCard = ATM::factory()->isNotVip()->create()->fresh();
}

public function test_it_throw_exception_when_card_is_not_exist()
{
$this->expectException(InvalidArgumentException::class);
$this->atmService->calculate(-1);
}

/*
* ==================== VIP TEST ====================
*/
public function test_it_calculate_fee_when_card_is_vip()
{
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_card_is_vip_in_weekend()
{
$date = Carbon::parse('2021-09-26');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_card_is_vip_in_holiday()
{
$date = Carbon::parse('2021-01-01');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_card_is_vip_in_normal_date_special_time_1()
{
list($minTime) = ATMService::TIME_PERIOD_1;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_card_is_vip_in_normal_date_special_time_2()
{
list($minTime) = ATMService::TIME_PERIOD_2;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_card_is_vip_in_normal_date_special_time_3()
{
list($minTime) = ATMService::TIME_PERIOD_3;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->vipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

/*
* ==================== NORMAL TEST ====================
*/
public function test_it_calculate_fee_when_normal_card_in_weekend()
{
$date = Carbon::parse('2021-09-26');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->notVipCard->card_id);

$this->assertEquals(ATMService::NORMAL_FEE, $fee);
}

public function test_it_calculate_fee_when_normal_card_in_holiday()
{
$date = Carbon::parse('2021-01-01');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->notVipCard->card_id);

$this->assertEquals(ATMService::NORMAL_FEE, $fee);
}

public function test_it_calculate_fee_when_normal_card_in_normal_date_special_time_1()
{
list($minTime) = ATMService::TIME_PERIOD_1;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->notVipCard->card_id);

$this->assertEquals(ATMService::NORMAL_FEE, $fee);
}

public function test_it_calculate_fee_when_normal_card_in_normal_date_special_time_2()
{
list($minTime) = ATMService::TIME_PERIOD_2;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->notVipCard->card_id);

$this->assertEquals(ATMService::NO_FEE, $fee);
}

public function test_it_calculate_fee_when_normal_card_in_normal_date_special_time_3()
{
list($minTime) = ATMService::TIME_PERIOD_3;
$date = Carbon::parse('2021-09-22 ' . $minTime . ' +30 minutes');
Carbon::setTestNow($date);
$fee = $this->atmService->calculate($this->notVipCard->card_id);

$this->assertEquals(ATMService::NORMAL_FEE, $fee);
}
}
3 changes: 1 addition & 2 deletions Modules/Exercise03/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Modules\Exercise03\Http\Controllers;

use Illuminate\Routing\Controller;
use Modules\Exercise03\Services\ProductService;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Modules\Exercise03\Repositories\ProductRepository;
use Modules\Exercise03\Services\ProductService;

/**
* Class ProductController
Expand Down
7 changes: 4 additions & 3 deletions Modules/Exercise03/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Modules\Exercise03\Services;

use Illuminate\Support\Arr;
use InvalidArgumentException;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
Expand Down Expand Up @@ -38,9 +39,9 @@ public function __construct(ProductRepository $productRepository)
*/
public function calculateDiscount($totalProducts)
{
$cravat = $totalProducts[Product::CRAVAT_TYPE] ?? 0;
$whiteShirt = $totalProducts[Product::WHITE_SHIRT_TYPE] ?? 0;
$others = $totalProducts[Product::OTHER_TYPE] ?? 0;
$cravat = Arr::get($totalProducts, Product::CRAVAT_TYPE, 0);
$whiteShirt = Arr::get($totalProducts, Product::WHITE_SHIRT_TYPE, 0);
$others = Arr::get($totalProducts, Product::OTHER_TYPE, 0);
$discount = 0;

if ($cravat < 0 || $whiteShirt < 0 || $others < 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Modules\Exercise03\Tests\Feature\Http\Controllers;

use Tests\TestCase;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use Modules\Exercise03\Services\ProductService;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Modules\Exercise03\Http\Controllers\ProductController;

class ProductControllerTest extends TestCase
{
/**
* @var ProductController
*/
protected $productController;

/**
* @var \Mockery\MockInterface
*/
protected $productService;

protected function setUp(): void
{
parent::setup();

$this->productService = \Mockery::mock(ProductService::class);
$this->productController = new ProductController(
$this->productService
);
}

public function test_checkout_returns_json_response()
{
$mockedRequest = \Mockery::mock(CheckoutRequest::class);
$mockedRequest->shouldReceive('input')->andReturn([]);
$discount = 1;
$this->productService->shouldReceive('calculateDiscount')->andReturn($discount);
$response = $this->productController->checkout($mockedRequest);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(['discount' => $discount], $response->getOriginalContent());
}

public function test_index_returns_view()
{
$products = ['foo' => 'bar'];
$this->productService->shouldReceive('getAllProducts')->andReturn($products);
$response = $this->productController->index();

$this->assertInstanceOf(View::class, $response);
$this->assertEquals('exercise03::index', $response->getName());
$this->assertEquals(compact('products'), $response->getData());
}
}
Loading