diff --git a/Modules/Exercise03/Tests/Feature/Http/Controllers/ProductControllerTest.php b/Modules/Exercise03/Tests/Feature/Http/Controllers/ProductControllerTest.php new file mode 100644 index 0000000..43e3bd9 --- /dev/null +++ b/Modules/Exercise03/Tests/Feature/Http/Controllers/ProductControllerTest.php @@ -0,0 +1,47 @@ +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()); + } +} diff --git a/Modules/Exercise03/Tests/Feature/Http/Models/ProductModelTest.php b/Modules/Exercise03/Tests/Feature/Http/Models/ProductModelTest.php new file mode 100644 index 0000000..cf65a25 --- /dev/null +++ b/Modules/Exercise03/Tests/Feature/Http/Models/ProductModelTest.php @@ -0,0 +1,17 @@ +make(); + + $this->assertInstanceOf(Product::class, $product); + } +} diff --git a/Modules/Exercise03/Tests/Feature/Http/Repositories/ProductRepositoryTest.php b/Modules/Exercise03/Tests/Feature/Http/Repositories/ProductRepositoryTest.php new file mode 100644 index 0000000..253acb2 --- /dev/null +++ b/Modules/Exercise03/Tests/Feature/Http/Repositories/ProductRepositoryTest.php @@ -0,0 +1,23 @@ +all(); + $this->assertInstanceOf(Collection::class, $getAll); + } +} diff --git a/Modules/Exercise03/Tests/Feature/Http/Requests/CheckoutRequestTest.php b/Modules/Exercise03/Tests/Feature/Http/Requests/CheckoutRequestTest.php new file mode 100644 index 0000000..d653588 --- /dev/null +++ b/Modules/Exercise03/Tests/Feature/Http/Requests/CheckoutRequestTest.php @@ -0,0 +1,46 @@ +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()); + } +} diff --git a/Modules/Exercise03/Tests/Feature/Http/Services/ProductServiceTest.php b/Modules/Exercise03/Tests/Feature/Http/Services/ProductServiceTest.php new file mode 100644 index 0000000..d8949f9 --- /dev/null +++ b/Modules/Exercise03/Tests/Feature/Http/Services/ProductServiceTest.php @@ -0,0 +1,74 @@ +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, + ]); + } +} diff --git a/infection.json b/infection.json index e93241b..bec3be1 100644 --- a/infection.json +++ b/infection.json @@ -1,7 +1,7 @@ { "source": { "directories": [ - "Modules/" + "Modules/Exercise03/" ], "excludes": [ "Config", diff --git a/phpunit.xml b/phpunit.xml index 77442f0..3ac8815 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,9 +6,7 @@ > - ./tests/Unit - ./tests/Feature - ./Modules/**/Tests + ./Modules/Exercise03/Tests ./Modules/Exercise01/Tests @@ -47,12 +45,7 @@ ./Modules - ./Modules/**/Config - ./Modules/**/Database - ./Modules/**/Resources - ./Modules/**/Routes - ./Modules/**/Tests - ./Modules/**/Providers + ./Modules/Exercise03/Tests