Skip to content

[KienNT] exercise03 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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,47 @@
<?php

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


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

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

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

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

function test_it_index()
{
$this->productService->shouldReceive('getAllProducts')->andReturn([]);
$url = action([ProductController::class, 'index']);
$response = $this->get($url);
$response->assertViewIs('exercise03::index');
$response->assertViewHas('products');
}

function test_it_checkout(){
$input['total_products'] = [
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 2,
Product::OTHER_TYPE => 3
];

$this->productService->shouldReceive('calculateDiscount')->with($input)->andReturn(5);
$mockRequest = \Mockery::mock(CheckoutRequest::class);
$mockRequest->shouldReceive('input')->andReturn($input);
$response = $this->productController->checkout($mockRequest);
$this->assertEquals(['discount' => 5], $response->getOriginalContent());
}
}
17 changes: 17 additions & 0 deletions Modules/Exercise03/Tests/Feature/Http/Models/ProductModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Modules\Exercise03\Tests\Feature\Models;


use Modules\Exercise03\Models\Product;
use Tests\TestCase;

class ProductModelTest extends TestCase
{
public function test_product_new_factory()
{
$product = Product::factory()->make();

$this->assertInstanceOf(Product::class, $product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

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

class ProductRepositoryTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function test_all()
{
$repository = new ProductRepository(new Product());
$getAll = $repository->all();
$this->assertInstanceOf(Collection::class, $getAll);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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


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

class CheckoutRequestTest extends TestCase
{
private $request;

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

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

public function test_default_rules()
{
$request = new CheckoutRequest();

$this->assertEquals([
'total_products' => 'required|array',
'total_products.*' => 'nullable|integer|min:0'
], $request->rules());
}

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

public function test_valid_data()
{
$data = [
'total_products' => [0, 1, 2, 3],
];

$validator = Validator::make($data, $this->request->rules());
$this->assertTrue($validator->passes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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

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 $service;

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

$this->productRepository = $this->mock(ProductRepository::class);
$this->service = new ProductService($this->productRepository);

}

public function test_get_all_products()
{
$this->productRepository->shouldReceive('all')
->andReturn([]);
$products = $this->service->getAllProducts();

$this->assertEquals($products, []);
}

/**
* @param $totalProducts
* @param $expectedValue
* @dataProvider provide_total_products_data
* */
public function test_calculate_discount_with_valid_data($totalProducts, $expectedValue)
{
$discount = $this->service->calculateDiscount($totalProducts);
$this->assertEquals($expectedValue, $discount);
}

public function provide_total_products_data()
{
return [
[
[
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 2,
Product::OTHER_TYPE => 0,
], ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT
],
[
[
Product::CRAVAT_TYPE => 2,
Product::WHITE_SHIRT_TYPE => 3,
Product::OTHER_TYPE => 5,
], 12
],
];
}

public function test_calculate_discount_throw_exception()
{
$this->expectException(InvalidArgumentException::class);
$this->service->calculateDiscount([
Product::CRAVAT_TYPE => -1,
Product::WHITE_SHIRT_TYPE => -2,
Product::OTHER_TYPE => -3,
]);
}
}
2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"source": {
"directories": [
"Modules/"
"Modules/Exercise03/"
],
"excludes": [
"Config",
Expand Down
11 changes: 2 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
>
<testsuites>
<testsuite name="All">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests/Feature</directory>
<directory suffix="Test.php">./Modules/**/Tests</directory>
<directory suffix="Test.php">./Modules/Exercise03/Tests</directory>
</testsuite>
<testsuite name="Exercise01">
<directory suffix="Test.php">./Modules/Exercise01/Tests</directory>
Expand Down Expand Up @@ -47,12 +45,7 @@
<directory suffix=".php">./Modules</directory>
</include>
<exclude>
<directory>./Modules/**/Config</directory>
<directory>./Modules/**/Database</directory>
<directory>./Modules/**/Resources</directory>
<directory>./Modules/**/Routes</directory>
<directory>./Modules/**/Tests</directory>
<directory>./Modules/**/Providers</directory>
<directory suffix="Test.php">./Modules/Exercise03/Tests</directory>
</exclude>
</coverage>
<php>
Expand Down