Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martynmjones committed Sep 20, 2024
1 parent 4b0afb4 commit c4b11d7
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions tests/Unit/Product/Attributes/GtinMappingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Product\Attributes;

use Automattic\WooCommerce\GoogleListingsAndAds\Product\WCProductAdapter;
use PHPUnit\Framework\TestCase;
use WC_Helper_Product;

/**
* Class GtinMappingTest
*
* Unit tests to confirm that the GTIN value is mapped correctly.
* The value should be prioritised in the following order:
*

Check failure on line 15 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Whitespace found at end of line
* 1. WooCommerce Core: Global Unique ID
* 2. Google for WooCommerce: GTIN attribute
* 3. Google for WooCommerce: Attribute mapping rules
*/
class GtinMappingTest extends TestCase {

/** @var string $core_gtin Mock value to be used as the core gtin field. */

Check failure on line 22 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
private $core_gtin = '219837492834';

Check failure on line 23 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

/** @var string $gla_gtin Mock value to be used as the Google for WooCommerce gtin field. */

Check failure on line 25 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
private $gla_gtin = 'gla-gtin-field';

Check failure on line 26 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

/** @var string $gla_attribute_mapping_gtin Mock value to be used as the Google for WooCommerce attribute mapping gtin value. */

Check failure on line 28 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
private $gla_attribute_mapping_gtin = 'gla-attribute-mapping-gtin';

Check failure on line 29 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

/**
* Test GTIN mapping from WooCommerce Core Global Unique ID.
*

Check failure on line 33 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
* @return void

Check failure on line 34 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
*/
public function test_gtin_populated_from_wc_core_global_unique_id() {
$mock_product = WC_Helper_Product::create_simple_product( false );

Check failure on line 37 in tests/Unit/Product/Attributes/GtinMappingTest.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
$mock_product->set_global_unique_id( $this->core_gtin );

$adapter = new WCProductAdapter();
$adapter->mapTypes( [
'wc_product' => $mock_product,
'targetCountry' => 'US',
'gla_attributes' => [
'gtin' => $this->gla_gtin
],
] );

$this->assertEquals( $this->core_gtin, $adapter->getGtin() );
}

/**
* Test GTIN mapping from Google for WooCommerce GTIN attribute.
*
* @return void
*/
public function test_gtin_populated_from_gla_gtin_attribute() {
$mock_product = WC_Helper_Product::create_simple_product( false );
$mock_product->set_sku( $this->gla_attribute_mapping_gtin );

$adapter = new WCProductAdapter();
$adapter->mapTypes( [
'wc_product' => $mock_product,
'targetCountry' => 'US',
'mapping_rules' => [
[
'attribute' => 'gtin',
'source' => 'product:sku',
'category_condition_type' => 'all',
'categories' => ''
]
],
'gla_attributes' => [
'gtin' => $this->gla_gtin
],
] );

$this->assertEquals( $this->gla_gtin, $adapter->getGtin() );
}

/**
* Test GTIN mapping from Google for WooCommerce attribute mapping rules.
*
* @return void
*/
public function test_gtin_populated_from_attribute_mapping_rules() {
$mock_product = WC_Helper_Product::create_simple_product( false );
$mock_product->set_sku( $this->gla_attribute_mapping_gtin );

$adapter = new WCProductAdapter();
$adapter->mapTypes( [
'wc_product' => $mock_product,
'targetCountry' => 'US',
'mapping_rules' => [
[
'attribute' => 'gtin',
'source' => 'product:sku',
'category_condition_type' => 'all',
'categories' => ''
]
],
] );

$this->assertEquals( $this->gla_attribute_mapping_gtin, $adapter->getGtin() );
}

/**
* Test GTIN remains empty when no data is available.
*
* @return void
*/
public function test_gtin_remains_empty_when_no_data_available() {
$mock_product = WC_Helper_Product::create_simple_product( false );

$adapter = new WCProductAdapter();
$adapter->mapTypes( [
'wc_product' => $mock_product,
'targetCountry' => 'US',
] );

$this->assertEquals( '', $adapter->getGtin() );
}
}

0 comments on commit c4b11d7

Please sign in to comment.