Replace deprecated doctrine methods in tests directory

[MAILPOET-3889]
This commit is contained in:
Rostislav Wolny
2021-10-27 10:43:39 +02:00
committed by Veljko V
parent 73a9bed483
commit b8d72522a2
10 changed files with 45 additions and 41 deletions

View File

@ -20,7 +20,7 @@ class FeatureFlagsTest extends \MailPoetTest {
parent::_before(); parent::_before();
$this->repository = $this->diContainer->get(FeatureFlagsRepository::class); $this->repository = $this->diContainer->get(FeatureFlagsRepository::class);
$tableName = $this->entityManager->getClassMetadata(FeatureFlagEntity::class)->getTableName(); $tableName = $this->entityManager->getClassMetadata(FeatureFlagEntity::class)->getTableName();
$this->entityManager->getConnection()->executeUpdate("TRUNCATE $tableName"); $this->entityManager->getConnection()->executeStatement("TRUNCATE $tableName");
} }
public function testItReturnsDefaults() { public function testItReturnsDefaults() {

View File

@ -62,6 +62,6 @@ class UserFlagsTest extends \MailPoetTest {
private function cleanup() { private function cleanup() {
$tableName = $this->entityManager->getClassMetadata(UserFlagEntity::class)->getTableName(); $tableName = $this->entityManager->getClassMetadata(UserFlagEntity::class)->getTableName();
$this->entityManager->getConnection()->executeUpdate("TRUNCATE $tableName"); $this->entityManager->getConnection()->executeStatement("TRUNCATE $tableName");
} }
} }

View File

@ -32,11 +32,12 @@ class ConnectionFactoryTest extends \MailPoetTest {
expect($connection->getWrappedConnection())->isInstanceOf(PDO::class); expect($connection->getWrappedConnection())->isInstanceOf(PDO::class);
expect($connection->getDriver())->isInstanceOf(PDOMySql\Driver::class); expect($connection->getDriver())->isInstanceOf(PDOMySql\Driver::class);
expect($connection->getDatabasePlatform())->isInstanceOf(MySqlPlatform::class); expect($connection->getDatabasePlatform())->isInstanceOf(MySqlPlatform::class);
expect($connection->getHost())->equals(Env::$dbHost); $params = $connection->getParams();
expect($connection->getParams())->notContains('unix_socket'); expect($params['host'])->equals(Env::$dbHost);
expect($connection->getUsername())->equals(Env::$dbUsername); expect($params)->notContains('unix_socket');
expect($connection->getPassword())->equals(Env::$dbPassword); expect($params['user'])->equals(Env::$dbUsername);
expect($connection->getParams()['charset'])->equals(Env::$dbCharset); expect($params['password'])->equals(Env::$dbPassword);
expect($params['charset'])->equals(Env::$dbCharset);
expect($connection->getDatabase())->equals(Env::$dbName); expect($connection->getDatabase())->equals(Env::$dbName);
expect(Type::getType(JsonType::NAME))->isInstanceOf(JsonType::class); expect(Type::getType(JsonType::NAME))->isInstanceOf(JsonType::class);
@ -49,7 +50,7 @@ class ConnectionFactoryTest extends \MailPoetTest {
Env::$dbPort = 3456; Env::$dbPort = 3456;
$connectionFactory = new ConnectionFactory(); $connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(); $connection = $connectionFactory->createConnection();
expect($connection->getPort())->equals(3456); expect($connection->getParams()['port'])->equals(3456);
} finally { } finally {
Env::$dbPort = $backup; Env::$dbPort = $backup;
} }
@ -68,10 +69,10 @@ class ConnectionFactoryTest extends \MailPoetTest {
Env::$dbSocket = 'socket'; Env::$dbSocket = 'socket';
$connectionFactory = new ConnectionFactory(); $connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(); $connection = $connectionFactory->createConnection();
$params = $connection->getParams();
expect($connection->getHost())->null(); expect(isset($params['host']))->false();
expect($connection->getPort())->null(); expect(isset($params['port']))->false();
expect($connection->getParams()['unix_socket'])->equals('socket'); expect($params['unix_socket'])->equals('socket');
} }
public function testItSetsUpIpV6() { public function testItSetsUpIpV6() {
@ -80,19 +81,19 @@ class ConnectionFactoryTest extends \MailPoetTest {
Env::$dbHost = '::1'; Env::$dbHost = '::1';
$connectionFactory = new ConnectionFactory(); $connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(); $connection = $connectionFactory->createConnection();
expect($connection->getHost())->equals('[::1]'); expect($connection->getParams()['host'])->equals('[::1]');
Env::$dbHost = 'b57e:9b70:ab96:6a0b:5ba2:49e3:ebba:a036'; Env::$dbHost = 'b57e:9b70:ab96:6a0b:5ba2:49e3:ebba:a036';
$connectionFactory = new ConnectionFactory(); $connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(); $connection = $connectionFactory->createConnection();
expect($connection->getHost())->equals('[b57e:9b70:ab96:6a0b:5ba2:49e3:ebba:a036]'); expect($connection->getParams()['host'])->equals('[b57e:9b70:ab96:6a0b:5ba2:49e3:ebba:a036]');
// try to actually connect to the DB over IPv6 // try to actually connect to the DB over IPv6
Env::$dbHost = '::ffff:' . gethostbyname($this->envBackup['db_host']); Env::$dbHost = '::ffff:' . gethostbyname($this->envBackup['db_host']);
$connectionFactory = new ConnectionFactory(); $connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(); $connection = $connectionFactory->createConnection();
expect($connection->getWrappedConnection())->isInstanceOf(PDO::class); expect($connection->getWrappedConnection())->isInstanceOf(PDO::class);
expect($connection->executeQuery('SELECT 1')->fetchColumn())->same('1'); expect($connection->executeQuery('SELECT 1')->fetchOne())->same('1');
} }
public function testItSetsDriverOptions() { public function testItSetsDriverOptions() {
@ -108,7 +109,7 @@ class ConnectionFactoryTest extends \MailPoetTest {
@@session.character_set_connection, @@session.character_set_connection,
@@session.character_set_results, @@session.character_set_results,
@@session.collation_connection @@session.collation_connection
')->fetch(); ')->fetchAssociative();
// check timezone, SQL mode, wait timeout // check timezone, SQL mode, wait timeout
expect($result['@@session.time_zone'])->equals(Env::$dbTimezoneOffset); expect($result['@@session.time_zone'])->equals(Env::$dbTimezoneOffset);

View File

@ -40,8 +40,8 @@ class TimestampListenerTest extends \MailPoetTest {
$this->replaceListeners($originalListener, $newTimestampListener); $this->replaceListeners($originalListener, $newTimestampListener);
$this->tableName = $this->entityManager->getClassMetadata(TimestampEntity::class)->getTableName(); $this->tableName = $this->entityManager->getClassMetadata(TimestampEntity::class)->getTableName();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->tableName"); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
$this->connection->executeUpdate(" $this->connection->executeStatement("
CREATE TABLE $this->tableName ( CREATE TABLE $this->tableName (
id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at timestamp NULL, created_at timestamp NULL,
@ -63,7 +63,7 @@ class TimestampListenerTest extends \MailPoetTest {
} }
public function testItSetsTimestampOnUpdate() { public function testItSetsTimestampOnUpdate() {
$this->connection->executeUpdate(" $this->connection->executeStatement("
INSERT INTO $this->tableName (id, created_at, updated_at, name) VALUES ( INSERT INTO $this->tableName (id, created_at, updated_at, name) VALUES (
123, 123,
'2000-01-01 12:00:00', '2000-01-01 12:00:00',
@ -83,7 +83,7 @@ class TimestampListenerTest extends \MailPoetTest {
public function _after() { public function _after() {
parent::_after(); parent::_after();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->tableName"); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
} }
/** /**

View File

@ -28,8 +28,8 @@ class ValidationTest extends \MailPoetTest {
$this->wp = new WPFunctions(); $this->wp = new WPFunctions();
$this->entityManager = $this->createEntityManager(); $this->entityManager = $this->createEntityManager();
$this->tableName = $this->entityManager->getClassMetadata(ValidatedEntity::class)->getTableName(); $this->tableName = $this->entityManager->getClassMetadata(ValidatedEntity::class)->getTableName();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->tableName"); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
$this->connection->executeUpdate(" $this->connection->executeStatement("
CREATE TABLE $this->tableName ( CREATE TABLE $this->tableName (
id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL name varchar(255) NOT NULL
@ -52,7 +52,7 @@ class ValidationTest extends \MailPoetTest {
public function testItValidatesUpdatedEntity() { public function testItValidatesUpdatedEntity() {
$id = 1; $id = 1;
$name = 'Test name'; $name = 'Test name';
$this->connection->executeUpdate("INSERT INTO $this->tableName (id, name) VALUES (?, ?)", [$id, $name]); $this->connection->executeStatement("INSERT INTO $this->tableName (id, name) VALUES (?, ?)", [$id, $name]);
/** @var ValidatedEntity $entity */ /** @var ValidatedEntity $entity */
$entity = $this->entityManager->find(ValidatedEntity::class, $id); $entity = $this->entityManager->find(ValidatedEntity::class, $id);

View File

@ -40,8 +40,8 @@ class JsonTypesTest extends \MailPoetTest {
$this->wp = new WPFunctions(); $this->wp = new WPFunctions();
$this->entityManager = $this->createEntityManager(); $this->entityManager = $this->createEntityManager();
$this->tableName = $this->entityManager->getClassMetadata(JsonEntity::class)->getTableName(); $this->tableName = $this->entityManager->getClassMetadata(JsonEntity::class)->getTableName();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->tableName"); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
$this->connection->executeUpdate(" $this->connection->executeStatement("
CREATE TABLE $this->tableName ( CREATE TABLE $this->tableName (
id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, id int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
json_data longtext NULL, json_data longtext NULL,
@ -57,13 +57,13 @@ class JsonTypesTest extends \MailPoetTest {
$this->entityManager->persist($entity); $this->entityManager->persist($entity);
$this->entityManager->flush(); $this->entityManager->flush();
$savedData = $this->connection->executeQuery("SELECT * FROM $this->tableName")->fetch(); $savedData = $this->connection->executeQuery("SELECT * FROM $this->tableName")->fetchAssociative() ?: [];
expect($savedData['json_data'])->same(json_encode($this->testData)); expect($savedData['json_data'])->same(json_encode($this->testData));
expect($savedData['json_or_serialized_data'])->same(json_encode($this->testData)); expect($savedData['json_or_serialized_data'])->same(json_encode($this->testData));
} }
public function testItLoadsJsonData() { public function testItLoadsJsonData() {
$this->connection->executeUpdate( $this->connection->executeStatement(
"INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)", "INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)",
[ [
1, 1,
@ -79,7 +79,7 @@ class JsonTypesTest extends \MailPoetTest {
} }
public function testItLoadsSerializedData() { public function testItLoadsSerializedData() {
$this->connection->executeUpdate( $this->connection->executeStatement(
"INSERT INTO $this->tableName (id, json_or_serialized_data) VALUES (?, ?)", "INSERT INTO $this->tableName (id, json_or_serialized_data) VALUES (?, ?)",
[ [
1, 1,
@ -100,13 +100,14 @@ class JsonTypesTest extends \MailPoetTest {
$this->entityManager->persist($entity); $this->entityManager->persist($entity);
$this->entityManager->flush(); $this->entityManager->flush();
$savedData = $this->connection->executeQuery("SELECT * FROM $this->tableName")->fetch(); $savedData = $this->connection->executeQuery("SELECT * FROM $this->tableName")->fetchAssociative() ?: [];
expect($savedData)->array();
expect($savedData['json_data'])->null(); expect($savedData['json_data'])->null();
expect($savedData['json_or_serialized_data'])->null(); expect($savedData['json_or_serialized_data'])->null();
} }
public function testItLoadsNullData() { public function testItLoadsNullData() {
$this->connection->executeUpdate( $this->connection->executeStatement(
"INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)", "INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)",
[ [
1, 1,
@ -122,7 +123,7 @@ class JsonTypesTest extends \MailPoetTest {
} }
public function testItLoadsEmptyStringAsNull() { public function testItLoadsEmptyStringAsNull() {
$this->connection->executeUpdate( $this->connection->executeStatement(
"INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)", "INSERT INTO $this->tableName (id, json_data, json_or_serialized_data) VALUES (?, ?, ?)",
[ [
1, 1,
@ -152,7 +153,7 @@ class JsonTypesTest extends \MailPoetTest {
} }
public function testItDoesNotLoadInvalidData() { public function testItDoesNotLoadInvalidData() {
$this->connection->executeUpdate( $this->connection->executeStatement(
"INSERT INTO $this->tableName (id, json_data) VALUES (?, ?)", "INSERT INTO $this->tableName (id, json_data) VALUES (?, ?)",
[ [
1, 1,
@ -171,7 +172,7 @@ class JsonTypesTest extends \MailPoetTest {
public function _after() { public function _after() {
parent::_after(); parent::_after();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->tableName"); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
} }
private function createEntityManager() { private function createEntityManager() {

View File

@ -600,11 +600,13 @@ class WPTest extends \MailPoetTest {
private function getUser(int $id): array { private function getUser(int $id): array {
global $wpdb; global $wpdb;
return $this->entityManager->getConnection()->executeQuery(' $user = $this->entityManager->getConnection()->executeQuery('
SELECT user_login, user_email, user_registered SELECT user_login, user_email, user_registered
FROM ' . $wpdb->users . ' FROM ' . $wpdb->users . '
WHERE id = :id WHERE id = :id
', ['id' => $id])->fetch(); ', ['id' => $id])->fetchAssociative();
$this->assertIsArray($user);
return $user;
} }
private function updateWPUserEmail($id, $email) { private function updateWPUserEmail($id, $email) {

View File

@ -125,7 +125,7 @@ class SettingsControllerTest extends \MailPoetTest {
private function createOrUpdateSetting($name, $value) { private function createOrUpdateSetting($name, $value) {
$tableName = $this->entityManager->getClassMetadata(SettingEntity::class)->getTableName(); $tableName = $this->entityManager->getClassMetadata(SettingEntity::class)->getTableName();
$this->connection->executeUpdate(" $this->connection->executeStatement("
INSERT INTO $tableName (name, value) VALUES (?, ?) INSERT INTO $tableName (name, value) VALUES (?, ?)
ON DUPLICATE KEY UPDATE value = ? ON DUPLICATE KEY UPDATE value = ?
", [$name, $value, $value]); ", [$name, $value, $value]);
@ -133,6 +133,6 @@ class SettingsControllerTest extends \MailPoetTest {
private function getSettingValue($name) { private function getSettingValue($name) {
$tableName = $this->entityManager->getClassMetadata(SettingEntity::class)->getTableName(); $tableName = $this->entityManager->getClassMetadata(SettingEntity::class)->getTableName();
return $this->connection->executeQuery("SELECT value FROM $tableName WHERE name = ?", [$name])->fetchColumn(); return $this->connection->executeQuery("SELECT value FROM $tableName WHERE name = ?", [$name])->fetchOne();
} }
} }

View File

@ -117,6 +117,6 @@ class UserFlagsControllerTest extends \MailPoetTest {
private function cleanup() { private function cleanup() {
$tableName = $this->entityManager->getClassMetadata(UserFlagEntity::class)->getTableName(); $tableName = $this->entityManager->getClassMetadata(UserFlagEntity::class)->getTableName();
$this->connection->executeUpdate("TRUNCATE $tableName"); $this->connection->executeStatement("TRUNCATE $tableName");
} }
} }

View File

@ -223,9 +223,9 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
$classMetadata = $this->entityManager->getClassMetadata($entityName); $classMetadata = $this->entityManager->getClassMetadata($entityName);
$tableName = $classMetadata->getTableName(); $tableName = $classMetadata->getTableName();
$connection = $this->entityManager->getConnection(); $connection = $this->entityManager->getConnection();
$connection->query('SET FOREIGN_KEY_CHECKS=0'); $connection->executeQuery('SET FOREIGN_KEY_CHECKS=0');
$connection->executeUpdate("TRUNCATE $tableName"); $connection->executeStatement("TRUNCATE $tableName");
$connection->query('SET FOREIGN_KEY_CHECKS=1'); $connection->executeQuery('SET FOREIGN_KEY_CHECKS=1');
} }
public function clearSubscribersCountCache() { public function clearSubscribersCountCache() {