Skip to content

Example workflow #14

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 2 commits 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,144 @@
<?php

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

use Tests\TestCase;
use Modules\Exercise01\Http\Controllers\OrderController;
use Modules\Exercise01\Models\Voucher;
use Modules\Exercise01\Services\DTO\Price;
use Modules\Exercise01\Services\PriceService;
use Tests\SetupDatabaseTrait;

class OrderControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $priceServiceMock;

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

// Laravel helper: mock and bind to service container
$this->priceServiceMock = $this->mock(PriceService::class);
}

function test_it_show_form_order()
{
$url = action([OrderController::class, 'showForm']);

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

$response->assertViewIs('exercise01::order');
$response->assertViewHasAll([
'unitPrice',
'voucherUnitPrice',
'specialTimeUnitPrice',
'specialTimePeriod',
]);
$response->assertSessionMissing('order');
}

private function inValidInputs($inputs)
{
$validInputs = [
'quantity' => 1,
'voucher' => null,
];

return array_filter(array_merge($validInputs, $inputs), function ($value) {
return $value !== null;
});
}

/**
* @dataProvider provideWrongQuantity
* @dataProvider provideWrongVoucher
*/
function test_it_show_error_when_input_invalid($inputKey, $inputValue)
{
$url = action([OrderController::class, 'create']);
$inputs = $this->inValidInputs([
$inputKey => is_callable($inputValue) ? $inputValue() : $inputValue,
]);

$response = $this->post($url, $inputs);

$response->assertSessionHasErrors([$inputKey]);
}

function provideWrongQuantity()
{
return [
'Quantity is required' => ['quantity', null],
'Quantity should be integer' => ['quantity', 1.1],
'Quantity should be greater than 1' => ['quantity', 0],
];
}

function provideWrongVoucher()
{
return [
'Voucher must exist' => ['voucher', 'this-voucher-not-exist'],
'Voucher must be active' => [
'voucher',
function () {
Voucher::factory()->inactive()->create(['code' => 'existed-voucher-but-inactive']);

return 'existed-voucher-but-inactive';
},
],
];
}

/**
* @dataProvider provideEmptyVoucher
*/
function test_it_should_not_error_when_input_empty_voucher($voucher)
{
$url = action([OrderController::class, 'create']);
$dummyPrice = new Price(100, 0, 0);
$this->priceServiceMock
->shouldReceive('calculate')
->andReturn($dummyPrice);

$response = $this->post($url, [
'quantity' => 1,
'voucher' => $voucher,
]);

$response->assertSessionDoesntHaveErrors(['voucher']);
}

function provideEmptyVoucher()
{
return [
'Voucher can be null' => [null],
'Voucher can be empty string' => [''],
'Voucher can be string with spaces only' => [' '],
];
}

function test_it_create_order_when_input_valid_quantity_and_voucher_code()
{
Voucher::factory()->active()->create(['code' => 'existed-voucher']);
$dummyPrice = new Price(100, 0, 0);
$this->priceServiceMock
->shouldReceive('calculate')
->andReturn($dummyPrice);

$url = action([OrderController::class, 'create']);

$response = $this->post($url, [
'quantity' => 1,
'voucher' => 'existed-voucher',
]);

$response->assertSessionDoesntHaveErrors(['quantity']);
$response->assertSessionDoesntHaveErrors(['voucher']);
$response->assertSessionHasInput(['quantity', 'voucher']);
$response->assertSessionHas('order', function ($order) {
return $order['quantity'] == 1 && $order['price'] instanceof Price;
});
}
}
6 changes: 5 additions & 1 deletion infection.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"source": {
"directories": [
"Modules/"
"Modules/Exercise01/",
"Modules/Exercise03/",
"Modules/Exercise04/",
"Modules/Exercise05/",
"Modules/Exercise07/"
],
"excludes": [
"Config",
Expand Down
15 changes: 10 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
>
<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/Exercise01/Tests</directory>
<directory suffix="Test.php">./Modules/Exercise03/Tests</directory>
<directory suffix="Test.php">./Modules/Exercise04/Tests</directory>
<directory suffix="Test.php">./Modules/Exercise05/Tests</directory>
<directory suffix="Test.php">./Modules/Exercise07/Tests</directory>
</testsuite>
<testsuite name="Exercise01">
<directory suffix="Test.php">./Modules/Exercise01/Tests</directory>
Expand Down Expand Up @@ -43,8 +45,11 @@
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./Modules</directory>
<directory suffix=".php">./Modules/Exercise01</directory>
<directory suffix=".php">./Modules/Exercise03</directory>
<directory suffix=".php">./Modules/Exercise04</directory>
<directory suffix=".php">./Modules/Exercise05</directory>
<directory suffix=".php">./Modules/Exercise07</directory>
</include>
<exclude>
<directory>./Modules/**/Config</directory>
Expand Down