Fixing the test suite
-
Has anyone been able to fix the test suite? All tests seed to dependant on a fresh seed and each other. Which goes against the very basis of test structures.
I have to say I am disappointed by this messy test suite.
-
Hello @MartintheMartian ,
As I read your concern, I was unable to find what you meant to say. Are you talking about test cases that we have implemented in Bagisto or what?If you are talking about test cases, then please elaborate on what you wanted in the flow and structure of the test cases.
-
@amit-webkul My issue is that all the tests seem to be dependant on a seed of the DatabaseSeeder and they are also dependant on each other. Using magic numbers based on previous tests.
it('should add a grouped product to the cart with a cart rule of the specific coupon type for guest customer', function () { // Arrange. $product = (new ProductFaker([ 'attributes' => [ 5 => 'new', 6 => 'featured', 11 => 'price', 26 => 'guest_checkout', ], 'attribute_value' => [ 'new' => [ 'boolean_value' => true, ], 'featured' => [ 'boolean_value' => true, ], 'price' => [ 'float_value' => rand(1000, 5000), ], 'guest_checkout' => [ 'boolean_value' => true, ], ], ]))->getGroupedProductFactory()->create(); $cartRule = CartRule::factory()->afterCreating(function (CartRule $cartRule) { $cartRule->cart_rule_customer_groups()->sync([1]); $cartRule->cart_rule_channels()->sync([1]); })->create([ 'name' => fake()->uuid(), 'description' => fake()->sentence(), 'action_type' => 'by_fixed', 'discount_amount' => rand(20, 50), 'usage_per_customer' => rand(1, 50), 'uses_per_coupon' => rand(1, 50), 'condition_type' => 2, 'status' => 1, 'discount_quantity' => 1, 'apply_to_shipping' => 1, 'use_auto_generation' => 0, 'times_used' => 0, 'coupon_type' => 1, 'end_other_rules' => 0, 'uses_attribute_conditions' => 0, 'discount_step' => 0, 'free_shipping' => 0, 'sort_order' => 0, 'conditions' => json_decode('[{"value": "20000", "operator": "<=", "attribute": "cart_item|base_price", "attribute_type": "price"}]'), 'starts_from' => null, 'ends_till' => null, ]); $cartRuleCoupon = CartRuleCoupon::factory()->create([ 'cart_rule_id' => $cartRule->id, 'code' => $couponCode = fake()->numerify('bagisto-########'), 'type' => 0, 'is_primary' => 1, ]); $groupedProducts = $product->grouped_products()->with('associated_product')->get(); $data = [ 'quantities' => [], 'prices' => [], ]; foreach ($groupedProducts as $groupedProduct) { $data['quantities'][$groupedProduct->associated_product_id] = $groupedProduct->qty; $data['prices'][] = $groupedProduct->associated_product->price * $groupedProduct->qty; } $cart = cart()->addProduct($product, [ 'product_id' => $product->id, 'quantity' => 1, 'is_buy_now' => '0', 'rating' => '0', 'qty' => $data['quantities'], ]); cart()->setCart($cart); cart()->collectTotals(); // Act and Assert. $response = postJson(route('shop.api.checkout.cart.coupon.apply'), [ 'code' => $couponCode, ]) ->assertOk() ->assertJsonPath('message', trans('shop::app.checkout.coupon.success-apply')) ->assertJsonPath('data.items_qty', array_sum($data['quantities'])) ->assertJsonPath('data.items_count', 4); $this->assertPrice(array_sum($data['prices']) - ($cartRule->discount_amount * 4), $response['data']['grand_total']); $this->assertPrice(array_sum($data['prices']), $response['data']['sub_total']); $this->assertPrice($cartRule->discount_amount * 4, $response['data']['discount_amount']); $this->assertModelWise([ CartRule::class => [ $this->prepareCartRule($cartRule), ], ]); $this->prepareCartRuleCustomerGroup($cartRule); $this->prepareCartRuleChannel($cartRule); $this->prepareCartRuleCoupon($cartRuleCoupon); });
The assertion of
->assertJsonPath('data.items_count', 4);
for example. -
I would also be helped if you could tell how to run the test suites in its entirety or per test file locally.
-
Hello @MartintheMartian ,
If you want to run test cases on your local system, you can easily do this by running the command
vendor/bin/pest
. It will run all the testcases of the application.And If you want to run specific test cases, then you run using this command:
vendor/bin/pest --filter="You test case name here"
.
FYI: For running the test cases in your local system, you have to create one more
.env
file whose name should be like.env.testing
.'''