I'm using PhoneNumberBundle for validates phone number on my application. I'm using also NelmioAliceBundle together with AliceFixtureBundle. Having that as start point I'm writing a fixture for a entity that has a PhoneNumberBundle
assert for validate the phone number. Here is a snippet of that file:
/**
* @AssertPhoneNumber(defaultRegion="VE")
* @ORM\Column(name="phone", type="phone_number", length=11)
*/
protected $phone;
I don't know how to use external libraries on the fixture itself so the only solution I see if to write my own faker and return the well formated number phone and pass back to the fixture. Then I did this:
TananeFakerProvider.php
class TananeFakerProvider {
public function formatPhoneNumber($fakePhoneNumber)
{
return $this->container->get('libphonenumber.phone_number_util')->parse($fakePhoneNumber);
}
}
services.yml
services:
tanane.faker.provider:
class: CommonBundle\Tools\TananeFakerProvider
arguments: ["@service_container"]
tags:
- { name: h4cc_alice_fixtures.provider }
And finally Orders.yml (the fixture):
FrontendBundle\Entity\Orders:
Orders{1..50}:
nickname: <text(15)>
# trying to pass the fake number back to the custom faker
phone: <formatPhoneNumber(phoneNumber())>
email: <companyEmail()>
fiscal_address: <address()>
shipping_address: <address()>
shipping_from: <randomElement(array('MRW','DOMESA', 'ZOOM'))>
payment_type: @PaymentType*
order_amount: <randomFloat(2)>
bank: @Bank*
transaction: <randomNumber()>
comments: <sentence(15)>
secure: <boolean(35)>
person: <randomElement(array(@Natural*, @Legal*))>
status: @OrderStatus*
But I got this error:
[Symfony\Component\Debug\Exception\ContextErrorException] Notice: Use of undefined constant phoneNumber - assumed 'phoneNumber' in /var/www/html/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php(630) : eval()'d code line 1
So I'm passing the value in the wrong way, could any give me some help on this? Or maybe give me another idea in how to achieve this?
As an alternative you can pass a Faker Provider object as a second parameter when instantiating a Nelmio/Alice/Fixtures/Loader class.
1.- Create a Provider Class
2.-
3.- Finally create a example.yml file (at the same level than the Loader class in this case) with some data and a custom call to tell_me_something()
(Settled on github, but for posterity here is a copy)
You can use the following in Alice 1.x:
And this in the 2.x line: