Skip to content

Commit

Permalink
Add initial infra for unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedanshujain committed Dec 24, 2024
1 parent 1f91c45 commit 3ec769a
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 178 deletions.
7 changes: 5 additions & 2 deletions app/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" ?>
<phpunit bootstrap="tests/bootstrap.php" displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="Serverless">
<directory suffix="Test.php">tests</directory>
<testsuite name="E2E">
<directory suffix="Test.php">tests/e2e</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>
177 changes: 177 additions & 0 deletions app/tests/bootstrap-e2e.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php declare( strict_types=1 );


function e2e_get_new_site_id() {
return str_replace( '.', '', uniqid( '', true ) );
}

function e2e_get_test_api_domain() {
return 'localhost';
}

function e2e_get_new_test_site() {
$site = e2e_get_new_site_id();
$site_info = array(
'id' => $site,
'url' => $site . '.integration.test',
'tax_settings' => array(
'tax_enabled' => true,
'price_includes_tax' => true,
'shipping_tax_class' => 'inherit',
),
'tax_rates' => array(
array(
'rate_id' => 1,
'rate' => 10,
'name' => 'CGST',
),
array(
'rate_id' => 2,
'rate' => 12,
'name' => 'SGST',
),
),
'cors_allowed_origins' => array(
'http://example.test',
),
'last_reserved_order_id' => 1,
'salts' => array(
'cart' => uniqid( '', true ),
),
);

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/sites?token=test',
array(
'json' => $site_info,
)
);
assert( $response->getStatusCode() === 201 );
return json_decode( $response->getBody()->getContents(), true )['data'];
}

function e2e_register_payment_gateway_woopay( $site_info ) {
$payment_gateway = array(
'id' => 'woopay',
'name' => 'woopay',
'wcpay_version' => '1.0.0',
'store_name' => 'Test store',
'store_logo' => 'https://example.com/logo.png',
'blog_id' => '1',
'blog_url' => 'https://example.com',
'blog_checkout_url' => 'https://example.com/checkout',
'blog_shop_url' => 'https://example.com/shop',
'blog_timezone' => 'UTC',
'store_api_url' => '',
'account_id' => 'test',
'test_mode' => true,
'capture_method' => 'automatic',
'is_subscription_plugin_active' => false,
'woocommerce_tax_display_cart' => 'excl',
'ship_to_billing_address_only' => true,
'blog_token' => 'test',
);

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/webhooks/payment-gateways?key=' . $site_info['wh_key'],
array(
'json' => $payment_gateway,
)
);
assert( $response->getStatusCode() === 200 );
return json_decode( $response->getBody()->getContents(), true );
}

function e2e_register_payment_gateway_cash_on_delivery( $site_info ) {
$payment_gateway = array(
'id' => 'cod',
'name' => 'Cash on Delivery',
);

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/webhooks/payment-gateways?key=' . $site_info['wh_key'],
array(
'json' => $payment_gateway,
)
);
assert( $response->getStatusCode() === 200 );
return json_decode( $response->getBody()->getContents(), true );
}

function e2e_get_product_data_array( $custom_data = array() ) {
global $product_id;
$product_id = $product_id ?? 0;
++$product_id;
return array_merge(
array(
'id' => $product_id,
'name' => 'Sample product',
'description' => 'Sample product',
'slug' => 'sample-product',
'date_created' => '2021-01-01 00:00:00',
'date_created_gmt' => '2021-01-01 00:00:00',
'date_modified' => '2021-01-01 00:00:00',
'date_modified_gmt' => '2021-01-01 00:00:00',
'type' => 'simple',
'status' => 'publish',
'featured' => false,
'catalog_visibility' => 'visible',
'short_description' => 'Short description',
'sku' => 'SK' . $product_id,
'price' => 100,
'regular_price' => 100,
'sale_price' => 0,
'date_on_sale_from' => null,
'date_on_sale_from_gmt' => null,
'date_on_sale_to' => null,
'date_on_sale_to_gmt' => null,
'price_html' => '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>59.00</span>&ndash;<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>100.00</span>',
'on_sale' => true,
'purchasable' => true,
'total_sales' => 0,
'virtual' => false,
'downloadable' => false,
'downloads' => array(),
'download_limit' => -1,
'download_expiry' => -1,
'external_url' => '',
'button_text' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'manage_stock' => false,
'stock_quantity' => null,
'stock_status' => 'instock',
'backorders' => 'no',
'backorders_allowed' => false,
'backordered' => false,
'sold_individually' => false,
'weight' => '',
'dimensions' => array(),
'shipping_required' => true,
'shipping_taxable' => true,
'shipping_class' => '',
'shipping_class_id' => 0,
'reviews_allowed' => true,
'average_rating' => '0.00',
'rating_count' => 0,
'related_ids' => array(),
'upsell_ids' => array(),
'cross_sell_ids' => array(),
'parent_id' => 0,
'purchase_note' => '',
'categories' => array(),
'tags' => array(),
'images' => array(),
'attributes' => array(),
'default_attributes' => array(),
'variations' => array(),
'grouped_products' => array(),
'menu_order' => 0,
'meta_data' => array(),
),
$custom_data
);
}
183 changes: 15 additions & 168 deletions app/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,178 +1,25 @@
<?php

function e2e_get_new_site_id() {
return str_replace( '.', '', uniqid( '', true ) );
}
error_reporting( E_ALL );

