|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modules\Exercise01\Tests\Feature\Http\Controllers; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use Modules\Exercise01\Http\Controllers\OrderController; |
| 7 | +use Modules\Exercise01\Models\Voucher; |
| 8 | +use Modules\Exercise01\Services\DTO\Price; |
| 9 | +use Modules\Exercise01\Services\PriceService; |
| 10 | +use Tests\SetupDatabaseTrait; |
| 11 | + |
| 12 | +class OrderControllerTest extends TestCase |
| 13 | +{ |
| 14 | + use SetupDatabaseTrait; |
| 15 | + |
| 16 | + protected $priceServiceMock; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + // Laravel helper: mock and bind to service container |
| 23 | + $this->priceServiceMock = $this->mock(PriceService::class); |
| 24 | + } |
| 25 | + |
| 26 | + function test_it_show_form_order() |
| 27 | + { |
| 28 | + $url = action([OrderController::class, 'showForm']); |
| 29 | + |
| 30 | + $response = $this->get($url); |
| 31 | + |
| 32 | + $response->assertViewIs('exercise01::order'); |
| 33 | + $response->assertViewHasAll([ |
| 34 | + 'unitPrice', |
| 35 | + 'voucherUnitPrice', |
| 36 | + 'specialTimeUnitPrice', |
| 37 | + 'specialTimePeriod', |
| 38 | + ]); |
| 39 | + $response->assertSessionMissing('order'); |
| 40 | + } |
| 41 | + |
| 42 | + private function inValidInputs($inputs) |
| 43 | + { |
| 44 | + $validInputs = [ |
| 45 | + 'quantity' => 1, |
| 46 | + 'voucher' => null, |
| 47 | + ]; |
| 48 | + |
| 49 | + return array_filter(array_merge($validInputs, $inputs), function ($value) { |
| 50 | + return $value !== null; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider provideWrongQuantity |
| 56 | + * @dataProvider provideWrongVoucher |
| 57 | + */ |
| 58 | + function test_it_show_error_when_input_invalid($inputKey, $inputValue) |
| 59 | + { |
| 60 | + $url = action([OrderController::class, 'create']); |
| 61 | + $inputs = $this->inValidInputs([ |
| 62 | + $inputKey => is_callable($inputValue) ? $inputValue() : $inputValue, |
| 63 | + ]); |
| 64 | + |
| 65 | + $response = $this->post($url, $inputs); |
| 66 | + |
| 67 | + $response->assertSessionHasErrors([$inputKey]); |
| 68 | + } |
| 69 | + |
| 70 | + function provideWrongQuantity() |
| 71 | + { |
| 72 | + return [ |
| 73 | + 'Quantity is required' => ['quantity', null], |
| 74 | + 'Quantity should be integer' => ['quantity', 1.1], |
| 75 | + 'Quantity should be greater than 1' => ['quantity', 0], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + function provideWrongVoucher() |
| 80 | + { |
| 81 | + return [ |
| 82 | + 'Voucher must exist' => ['voucher', 'this-voucher-not-exist'], |
| 83 | + 'Voucher must be active' => [ |
| 84 | + 'voucher', |
| 85 | + function () { |
| 86 | + Voucher::factory()->inactive()->create(['code' => 'existed-voucher-but-inactive']); |
| 87 | + |
| 88 | + return 'existed-voucher-but-inactive'; |
| 89 | + }, |
| 90 | + ], |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @dataProvider provideEmptyVoucher |
| 96 | + */ |
| 97 | + function test_it_should_not_error_when_input_empty_voucher($voucher) |
| 98 | + { |
| 99 | + $url = action([OrderController::class, 'create']); |
| 100 | + $dummyPrice = new Price(100, 0, 0); |
| 101 | + $this->priceServiceMock |
| 102 | + ->shouldReceive('calculate') |
| 103 | + ->andReturn($dummyPrice); |
| 104 | + |
| 105 | + $response = $this->post($url, [ |
| 106 | + 'quantity' => 1, |
| 107 | + 'voucher' => $voucher, |
| 108 | + ]); |
| 109 | + |
| 110 | + $response->assertSessionDoesntHaveErrors(['voucher']); |
| 111 | + } |
| 112 | + |
| 113 | + function provideEmptyVoucher() |
| 114 | + { |
| 115 | + return [ |
| 116 | + 'Voucher can be null' => [null], |
| 117 | + 'Voucher can be empty string' => [''], |
| 118 | + 'Voucher can be string with spaces only' => [' '], |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + function test_it_create_order_when_input_valid_quantity_and_voucher_code() |
| 123 | + { |
| 124 | + Voucher::factory()->active()->create(['code' => 'existed-voucher']); |
| 125 | + $dummyPrice = new Price(100, 0, 0); |
| 126 | + $this->priceServiceMock |
| 127 | + ->shouldReceive('calculate') |
| 128 | + ->andReturn($dummyPrice); |
| 129 | + |
| 130 | + $url = action([OrderController::class, 'create']); |
| 131 | + |
| 132 | + $response = $this->post($url, [ |
| 133 | + 'quantity' => 1, |
| 134 | + 'voucher' => 'existed-voucher', |
| 135 | + ]); |
| 136 | + |
| 137 | + $response->assertSessionDoesntHaveErrors(['quantity']); |
| 138 | + $response->assertSessionDoesntHaveErrors(['voucher']); |
| 139 | + $response->assertSessionHasInput(['quantity', 'voucher']); |
| 140 | + $response->assertSessionHas('order', function ($order) { |
| 141 | + return $order['quantity'] == 1 && $order['price'] instanceof Price; |
| 142 | + }); |
| 143 | + } |
| 144 | +} |
0 commit comments