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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

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

use Modules\Exercise03\Http\Controllers\ProductController;
use Tests\TestCase;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Services\ProductService;
use Tests\SetupDatabaseTrait;

class ProductControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $productServiceMock;

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

$this->productServiceMock = $this->mock(ProductService::class);
}

public function test_index_return_view()
{
$this->productServiceMock
->shouldReceive('getAllProducts')
->andReturn([]);

$url = action([ProductController::class, 'index']);
$response = $this->get($url);

$response->assertViewIs('exercise03::index');
$response->assertViewHas('products');
}

public function test_checkout_success()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái này anh thiếu check mấy case khi $paramRequest fail ạ

{
$url = action([ProductController::class, 'checkout']);
$paramRequest = [
1 => 7,
2 => 9,
];
$this->productServiceMock->shouldReceive('calculateDiscount')->with($paramRequest)
->andReturn(12);

$response = $this->post($url, ['total_products' => $paramRequest]);

$this->assertEquals($response->status(), 200);
$this->assertEquals($response->getData(true), ['discount' => 12]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

use Tests\TestCase;
use Illuminate\Support\Facades\Validator;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Tests\SetupDatabaseTrait;
use Illuminate\Support\Arr;

class CheckoutRequestTest extends TestCase
{
use SetupDatabaseTrait;

protected $checkoutRequest;

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

$this->checkoutRequest = new CheckoutRequest();
}

public function test_contains_valid_rules()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anh ơi cái này nên có khi không cần test kiểu này mà mình sẽ test vào với controller để xem với trường hợp đúng thì response ok không. Với trường hợp request trueyefn vào sai có đúng redirect được đi không ạ.
Cái này mới chỉ là check mình viết có đúng cú pháp chưa thôi ạ

{
$this->assertEquals([
'total_products' => 'required|array',
'total_products.*' => 'nullable|integer|min:0'
], $this->checkoutRequest->rules());
}
}
17 changes: 17 additions & 0 deletions Modules/Exercise03/Tests/Unit/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Models;

use Modules\Exercise03\Models\Product;
use Tests\TestCase;
use Modules\Exercise03\Database\Factories\ProductFactory;

class ProductTest extends TestCase
{
function test_new_factory()
{
$product = Product::newFactory();

$this->assertInstanceOf(ProductFactory::class, $product);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

á à

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Unit\Repositories;

use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
use Tests\TestCase;
use Illuminate\Database\Eloquent\Collection;

class ProductRepositoryTest extends TestCase
{
public function test__construct()
{
$product = new Product();
$repository = new ProductRepository($product);

$this->assertInstanceOf(ProductRepository::class, $repository);
}

public function test_all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái tên nên đặt xem test all này sucess hay fail j j đó ạ

{
$product = new Product();
$repository = new ProductRepository($product);

$this->assertInstanceOf(Collection::class, $repository->all());
}
}
74 changes: 74 additions & 0 deletions Modules/Exercise03/Tests/Unit/Services/ProductServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tests\Unit\Services;

use Modules\Exercise03\Services\ProductService;
use Modules\Exercise03\Repositories\ProductRepository;
use Modules\Exercise03\Models\Product;
use InvalidArgumentException;
use Tests\SetupDatabaseTrait;
use Tests\TestCase;

class ProductServiceTest extends TestCase
{
use SetupDatabaseTrait;

protected $productService;

protected $product;

public function setUp(): void
{
parent::setUp();
$this->product = new Product();
$repository = new ProductRepository($this->product);
$this->productService = new ProductService($repository);
}

public function test_throw_exception_when_product_type_does_not_exist()
{
$this->expectException(InvalidArgumentException::class);
$this->productService->calculateDiscount([
Product::WHITE_SHIRT_TYPE => -1111,
]);
}

public function test_case_calculate_discount_return_caravat_white_shirt()
{
$discount = $this->expectException(InvalidArgumentException::class);
$this->productService->calculateDiscount([
Product::CRAVAT_TYPE => 12,
Product::WHITE_SHIRT_TYPE => 13,
Product::OTHER_TYPE => -14,
]);

$this->assertEquals(ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT, $discount);
}

public function test_case_calculate_discount_return_quantity_discount()
{
$discount = $this->productService->calculateDiscount([
Product::CRAVAT_TYPE => 5,
Product::WHITE_SHIRT_TYPE => 4,
Product::OTHER_TYPE => 0,
]);

$this->assertEquals(12, $discount);
}

public function test_get_all_products()
{
$product = Product::newFactory();

$this->cravat = $product->cravat()->create();
$this->whiteShirt = $product->whiteShirt()->create();
$this->other = $product->other()->create();

$products = $this->productService->getAllProducts();

$this->assertEquals(3, $products->count());
$this->assertEquals($products[0]['name'], $this->cravat->name);
$this->assertEquals($products[1]['name'], $this->whiteShirt->name);
$this->assertEquals($products[2]['name'], $this->other->name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Feature\Http\Controllers;

use Modules\Exercise04\Http\Controllers\CalendarController;
use Tests\TestCase;
use Modules\Exercise04\Services\CalendarService;
use Tests\SetupDatabaseTrait;

class CalendarControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $calendarServiceMock;

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

$this->calendarServiceMock = $this->mock(CalendarService::class);
}

function test_index()
{
$url = action([CalendarController::class, 'index']);

$this->calendarServiceMock->shouldReceive('getDateClass')->andReturn(CalendarService::COLOR_BLACK);

$response = $this->get($url);

$response->assertViewIs('exercise04::calendar');
$response->assertViewHas('calendars');
}
}
47 changes: 47 additions & 0 deletions Modules/Exercise04/Tests/Unit/Services/CalendarServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Modules\Exercise04\Tests\Unit\Services;

use Carbon\Carbon;
use Modules\Exercise04\Services\CalendarService;
use Tests\TestCase;

class CalendarServiceTest extends TestCase
{
protected $calendarService;

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

$this->calendarService = new CalendarService();
}

/**
* @dataProvider provideData
*/
public function test_get_date_class($date, $class)
{
$response =$this->calendarService->getDateClass($date, ['2021-05-20']);

$this->assertEquals($class, $response);
}

function provideData()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anh nên chia case như bài 3 ấy ạ
Với lại thiếu case normal date trả về màu đen ạ

{
return [
[
Carbon::createFromFormat('Y-m-d', '2021-05-23'),
CalendarService::COLOR_RED,
],
[
Carbon::createFromFormat('Y-m-d', '2021-05-22'),
CalendarService::COLOR_BLUE,
],
[
Carbon::createFromFormat('Y-m-d', '2021-05-20'),
CalendarService::COLOR_RED,
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

use Tests\TestCase;
use Modules\Exercise06\Http\Controllers\Exercise06Controller;
use Modules\Exercise06\Services\CalculateService;
use Tests\SetupDatabaseTrait;
use Modules\Exercise06\Http\Requests\Exercise06Request;

class Exercise06ControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $service;

protected function setUp(): void
{
parent::setUp();
$this->service = new CalculateService();
}

function test_index_return_view()
{
$url = action([Exercise06Controller::class, 'index']);
$response = $this->get($url);

$response->assertViewIs('exercise06::index');
$this->assertEquals($response->viewData('case1'), $this->service::CASE_1);
$this->assertEquals($response->viewData('case2'), $this->service::CASE_2);
$this->assertEquals($response->viewData('freeTimeForMovie'), $this->service::FREE_TIME_FOR_MOVIE);
}

function test_calculate()
{
$request = $this->mock(Exercise06Request::class);
$request->shouldReceive('validated')->andReturn([
'bill' => 5000,
'has_watch' => 1
]);

$calculateServiceMock = $this->mock(CalculateService::class);
$calculateServiceMock->shouldReceive('calculate')
->andReturn(12);

$url = action([Exercise06Controller::class, 'calculate']);
$response = $this->post($url);
$response->assertRedirect();
$this->assertEquals(['time' => 12], $response->getSession()->all()['result']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Modules\Exercise06\Tests\Feature\Http\Requests;

use Tests\TestCase;
use Illuminate\Support\Facades\Validator;
use Modules\Exercise06\Http\Requests\Exercise06Request;
use Tests\SetupDatabaseTrait;
use Illuminate\Support\Arr;

class Exercise06RequestTest extends TestCase
{
use SetupDatabaseTrait;

public function test_contain_default_rules()
{
$request = new Exercise06Request();

$this->assertEquals([
'bill' => 'required|integer|min:0',
'has_watch' => 'nullable|boolean',
], $request->rules());
}
}
Loading