introduce prepareCart() to be able to do checkout tests [wip] [ci-skip]

This commit is contained in:
Herbert Maschke 2020-02-28 11:41:48 +01:00
parent 6c5e9a73f9
commit 3e9c68f01d
7 changed files with 742 additions and 619 deletions

View File

@ -21,7 +21,7 @@
"barryvdh/laravel-dompdf": "0.8.5",
"doctrine/dbal": "2.9.2",
"fideloper/proxy": "^4.0",
"flynsarmy/db-blade-compiler": "*",
"flynsarmy/db-blade-compiler": "^5.5",
"guzzlehttp/guzzle": "~6.0",
"intervention/image": "^2.4",
"intervention/imagecache": "^2.3",
@ -33,9 +33,7 @@
"maatwebsite/excel": "3.1.18",
"nwidart/laravel-modules": "^3.2",
"prettus/l5-repository": "^2.6",
"tymon/jwt-auth": "^1.0.0",
"barryvdh/laravel-debugbar": "^3.1",
"fzaninotto/faker": "^1.4"
"tymon/jwt-auth": "^1.0.0"
},
"require-dev": {
@ -44,10 +42,11 @@
"codeception/module-filesystem": "^1.0",
"codeception/module-laravel5": "^1.0",
"filp/whoops": "^2.0",
"laravel/dusk": "^5.7.0",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^9.0",
"barryvdh/laravel-debugbar": "^3.1",
"fzaninotto/faker": "^1.4"
},
"replace": {

1194
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,14 @@ namespace Webkul\Core\Helpers;
// all public methods declared in helper class will be available in $I
use Codeception\Module\Laravel5;
use Webkul\Checkout\Models\Cart;
use Webkul\Customer\Models\Customer;
use Webkul\Checkout\Models\CartItem;
use Illuminate\Support\Facades\Event;
use Webkul\Product\Models\Product;
use Webkul\Checkout\Models\CartAddress;
use Webkul\Product\Models\ProductInventory;
use Webkul\Customer\Models\CustomerAddress;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Product\Models\ProductDownloadableLink;
use Webkul\Product\Models\ProductDownloadableLinkTranslation;
@ -64,6 +69,93 @@ class Laravel5Helper extends Laravel5
return $attributes[$attribute];
}
/**
* Generate a cart for the customer. Usually this is necessary to prepare the database
* before testing the checkout.
*
* @param array $options pass some options to configure some of the properties of the cart
*
* @return array the generated mocks as array
*
* @throws \Exception
*/
public function prepareCart(array $options = []): array
{
$faker = \Faker\Factory::create();
$I = $this;
$product = $I->haveProduct(self::SIMPLE_PRODUCT, $options['productOptions'] ?? []);
if (isset($options['customer'])) {
$customer = $options['customer'];
} else {
$customer = $I->have(Customer::class);
}
$I->have(CustomerAddress::class, [
'customer_id' => $customer->id,
'default_address' => 1,
'first_name' => $customer->first_name,
'last_name' => $customer->last_name,
'company_name' => $faker->company,
]);
if (isset($options['payment_method']) && $options['payment_method'] === 'free_of_charge') {
$grand_total = '0.0000';
$base_grand_total = '0.0000';
} else {
$grand_total = (string)$faker->numberBetween(1, 666);
$base_grand_total = $grand_total;
}
$cart = $I->have(Cart::class, [
'customer_id' => $customer->id,
'customer_first_name' => $customer->first_name,
'customer_last_name' => $customer->last_name,
'customer_email' => $customer->email,
'is_active' => 1,
'channel_id' => 1,
'grand_total' => $grand_total,
'base_grand_total' => $base_grand_total,
]);
$cartAddress = $I->have(CartAddress::class, ['cart_id' => $cart->id]);
if (isset($options['product_type'])) {
$type = $options['product_type'];
} else {
$type = 'virtual';
}
$generatedCartItems = rand(3, 10);
for ($i = 2; $i <= $generatedCartItems; $i++) {
$cartItem = $I->have(CartItem::class, [
'type' => $type,
'quantity' => random_int(1, 10),
'cart_id' => $cart->id,
'product_id' => $product->id,
]);
}
// actually set the cart to the user's session
// when in an functional test:
$stub = new \StdClass();
$stub->id = $cart->id;
$I->setSession(['cart' => $stub]);
return [
'cart' => $cart,
'product' => $product,
'customer' => $customer,
'cartAddress' => $cartAddress,
'cartItem' => $cartItem,
];
}
/**
* Helper function to generate products for testing
*

View File

@ -8,12 +8,11 @@ use Webkul\Customer\Models\Customer;
$factory->define(Customer::class, function (Faker $faker) {
$now = date("Y-m-d H:i:s");
$gender = array_rand(['male', 'female', 'other']);
$password = $faker->password;
return [
'first_name' => $faker->firstName($gender),
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName,
'gender' => ucfirst($gender),
'gender' => array_random(['male', 'female', 'other']),
'email' => $faker->email,
'status' => 1,
'password' => Hash::make($password),
@ -21,7 +20,7 @@ $factory->define(Customer::class, function (Faker $faker) {
'is_verified' => 1,
'created_at' => $now,
'updated_at' => $now,
'notes' => json_encode(array('plain_password' => $password)),
'notes' => json_encode(['plain_password' => $password]),
];
});

View File

@ -6,6 +6,10 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Webkul\User\Models\Admin;
use Webkul\Customer\Models\Customer;
use Webkul\Customer\Models\CustomerAddress;
use Webkul\Checkout\Models\Cart;
use Webkul\Checkout\Models\CartItem;
use Webkul\Checkout\Models\CartAddress;
/**
* Inherited Methods
@ -104,9 +108,11 @@ class FunctionalTester extends \Codeception\Actor
* // TODO: change method as soon as there is a method to set core config data
*
* @param $data array containing 'code => value' pairs
*
* @return void
*/
public function setConfigData($data): void {
public function setConfigData($data): void
{
foreach ($data as $key => $value) {
if (DB::table('core_config')->where('code', '=', $key)->exists()) {
DB::table('core_config')->where('code', '=', $key)->update(['value' => $value]);
@ -115,7 +121,7 @@ class FunctionalTester extends \Codeception\Actor
'code' => $key,
'value' => $value,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
'updated_at' => date('Y-m-d H:i:s'),
]);
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace Helper;
// here you can define custom actions
@ -6,5 +7,21 @@ namespace Helper;
class Functional extends \Codeception\Module
{
/**
* Apply the given theme by setting the value to the default (or given) channel.
*
* @param string $theme
*/
public function applyTheme(string $theme, string $channel = 'default'): void
{
$channel = Channel::where('code', $channel)->first();
if (! $channel) {
throw new \Exception(
"Given theme '$theme' could not applied because channel '$channel' could not be fetched from database"
);
}
$channel->update(['theme' => $theme]);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Tests\Functional\Checkout\Cart;
use FunctionalTester;
use Cart;
use Codeception\Example;
class OrderCest
{
/**
* @param \FunctionalTester $I
*
* @example {"isGuest": true}
*
* @example {"isGuest": false}
*/
public function testCheckout(FunctionalTester $I, Example $example)
{
if (! $example['isGuest']) {
$I->loginAsCustomer();
}
$I->prepareCart();
}
}