2022-07-06 07:51:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\Utils;
|
|
|
|
|
|
|
|
use Automattic\WooCommerce\Blocks\Migration;
|
|
|
|
use Automattic\WooCommerce\Blocks\Options;
|
2022-11-09 15:28:08 +00:00
|
|
|
use Mockery;
|
2022-07-06 07:51:39 +00:00
|
|
|
|
|
|
|
class MigrationTest extends \WP_UnitTestCase {
|
2022-11-09 15:28:08 +00:00
|
|
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
2022-07-06 07:51:39 +00:00
|
|
|
parent::setUp();
|
|
|
|
delete_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
|
|
|
|
delete_option( Options::WC_BLOCK_VERSION );
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
protected function tearDown(): void {
|
|
|
|
\Mockery::close();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2022-07-06 07:51:39 +00:00
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
private function set_db_upgrades( Migration $mock, array $data ) {
|
2022-07-06 07:51:39 +00:00
|
|
|
$reflection = new \ReflectionClass( Migration::class );
|
|
|
|
$property = $reflection->getProperty( 'db_upgrades' );
|
|
|
|
$property->setAccessible( true );
|
|
|
|
$property->setValue(
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock,
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_migrations_run() {
|
|
|
|
update_option( Options::WC_BLOCK_VERSION, '1.0.0' );
|
|
|
|
|
|
|
|
$mock = Mockery::mock( Migration::class )->makePartial();
|
|
|
|
|
|
|
|
$mock = $this->set_db_upgrades(
|
2022-07-06 07:51:39 +00:00
|
|
|
$mock,
|
|
|
|
array(
|
|
|
|
'2.0.0' => array(
|
|
|
|
'execute_migration_2_0_0',
|
|
|
|
'execute_second_migration_2_0_0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock->expects()->execute_migration_2_0_0()->once();
|
|
|
|
$mock->expects()->execute_second_migration_2_0_0()->once();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
|
|
|
$mock->run_migrations();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_multiple_migrations_run() {
|
|
|
|
update_option( Options::WC_BLOCK_VERSION, '0.0.9' );
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = Mockery::mock( Migration::class )->makePartial();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = $this->set_db_upgrades(
|
2022-07-06 07:51:39 +00:00
|
|
|
$mock,
|
|
|
|
array(
|
|
|
|
'1.0.0' => array(
|
|
|
|
'execute_migration_1_0_0',
|
|
|
|
),
|
|
|
|
'2.0.0' => array(
|
|
|
|
'execute_migration_2_0_0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock->expects()->execute_migration_1_0_0()->once();
|
|
|
|
$mock->expects()->execute_migration_2_0_0()->once();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
|
|
|
$mock->run_migrations();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_skip_migrations() {
|
|
|
|
update_option( Options::WC_BLOCK_VERSION, '2.0.0' );
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = Mockery::mock( Migration::class )->makePartial();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = $this->set_db_upgrades(
|
2022-07-06 07:51:39 +00:00
|
|
|
$mock,
|
|
|
|
array(
|
|
|
|
'2.0.0' => array(
|
|
|
|
'execute_migration_2_0_0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock->expects()->execute_migration_2_0_0()->never();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
|
|
|
$mock->run_migrations();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_skip_migrations_when_missing_version_option() {
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = Mockery::mock( Migration::class )->makePartial();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock = $this->set_db_upgrades(
|
2022-07-06 07:51:39 +00:00
|
|
|
$mock,
|
|
|
|
array(
|
|
|
|
'2.0.0' => array(
|
|
|
|
'execute_migration_2_0_0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-11-09 15:28:08 +00:00
|
|
|
$mock->expects()->execute_migration_2_0_0()->never();
|
2022-07-06 07:51:39 +00:00
|
|
|
|
|
|
|
$mock->run_migrations();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|