-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SQL Enum support #179
Add SQL Enum support #179
Conversation
I have made some changes to .gitignore, to ignore .idea files (I assume I cant be the only one working in PHPStorm) as its standard used by both Symfony and Doctrine. I have also included phpunit and psalm dependencies as dev, because well we have lot of @psalm annotations, and then because we actually do have tests for this bundle, so it makes no sense not to have phpunit included. Speaking of tests, I would appreciate some help on writing them, as I am still a bit lost as to how this bundle is structured. We can start by writing down list of tests that need to be implemented and move on from there:
Possible considerations/discussion:
|
You're not the only one 😉 But the good practice actually is to register these specific files in a global
Please revert these changes, since these are not necessary. We already have PHPUnit installed through the Symfony phpunit-bridge. Use About Psalm, the annotations were added for using psalm/phpstan on userland code, we do not have any CI task enforcing it yet ; it should be added in another PR along with proper config & CI. About tests, some existing resources:
The integration tests are using a Symfony app to configure some enum using the bundle and the ORM config: Then, an integration test case class using the Symfony Kernel make some assertions by consuming the app: There are also unit tests for this and the following points for:
See this integration test and unit test
See this integration test and unit test
Such tests are made with unit tests:
this should be tested in both unit tests case mentioned below (config & extension).
I think sql enum should preferably be stored as string. So let's always use About
ODM is already supported since #176. There is no change to made in tis PR regarding a "native" enum type in MongoDB.
I'd go with
That's a bit out-of-scope, so I'd say please avoid doing this in this PR 😄 Anyway, keep in mind not every const values make sense as enums. Regarding the TypesDumper ones, it doesn't bring much value, mostly because it's internal as well as using enums instead might complexify a bit the Symfony DI config handling which does not support backed enums out-of-the-box. |
Good to know about that global .gitignore, I learnt something new. I have removed the newly added dependencies, except for doctrine-odm-bundle. This is vital for phpunit tests of ODM. If I dont add it, I always get errors when running tests. Psalm and phpunit gone. I have removed int ENUMs, so they are always strings. Did not yet rename SINGLE to SCALAR, I will do so once we agree on it globally. Or does your ring rule them all @ogizanagi ? :) Will have a look at the remaining points and mainly tests later. |
That's unexpected : the test cases should be skipped when the bundle is not installed 🤔 : PhpEnums/tests/Integration/Bridge/Doctrine/ODM/Type/EnumTypeTest.php Lines 28 to 30 in 49a73e0
Let's go with |
Ah, I didnt realize its test skip not an error. So this is desired? I think that if we have tests for that part of bridge, then why not require it as dev? Anyways removing since I am not touching ODM so it should not be part of this PR |
The reasoning here simply is testing ODM requires an advanced setup on your machine (php mongodb extension, a mongodb instance running, …), so we won't impose it be default and simply skip the related test cases when the ODM bundle is not installed (it's installed on CI in the github actions, so any PR is fully tested). |
I came across one "issue" or better said property of current I tried this config: elao_enum:
doctrine:
types:
App\Enum\Suit:
default: !php/const App\Enum\Suit::Spades
suit_sql_enum:
class: App\Enum\Suit
type: enum So I wanted to have an alias for the same Enum. This resulted in this code by <?php
namespace ELAO_ENUM_DT_DBAL\App\Enum {
if (!\class_exists(SuitType::class)) {
class SuitType extends \Elao\Enum\Bridge\Doctrine\DBAL\Types\AbstractEnumType
{
protected function getEnumClass(): string
{
return \App\Enum\Suit::class;
}
protected function onNullFromDatabase(): ?\BackedEnum
{
return \App\Enum\Suit::from('S');
}
protected function onNullFromPhp(): int|string|null
{
return 'S';
}
}
}
if (!\class_exists(SuitType::class)) {
class SuitType extends \Elao\Enum\Bridge\Doctrine\DBAL\Types\AbstractEnumSQLDeclarationType
{
protected function getEnumClass(): string
{
return \App\Enum\Suit::class;
}
public function getName(): string
{
return 'suit_sql_enum';
}
}
}
} So the issue is clear. The name for the types is based on the Enum class name. I think it should be based instead on the type name as specified in config and slugged. Opinions? |
You're right indeed, using the PascalCased type name might be better for such edge-cases. |
@ogizanagi Running into issues with tests with SQLite... It has no ENUM type. What do you suggest? I have checked the original test in 1.x and there seems to be no testing of actual SQLEnum type functionality |
There are further issues with this. Because the test requires use of SchemaManager to create the DB, I am getting the following errors when making the tests:
I think this is the reason why we didnt have tests in 1.x. I am not sure what we can do fix this, any ideas? |
I have pushed the changes so you can have a look too. But I dont think I can resolve this. The original version also did not have tests and I believe the main issue is due to SQLite lack of support. However even if we use locally MySQL, I have the problems with SchemaManager trying to map enum type, which is not supported by the platform officially. I do not have this issue when using migrations / working with ORM only, so I assume it has to do with the SchemaManager itself. But please, a review is needed. In the meantime I have made the SINGLE -> SCALAR change. I have also changed how the TypesDumper creates names - if no custom name is specified then nothing changes. But if custom name is used in config, then its pascal-cased and used instead of the base name of the enum The tests I included in this PR are marked as skipped for the above mentioned issues |
@ogizanagi Can we merge this? |
@ogizanagi fixed lint errors, please re-run tests |
src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
Outdated
Show resolved
Hide resolved
src/Bridge/Doctrine/DBAL/Types/AbstractEnumSQLDeclarationType.php
Outdated
Show resolved
Hide resolved
src/Bridge/Doctrine/DBAL/Types/AbstractEnumSQLDeclarationType.php
Outdated
Show resolved
Hide resolved
tests/Unit/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php
Show resolved
Hide resolved
tests/Unit/Bridge/Symfony/Bundle/DependencyInjection/ElaoEnumExtensionTest.php
Show resolved
Hide resolved
…ng XML and YAML declaration
@ogizanagi I went over your comments and resolved most things. I also added some more tests which led me to find some bugs. All fixed. I have created a MySQL test class. It can be enabled by changing I still dont get your feedback on Please review once again and let me know if the way I enable MySQL tests is ok, or you wish to use some different approach. Right now I am checking |
I have a failing test on Windows due to DBAL URL I moved into <env name="DOCTRINE_DBAL_URL" value="sqlite:////%kernel.cache_dir%/db.sqlite" /> Anybody has windows who can test and tell me what it should be to work there? Thanks! |
@ogizanagi I tried to fix it, can you re-run tests please? |
That didnt work well. lets see if this version works, seems I had an extra |
All green now ✅ |
I think it would be a good idea to set up MySQL (or MariaDB) workflow test at least for Symfony 5.4 with PHP 8.1 on Linux |
That's part of the plan indeed :) |
I finally understand the I also thought it might be nice to have an actual Symfony Form type of |
Both of these features are listed in #124 indeed. |
This PR was merged into the 2.x-dev branch. Discussion ---------- Add SQL Enum support Had some mess with branches (was working on main one, not on fork branch), so after renaming it got deleted in the old PR #179 This is the same content, just different branch name is used so that I can work on other stuff before this is merged. Sorry for the mix-up, didnt know renaming branch would close existing PR, I expected itd just be renamed there too Commits ------- 8b58253 [Dev] Rework Makefile a11eda8 [Tests] Fix skipped tests in PHPUnit beforeClass hooks f35bebc [CI] Add MySQL for integration tests 371db9f Add SQL Enum support
Well given that I need lot of code from this PR and you plan to modify it
yet, I think Ill wait for the final merge
|
@michnovka : What do you mean? #182 was merged |
Backport the feature originally present in 1.x for native ENUM