Skip to content

Commit

Permalink
Added providers
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Feb 4, 2015
1 parent b2aa8ab commit 9abb179
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
16 changes: 15 additions & 1 deletion FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ class FakerServiceProvider implements ServiceProviderInterface
*/
public function register(Application $app)
{
$app['faker'] = Factory::create($app['locale']);
$app['faker'] = null;
$app['faker.providers'] = array();
}

/**
* {@inheritdoc}
*/
public function boot(Application $app)
{
// generator instance
$app['faker'] = $app->share(function ($app) {
return Factory::create($app['locale']);
});

// third-party providers
$providers = array_filter((array) $app['faker.providers'], function ($provider) {
return class_exists($provider) && is_subclass_of($provider, 'Faker\\Provider\\Base');
});

foreach ($providers as $provider) {
$app['faker']->addProvider(new $provider($app['faker']));
}
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ Initialize it using `register`, it allows only the `locale` option
use EmanueleMinotto\FakerServiceProvider\FakerServiceProvider;

$app->register(new FakerServiceProvider(), array(
'locale' => 'it_IT' // default: en_US
'faker.providers' => array(
'CompanyNameGenerator\\FakerProvider',
'EmanueleMinotto\\Faker\\PlaceholdItProvider',
), // default empty
'locale' => 'it_IT', // default: en_US
));
```

Expand Down
40 changes: 39 additions & 1 deletion Tests/FakerServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,46 @@ class FakerServiceProviderTest extends PHPUnit_Framework_TestCase
public function testRegisterServiceProvider()
{
$app = new Application();
$app->register(new FakerServiceProvider());
$app->register(new FakerServiceProvider(), array(
'locale' => 'ro_RO',
));
$app->boot();


$this->assertInstanceOf('Faker\\Generator', $app['faker']);
$this->assertContainsOnlyInstancesOf('Faker\\Provider\\Base', $app['faker']->getProviders());

$this->assertNotEmpty(array_filter($app['faker']->getProviders(), function ($provider) {
return $provider instanceof \Faker\Provider\ro_RO\Address;
}));
}

/**
* @covers ::boot
* @covers ::register
*/
public function testServiceProviders()
{
$app = new Application();
$app->register(new FakerServiceProvider(), array(
'faker.providers' => array(
'CompanyNameGenerator\\FakerProvider',
'EmanueleMinotto\\Faker\\PlaceholdItProvider',
),
));
$app->boot();


$this->assertInstanceOf('Faker\\Generator', $app['faker']);

$this->assertCount(2, $app['faker.providers']);
$this->assertContainsOnlyInstancesOf('Faker\\Provider\\Base', $app['faker']->getProviders());

$this->assertNotEmpty(array_filter($app['faker']->getProviders(), function ($provider) {
return $provider instanceof \CompanyNameGenerator\FakerProvider;
}));

$this->assertSame('http://placehold.it/50.gif', $app['faker']->imageUrl(50));
}

/**
Expand All @@ -41,6 +78,7 @@ public function testRequest()

$request = Request::create('/');
$response = $app->handle($request);


$this->assertTrue($response->isOk());
$this->assertRegExp('/\d+/', $response->getContent());
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"silex/silex": "~1"
},
"require-dev": {
"emanueleminotto/faker-placehold-it-provider": "~1.0",
"fzaninotto/company-name-generator": "dev-master",
"phpunit/phpunit": "~4.4"
},
"suggest": {
"emanueleminotto/faker-placehold-it-provider": "placehold.it provider",
"fzaninotto/company-name-generator": "generate names for english tech companies with class"
}
}

0 comments on commit 9abb179

Please sign in to comment.