Skip to content

Commit

Permalink
Fix PHP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Feb 13, 2024
1 parent b34e1ae commit 028714b
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,38 @@
use Automattic\WooCommerce\Blocks\Migration;
use Automattic\WooCommerce\Blocks\Options;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Templates\ProductCatalogTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductCategoryTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductTagTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplate;
use WP_UnitTestCase;

/**
* Tests for the BlockTemplateUtils class.
*/
class BlockTemplateUtilsTest extends WP_UnitTestCase {

/**
* Holds an instance of the dependency injection container.
*
* @var Container
*/
private $container;

/**
* Setup test environment.
*/
protected function setUp(): void {
parent::setUp();

// Switch to a block theme and register templates.
$this->container = Package::container();
switch_theme( 'twentytwentytwo' );
$this->register_block_templates();

// Reset options.
delete_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
delete_option( Options::WC_BLOCK_VERSION );
}
Expand All @@ -33,6 +53,27 @@ public function provideFallbackData() {
);
}

/**
* Constructs template classes. Needed so template are registered after
* switching to a block theme.
*
* @return void
*/
public function register_block_templates(): void {
$templates = [
ProductCatalogTemplate::class,
ProductCategoryTemplate::class,
ProductTagTemplate::class,
ProductAttributeTemplate::class,
SingleProductTemplate::class,
];

foreach ( $templates as $template_class ) {
$template = $this->container->get( $template_class );
$template->__construct();
}
}

/**
* Test template_is_eligible_for_product_archive_fallback.
*
Expand Down

0 comments on commit 028714b

Please sign in to comment.