Skip to content
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

how to use current() in identity() #374

Closed
plandolt opened this issue May 24, 2016 · 8 comments
Closed

how to use current() in identity() #374

plandolt opened this issue May 24, 2016 · 8 comments
Milestone

Comments

@plandolt
Copy link

I found the documentation about identity and its use of variables in it. I also found several issues regarding using current() in identity(). Still I did not manage to get it working.

As far as I could read in the issues it has been implemented. So... Can anyone provide an example? As a bonus you could in that case directly add a PR explaining it in the docs.

@theofidry
Copy link
Member

Hello,

identity() is a simple "faker" function, not provided by faker natively, but by a Faker provider still: IdentityProvider.

You have a piece of doc here, but I agree it could be better. However here's some examples:

Nelmio\Entity\User:
  user1:
    username: 'testuser'
    fullname: '<identity($username)>'
  user2:
    username: 'test_user'
    fullname: '<identity(str_replace("_", " ", $username))>'

<current()> is also explained here. Same I think it could be better so any PR to help is welcomed :)

In short, <current()> can be used when dealing with a fixture collection such as user_{1..10} or user_{alice, bob} and returns the "current" value of the collection:

Nelmio\Entity\User:
  user_{1..10}: # creates 10 users "user_1", ..., "user_10"
    username: '<current()>' # will be "1", ..., "10"
  user_{alice, bob}: # creates two users "user_alice" and "user_bob"
    username: '<current()>' # respectively "alice" and then "bob"

If my memory serves me right, it throws an exception if you try to use <current()> outside of that context (needs to be checked).

Best,

@plandolt
Copy link
Author

Hi @theofidry

Thanks for your response. I know both features <identity()> and <current()>. But what I still dont know about how to use both at the same time. I try to have <current()> subtracted with 2.

What I want to achieve is this (which is not working):

Acme\Entity\User:
  user_ {3..10}:
    anything: <identity(<current()> - 2)>

Is there a syntax which will give me 1..8 or is this not possible at all?

@theofidry
Copy link
Member

Ah, as mentioned in #360 the correct syntax would be '<identity(current() - 2)>' but I couldn't test if it was working or not... :/

@plandolt
Copy link
Author

I just gave it a try but did not work:

[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: current() expects exactly 1 parameter, 0 given

@theofidry
Copy link
Member

Indeed, I'm not sure why I couldn't find much after a bit of debugging. I'm honestly not sure that this will be fixed in v2. In the meantime what you can do is having a provider to do the calculation:

Acme\Entity\User:
  user_ {3..10}:
    anything: '<anything(current())>'

@plandolt
Copy link
Author

Not sure if that works as <identity(current())> did not. But I'll give it a try when i come across this issue again.

@theofidry
Copy link
Member

Erf, I don't know then. I can only make sure that this will be fixed in v3 for now. If I find a fix on the way I'll make sure to include it.

@theofidry
Copy link
Member

Closing in favour of #360

karser added a commit to karser/alice that referenced this issue Nov 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants