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
10 changes: 10 additions & 0 deletions Modules/Exercise02/Tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

use Modules\Exercise02\Http\Controllers\Exercise02Controller;
use Modules\Exercise02\Http\Requests\ATMRequest;
use Modules\Exercise02\Services\ATMService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Mockery as m;
use Tests\TestCase;

class Exercise02ControllerTest extends TestCase
{
protected $exercise02Controller;
protected $atmService;

public function setUp(): void
{
parent::setUp();
$this->atmService = m::mock(ATMService::class);

$this->exercise02Controller = new Exercise02Controller($this->atmService);
}

public function test_index()
{
$result = $this->exercise02Controller->index();
$this->assertInstanceOf(View::class, $result);
}

public function test_take_atm_fee()
{
$request = m::mock(ATMRequest::class);

$request->shouldReceive('validated')->andreturn([
'card_id' => 2,
]);
$this->atmService->shouldReceive('calculate')->andReturn(0);

$result = $this->exercise02Controller->takeATMFee($request);
return $this->assertInstanceOf(RedirectResponse::class, $result);
}
}
31 changes: 31 additions & 0 deletions Modules/Exercise02/Tests/Unit/Http/Models/ATMTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Modules\Exercise02\Tests\Unit\Models;

use Modules\Exercise02\Database\Factories\ATMFactory;
use Modules\Exercise02\Models\ATM;
use Tests\TestCase;
use Mockery as m;

class ATMTest extends TestCase
{
public function test_model_configuration()
{
$this->runConfigurationAssertions(new ATM(), [
'fillable' => [
'card_id',
'is_vip',
],
'casts' => [
'is_vip' => 'boolean',
'id' => 'int',
],
]);
}

public function test_new_factory()
{
$model = m::Mock(ATM::class);
return $this->assertInstanceOf(ATMFactory::class, $model->newFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Modules\Exercise03\Tests\Unit\Http\Requests;

use Modules\Exercise02\Repositories\ATMRepository;
use Modules\Exercise02\Models\ATM;
use Tests\TestCase;
use Mockery as m;

class ATMRepositoryTest extends TestCase
{
protected $atmRepo;
protected $model;

public function setUp(): void
{
parent::setUp();

$this->model = m::mock(ATM::class)->makePartial();
$this->atmRepo = new ATMRepository($this->model);
}

public function test_find()
{
$this->model->shouldReceive('where->first')->andReturn([]);

$result = $this->atmRepo->find(1);
return $this->assertEquals([], $result);
}
}
33 changes: 33 additions & 0 deletions Modules/Exercise02/Tests/Unit/Http/Requests/ATMRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Modules\Exercise02\Tests\Unit\Http\Requests;

use Illuminate\Support\Facades\Validator;
use Tests\TestCase;
use Modules\Exercise02\Http\Requests\ATMRequest;

class ATMRequestTest extends TestCase
{
protected $aTMRequest;

public function setUp(): void
{
parent::setUp();
$this->aTMRequest = new ATMRequest();
}

public function test_validate_fails()
{
$validator = Validator::make([], $this->aTMRequest->rules());
$this->assertTrue($validator->fails());
}

public function test_rules_pass()
{
$rules = [
'card_id' => 'required|exists:atms,card_id',
];

$this->assertEquals($rules, $this->aTMRequest->rules());
}
}
157 changes: 157 additions & 0 deletions Modules/Exercise02/Tests/Unit/Services/ATMServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Modules\Exercise02\Tests\Unit\Services;

use Modules\Exercise02\Repositories\ATMRepository;
use Modules\Exercise02\Services\ATMService;
use Modules\Exercise02\Models\ATM;
use InvalidArgumentException;
use Carbon\Carbon;
use Mockery as m;
use Tests\TestCase;

class ATMServiceTest extends TestCase
{
protected $atmService;
protected $atmRepo;

public function setUp(): void
{
parent::setUp();

$this->atmRepo = m::mock(ATMRepository::class);
$this->atmService = new ATMService($this->atmRepo);
}

public function test_has_not_card()
{
$this->atmRepo->shouldReceive('find')->andReturn([]);

$result = $this->atmService->calculate(1);

$this->expectExceptionMessage('Card ID is invalid!');
$this->expectException(InvalidArgumentException::class);

return $this->assertInstanceOf(InvalidArgumentException::class, $result);
}

public function test_card_is_vip()
{
$atm = m::mock(ATM::class)->makePartial();
$atm->is_vip = true;
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(0, $result);
}

public function test_day_is_holiday()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-29'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}

public function test_day_is_holiday_and_is_saturday()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-05-01'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
$this->assertEquals(110, $result);
}

public function test_day_is_holiday_and_is_sunday()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-02-14'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}

public function test_day_is_saturday()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-2'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}

public function test_day_is_sunday()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-25'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}

public function test_normal_day_at_8h44 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-27 08:44:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}

public function test_normal_day_at_8h45 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-27 08:45:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(0, $result);
}

public function test_normal_day_at_8h46 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-15 08:46:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(0, $result);
}

public function test_normal_day_at_17h58 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-15 17:58:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(0, $result);
}

public function test_normal_day_at_17h59 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-15 17:59:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(0, $result);
}

public function test_normal_day_at_18h00 ()
{
$atm = m::mock(ATM::class)->makePartial();
Carbon::setTestNow(Carbon::parse('2021-04-15 18:00:00'));
$this->atmRepo->shouldReceive('find')->andReturn($atm);

$result = $this->atmService->calculate(1);
return $this->assertEquals(110, $result);
}
}
2 changes: 1 addition & 1 deletion Modules/Exercise03/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function calculateDiscount($totalProducts)
$others = $totalProducts[Product::OTHER_TYPE] ?? 0;
$discount = 0;

if ($cravat < 0 || $whiteShirt < 0 || $others < 0) {
if ($cravat <= 0 && $whiteShirt <= 0 && $others <= 0) {
throw new InvalidArgumentException();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

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

class ProductControllerTest extends TestCase
{
protected $productController;
protected $productService;

public function setUp(): void
{
parent::setUp();

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

public function test_index()
{
$this->productService->shouldReceive('getAllProducts')->andreturn([]);

$result = $this->productController->index();
$this->assertInstanceOf(View::class, $result);
}

public function test_checkout()
{
$request = m::mock(CheckoutRequest::class);

$request->shouldReceive('input')->andreturn([
'total_products' => 1,
]);
$this->productService->shouldReceive('calculateDiscount')->andreturn([]);

$result = $this->productController->checkout($request);
return $this->assertInstanceOf(JsonResponse::class, $result);
}
}
Loading