From 7daa3fdb0807a6360bd320eebecfc542a735bf8a Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 20 Jul 2023 16:06:01 +1200 Subject: [PATCH 1/2] FIX Short-array syntax for Enum --- src/ORM/Connect/DBSchemaManager.php | 11 +++-- tests/php/ORM/DBEnumTest.php | 76 ++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/ORM/Connect/DBSchemaManager.php b/src/ORM/Connect/DBSchemaManager.php index 4a8936228..e1f77ab5f 100644 --- a/src/ORM/Connect/DBSchemaManager.php +++ b/src/ORM/Connect/DBSchemaManager.php @@ -396,13 +396,16 @@ abstract class DBSchemaManager // Create custom fields if ($fieldSchema) { foreach ($fieldSchema as $fieldName => $fieldSpec) { + $fieldSpec = $fieldSpec ??= ''; + // convert Enum short array syntax to long array syntax to make parsing $arrayValue below easier + $fieldSpec = preg_replace('/^(enum\()\[(.*?)\]/i', '$1array($2)', $fieldSpec); //Is this an array field? $arrayValue = ''; - if (strpos($fieldSpec ?? '', '[') !== false) { + $pos = strpos($fieldSpec, '['); + if ($pos !== false) { //If so, remove it and store that info separately - $pos = strpos($fieldSpec ?? '', '['); - $arrayValue = substr($fieldSpec ?? '', $pos ?? 0); - $fieldSpec = substr($fieldSpec ?? '', 0, $pos); + $arrayValue = substr($fieldSpec, $pos); + $fieldSpec = substr($fieldSpec, 0, $pos); } /** @var DBField $fieldObj */ diff --git a/tests/php/ORM/DBEnumTest.php b/tests/php/ORM/DBEnumTest.php index 7fe37403a..c0d1f71c0 100644 --- a/tests/php/ORM/DBEnumTest.php +++ b/tests/php/ORM/DBEnumTest.php @@ -16,25 +16,67 @@ class DBEnumTest extends SapphireTest protected $usesDatabase = true; - public function testDefault() + /** + * @dataProvider provideParse + * + * nullifyEmpty is an option on DBString, which DBEnum extends + * Mainly used for testing that Enum short-array style syntax works while passing in options + */ + public function testParse(?string $expectedDefault, bool $expectedNullifyEmpty, string $spec) { - /** @var DBEnum $enum1 */ - $enum1 = DBField::create_field('Enum("A, B, C, D")', null); - /** @var DBEnum $enum2 */ - $enum2 = DBField::create_field('Enum("A, B, C, D", "")', null); - /** @var DBEnum $enum3 */ - $enum3 = DBField::create_field('Enum("A, B, C, D", null)', null); - /** @var DBEnum $enum4 */ - $enum4 = DBField::create_field('Enum("A, B, C, D", 1)', null); + /** @var DBEnum $enum */ + $enum = DBField::create_field($spec, null); + $this->assertEquals($expectedDefault, $enum->getDefaultValue()); + $this->assertEquals($expectedDefault, $enum->getDefault()); + $this->assertEquals($expectedNullifyEmpty, $enum->getNullifyEmpty()); + } - $this->assertEquals('A', $enum1->getDefaultValue()); - $this->assertEquals('A', $enum1->getDefault()); - $this->assertEquals(null, $enum2->getDefaultValue()); - $this->assertEquals(null, $enum2->getDefault()); - $this->assertEquals(null, $enum3->getDefaultValue()); - $this->assertEquals(null, $enum3->getDefault()); - $this->assertEquals('B', $enum4->getDefaultValue()); - $this->assertEquals('B', $enum4->getDefault()); + public function provideParse() + { + return [ + // standard syntax - double quotes + ['A', true, 'Enum("A, B, C, D")'], + ['B', true, 'Enum("A, B, C, D", "B")'], + ['C', true, 'Enum("A, B, C, D", 2)'], + [null, true, 'Enum("A, B, C, D", "")'], + [null, true, 'Enum("A, B, C, D", null)'], + ['B', false, 'Enum("A, B, C, D", "B", ["nullifyEmpty" => false])'], + // standard syntax - single quotes + ['A', true, "Enum('A, B, C, D')"], + ['B', true, "Enum('A, B, C, D', 'B')"], + ['C', true, "Enum('A, B, C, D', 2)"], + [null, true, "Enum('A, B, C, D', '')"], + [null, true, "Enum('A, B, C, D', null)"], + ['B', false, "Enum('A, B, C, D', 'B', ['nullifyEmpty' => false])"], + // long array syntax - double quotes + ['A', true, 'Enum(array("A", "B", "C", "D"))'], + ['B', true, 'Enum(array("A", "B", "C", "D"), "B")'], + ['C', true, 'Enum(array("A", "B", "C", "D"), 2)'], + [null, true, 'Enum(array("A", "B", "C", "D"), "")'], + [null, true, 'Enum(array("A", "B", "C", "D"), null)'], + ['B', false, 'Enum(array("A", "B", "C", "D"), "B", ["nullifyEmpty" => false])'], + // long array syntax - single quotes + ['A', true, "Enum(array('A', 'B', 'C', 'D'))"], + ['B', true, "Enum(array('A', 'B', 'C', 'D'), 'B')"], + ['C', true, "Enum(array('A', 'B', 'C', 'D'), 2)"], + [null, true, "Enum(array('A', 'B', 'C', 'D'), '')"], + [null, true, "Enum(array('A', 'B', 'C', 'D'), null)"], + ['B', false, "Enum(array('A', 'B', 'C', 'D'), 'B', ['nullifyEmpty' => false])"], + // short array syntax - double quotes + ['A', true, 'Enum(["A", "B", "C", "D"])'], + ['B', true, 'Enum(["A", "B", "C", "D"], "B")'], + ['C', true, 'Enum(["A", "B", "C", "D"], 2)'], + [null, true, 'Enum(["A", "B", "C", "D"], "")'], + [null, true, 'Enum(["A", "B", "C", "D"], null)'], + ['B', false, 'Enum(["A", "B", "C", "D"], "B", ["nullifyEmpty" => false])'], + // short array syntax - single quotes + ['A', true, "Enum(['A', 'B', 'C', 'D'])"], + ['B', true, "Enum(['A', 'B', 'C', 'D'], 'B')"], + ['C', true, "Enum(['A', 'B', 'C', 'D'], 2)"], + [null, true, "Enum(['A', 'B', 'C', 'D'], '')"], + [null, true, "Enum(['A', 'B', 'C', 'D'], null)"], + ['B', false, "Enum(['A', 'B', 'C', 'D'], 'B', ['nullifyEmpty' => false])"], + ]; } public function testObsoleteValues() From a16b268ff71c27055085b69b879fca41f609287e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 20 Jul 2023 18:19:14 +1200 Subject: [PATCH 2/2] MNT Tidy up double assignment --- src/ORM/Connect/DBSchemaManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ORM/Connect/DBSchemaManager.php b/src/ORM/Connect/DBSchemaManager.php index e1f77ab5f..a4e8f6aac 100644 --- a/src/ORM/Connect/DBSchemaManager.php +++ b/src/ORM/Connect/DBSchemaManager.php @@ -396,7 +396,7 @@ abstract class DBSchemaManager // Create custom fields if ($fieldSchema) { foreach ($fieldSchema as $fieldName => $fieldSpec) { - $fieldSpec = $fieldSpec ??= ''; + $fieldSpec ??= ''; // convert Enum short array syntax to long array syntax to make parsing $arrayValue below easier $fieldSpec = preg_replace('/^(enum\()\[(.*?)\]/i', '$1array($2)', $fieldSpec); //Is this an array field?