Skip to content

Commit

Permalink
Merge pull request #40 from cfebs/cl-fix-text-nomax
Browse files Browse the repository at this point in the history
fix text and char column php code args
  • Loading branch information
cfebs committed Feb 9, 2023
2 parents 21a606f + d23bf7b commit 1b71076
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/Schema/Column/CharacterColumn.php
Expand Up @@ -67,11 +67,13 @@ public function getPhpCode() : string
}
}

return '(new \\' . static::class . '('
. $this->max_string_length
. ($this->character_set !== null ? ', \'' . $this->character_set . '\'' : '')
. ($this->collation !== null ? ', \'' . $this->collation . '\'' : '')
. '))'
$args = [
$this->max_string_length,
$this->character_set === null ? 'null' : "'{$this->character_set}'",
$this->collation === null ? 'null' : "'{$this->collation}'",
];

return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
. $default
. $this->getNullablePhp();
}
Expand Down
13 changes: 7 additions & 6 deletions src/Schema/Column/TextTrait.php
Expand Up @@ -8,12 +8,13 @@ trait TextTrait
public function getPhpCode() : string
{
$default = $this->getDefault() !== null ? '\'' . $this->getDefault() . '\'' : 'null';

return '(new \\' . static::class . '('
. ($this->character_set !== null && $this->collation !== null
? ', \'' . $this->character_set . '\'' . ', \'' . $this->collation . '\''
: '')
. '))'

$args = [
$this->character_set === null ? 'null' : "'{$this->character_set}'",
$this->collation === null ? 'null' : "'{$this->collation}'",
];

return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
. ($this->hasDefault() ? '->setDefault(' . $default . ')' : '')
. $this->getNullablePhp();
}
Expand Down
10 changes: 10 additions & 0 deletions tests/CreateTableParseTest.php
Expand Up @@ -34,7 +34,17 @@ public function testSimpleParse()

// specific parsing checks
$this->assertInstanceOf(TableDefinition::class, $table_defs['tweets']);
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['title']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['title']->getCollation());
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['text']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['text']->getCollation());

$this->assertInstanceOf(TableDefinition::class, $table_defs['texts']);
$this->assertEquals('utf8mb4', $table_defs['texts']->columns['title_char_col']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_char_col']->getCollation());
$this->assertNull($table_defs['texts']->columns['title_col']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_col']->getCollation());
$this->assertNull($table_defs['texts']->columns['title']->getCharacterSet());
$this->assertNull($table_defs['texts']->columns['title']->getCollation());
}
}
18 changes: 16 additions & 2 deletions tests/fixtures/create_table.sql
Expand Up @@ -60,11 +60,25 @@ CREATE TABLE `orders`
`created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `tweets` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`text` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`title` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `texts` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title_char_col` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`title_col` varchar(256) COLLATE utf8mb4_unicode_ci,
`title` varchar(256)
`text_char_col` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`text_col` text COLLATE utf8mb4_unicode_ci,
`text` text,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

0 comments on commit 1b71076

Please sign in to comment.