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
1 change: 1 addition & 0 deletions Modules/Exercise03/Database/Factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function definition()
return [
'name' => $this->faker->name,
'thumbnail' => $this->faker->image(),
'type' => $this->cravat(),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Modules\Exercise03\Tests\Http\Controllers;

use Mockery;
use Tests\TestCase;
use Modules\Exercise03\Services\ProductService;
use Modules\Exercise03\Http\Controllers\ProductController;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\View\View;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Illuminate\Http\JsonResponse;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;

class ProductControllerTest extends TestCase
{
public function testConstruct()
{
$product = new Product;
$repository = new ProductRepository($product);
$service = new ProductService($repository);
$controller = new ProductController($service);

$this->assertInstanceOf(ProductController::class, $controller);
}

public function testIndex()
{
$collection = Mockery::mock(Collection::class);

$service = Mockery::mock(ProductService::class);
$service->shouldReceive('getAllProducts')->once()->andReturn($collection);

$controller = new ProductController($service);

$this->assertInstanceOf(View::class, $controller->index());
}

public function testCheckout()
{
$input = [
1 => 1,
2 => 2,
3 => 3,
];
$response = 5;

$request = Mockery::mock(CheckoutRequest::class);
$request->shouldReceive('input')->once()->with('total_products')->andReturn($input);

$service = Mockery::mock(ProductService::class);
$service->shouldReceive('calculateDiscount')->once()->with($input)->andReturn($response);

$controller = new ProductController($service);

$this->assertInstanceOf(JsonResponse::class, $controller->checkout($request));
}
}
29 changes: 29 additions & 0 deletions Modules/Exercise03/Tests/Http/Requests/CheckoutRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Modules\Exercise03\Tests\Http\Requests;

use Mockery;
use Tests\TestCase;
use Tests\TestValidation;
use Modules\Exercise03\Http\Requests\CheckoutRequest;

class CheckoutRequestTest extends TestCase
{
use TestValidation;

protected function setUp(): void
{
parent::setUp();
$this->rules = (new CheckoutRequest())->rules();
$this->validator = $this->app['validator'];
}

public function testTotalProducts()
{
$this->assertTrue($this->validateField('total_products.*', [
1,
2,
3,
])
);
}
}
16 changes: 16 additions & 0 deletions Modules/Exercise03/Tests/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Modules\Exercise03\Tests\Models;

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

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

$this->assertInstanceOf(ProductFactory::class, $product);
}
}
27 changes: 27 additions & 0 deletions Modules/Exercise03/Tests/Repositories/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Modules\Exercise03\Tests\Repositories;

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

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

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

public function testAll()
{
$product = new Product;
$repository = new ProductRepository($product);
$this->assertInstanceOf(Collection::class, $repository->all());
}
}
78 changes: 78 additions & 0 deletions Modules/Exercise03/Tests/Services/PriceServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace App\Modules\Exercise03\Tests\Services;

use Mockery;
use Tests\TestCase;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
use Modules\Exercise03\Services\ProductService;
use Illuminate\Support\Collection;
use InvalidArgumentException;

