Issue #3467344 by mondrake: Method getMockForAbstractClass() of class PHPUnit\Framework\TestCase is deprecated in PHPUnit 10 - replace in class InstallerRedirectTraitTest

merge-requests/9237/head
catch 2024-08-17 08:51:07 +09:00
parent 020d5fb7e7
commit 8b947677b3
5 changed files with 172 additions and 139 deletions

View File

@ -2391,18 +2391,6 @@ $ignoreErrors[] = [
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php',
];
$ignoreErrors[] = [
// identifier: method.deprecated
'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\.$#',
'count' => 2,
'path' => __DIR__ . '/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php',
];
$ignoreErrors[] = [
// identifier: method.deprecated
'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php',
];
$ignoreErrors[] = [
// identifier: isset.variable
'message' => '#^Variable \\$value in isset\\(\\) always exists and is not nullable\\.$#',

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Drupal\KernelTests\Core\Installer;
use Drupal\Core\Installer\InstallerRedirectTrait;
/**
* A class using the InstallerRedirectTrait for mocking purposes.
*/
class InstallerRedirectTraitMockableClass {
use InstallerRedirectTrait;
}

View File

@ -4,12 +4,11 @@ declare(strict_types=1);
namespace Drupal\KernelTests\Core\Installer;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\DatabaseNotFoundException;
use Drupal\Core\Database\Schema;
use Drupal\Core\Installer\InstallerRedirectTrait;
use Drupal\Tests\Core\Database\Stub\StubConnection;
use Drupal\Tests\Core\Database\Stub\StubSchema;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@ -69,70 +68,64 @@ class InstallerRedirectTraitTest extends KernelTestBase {
* @covers ::shouldRedirectToInstaller
* @dataProvider providerShouldRedirectToInstaller
*/
public function testShouldRedirectToInstaller($expected, $exception, $connection, $connection_info, $sequences_table_exists = TRUE): void {
public function testShouldRedirectToInstaller(bool $expected, string $exception, bool $connection, bool $connection_info, bool $sequences_table_exists = TRUE): void {
// Mock the trait.
$trait = $this->getMockBuilder(InstallerRedirectTraitMockableClass::class)
->onlyMethods(['isCli'])
->getMock();
// Make sure that the method thinks we are not using the cli.
$trait->expects($this->any())
->method('isCli')
->willReturn(FALSE);
// If testing no connection info, we need to make the 'default' key not
// visible.
if (!$connection_info) {
Database::renameConnection('default', __METHOD__);
}
if ($connection) {
// Mock the database connection.
$connection = $this->getMockBuilder(StubConnection::class)
->disableOriginalConstructor()
->onlyMethods(['schema'])
->getMock();
if ($connection_info) {
// Mock the database schema class.
$schema = $this->getMockBuilder(StubSchema::class)
->disableOriginalConstructor()
->onlyMethods(['tableExists'])
->getMock();
$schema->expects($this->any())
->method('tableExists')
->with('sequences')
->willReturn($sequences_table_exists);
$connection->expects($this->any())
->method('schema')
->willReturn($schema);
}
}
else {
// Set the database connection if there is none.
$connection = NULL;
}
try {
throw new $exception();
}
catch (\Exception $e) {
// Mock the trait.
$trait = $this->getMockBuilder(InstallerRedirectTraitMockableClass::class)
->onlyMethods(['isCli'])
->getMock();
// Make sure that the method thinks we are not using the cli.
$trait->expects($this->any())
->method('isCli')
->willReturn(FALSE);
// Un-protect the method using reflection.
$method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
// Mock the database connection info.
$db = $this->getMockForAbstractClass(Database::class);
$property_ref = new \ReflectionProperty($db, 'databaseInfo');
$property_ref->setValue($db, ['default' => $connection_info]);
if ($connection) {
// Mock the database connection.
$connection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->onlyMethods(['schema'])
->getMockForAbstractClass();
if ($connection_info) {
// Mock the database schema class.
$schema = $this->getMockBuilder(Schema::class)
->disableOriginalConstructor()
->onlyMethods(['tableExists'])
->getMockForAbstractClass();
$schema->expects($this->any())
->method('tableExists')
->with('sequences')
->willReturn($sequences_table_exists);
$connection->expects($this->any())
->method('schema')
->willReturn($schema);
}
}
else {
// Set the database connection if there is none.
$connection = NULL;
}
// Call shouldRedirectToInstaller.
$method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
$this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));
}
if (!$connection_info) {
Database::renameConnection(__METHOD__, 'default');
}
}
}
/**
* A class using the InstallerRedirectTrait for mocking purposes.
*/
class InstallerRedirectTraitMockableClass {
use InstallerRedirectTrait;
}

View File

@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\Core\Database\Stub;
use Drupal\Core\Database\Schema as DatabaseSchema;
/**
* A stub of the abstract Schema class for testing purposes.
*
* Includes minimal implementations of Schema's abstract methods.
*/
class StubSchema extends DatabaseSchema {
/**
* {@inheritdoc}
*/
public function getFieldTypeMap() {
return [];
}
/**
* {@inheritdoc}
*/
public function renameTable($table, $new_name) {
}
/**
* {@inheritdoc}
*/
public function dropTable($table) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function addField($table, $field, $spec, $keys_new = []) {
}
/**
* {@inheritdoc}
*/
public function dropField($table, $field) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function indexExists($table, $name) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function addPrimaryKey($table, $fields) {
}
/**
* {@inheritdoc}
*/
public function dropPrimaryKey($table) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function addUniqueKey($table, $name, $fields) {
}
/**
* {@inheritdoc}
*/
public function dropUniqueKey($table, $name) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function addIndex($table, $name, $fields, array $spec) {
}
/**
* {@inheritdoc}
*/
public function dropIndex($table, $name) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function changeField($table, $field, $field_new, $spec, $keys_new = []) {
}
}

View File

@ -2,76 +2,11 @@
namespace Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses;
use Drupal\Core\Database\Schema as DatabaseSchema;
use Drupal\Tests\Core\Database\Stub\StubSchema;
/**
* CoreFakeWithAllCustomClasses implementation of \Drupal\Core\Database\Schema.
*/
class Schema extends DatabaseSchema {
/**
* {@inheritdoc}
*/
public function getFieldTypeMap() {}
/**
* {@inheritdoc}
*/
public function renameTable($table, $new_name) {}
/**
* {@inheritdoc}
*/
public function dropTable($table) {}
/**
* {@inheritdoc}
*/
public function addField($table, $field, $spec, $keys_new = []) {}
/**
* {@inheritdoc}
*/
public function dropField($table, $field) {}
/**
* {@inheritdoc}
*/
public function indexExists($table, $name) {}
/**
* {@inheritdoc}
*/
public function addPrimaryKey($table, $fields) {}
/**
* {@inheritdoc}
*/
public function dropPrimaryKey($table) {}
/**
* {@inheritdoc}
*/
public function addUniqueKey($table, $name, $fields) {}
/**
* {@inheritdoc}
*/
public function dropUniqueKey($table, $name) {}
/**
* {@inheritdoc}
*/
public function addIndex($table, $name, $fields, array $spec) {}
/**
* {@inheritdoc}
*/
public function dropIndex($table, $name) {}
/**
* {@inheritdoc}
*/
public function changeField($table, $field, $field_new, $spec, $keys_new = []) {}
class Schema extends StubSchema {
}