[dev] PSR-4 naming fixes in tests (part 2) (#51198)

In this PR we address PSR-4 naming-related violations (class FQN vs path mismatches and co) in tests.
This commit is contained in:
Vladimir Reznichenko 2024-09-09 11:26:55 +02:00 committed by GitHub
parent d185a34145
commit 8dcb562c39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 207 additions and 83 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Coding standatds: fix PSR-4 naming violation in tests (part 2).

View File

@ -55,11 +55,16 @@
<include-pattern>tests/php/src/</include-pattern>
<exclude-pattern>plugins/woocommerce/src/Admin/Notes/DeprecatedNotes.php</exclude-pattern>
<exclude-pattern>plugins/woocommerce/tests/php/src/Proxies/ExampleClasses/</exclude-pattern>
<exclude-pattern>plugins/woocommerce/tests/php/src/Internal/DependencyManagement/ExampleClasses/</exclude-pattern>
<properties>
<property name="composerJsonPath" value="composer.json"/>
</properties>
</rule>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod.Found">
<exclude-pattern>plugins/woocommerce/tests/php/src/Blocks/Mocks/</exclude-pattern>
</rule>
<rule ref="Generic.PHP.RequireStrictTypes">
<include-pattern>includes/</include-pattern>
<include-pattern>lib/</include-pattern>

View File

@ -1,6 +1,7 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Admin\Features\ShippingPartnerSuggestions;
namespace Automattic\WooCommerce\Tests\Admin\ShippingPartnerSuggestions;
use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\EvaluateSuggestion;
use Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions\DefaultShippingPartners;

View File

@ -1,7 +1,7 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Templates;
namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;
use Automattic\WooCommerce\Tests\Blocks\Mocks\BlockHooksTestBlock;
use WP_UnitTestCase;

View File

@ -1,25 +1,47 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Utils;
namespace Automattic\WooCommerce\Tests\Blocks;
use Automattic\WooCommerce\Blocks\Migration;
use Automattic\WooCommerce\Blocks\Options;
use Mockery;
/**
* Unit tests for the Migration class.
*/
class MigrationTest extends \WP_UnitTestCase {
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
/**
* Setup testing environment.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();
delete_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
delete_option( Options::WC_BLOCK_VERSION );
}
/**
* Cleanup testing environment.
*
* @return void
*/
protected function tearDown(): void {
\Mockery::close();
parent::tearDown();
}
/**
* Inject test data into migration mock.
*
* @param Migration $mock The mock to patch.
* @param array $data The data to inject.
*
* @return Migration
*/
private function set_db_upgrades( Migration $mock, array $data ) {
$reflection = new \ReflectionClass( Migration::class );
$property = $reflection->getProperty( 'db_upgrades' );
@ -32,6 +54,9 @@ class MigrationTest extends \WP_UnitTestCase {
return $mock;
}
/**
* Test running single migration.
*/
public function test_migrations_run() {
update_option( Options::WC_BLOCK_VERSION, '1.0.0' );
@ -53,6 +78,9 @@ class MigrationTest extends \WP_UnitTestCase {
$mock->run_migrations();
}
/**
* Test running multiple migrations.
*/
public function test_multiple_migrations_run() {
update_option( Options::WC_BLOCK_VERSION, '0.0.9' );
@ -76,6 +104,9 @@ class MigrationTest extends \WP_UnitTestCase {
$mock->run_migrations();
}
/**
* Test skipping executed migrations.
*/
public function test_skip_migrations() {
update_option( Options::WC_BLOCK_VERSION, '2.0.0' );
@ -95,6 +126,9 @@ class MigrationTest extends \WP_UnitTestCase {
$mock->run_migrations();
}
/**
* Test skipping executed migrations.
*/
public function test_skip_migrations_when_missing_version_option() {
$mock = Mockery::mock( Migration::class )->makePartial();
@ -112,5 +146,4 @@ class MigrationTest extends \WP_UnitTestCase {
$mock->run_migrations();
}
}

View File

@ -0,0 +1,70 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
/**
* A mock class.
*/
class AssetDataRegistryMock extends AssetDataRegistry {
/**
* Debug flag.
*
* @var bool
*/
private $debug = true;
/**
* Sets the debug flag value.
*
* @param bool $debug The debug flag value to set.
*
* @return void
*/
public function set_debug( $debug ) {
$this->debug = $debug;
}
/**
* Returns the debug flag value.
*
* @return bool
*/
protected function debug() {
return $this->debug;
}
/**
* Exposes private registered data to child classes.
*
* @return array The registered data on the private data property
*/
public function get() {
return parent::get();
}
/**
* Used for on demand initialization of asset data and registering it with
* the internal data registry.
*
* Note: core data will overwrite any externally registered data via the api.
*/
public function initialize_core_data() {
parent::initialize_core_data();
}
/**
* Loops through each registered lazy data callback and adds the returned
* value to the data array.
*
* This method is executed right before preparing the data for printing to
* the rendered screen.
*
* @return void
*/
public function execute_lazy_data() {
parent::execute_lazy_data();
}
}

View File

@ -1,4 +1,6 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
@ -10,9 +12,9 @@ class CartCheckoutUtilsMock extends CartCheckoutUtils {
/**
* Protected test wrapper for deep_sort_with_accents.
*
* @param array $array The array we want to sort.
* @param array $array_to_sort The array we want to sort.
*/
public static function deep_sort_test( $array ) {
return self::deep_sort_with_accents( $array );
public static function deep_sort_test( $array_to_sort ) {
return self::deep_sort_with_accents( $array_to_sort );
}
}

View File

@ -1,11 +1,25 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
/**
* A mock class.
*/
class MockTestDependency {
/**
* Dependency.
*
* @var object
*/
public $dependency;
/**
* Constructor.
*
* @param object $dependency Another dependency.
*/
public function __construct( $dependency = null ) {
$this->dependency = $dependency;
}
};
}

View File

@ -1,4 +1,6 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks\OrderConfirmation;
use Automattic\WooCommerce\Blocks\Package;
@ -23,15 +25,24 @@ class TotalsMock extends Totals {
);
}
public function render_content( $order, $permission = false, $attributes = [], $content = '' ) {
return parent::render_content( $order, $permission, $attributes, $content );
}
/**
* For now don't need to initialize anything in tests so let's
* just override the default behaviour.
*/
protected function initialize() {
}
}
/**
* This renders the content of the block within the wrapper.
*
* @param \WC_Order $order Order object.
* @param string|false $permission If the current user can view the order details or not.
* @param array $attributes Block attributes.
* @param string $content Original block content.
*
* @return string
*/
public function render_content( $order, $permission = false, $attributes = [], $content = '' ) {
return parent::render_content( $order, $permission, $attributes, $content );
}
}

View File

@ -1,4 +1,6 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
use Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection;

View File

@ -1,4 +1,6 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
use Automattic\WooCommerce\Blocks\BlockTypes\ProductQuery;

View File

@ -1,16 +1,19 @@
<?php
/**
* CartController Tests.
*/
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Unit tests for the CartController class.
*/
class CartControllerTests extends TestCase {
/**
* Test cart error code is getting exposed.
*/
public function test_get_cart_errors() {
$class = new CartController();
$fixtures = new FixtureData();
@ -25,7 +28,7 @@ class CartControllerTests extends TestCase {
$out_of_stock_product_key = wc()->cart->add_to_cart( $out_of_stock_product->get_id(), 2 );
$out_of_stock_in_cart = wc()->cart->get_cart_item( $out_of_stock_product_key )['data'];
// This product will have exact levels of stock known
// This product will have exact levels of stock known.
$partially_out_of_stock_product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 2',
@ -35,7 +38,7 @@ class CartControllerTests extends TestCase {
$partially_out_of_stock_key = wc()->cart->add_to_cart( $partially_out_of_stock_product->get_id(), 4 );
$partially_out_of_stock_in_cart = wc()->cart->get_cart_item( $partially_out_of_stock_key )['data'];
// This product will have exact levels of stock known
// This product will have exact levels of stock known.
$too_many_in_cart_product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 3',
@ -50,7 +53,7 @@ class CartControllerTests extends TestCase {
$partially_out_of_stock_in_cart->set_stock_quantity( 2 );
$too_many_in_cart_in_cart->set_sold_individually( true );
// This product will not be purchasable
// This product will not be purchasable.
$not_purchasable_product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 4',
@ -59,10 +62,10 @@ class CartControllerTests extends TestCase {
);
wc()->cart->add_to_cart( $not_purchasable_product->get_id(), 2 );
// This function will force the $product->is_purchasable() function to return false for our $not_purchasable_product
// This function will force the $product->is_purchasable() function to return false for our $not_purchasable_product.
add_filter(
'woocommerce_is_purchasable',
function( $is_purchasable, $product ) use ( $not_purchasable_product ) {
function ( $is_purchasable, $product ) use ( $not_purchasable_product ) {
if ( $product->get_id() === $not_purchasable_product->get_id() ) {
return false;
}
@ -88,7 +91,5 @@ class CartControllerTests extends TestCase {
foreach ( $expected_errors as $expected_error ) {
$this->assertContains( $expected_error, $error_codes );
}
}
}

View File

@ -1,7 +1,5 @@
<?php
/**
* NoticeHandler Tests.
*/
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;

View File

@ -1,7 +1,5 @@
<?php
/**
* OrderController Tests.
*/
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;

View File

@ -1,10 +1,15 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\StoreApi\Utilities\ProductQueryFilters;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* Unit tests for the ProductQueryFilters class.
*/
class ProductQueryFiltersTest extends TestCase {
/**
* Setup test product data. Called before every test.
@ -16,7 +21,7 @@ class ProductQueryFiltersTest extends TestCase {
add_filter(
'woocommerce_product_stock_status_options',
function( $status ) {
function ( $status ) {
$status['custom1'] = 'Custom status 1';
$status['custom2'] = 'Custom status 2';
return $status;

View File

@ -1,6 +1,7 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Utilities;
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Utilities;
use Automattic\WooCommerce\StoreApi\Utilities\SanitizationUtils;

View File

@ -1,5 +1,7 @@
<?php
namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\Utils;
use Automattic\WooCommerce\Blocks\Utils\MiniCartUtils;

View File

@ -1,30 +0,0 @@
<?php
namespace Automattic\WooCommerce\Tests\Blocks\Mocks;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
class AssetDataRegistryMock extends AssetDataRegistry {
private $debug = true;
public function execute_lazy_data() {
parent::execute_lazy_data();
}
public function get() {
return parent::get();
}
public function set_debug( $debug ) {
$this->debug = $debug;
}
public function initialize_core_data() {
return parent::initialize_core_data();
}
protected function debug() {
return $this->debug;
}
}

View File

@ -1,11 +1,12 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\Admin\ShippingPartnerSuggestions;
namespace Automattic\WooCommerce\Tests\Internal\Admin\MarketingRecommendations;
use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller;
use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\Init;
use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\DefaultMarketingRecommendations;
use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\Init;
use Automattic\WooCommerce\Admin\Features\MarketingRecommendations\MarketingRecommendationsDataSourcePoller;
use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller;
use WC_Unit_Test_Case;
/**

View File

@ -1,11 +1,14 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\Admin\Orders\COTRedirectionController;
/**
* Describes our redirection logic covering HPOS admin screens when Custom Order Tables are not authoritative.
*/
class COTRedirectionControllerTest extends WC_Unit_Test_Case {
class COTRedirectionControllerTest extends \WC_Unit_Test_Case {
/**
* @var COTRedirectionController
*/

View File

@ -1,7 +1,7 @@
<?php
/**
* Tests for BatchProcessingController class.
*/
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\BatchProcessing;
use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessingController;
use Automattic\WooCommerce\Internal\BatchProcessing\BatchProcessorInterface;
@ -10,7 +10,7 @@ use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
/**
* Class BatchProcessingControllerTests.
*/
class BatchProcessingControllerTests extends WC_Unit_Test_Case {
class BatchProcessingControllerTests extends \WC_Unit_Test_Case {
/**
* Instance of BatchProcessingController.

View File

@ -1,6 +1,7 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\ComingSoon;
namespace Automattic\WooCommerce\Tests\Internal\ComingSoon;
use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonCacheInvalidator;

View File

@ -1,6 +1,7 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\ComingSoon;
namespace Automattic\WooCommerce\Tests\Internal\ComingSoon;
use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonHelper;

View File

@ -1,9 +1,9 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\ComingSoon;
namespace Automattic\WooCommerce\Tests\Internal\ComingSoon;
use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonRequestHandler;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Tests for the coming soon cache invalidator class.

View File

@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\Orders;

View File

@ -1,9 +1,9 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\ProductDownloads;
namespace Automattic\WooCommerce\Tests\Internal\ProductDownloads\ApprovedDirectories;
use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register;
use WC_Unit_Test_Case;
/**

View File

@ -1,12 +1,10 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\ProductDownloads;
namespace Automattic\WooCommerce\Tests\Internal\ProductDownloads\ApprovedDirectories;
use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Synchronize;
use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
use Automattic\WooCommerce\Testing\Tools\FakeQueue;
use WC_Queue_Interface;
use WC_Unit_Test_Case;