function e2e_get_test_api_domain() {
return 'localhost';
}
require_once __DIR__ . '/bootstrap-e2e.php';
require_once __DIR__ . '/unit/MockSerializableKVStore.php';
require_once __DIR__ . '/unit/MockHotStore.php';
require_once './vendor/autoload.php';

function e2e_get_new_test_site() {
$site = e2e_get_new_site_id();
$site_info = array(
'id' => $site,
'url' => $site . '.integration.test',
'tax_settings' => array(
'tax_enabled' => true,
'price_includes_tax' => true,
'shipping_tax_class' => 'inherit',
),
'tax_rates' => array(
array(
'rate_id' => 1,
'rate' => 10,
'name' => 'CGST',
),
array(
'rate_id' => 2,
'rate' => 12,
'name' => 'SGST',
),
),
'cors_allowed_origins' => array(
'http://example.test',
),
'last_reserved_order_id' => 1,
'salts' => array(
'cart' => uniqid( '', true ),
),
);
use App\Lib\KVStore\SerializedKVStoreInterface;
use App\Lib\Interfaces\SerializableHotStoreInterface;
use App\Tests\Unit\MockSerializableKVStore;
use App\Tests\Unit\MockHotStore;

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/sites?token=test',
array(
'json' => $site_info,
)
);
assert( $response->getStatusCode() === 201 );
return json_decode( $response->getBody()->getContents(), true )['data'];
function send_error_and_exit( string $message ) {
throw new Exception( $message );
}

function e2e_register_payment_gateway_woopay( $site_info ) {
$payment_gateway = array(
'id' => 'woopay',
'name' => 'woopay',
'wcpay_version' => '1.0.0',
'store_name' => 'Test store',
'store_logo' => 'https://example.com/logo.png',
'blog_id' => '1',
'blog_url' => 'https://example.com',
'blog_checkout_url' => 'https://example.com/checkout',
'blog_shop_url' => 'https://example.com/shop',
'blog_timezone' => 'UTC',
'store_api_url' => '',
'account_id' => 'test',
'test_mode' => true,
'capture_method' => 'automatic',
'is_subscription_plugin_active' => false,
'woocommerce_tax_display_cart' => 'excl',
'ship_to_billing_address_only' => true,
'blog_token' => 'test',
);

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/webhooks/payment-gateways?key=' . $site_info['wh_key'],
array(
'json' => $payment_gateway,
)
);
assert( $response->getStatusCode() === 200 );
return json_decode( $response->getBody()->getContents(), true );
function unit_get_kv_store(): SerializedKVStoreInterface {
return new MockSerializableKVStore();
}

function e2e_register_payment_gateway_cash_on_delivery( $site_info ) {
$payment_gateway = array(
'id' => 'cod',
'name' => 'Cash on Delivery',
);

$client = new GuzzleHttp\Client();
$response = $client->post(
e2e_get_test_api_domain() . '/webhooks/payment-gateways?key=' . $site_info['wh_key'],
array(
'json' => $payment_gateway,
)
);
assert( $response->getStatusCode() === 200 );
return json_decode( $response->getBody()->getContents(), true );
function unit_get_hot_store(): SerializableHotStoreInterface {
return MockHotStore::get_instance();
}

function e2e_get_product_data_array( $custom_data = array() ) {
global $product_id;
$product_id = $product_id ?? 0;
++$product_id;
return array_merge(
array(
'id' => $product_id,
'name' => 'Sample product',
'description' => 'Sample product',
'slug' => 'sample-product',
'date_created' => '2021-01-01 00:00:00',
'date_created_gmt' => '2021-01-01 00:00:00',
'date_modified' => '2021-01-01 00:00:00',
'date_modified_gmt' => '2021-01-01 00:00:00',
'type' => 'simple',
'status' => 'publish',
'featured' => false,
'catalog_visibility' => 'visible',
'short_description' => 'Short description',
'sku' => 'SK' . $product_id,
'price' => 100,
'regular_price' => 100,
'sale_price' => 0,
'date_on_sale_from' => null,
'date_on_sale_from_gmt' => null,
'date_on_sale_to' => null,
'date_on_sale_to_gmt' => null,
'price_html' => '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>59.00</span>&ndash;<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>100.00</span>',
'on_sale' => true,
'purchasable' => true,
'total_sales' => 0,
'virtual' => false,
'downloadable' => false,
'downloads' => array(),
'download_limit' => -1,
'download_expiry' => -1,
'external_url' => '',
'button_text' => '',
'tax_status' => 'taxable',
'tax_class' => '',
'manage_stock' => false,
'stock_quantity' => null,
'stock_status' => 'instock',
'backorders' => 'no',
'backorders_allowed' => false,
'backordered' => false,
'sold_individually' => false,
'weight' => '',
'dimensions' => array(),
'shipping_required' => true,
'shipping_taxable' => true,
'shipping_class' => '',
'shipping_class_id' => 0,
'reviews_allowed' => true,
'average_rating' => '0.00',
'rating_count' => 0,
'related_ids' => array(),
'upsell_ids' => array(),
'cross_sell_ids' => array(),
'parent_id' => 0,
'purchase_note' => '',
'categories' => array(),
'tags' => array(),
'images' => array(),
'attributes' => array(),
'default_attributes' => array(),
'variations' => array(),
'grouped_products' => array(),
'menu_order' => 0,
'meta_data' => array(),
),
$custom_data
);
}

error_reporting( E_ALL );
Loading

0 comments on commit 3ec769a

Please sign in to comment.