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,46 @@
<?php

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

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

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

protected function setUp(): void
{
parent::setUp();
$this->productService = Mockery::mock(ProductService::class)->makePartial();
$this->productController = new ProductController($this->productService);
}

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

$response = $this->productController->index();

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

public function test_checkout()
{
$request = new CheckoutRequest([
'total_products' => [],
]);

$this->productService->shouldReceive('calculateDiscount')->andReturn(0);

$response = $this->productController->checkout($request);

$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
}
146 changes: 146 additions & 0 deletions tests/Unit/Modules/Exercise03/Services/ProductServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace Tests\Unit\Modules\Exercise03\Services;

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

class ProductServiceTest extends TestCase
{
protected $productRepository;
protected $productService;

protected function setUp(): void
{
parent::setUp();
$this->productRepository = Mockery::mock(ProductRepository::class)->makePartial();
$this->productService = new ProductService($this->productRepository);
}

public function test_get_all_products()
{
$this->productRepository->shouldReceive('all')->andReturn(collect([]));

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

public function test_not_valid_quantity()
{
$totalProducts = [
Product::CRAVAT_TYPE => -1,
Product::WHITE_SHIRT_TYPE => 0,
Product::OTHER_TYPE => 0,
];
$this->expectException(InvalidArgumentException::class);
$this->productService->calculateDiscount($totalProducts);
}

public function test_calculate_discount_total_product_lower_than_7_no_white_shirt_no_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 0,
Product::WHITE_SHIRT_TYPE => 0,
Product::OTHER_TYPE => 0,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_lower_than_7_no_white_shirt_but_have_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 0,
Product::OTHER_TYPE => 0,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_lower_than_7_have_white_shirt_no_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 0,
Product::WHITE_SHIRT_TYPE => 1,
Product::OTHER_TYPE => 0,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_lower_than_7_have_white_shirt_and_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 1,
Product::OTHER_TYPE => 0,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_bigger_or_equal_7_no_white_shirt_no_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 0,
Product::WHITE_SHIRT_TYPE => 0,
Product::OTHER_TYPE => 7,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_bigger_or_equal_7_no_white_shirt_but_have_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 0,
Product::WHITE_SHIRT_TYPE => 1,
Product::OTHER_TYPE => 7,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_bigger_or_equal_7_have_white_shirt_no_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 0,
Product::OTHER_TYPE => 7,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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

public function test_calculate_discount_total_product_bigger_or_equal_7_have_white_shirt_and_tie()
{
$totalProducts = [
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 1,
Product::OTHER_TYPE => 7,
];

$discount = $this->productService->calculateDiscount($totalProducts);

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