Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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,51 @@
<?php

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

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

class ProductControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $service;

protected function setUp(): void
{
parent::setUp();
$this->service = $this->mock(ProductService::class);
}

function testIndex()
{
$this->service
->shouldReceive('getAllProducts')
->andReturn([]);

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

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

function testCheckout()
{
$request = $this->mock(CheckoutRequest::class);
$request->shouldReceive('input')->andReturn([1 => -1, 2 => 1, 3 => 3]);

$this->service
->shouldReceive('calculateDiscount')
->andReturn(12);

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

$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,83 @@
<?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 ProductRequestTest extends TestCase
{
use SetupDatabaseTrait;

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

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

/**
* @dataProvider wrongTotalProducts
* @dataProvider wrongTotalCravat
* @dataProvider wrongTotalWhiteShirt
* @dataProvider wrongTotalOther
*/
public function testValidationInvalid($key, $value)
{
$request = new CheckoutRequest();
$validator = Validator::make(['total_products' => $value], $request->rules());

$this->assertTrue(Arr::exists($validator->errors()->messages(), $key));
}

function wrongTotalProducts()
{
return [
['total_products', null],
['total_products', 1]
];
}

function wrongTotalCravat()
{
return [
['total_products.1', [1 => 1.1111, 2 => 2, 3 => 3]],
['total_products.1', [1 => -1, 2 => 2, 3 => 3]],
['total_products.1', [1 => 'test Cravat', 2 => 2, 3 => 3]],
];
}

function wrongTotalWhiteShirt()
{
return [
['total_products.2', [1 => 1, 2 => 2.22222, 3 => 3]],
['total_products.2', [1 => 1, 2 => -1, 3 => 3]],
['total_products.2', [1 => 1, 2 => 'test WhiteShirt', 3 => 3]],
];
}

function wrongTotalOther()
{
return [
['total_products.3', [1 => 1, 2 => 2, 3 => 3.33333]],
['total_products.3', [1 => 1, 2 => 2, 3 => -1]],
['total_products.3', [1 => 1, 2 => 2, 3 => 'test Other']],
];
}

public function testValidationSuccess()
{
$request = new CheckoutRequest();
$validator = Validator::make([
'total_products' => [1 => 1, 2 => 2, 3 => 3],
], $request->rules());

$this->assertTrue($validator->passes());
}
}
38 changes: 38 additions & 0 deletions Modules/Exercise03/Tests/Feature/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Modules\Exercise03\Tests\Feature\Models;

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

class ProductTest extends TestCase
{
use SetupDatabaseTrait;

/**
* This test does not count coverage for model Product,
* because we are test for class property `fillable`, not method
*
* But it is added to ensure we initialize property correctly
*/
public function testProductFields()
{
$inputs = [
'name' => 'John',
'type' => rand(Product::CRAVAT_TYPE, Product::OTHER_TYPE),
];

$product = Product::create($inputs);

$this->assertEquals($inputs['name'], $product->name);
$this->assertEquals($inputs['type'], $product->type);
}

public function testProductFactory()
{
$product = Product::factory()->make();

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

namespace Modules\Exercise03\Tests\Unit\Repositories;

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

class ProductRepositoryTest extends TestCase
{
use SetupDatabaseTrait;

/**
* @var ATMRepository
*/
protected $repository;

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

$this->repository = new ProductRepository(new Product());
}

public function testGetAllProduct()
{
$cravat = Product::factory()->cravat()->create()->fresh();
$whiteShirt = Product::factory()->whiteShirt()->create()->fresh();
$other = Product::factory()->other()->create()->fresh();

$collection = $this->repository->all();

$this->assertInstanceOf(Collection::class, $collection);
}
}
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 Modules\Exercise03\Tests\Unit\Services;

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

class ProductServiceTest extends TestCase
{
use SetupDatabaseTrait;

protected $service;

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

public function testGetAllProducts()
{

$productRepository = $this->mock(ProductRepository::class);
$productRepository
->shouldReceive('all')
->andReturn([]);
$products = $this->service->getAllProducts();

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

/**
* @dataProvider provideValidTotalProducts
*/
public function testCalculateDiscount($discount, $totalProducts)
{
$result = $this->service->calculateDiscount($totalProducts);

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

function provideValidTotalProducts()
{
return [
[5, [1 => 2, 2 => 2, 3 => 2]],
[12, [1 => 1, 2 => 2, 3 => 4]],
[7, [1 => 1, 2 => 0, 3 => 6]],
[0, [1 => 1, 2 => 0, 3 => 3]],
];
}

/**
* @dataProvider provideInValidTotalProducts
*/
public function testCalculateDiscountInvalid($totalProducts)
{
$this->expectException(InvalidArgumentException::class);
$this->service->calculateDiscount($totalProducts);
}

function provideInValidTotalProducts()
{
return [
[[1 => -1, 2 => 0, 3 => 0]],
[[1 => 0, 2 => -1, 3 => 0]],
[[1 => 0, 2 => 0, 3 => -1]]
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

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

class CalendarControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $service;

protected function setUp(): void
{
parent::setUp();
$this->service = \Mockery::mock(CalendarService::class);
}

function testIndex()
{
$response = $this->get(route('calendar'));
$response->assertStatus(200);
$response->assertViewIs('exercise04::calendar');

$this->service
->shouldReceive('getDateClass')
->with(Carbon::parse('2020-09-01'), ['2020-09-25'])
->andReturn(CalendarService::COLOR_BLACK);

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

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

namespace Modules\Exercise04\Tests\Unit\Services;

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

class CalendarServiceTest extends TestCase
{
use SetupDatabaseTrait;

protected $service;

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

/**
* @dataProvider provideDate
*/
public function testGetDateClass($color, $date)
{
$result = $this->service->getDateClass($date, ['2020-09-26']);

$this->assertEquals($result, $color);
}

function provideDate()
{
return [
[CalendarService::COLOR_RED, 'date' => Carbon::createFromDate(2020, 9, 13)],
[CalendarService::COLOR_BLUE, 'date' => Carbon::createFromDate(2020, 9, 12)],
[CalendarService::COLOR_RED, 'date' => Carbon::createFromDate(2020, 9, 26)],
[CalendarService::COLOR_BLACK, 'date' => Carbon::createFromDate(2020, 9, 30)],
];
}
}
Loading