1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-23 08:49:53 +01:00

Faker\Generator::addProvider() now allows to override default formatters

This commit is contained in:
Francois Zaninotto 2011-10-24 00:30:48 +02:00
parent 1bf9752a74
commit 9287247053
3 changed files with 19 additions and 3 deletions

@ -245,7 +245,7 @@ $faker->addProvider(new Faker\Provider\Lorem($faker));
$faker->addProvider(new Faker\Provider\Internet($faker));
````
Whenever you try to access a property on the `$faker` object, the generator looks for a method with the same name in all the providers attached to it. For instance, calling `$faker->name` triggers a call to `Faker\Provider\Name::name()`.
Whenever you try to access a property on the `$faker` object, the generator looks for a method with the same name in all the providers attached to it. For instance, calling `$faker->name` triggers a call to `Faker\Provider\Name::name()`. And since Faker starts with the last provider, you can easily override existing formatters: just add a provider containing methods named after the formatters you want to override.
That means that you can esily add your own providers to a `Faker\Generator`. Just have a look at the existing providers to see how you can design powerful data generators in no time.

@ -7,9 +7,9 @@ class Generator
protected $providers = array();
protected $formatters = array();
public function addProvider($providers)
public function addProvider($provider)
{
$this->providers[]= $providers;
array_unshift($this->providers, $provider);
}
public function getProviders()

@ -8,6 +8,14 @@ use Faker\Generator;
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testAddProviderGivesPriorityToNewlyAddedProvider()
{
$generator = new Generator;
$generator->addProvider(new FooProvider());
$generator->addProvider(new BarProvider());
$this->assertEquals('barfoo', $generator->format('fooFormatter'));
}
public function testGetFormatterReturnsCallable()
{
$generator = new Generator;
@ -113,4 +121,12 @@ class FooProvider
{
return 'baz' . $value;
}
}
class BarProvider
{
public function fooFormatter()
{
return 'barfoo';
}
}