class PriceServiceTest extends TestCase
{
public function testConstruct()
{
$product = new Product;
$productRepository = new ProductRepository($product);
$service = new ProductService($productRepository);

$this->assertInstanceOf(ProductService::class, $service);
}

/**
* @dataProvider providerTestCalculateDiscount
*/
public function testCalculateDiscount($input, $expect)
{
$product = new Product;
$repository = new ProductRepository($product);
$service = new ProductService($repository);
$response = $service->calculateDiscount($input);

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

public function testCalculateDiscountResponseIsException()
{
$this->expectException(InvalidArgumentException::class);
$input = [
1 => -1,
2 => 1,
3 => 2,
];
$product = new Product;
$repository = new ProductRepository($product);
$service = new ProductService($repository);
$service->calculateDiscount($input);
}

public function testGetAllProducts()
{
$product = new Product;
$repository = new ProductRepository($product);
$service = new ProductService($repository);

$this->assertInstanceOf(Collection::class, $service->getAllProducts());
}

public function providerTestCalculateDiscount()
{
return [
$this->exampleData(1, 3, 2, ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
$this->exampleData(5, 5, 5, ProductService::QUANTITY_DISCOUNT + ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
];
}

public function exampleData($cravat, $whiteShirt, $others, $expert)
{
return [
[
1 => $cravat,
2 => $whiteShirt,
3 => $others,
],
$expert,
];
}
}
30 changes: 30 additions & 0 deletions Modules/Exercise04/Tests/Http/CalendarControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Modules\Exercise04\Tests\Http\Controllers;

use Mockery;
use Tests\TestCase;
use Modules\Exercise04\Services\CalendarService;
use Modules\Exercise04\Http\Controllers\CalendarController;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;

class CalendarControllerTest extends TestCase
{
public function testConstruct()
{
$service = new CalendarService;
$controller = new CalendarController($service);

$this->assertInstanceOf(CalendarController::class, $controller);
}

public function testIndex()
{
$service = Mockery::mock(CalendarService::class);
$service->shouldReceive('getDateClass')->times(30)->andReturn('any_color');

$controller = new CalendarController($service);
$this->assertInstanceOf(View::class, $controller->index());
}
}
45 changes: 45 additions & 0 deletions Modules/Exercise04/Tests/Services/CalendarServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace App\Modules\Exercise04\Tests\Services;

use Mockery;
use Tests\TestCase;
use Modules\Exercise04\Services\CalendarService;
use Illuminate\Support\Carbon;

class CalendarServiceTest extends TestCase
{

/**
* @dataProvider providerTestGetDateClass
*/
public function testGetDateClass($input, $expect)
{
$holidays = ['2021-04-25'];
$service = new CalendarService;
$response = $service->getDateClass($input, $holidays);

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

public function providerTestGetDateClass()
{
return [
[
new Carbon('2021-04-21'),
CalendarService::COLOR_BLACK,
],
[
new Carbon('2021-04-25'),
CalendarService::COLOR_RED,
],
[
new Carbon('2021-04-24'),
CalendarService::COLOR_BLUE,
],
[
new Carbon('2021-04-25'),
CalendarService::COLOR_RED,
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Modules\Exercise05\Tests\Http\Controllers;

use Mockery;
use Tests\TestCase;
use Modules\Exercise05\Services\OrderService;
use Modules\Exercise05\Http\Controllers\Exercise05Controller;
use Illuminate\Contracts\Support\Renderable;
use Modules\Exercise05\Http\Requests\OrderRequest;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;

class Exercise05ControllerTest extends TestCase
{
public function testConstruct()
{
$service = new OrderService;
$controller = new Exercise05Controller($service);

$this->assertInstanceOf(Exercise05Controller::class, $controller);
}

public function testIndex()
{
$service = new OrderService;
$controller = new Exercise05Controller($service);

$this->assertInstanceOf(View::class, $controller->index());
}

public function testStore()
{
$input = [
'price' => 1500,
'option_receive' => '2',
'option_coupon' => '1',
];
$response = [
'price' => 1200,
'discount_pizza' => null,
'discount_potato' => null,
];

$request = Mockery::mock(OrderRequest::class);
$request->shouldReceive('only')->once()->andReturn($input);

$service = Mockery::mock(OrderService::class);
$service->shouldReceive('handleDiscount')->once()->with($input)->andReturn($response);

$controller = new Exercise05Controller($service);

$this->assertInstanceOf(View::class, $controller->store($request));
}
}
34 changes: 34 additions & 0 deletions Modules/Exercise05/Tests/Http/Requests/OrderRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Modules\Exercise05\Tests\Http\Requests;

use Mockery;
use Tests\TestCase;
use Tests\TestValidation;
use Modules\Exercise05\Http\Requests\OrderRequest;

class OrderRequestTest extends TestCase
{
use TestValidation;

protected function setUp(): void
{
parent::setUp();
$this->rules = (new OrderRequest())->rules();

Choose a reason for hiding this comment

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

indent e ơi

$this->validator = $this->app['validator'];
}

public function testPrice()
{
$this->assertTrue($this->validateField('price', 1200));

Choose a reason for hiding this comment

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

cần test cả những case fail nữa e nhé

}

public function testOptionReceive()
{
$this->assertTrue($this->validateField('option_receive', '1'));
}

public function testOptionCoupoin()

Choose a reason for hiding this comment

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

testOptionCoupoin ==> sai chính tả này

{
$this->assertTrue($this->validateField('option_coupon', '1'));
}
}
Loading