Skip to content

Instantly share code, notes, and snippets.

@larangin
Created January 23, 2024 04:40
Show Gist options
  • Select an option

  • Save larangin/53e90fcd8e639b9f63b6ca3dcce01163 to your computer and use it in GitHub Desktop.

Select an option

Save larangin/53e90fcd8e639b9f63b6ca3dcce01163 to your computer and use it in GitHub Desktop.
<?php
/**
* Shipping Service Rate Test
* @link https://www.demotest.net/
* @author test dev <test@demotest.net>
*/
namespace EcTva\Ecoteva\Tests\integration\Warehouse;
use Exception;
use WC_Product_Simple;
use EcTva\Ecoteva\Core\Controllers\Option\OptionCoreController;
use EcTva\Ecoteva\Core\Entities\Shipment;
use EcTva\Ecoteva\Core\Entities\Shipment\ShippingMethod;
use EcTva\Ecoteva\Core\Entities\Warehouse\ShippingService;
use EcTva\Ecoteva\Core\Entities\Warehouse\ShippingServiceRate;
use EcTva\Ecoteva\Core\Entities\Warehouse\Warehouse;
use EcTva\Ecoteva\Core\Migrations\WarehouseMigration;
use EcTva\Ecoteva\Core\Repositories\Warehouse\ShippingServiceRateRepo;
use EcTva\Ecoteva\Core\Repositories\Warehouse\ShippingServiceRepo;
use EcTva\Ecoteva\Core\Repositories\Warehouse\WarehouseRepo;
use EcTva\Ecoteva\Core\Services\Shipment\ShippingMethodsService;
use EcTva\Ecoteva\Core\Services\Warehouse\WarehouseService;
use EcTva\Ecoteva\Core\Utils\Components\Runner\PluginRunner;
use EcTva\Ecoteva\Core\Utils\Enums\EnumKeyMetadata;
use EcTva\Ecoteva\Core\Utils\Exceptions\ExceptionResponse;
use EcTva\Ecoteva\Tests\Tools\ProductsEcoTevaUnitTestCaseMultisite;
class EndpointsShippingServiceRateTest extends ProductsEcoTevaUnitTestCaseMultisite {
public function setUp() {
parent::setUp();
try {
$container = PluginRunner::getDefaultContainer();
$this->optionCoreController = $container->get(OptionCoreController::class);
$this->warehouseService = $container->get(WarehouseService::class);
$this->warehouseMigration = $container->get(WarehouseMigration::class);
$this->warehouse_repo = $container->get(WarehouseRepo::class);
$this->shipping_service_repo = $container->get(ShippingServiceRepo::class);
$this->shipping_service_rate_repo = $container->get(ShippingServiceRateRepo::class);
$this->shipping_methods_service = $container->get(ShippingMethodsService::class);
} catch (Exception $e) {
}
}
public function test_migration() {
$result = $this->warehouseMigration->apply();
$this->assertIsArray($result);
$this->assertEquals(WarehouseMigration::class, $result['migration']);
$this->assertIsArray($result['messages']);
$this->assertCount(3, $result['messages']);
}
public function provider_shipping_service_rate(): array {
$e0 = new ShippingServiceRate();
$e0->country = 'IE';
$e0->weight_from = 0;
$e0->price = 5;
$e1 = new ShippingServiceRate();
$e1->country = 'IE';
$e1->weight_from = 10;
$e1->price = 10;
$e2 = new ShippingServiceRate();
$e2->country = 'IE';
$e2->weight_from = 20;
$e2->price = 15;
return [
'Shipping Service Rate 0' => [
'shipping_service_rate' => $e0,
],
'Shipping Service Rate 10' => [
'shipping_service_rate' => $e1,
],
'Shipping Service Rate 20' => [
'shipping_service_rate' => $e2,
],
];
}
/**
* @dataProvider provider_shipping_service_rate
* @depends test_migration
*/
public function test_create_read_shipping_service_rate(ShippingServiceRate $shipping_service_rate) {
$wh = new Warehouse();
$wh->name = 'dublintest';
$wh->slug = 'dublintest';
$wh->phone = '1234567890';
$wh->email = 'dublintest@demotest.net';
$wh->zip_code = '12345';
$wh->country = 'IE';
$wh->state = 'dublintest';
$wh->city = 'dublintest';
$wh->address_line_1 = '123 Test Street';
$wh->address_line_2 = 'Line2';
$wh->handling_fee = .5;
$wh = $this->warehouse_repo->create($wh);
$this->assertInstanceOf(Warehouse::class, $wh);
$this->assertGreaterThan(0, $wh->id);
$ss = new ShippingService();
$ss->name = 'DHL';
$ss->slug = 'dhl';
$ss->warehouse = $wh;
$ss = $this->shipping_service_repo->create($ss);
$this->assertInstanceOf(ShippingService::class, $ss);
$this->assertGreaterThan(0, $ss->id);
$shipping_service_rate->shipping_service = $ss;
$shipping_service_rate_created = $this->shipping_service_rate_repo->create($shipping_service_rate, true);
$this->assertInstanceOf(ShippingServiceRate::class, $shipping_service_rate_created);
$this->assertGreaterThan(0, $shipping_service_rate_created->id);
$this->assertEquals($shipping_service_rate->country, $shipping_service_rate_created->country);
$this->assertEquals($shipping_service_rate->weight_from, $shipping_service_rate_created->weight_from);
$this->assertEquals($shipping_service_rate->price, $shipping_service_rate_created->price);
$this->assertEquals($shipping_service_rate->shipping_service->id, $shipping_service_rate_created->shipping_service->id);
}
/**
* @depends test_migration
*/
public function test_getRates_mainCase() {
$wh = new Warehouse();
$wh->name = 'dublintest';
$wh->slug = 'dublintest';
$wh->phone = '1234567890';
$wh->email = 'dublintest@demotest.net';
$wh->zip_code = '12345';
$wh->country = 'IE';
$wh->state = 'dublintest';
$wh->city = 'dublintest';
$wh->address_line_1 = '123 Test Street';
$wh->address_line_2 = 'Line2';
$wh->handling_fee = .5;
$wh = $this->warehouse_repo->create($wh);
$this->assertInstanceOf(Warehouse::class, $wh);
$this->assertGreaterThan(0, $wh->id);
$ss = new ShippingService();
$ss->name = 'DHL';
$ss->slug = 'dhl';
$ss->warehouse = $wh;
$ss = $this->shipping_service_repo->create($ss);
$this->assertInstanceOf(ShippingService::class, $ss);
$this->assertGreaterThan(0, $ss->id);
$ssr0 = new ShippingServiceRate();
$ssr0->country = 'IE';
$ssr0->weight_from = 0;
$ssr0->price = 5;
$ssr0->shipping_service = $ss;
$ssr0 = $this->shipping_service_rate_repo->create($ssr0, true);
$this->assertInstanceOf(ShippingServiceRate::class, $ssr0);
$this->assertGreaterThan(0, $ssr0->id);
// create a product with woocommerce
$wc_product = new WC_Product_Simple();
$wc_product->set_name('Test Product');
$wc_product->set_status('publish');
$wc_product->set_price(0);
// Dimensions
$wc_product->set_weight(1);
$wc_product->set_length(1);
$wc_product->set_width(1);
$wc_product->set_height(1);
$p_id = $wc_product->save();
$this->assertGreaterThan(0, $p_id);
update_post_meta($p_id, EnumKeyMetadata::UNIT_SELL_PRICE, 15);
update_post_meta($p_id, '_onbrand_hs_code', '1234567890');
update_post_meta($p_id, EnumKeyMetadata::METADATA_WAREHOUSE, $wh->id);
$shipment = new Shipment();
$shipment->amount = 1;
$shipment->country = 'IE';
$shipment->state = 'dublintest';
$shipment->city = 'dublintest';
$shipment->postcode = '12345';
$shipment->address1 = '123 Test Street';
$shipment->address2 = 'Line2';
try {
$shipping_methods = $this->shipping_methods_service->create_shipping_methods($shipment, [$p_id], [1]);
$this->assertIsArray($shipping_methods);
$this->assertCount(1, $shipping_methods);
$shipping_method = $shipping_methods[0];
$this->assertInstanceOf(ShippingMethod::class, $shipping_method);
$this->assertEquals('DHL', $shipping_method->name);
$this->assertEquals(5.5, $shipping_method->price);
} catch (ExceptionResponse $e) {
$this->fail($e->getMessage());
}
}
/**
* @depends test_migration
*/
public function test_getRates_zeroCase() {
$wh = new Warehouse();
$wh->name = 'dublintest';
$wh->slug = 'dublintest';
$wh->phone = '1234567890';
$wh->email = 'dublintest@demotest.net';
$wh->zip_code = '12345';
$wh->country = 'IE';
$wh->state = 'dublintest';
$wh->city = 'dublintest';
$wh->address_line_1 = '123 Test Street';
$wh->address_line_2 = 'Line2';
$wh->handling_fee = .5;
$wh = $this->warehouse_repo->create($wh);
$this->assertInstanceOf(Warehouse::class, $wh);
$this->assertGreaterThan(0, $wh->id);
$ss = new ShippingService();
$ss->name = 'DHL';
$ss->slug = 'dhl';
$ss->warehouse = $wh;
$ss = $this->shipping_service_repo->create($ss);
$this->assertInstanceOf(ShippingService::class, $ss);
$this->assertGreaterThan(0, $ss->id);
$ssr0 = new ShippingServiceRate();
$ssr0->country = 'IE';
$ssr0->weight_from = 0;
$ssr0->price = 5;
$ssr0->shipping_service = $ss;
$ssr0 = $this->shipping_service_rate_repo->create($ssr0, true);
$this->assertInstanceOf(ShippingServiceRate::class, $ssr0);
$this->assertGreaterThan(0, $ssr0->id);
$ssr1 = new ShippingServiceRate();
$ssr1->country = 'IE';
$ssr1->weight_from = 3;
$ssr1->price = 0;
$ssr1->shipping_service = $ss;
$ssr1 = $this->shipping_service_rate_repo->create($ssr1, true);
$this->assertInstanceOf(ShippingServiceRate::class, $ssr1);
$this->assertGreaterThan(0, $ssr1->id);
// create a product with woocommerce
$wc_product = new WC_Product_Simple();
$wc_product->set_name('Test Product');
$wc_product->set_status('publish');
$wc_product->set_price(0);
// Dimensions
$wc_product->set_weight(5);
$wc_product->set_length(1);
$wc_product->set_width(1);
$wc_product->set_height(1);
$p_id = $wc_product->save();
$this->assertGreaterThan(0, $p_id);
update_post_meta($p_id, EnumKeyMetadata::UNIT_SELL_PRICE, 15);
update_post_meta($p_id, '_onbrand_hs_code', '1234567890');
update_post_meta($p_id, EnumKeyMetadata::METADATA_WAREHOUSE, $wh->id);
$shipment = new Shipment();
$shipment->amount = 1;
$shipment->country = 'IE';
$shipment->state = 'dublintest';
$shipment->city = 'dublintest';
$shipment->postcode = '12345';
$shipment->address1 = '123 Test Street';
$shipment->address2 = 'Line2';
try {
$shipping_methods = $this->shipping_methods_service->create_shipping_methods($shipment, [$p_id], [1]);
$this->assertIsArray($shipping_methods);
$this->assertEmpty($shipping_methods);
} catch (ExceptionResponse $e) {
$this->fail($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment