EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Test refactoring
Yurii40ec65fbea9d
diff --git a/application/Espo/Core/Utils/Database/Schema/ColumnPreparators/MysqlColumnPreparator.php b/application/Espo/Core/Utils/Database/Schema/ColumnPreparators/MysqlColumnPreparator.phpindex e75a9faecf..9265feb3b5 100644--- a/application/Espo/Core/Utils/Database/Schema/ColumnPreparators/MysqlColumnPreparator.php+++ b/application/Espo/Core/Utils/Database/Schema/ColumnPreparators/MysqlColumnPreparator.php@@ -29,7 +29,6 @@namespace Espo\Core\Utils\Database\Schema\ColumnPreparators;-use Doctrine\DBAL\Types\StringType;use Doctrine\DBAL\Types\Types;use Espo\Core\Utils\Database\Dbal\Types\LongtextType;use Espo\Core\Utils\Database\Dbal\Types\MediumtextType;diff --git a/application/Espo/Core/Utils/Database/Schema/SchemaManager.php b/application/Espo/Core/Utils/Database/Schema/SchemaManager.phpindex 99dca1f4d5..85a41f6da5 100644--- a/application/Espo/Core/Utils/Database/Schema/SchemaManager.php+++ b/application/Espo/Core/Utils/Database/Schema/SchemaManager.php@@ -38,6 +38,7 @@ use Doctrine\DBAL\Schema\ComparatorConfig;use Doctrine\DBAL\Schema\Schema;use Doctrine\DBAL\Schema\SchemaDiff;use Doctrine\DBAL\Schema\SchemaException;+use Doctrine\DBAL\Types\Exception\TypesException;use Doctrine\DBAL\Types\Type;use Espo\Core\Binding\BindingContainerBuilder;@@ -46,6 +47,7 @@ use Espo\Core\Utils\Database\Helper;use Espo\Core\Utils\Log;use Espo\Core\Utils\Metadata\OrmMetadataData;+use RuntimeException;use Throwable;/**@@ -83,7 +85,7 @@ class SchemaManager// `$this->schemaManager->createComparator()`$this->comparator = new Comparator($this->getPlatform(), $comparatorConfig);- $this->initFieldTypes();+ $this->registerTypes();$this->builder = $this->injectableFactory->createWithBinding(Builder::class,@@ -111,20 +113,30 @@ class SchemaManagerreturn $this->helper->getDbalConnection();}+ private function registerTypes(): void+ {+ foreach ($this->metadataProvider->getDbalTypeClassNameMap() as $type => $className) {+ try {+ $this->registerType($type, $className);+ } catch (DbalException $e) {+ throw new RuntimeException("Could not register DBAL type '$type'.", previous: $e);+ }+ }+ }+/**+ * @param class-string<Type> $className* @throws DbalException*/- private function initFieldTypes(): void+ private function registerType(string $type, string $className): void{- foreach ($this->metadataProvider->getDbalTypeClassNameMap() as $type => $className) {- Type::hasType($type) ?- Type::overrideType($type, $className) :- Type::addType($type, $className);+ Type::hasType($type) ?+ Type::overrideType($type, $className) :+ Type::addType($type, $className);- $this->getDbalConnection()- ->getDatabasePlatform()- ->registerDoctrineTypeMapping($type, $type);- }+ $this->getDbalConnection()+ ->getDatabasePlatform()+ ->registerDoctrineTypeMapping($type, $type);}/**diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/AddressFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/AddressFieldTest.phpindex 04b92a82d1..eb9d667fbe 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/AddressFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/AddressFieldTest.php@@ -29,32 +29,33 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\StringType;use integration\Core\NoTransaction;-use PHPUnit\Framework\Attributes\DataProvider;-#[NoTransaction]class AddressFieldTest extends Base{- static public function fieldList()+ #[NoTransaction]+ public function testColumn(): void{- return [+ $list = [['testAddressStreet', 255],['testAddressCity', 100],['testAddressState', 100],['testAddressCountry', 100],['testAddressPostalCode', 40],];- }- #[DataProvider('fieldList')]- public function testColumn($fieldName, $length)- {- $column = $this->getColumnInfo('Test', $fieldName);+ foreach ($list as [$attribute, $length]) {+ $column = $this->getColumn('Test', $attribute);++ $this->assertNotNull($column);+ $this->assertInstanceOf(StringType::class, $column->getType());+ $this->assertEquals($length, $column->getLength());+ $this->assertFalse($column->getNotnull());- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals($length, $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/ArrayFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/ArrayFieldTest.phpindex 23267e3133..346f446a27 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/ArrayFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/ArrayFieldTest.php@@ -29,18 +29,22 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Espo\Core\Utils\Database\Dbal\Types\MediumtextType;use integration\Core\NoTransaction;-#[NoTransaction]class ArrayFieldTest extends Base{- public function testColumn()+ #[NoTransaction]+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testArray');+ $column = $this->getColumn('Test', 'testArray');- $this->assertNotEmpty($column);- $this->assertEquals('mediumtext', $column['COLUMN_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->assertNotNull($column);+ $this->assertFalse($column->getNotnull());++ if ($this->getPlatform() === 'Mysql') {+ $this->assertInstanceOf(MediumtextType::class, $column->getType());+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/AutoIncrementFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/AutoIncrementFieldTest.phpindex 0838017bed..cd5a3e99b5 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/AutoIncrementFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/AutoIncrementFieldTest.php@@ -29,92 +29,95 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\IntegerType;use integration\Core\NoTransaction;#[NoTransaction]class AutoIncrementFieldTest extends Base{- public function testColumn()+ public function testColumnNumber(): void{- $column = $this->getColumnInfo('Case', 'number');-- $this->assertNotEmpty($column);- $this->assertEquals('int', $column['DATA_TYPE']);- $this->assertEquals('NO', $column['IS_NULLABLE']);- $this->assertEquals('10', $column['NUMERIC_PRECISION']);- $this->assertEquals('auto_increment', $column['EXTRA']);- $this->assertEquals('UNI', $column['COLUMN_KEY']);+ $column = $this->getColumn('Case', 'number');++ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertTrue($column->getNotnull());+ $this->assertTrue($column->getAutoincrement());++ $this->runColumnOnExistingTable();+ $this->runDeleteColumnOnExistingTable();+ $this->testDeleteCreateColumnOnExistingTable();}- public function testColumnOnExistingTable(): void+ private function runColumnOnExistingTable(): void{$this->updateDefs('Test', 'testAutoIncrement', ['type' => 'autoincrement',]);- $column = $this->getColumnInfo('Test', 'testAutoIncrement');+ $column = $this->getColumn('Test', 'testAutoIncrement');- $this->assertNotEmpty($column);- $this->assertEquals('int', $column['DATA_TYPE']);- $this->assertEquals('NO', $column['IS_NULLABLE']);- $this->assertEquals('10', $column['NUMERIC_PRECISION']);- $this->assertEquals('auto_increment', $column['EXTRA']);- $this->assertEquals('UNI', $column['COLUMN_KEY']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertTrue($column->getNotnull());+ $this->assertTrue($column->getAutoincrement());}- public function testDeleteColumnOnExistingTable(): void+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ private function runDeleteColumnOnExistingTable(): void{- // 1. Create "testAutoIncrement" field- $this->testColumnOnExistingTable();+ // Create "testAutoIncrement" field.+ $this->runColumnOnExistingTable();- // 2. Delete "testAutoIncrement" field+ // Delete "testAutoIncrement" field.$this->getMetadata()->delete('entityDefs', 'Test', ['fields.testAutoIncrement']);$this->getMetadata()->save();- // Issue that it requires rebuilding between removing and adding a new indexes.+ // Issue that it requires rebuilding between removing and adding new indexes.$this->getDataManager()->rebuild();- $column = $this->getColumnInfo('Test', 'testAutoIncrement');+ $column = $this->getColumn('Test', 'testAutoIncrement');- $this->assertNotEmpty($column);- $this->assertEquals('int', $column['DATA_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('10', $column['NUMERIC_PRECISION']);- $this->assertEmpty($column['EXTRA']);- $this->assertEmpty($column['COLUMN_KEY']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertFalse($column->getNotnull());+ $this->assertFalse($column->getAutoincrement());}- public function testDeleteCreateColumnOnExistingTable(): void+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ private function testDeleteCreateColumnOnExistingTable(): void{- // 1. Create "testAutoIncrement" field- $this->testColumnOnExistingTable();+ // Create "testAutoIncrement" field.+ $this->runColumnOnExistingTable();- // 2. Delete "testAutoIncrement" field+ // Delete "testAutoIncrement" field.$metadata = $this->getMetadata();$metadata->delete('entityDefs', 'Test', ['fields.testAutoIncrement']);$metadata->save();$this->getDataManager()->rebuild();- // 3. Create "testAutoIncrement2" field+ // Create "testAutoIncrement2" field.$this->updateDefs('Test', 'testAutoIncrement2', ['type' => 'autoincrement',]);- $column = $this->getColumnInfo('Test', 'testAutoIncrement');- $this->assertNotEmpty($column);- $this->assertEquals('int', $column['DATA_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('10', $column['NUMERIC_PRECISION']);- $this->assertEmpty($column['EXTRA']);- $this->assertEmpty($column['COLUMN_KEY']);-- $column2 = $this->getColumnInfo('Test', 'testAutoIncrement2');- $this->assertNotEmpty($column2);- $this->assertEquals('int', $column2['DATA_TYPE']);- $this->assertEquals('NO', $column2['IS_NULLABLE']);- $this->assertEquals('10', $column2['NUMERIC_PRECISION']);- $this->assertEquals('auto_increment', $column2['EXTRA']);- $this->assertEquals('UNI', $column2['COLUMN_KEY']);+ $column = $this->getColumn('Test', 'testAutoIncrement');++ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertFalse($column->getNotnull());+ $this->assertFalse($column->getAutoincrement());++ $column = $this->getColumn('Test', 'testAutoIncrement2');++ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertTrue($column->getNotnull());+ $this->assertTrue($column->getAutoincrement());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/Base.php b/tests/integration/Espo/Core/Utils/Database/Fields/Base.phpindex 2b0002e3ff..a657ad78f2 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/Base.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/Base.php@@ -29,9 +29,10 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;-use Espo\Core\Utils\Config;+use Doctrine\DBAL\Schema\Column;+use Espo\Core\ORM\DatabaseParamsFactory;use Espo\Core\Utils\Database\Helper;-use Espo\Core\Utils\Metadata;+use Espo\Core\Utils\Database\Schema\SchemaManager;use Espo\Core\Utils\Util;use tests\integration\Core\BaseTestCase;@@ -43,34 +44,61 @@ abstract class Base extends BaseTestCaseprotected function beforeSetUp(): void{}- protected function getColumnInfo($entityName, $fieldName)+ protected function getPlatform(): ?string{- $pdo = $this->getInjectableFactory()- ->create(Helper::class)- ->getPDO();+ $params = $this->getInjectableFactory()->create(DatabaseParamsFactory::class)+ ->create();++ return $params->getPlatform();+ }- $dbName = $this->getContainer()- ->getByClass(Config::class)- ->get('database.dbname');+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ protected function getColumn(string $entityType, string $attribute): ?Column+ {+ $table = Util::camelCaseToUnderscore($entityType);+ $column = Util::camelCaseToUnderscore($attribute);- $query = "- SELECT * FROM information_schema.columns- WHERE table_name = '" . Util::toUnderScore($entityName) . "'- AND column_name = '" . Util::toUnderScore($fieldName) . "'- AND table_schema = '" . $dbName . "'- ";+ // Custom types are registered.+ $manager = $this->getInjectableFactory()->create(SchemaManager::class);- $sth = $pdo->prepare($query);- $sth->execute();+ $helper = $manager->getDatabaseHelper();- return $sth->fetch(\PDO::FETCH_ASSOC);+ $tables = $helper->getDbalConnection()+ ->createSchemaManager()+ ->introspectTables();++ foreach ($tables as $tableObject) {+ if ($tableObject->getObjectName()->getUnqualifiedName()->getValue() !== $table) {+ continue;+ }++ foreach ($tableObject->getColumns() as $columnObject) {+ if ($columnObject->getObjectName()->getIdentifier()->getValue() !== $column) {+ continue;+ }++ return $columnObject;+ }+ }++ return null;}- protected function updateDefs($entityName, $fieldName, array $fieldDefs = [], ?array $linkDefs = null)- {- $metadata = $this->getContainer()->getByClass(Metadata::class);+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ protected function updateDefs(+ string $entityType,+ string $attribute,+ array $fieldDefs = [],+ ?array $linkDefs = null,+ ): void {++ $metadata = $this->getMetadata();- $entityDefs = $metadata->get(['entityDefs', $entityName]);+ $entityDefs = $metadata->get(['entityDefs', $entityType]);if (empty($entityDefs)) {return;@@ -79,14 +107,14 @@ abstract class Base extends BaseTestCase$save = false;if (!empty($fieldDefs)) {- $currentFieldDefs = $entityDefs['fields'][$fieldName] ?? [];- $entityDefs['fields'][$fieldName] = array_merge($currentFieldDefs, $fieldDefs);+ $currentFieldDefs = $entityDefs['fields'][$attribute] ?? [];+ $entityDefs['fields'][$attribute] = array_merge($currentFieldDefs, $fieldDefs);$save = true;}if (!empty($linkDefs)) {- $currentLinkDefs = $entityDefs['links'][$fieldName] ?? [];- $entityDefs['links'][$fieldName] = array_merge($currentLinkDefs, $linkDefs);+ $currentLinkDefs = $entityDefs['links'][$attribute] ?? [];+ $entityDefs['links'][$attribute] = array_merge($currentLinkDefs, $linkDefs);$save = true;}@@ -94,7 +122,7 @@ abstract class Base extends BaseTestCase$metadata->set('entityDefs', 'Test', $entityDefs);$metadata->save();- $this->getDataManager()->rebuild([$entityName]);+ $this->getDataManager()->rebuild([$entityType]);}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/BooleanFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/BooleanFieldTest.phpindex ae96c5b3cc..8dd2f5ec75 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/BooleanFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/BooleanFieldTest.php@@ -29,32 +29,44 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\BooleanType;use integration\Core\NoTransaction;#[NoTransaction]class BooleanFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testBoolean');+ $column = $this->getColumn('Test', 'testBoolean');- $this->assertNotEmpty($column);- $this->assertEquals('tinyint', $column['DATA_TYPE']);- $this->assertEquals('0', $column['COLUMN_DEFAULT']);- $this->assertEquals('NO', $column['IS_NULLABLE']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(BooleanType::class, $column->getType());+ $this->assertTrue($column->getNotnull());++ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals(0, $column->getDefault());+ } else {+ $this->assertFalse($column->getDefault());+ }++ $this->runDefaultValue();}- public function testDefaultValue()+ public function runDefaultValue(): void{$this->updateDefs('Test', 'testBoolean', ['default' => true,]);- $column = $this->getColumnInfo('Test', 'testBoolean');+ $column = $this->getColumn('Test', 'testBoolean');++ $this->assertInstanceOf(BooleanType::class, $column->getType());+ $this->assertTrue($column->getNotnull());- $this->assertNotEmpty($column);- $this->assertEquals('tinyint', $column['DATA_TYPE']);- $this->assertEquals('1', $column['COLUMN_DEFAULT']);- $this->assertEquals('NO', $column['IS_NULLABLE']);+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals(1, $column->getDefault());+ } else {+ $this->assertTrue($column->getDefault());+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/CurrencyFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/CurrencyFieldTest.phpindex b29d703d7a..982b3c6cf0 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/CurrencyFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/CurrencyFieldTest.php@@ -29,29 +29,32 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\FloatType;+use Doctrine\DBAL\Types\StringType;use integration\Core\NoTransaction;-use PHPUnit\Framework\Attributes\DataProvider;#[NoTransaction]class CurrencyFieldTest extends Base{- static public function fieldList(): array+ public function testColumns(): void{- return [- ['testCurrency', 'double', null, null],- ['testCurrencyCurrency', 'varchar', 3, 'utf8mb4_unicode_ci'],+ $list = [+ ['testCurrency', FloatType::class, null, null],+ ['testCurrencyCurrency', StringType::class, 3, 'utf8mb4_unicode_ci'],];- }- #[DataProvider('fieldList')]- public function testColumns($fieldName, $type, $length, $collation): void- {- $column = $this->getColumnInfo('Test', $fieldName);+ foreach ($list as [$attribute, $typeClass, $length, $collation]) {+ $column = $this->getColumn('Test', $attribute);++ $this->assertNotNull($column);+ $this->assertFalse($column->getNotnull());+ $this->assertEquals($length, $column->getLength());++ $this->assertInstanceOf($typeClass, $column->getType());- $this->assertNotEmpty($column);- $this->assertEquals($type, $column['DATA_TYPE']);- $this->assertEquals($length, $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals($collation, $column['COLLATION_NAME']);+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals($collation, $column->getCollation());+ }+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/DateFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/DateFieldTest.phpindex e860ab5d33..9592c42240 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/DateFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/DateFieldTest.php@@ -29,17 +29,18 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\DateType;use integration\Core\NoTransaction;#[NoTransaction]class DateFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testDate');+ $column = $this->getColumn('Test', 'testDate');- $this->assertNotEmpty($column);- $this->assertEquals('date', $column['DATA_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(DateType::class, $column->getType());+ $this->assertFalse($column->getNotnull());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/DateTimeFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/DateTimeFieldTest.phpindex 321d5242e0..7f1a907570 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/DateTimeFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/DateTimeFieldTest.php@@ -29,17 +29,18 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\DateTimeType;use integration\Core\NoTransaction;#[NoTransaction]class DateTimeFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testDateTime');+ $column = $this->getColumn('Test', 'testDateTime');- $this->assertNotEmpty($column);- $this->assertEquals('datetime', $column['DATA_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(DateTimeType::class, $column->getType());+ $this->assertFalse($column->getNotnull());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/FileFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/FileFieldTest.phpindex 897da64eae..572c9414e7 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/FileFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/FileFieldTest.php@@ -29,19 +29,23 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\StringType;use integration\Core\NoTransaction;#[NoTransaction]class FileFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testFileId');+ $column = $this->getColumn('Test', 'testFileId');- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals(17, $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(StringType::class, $column->getType());+ $this->assertFalse($column->getNotnull());+ $this->assertEquals(17, $column->getLength());++ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/FloatFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/FloatFieldTest.phpindex 9b388b3cc1..a9586a1f9c 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/FloatFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/FloatFieldTest.php@@ -29,6 +29,7 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\FloatType;use integration\Core\NoTransaction;#[NoTransaction]@@ -36,10 +37,10 @@ class FloatFieldTest extends Base{public function testColumn(){- $column = $this->getColumnInfo('Test', 'testFloat');+ $column = $this->getColumn('Test', 'testFloat');- $this->assertNotEmpty($column);- $this->assertEquals('double', $column['DATA_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(FloatType::class, $column->getType());+ $this->assertFalse($column->getNotnull());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/IntegerFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/IntegerFieldTest.phpindex e5428e5c7b..6d7caacae0 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/IntegerFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/IntegerFieldTest.php@@ -29,6 +29,7 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Doctrine\DBAL\Types\IntegerType;use integration\Core\NoTransaction;#[NoTransaction]@@ -36,11 +37,11 @@ class IntegerFieldTest extends Base{public function testColumn(){- $column = $this->getColumnInfo('Test', 'testInteger');+ $column = $this->getColumn('Test', 'testInteger');- $this->assertNotEmpty($column);- $this->assertEquals('int', $column['DATA_TYPE']);- $this->assertEquals(10, $column['NUMERIC_PRECISION']);- $this->assertEquals('YES', $column['IS_NULLABLE']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(IntegerType::class, $column->getType());+ $this->assertFalse($column->getNotnull());+ $this->assertNull($column->getPrecision());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.phpindex ddca6353dd..463b1474c8 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/TextFieldTest.php@@ -29,55 +29,67 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;+use Espo\Core\Utils\Database\Dbal\Types\LongtextType;+use Espo\Core\Utils\Database\Dbal\Types\MediumtextType;use integration\Core\NoTransaction;#[NoTransaction]class TextFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testText');+ $column = $this->getColumn('Test', 'testText');- $this->assertNotEmpty($column);- $this->assertEquals('mediumtext', $column['COLUMN_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->assertNotNull($column);+ $this->assertInstanceOf(MediumtextType::class, $column->getType());+ $this->assertFalse($column->getNotnull());++ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }++ $this->runIncreaseColumnLength();+ $this->runReduceColumnLength();}- public function testIncreaseColumnLength()+ private function runIncreaseColumnLength(): void{$this->updateDefs('Test', 'testText', ['type' => 'text','dbType' => 'longtext',]);- $column = $this->getColumnInfo('Test', 'testText');+ $column = $this->getColumn('Test', 'testText');++ $this->assertNotNull($column);+ $this->assertInstanceOf(LongtextType::class, $column->getType());+ $this->assertFalse($column->getNotnull());- $this->assertNotEmpty($column);- $this->assertEquals('longtext', $column['COLUMN_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }}- public function testReduceColumnLength()+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ private function runReduceColumnLength(): void{- $this->updateDefs('Test', 'testText', [- 'type' => 'text',- 'dbType' => 'longtext',- ]);-- $this->getContainer()->get('metadata')->delete('entityDefs', 'Test', [+ $this->getMetadata()->delete('entityDefs', 'Test', ['fields.testText.dbType',]);- $this->getContainer()->get('metadata')->save();+ $this->getMetadata()->save();$this->getDataManager()->rebuildDatabase();- $column = $this->getColumnInfo('Test', 'testText');+ $column = $this->getColumn('Test', 'testText');++ $this->assertNotNull($column);+ $this->assertInstanceOf(LongtextType::class, $column->getType());+ $this->assertFalse($column->getNotnull());- $this->assertNotEmpty($column);- $this->assertEquals('longtext', $column['COLUMN_TYPE']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/VarcharFieldTest.php b/tests/integration/Espo/Core/Utils/Database/Fields/VarcharFieldTest.phpindex f67c8807bb..538d4db247 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/VarcharFieldTest.php+++ b/tests/integration/Espo/Core/Utils/Database/Fields/VarcharFieldTest.php@@ -29,201 +29,64 @@namespace tests\integration\Espo\Core\Utils\Database\Fields;-use Espo\Core\Utils\Database\Helper as DatabaseHelper;+use Doctrine\DBAL\Types\StringType;use integration\Core\NoTransaction;#[NoTransaction]class VarcharFieldTest extends Base{- public function testColumn()+ public function testColumn(): void{- $column = $this->getColumnInfo('Test', 'testVarchar');+ $column = $this->getColumn('Test', 'testVarchar');- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals(100, $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);- }+ $this->assertInstanceOf(StringType::class, $column?->getType());+ $this->assertEquals(100, $column->getLength());+ $this->assertFalse($column->getNotnull());- public function testIncreaseColumnLength()- {- $this->updateDefs('Test', 'testVarchar', [- 'maxLength' => 150,- ]);-- $column = $this->getColumnInfo('Test', 'testVarchar');+ if ($this->getPlatform() === 'Mysql') {+ $this->assertEquals('utf8mb4_unicode_ci', $column->getCollation());+ }- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('150', $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->runIncreaseColumnLength();+ $this->runReduceColumnLength1();+ $this->runReduceColumnLengthAndDefault();}- public function testReduceColumnLength()+ private function runIncreaseColumnLength(): void{$this->updateDefs('Test', 'testVarchar', [- 'maxLength' => 50,+ 'maxLength' => 150,]);- $column = $this->getColumnInfo('Test', 'testVarchar');+ $column = $this->getColumn('Test', 'testVarchar');- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('100', $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->assertNotNull($column);+ $this->assertEquals(150, $column->getLength());}- public function testReduceColumnLength2()+ private function runReduceColumnLength1(): void{$this->updateDefs('Test', 'testVarchar', ['maxLength' => 50,- 'default' => 'test-default',]);- $column = $this->getColumnInfo('Test', 'testVarchar');+ $column = $this->getColumn('Test', 'testVarchar');- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('100', $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);+ $this->assertInstanceOf(StringType::class, $column?->getType());+ $this->assertEquals(150, $column->getLength());}- public function testReduceColumnLength3()+ private function runReduceColumnLengthAndDefault(): void{- $this->executeQuery(- "ALTER TABLE test MODIFY COLUMN test_varchar VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL;"- );-$this->updateDefs('Test', 'testVarchar', ['maxLength' => 50,'default' => 'test-default',]);- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertNotEmpty($column);- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('100', $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('YES', $column['IS_NULLABLE']);-- $this->assertContains($column['COLLATION_NAME'], [- 'utf8_unicode_ci',- 'utf8mb3_unicode_ci'- ]);- }-- public function testCollationForExistingColumn()- {- $column = $this->getColumnInfo('Test', 'testVarchar');- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);-- $this->executeQuery(- "ALTER TABLE test MODIFY COLUMN test_varchar VARCHAR(". $column['CHARACTER_MAXIMUM_LENGTH'] .") CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL;"- );-- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertContains($column['COLLATION_NAME'], [- 'utf8_unicode_ci',- 'utf8mb3_unicode_ci'- ]);-- $this->updateDefs('Test', 'testVarchar', [- 'maxLength' => 150,- ]);-- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('150', $column['CHARACTER_MAXIMUM_LENGTH']);-- $this->assertContains($column['COLLATION_NAME'], [- 'utf8_unicode_ci',- 'utf8mb3_unicode_ci'- ]);- }-- public function testCollationForExistingColumn2()- {- $column = $this->getColumnInfo('Test', 'testVarchar');- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);-- $this->executeQuery(- "ALTER TABLE test MODIFY COLUMN test_varchar VARCHAR(". $column['CHARACTER_MAXIMUM_LENGTH'] .") CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL;"- );-- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertContains($column['COLLATION_NAME'], [- 'utf8_unicode_ci',- 'utf8mb3_unicode_ci'- ]);-- $this->updateDefs('Test', 'testVarchar', [- 'default' => 'test-default',- ]);-- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertEquals('varchar', $column['DATA_TYPE']);-- $this->assertContains($column['COLLATION_NAME'], [- 'utf8_unicode_ci',- 'utf8mb3_unicode_ci'- ]);- }-- public function testCollationForNewColumn()- {- $this->updateDefs('Test', 'newTestVarchar', [- 'type' => 'varchar',- ]);-- $column = $this->getColumnInfo('Test', 'newTestVarchar');-- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);- }-- public function testDefaultValue()- {- $this->updateDefs('Test', 'testVarchar', [- 'default' => 'test-default',- ]);-- $column = $this->getColumnInfo('Test', 'testVarchar');-- $this->assertEquals('varchar', $column['DATA_TYPE']);- $this->assertEquals('100', $column['CHARACTER_MAXIMUM_LENGTH']);- $this->assertEquals('utf8mb4_unicode_ci', $column['COLLATION_NAME']);-- $dbHelper = $this->getInjectableFactory()->create(DatabaseHelper::class);-- if (- $dbHelper->getType() == 'MariaDB'- && version_compare($dbHelper->getVersion(), '10.2.7', '>=')- ) {- $this->assertEquals("'test-default'", $column['COLUMN_DEFAULT']);- } else {- $this->assertEquals('test-default', $column['COLUMN_DEFAULT']);- }- }-- /**- * Make sure columns not removed.- */- public function testRemoveField(): void- {- $this->getMetadata()->delete('entityDefs', 'Test', ['fields.testVarchar']);- $this->getMetadata()->save();- $this->getDataManager()->rebuildDatabase();-- $column = $this->getColumnInfo('Test', 'testVarchar');+ $column = $this->getColumn('Test', 'testVarchar');- $this->assertTrue((bool) $column);+ $this->assertInstanceOf(StringType::class, $column?->getType());+ $this->assertEquals(150, $column->getLength());+ $this->assertEquals('test-default', $column->getDefault());}}diff --git a/tests/integration/Espo/Core/Utils/Database/Fields/HelperTest.php b/tests/integration/Espo/Core/Utils/Database/HelperTest.phpsimilarity index 58%rename from tests/integration/Espo/Core/Utils/Database/Fields/HelperTest.phprename to tests/integration/Espo/Core/Utils/Database/HelperTest.phpindex 3a57efcf0e..a6f550be90 100644--- a/tests/integration/Espo/Core/Utils/Database/Fields/HelperTest.php+++ b/tests/integration/Espo/Core/Utils/Database/HelperTest.php@@ -27,8 +27,9 @@* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.************************************************************************/-namespace tests\integration\Espo\Core\Utils\Database\Fields;+namespace tests\integration\Espo\Core\Utils\Database;+use Espo\Core\ORM\DatabaseParamsFactory;use Espo\Core\Utils\Database\Helper;use Doctrine\DBAL\Connection;use PDO;@@ -36,82 +37,61 @@ use tests\integration\Core\BaseTestCase;class HelperTest extends BaseTestCase{- /** @var ?Helper */- protected $helper;+ protected ?Helper$helper = null;protected function initTest(){$this->helper = $this->getInjectableFactory()->create(Helper::class);}- private function getDatabaseInfo()- {- $pdo = $this->getEntityManager()->getPDO();-- $sth = $pdo->prepare("select version()");- $sth->execute();-- $version = $sth->fetchColumn();-- $type = 'mysql';- if (preg_match('/mariadb/i', $version)) {- $type = 'mariadb';- }-- if (preg_match('/[0-9]+\.[0-9]+\.[0-9]+/', $version, $match)) {- $version = $match[0];- }-- return [- 'type' => $type,- 'version' => $version,- ];- }-- public function testGetDbalConnectionWithConfig()+ public function testGetDbalConnectionWithConfig(): void{$this->initTest();$this->assertInstanceOf(Connection::class, $this->helper->getDbalConnection());}- public function testGetPdoConnectionWithConfig()+ public function testGetPdoConnectionWithConfig(): void{$this->initTest();$this->assertInstanceOf(PDO::class, $this->helper->getPDO());}- public function testGetDatabaseInfo()+ public function testGetDatabaseInfo(): void{$this->initTest();- $databaseInfo = $this->getDatabaseInfo();- if (empty($databaseInfo)) {- return;- }+ $this->helper->getVersion();- $this->assertEquals($databaseInfo['type'], strtolower($this->helper->getType()));- $this->assertEquals($databaseInfo['version'], $this->helper->getVersion());+ $this->assertTrue(true);}- public function testGetDatabaseType()+ public function testGetDatabaseType(): void{$this->initTest();- $databaseInfo = $this->getDatabaseInfo();- if (empty($databaseInfo)) {- return;- }+ switch ($this->getPlatform()) {+ case 'Mysql':+ $this->assertContains($this->helper->getType(), [+ 'MySQL',+ 'MariaDB',+ ]);- switch ($databaseInfo['type']) {- case 'mysql':- $this->assertEquals('MySQL', $this->helper->getType());break;- case 'mariadb':- $this->assertEquals('MariaDB', $this->helper->getType());+ case 'Postgresql':+ $this->assertEquals('PostgreSQL', $this->helper->getType());+break;}}++ private function getPlatform(): ?string+ {+ $params = $this->getInjectableFactory()->create(DatabaseParamsFactory::class)+ ->create();++ return $params->getPlatform();+ }}