nelmio / alice

Expressive fixtures generator
MIT License
2.5k stars 329 forks source link

randomElement #645

Closed nlemoine closed 7 years ago

nlemoine commented 7 years ago

Hi,

I'm using the latest version of Alice (master branch) and have an issue with the randomElement method.

Test\Entity:
  entity{1..20}:
    some_field: <randomElement(['some value', 'some other value'])>

I get this error:

Uncaught Nelmio\Alice\Exception\FixtureBuilder\ExpressionLanguage\LexException: Could not lex the value "['some value'". in /path/to/vendor/nelmio/alice/src/Exception/FixtureBuilder/ExpressionLanguage/LexException.php:25

Am I missing something in array syntax or is it a confirmed bug?

theofidry commented 7 years ago

No this should work, I'll look into it

zajca commented 7 years ago

Hi, I can confirm same problem with static methods returning array.

domainType: <randomElement(\MyBundle\Entity\MyEntity::getAvailableDomainTypes())>

I take a look to my older projects and with 2.2.2 this was working. Now with v3.0.0-beta.1 it's not working.

theofidry commented 7 years ago

@zajca the correct syntax for yours would be <randomElement(<(\MyBundle\Entity\MyEntity::getAvailableDomainTypes())>)>, unlike in 2.x, there is a few things that changes in 3.x due to introducing a proper lexer. I wish I could keep the BC at this level, but there is far too much edge cases :/

theofidry commented 7 years ago

Duplicate of #663.

rouflak commented 7 years ago

@theofidry I'm getting the same issue and could not find any solution in the issue mentioned above.

Could you please write a brief conclusion to let us know how to deal with arrays and the randomElement fixture?

Thank you very much

theofidry commented 7 years ago

@rouflak did you retry with the latest version? I believed I fixed that one already.

dominics commented 7 years ago

@theofidry confirmed the issue still exists in both beta4, and in 4d47f90371 (latest master at this point). (My test case is like the original issue reporter's, just an array literal, not a static call)

Seems like this isn't a duplicate of #663 as well; that provider was being passed a string, not an array.

dominics commented 7 years ago

One workaround is to use an array parameter, which doesn't make the lexer spew:

parameters:
  planets:
    - mercury
    - venus
    - earth

Type:
  name:
    planet: "<randomElement(<{planets}>)>"

I also note there's no test case for a literal array ("<randomElement(['foo', 'bar'])>") in the LexerIntegrationTest.

theofidry commented 7 years ago

Yeah that's possible :) there is tests for the array and I try to do most of the combinations, but it's a bit hard to cover all the cases :P

I'll have a look in the coming days, unless you are willing to give it a try. I expect it to be just a matter of playing with reflexes in the Lexer On Thu, 6 Apr 2017 at 05:41, Dominic Scheirlinck notifications@github.com wrote:

One workaround is to use an array parameter, which doesn't make the lexer spew:

parameters: planets:

  • mercury
  • venus
  • earth

Type: name: planet: "<randomElement(<{planets}>)>"

I also note there's no test case for a literal array ("<randomElement(['foo', 'bar'])>") in the LexerIntegrationTest.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nelmio/alice/issues/645#issuecomment-292067265, or mute the thread https://github.com/notifications/unsubscribe-auth/AE76gfrwlGHMcSolhBOKJbkyB5QfDRXdks5rtG0TgaJpZM4K6dHo .

marina-endouble commented 7 years ago

Hi @theofidry , I keep getting the error with code like this: <randomElement(array('facebook','instagram','vkontakte'))> My alice version is v3.0.0-RC.1 The error message: Type error: Argument 1 passed to Faker\Provider\Base::randomElements() must be of the type array, string given, called in /var/www/vendor/fzaninotto/faker/src/Faker/Provider/Base.php on line 205

theofidry commented 7 years ago

<randomElement(<(array('facebook','instagram','vkontakte'))>)> might work

marina-endouble commented 7 years ago

That also didn't work :( [Nelmio\Alice\Throwable\Exception\FixtureBuilder\ExpressionLanguage\MalformedFunctionException]
The value "<(array('facebook'" contains an unclosed function.

theofidry commented 7 years ago

Better go for @dominics' solution then or submit a PR to make it work :/

marina-endouble commented 7 years ago

@theofidry yep @dominics' solution work And I'll go with PR soon :)

theofidry commented 7 years ago

Thanks :) Take a peak at the contribution guide if you need some hints on where are different stuff, you can also reach me out on Slack (the channel is on the readme)

marina-endouble commented 7 years ago

Sorry I didn't find your slack channel mention :P But just one question: should I create a PR via fork? I'm asking as now it doesn't let me push my branch in alice repo directly

theofidry commented 7 years ago

It should be a link on the readme on the Slack badge.

Regarding your other question yes :) IIRC there is a github contributing guide which gives a good example. You'll need to fork the project, push your changes on it and open a pull request.

Sorry I can't provide any link from my phone :/

On Mon, 31 Jul 2017 at 16:45, marina-endouble notifications@github.com wrote:

Sorry I didn't find your slack channel mention :P But just one question: should I create a PR via fork? I'm asking as now it doesn't let me push my branch in alice repo directly

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nelmio/alice/issues/645#issuecomment-319109901, or mute the thread https://github.com/notifications/unsubscribe-auth/AE76gRpPUcxVHBJhMzU1_c0IL92T7TI2ks5sTfZ9gaJpZM4K6dHo .

marinoska commented 7 years ago

@theofidry thank you very much, I've done a PR :)