forked from willdurand/BazingaFakerBundle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial commit
- Loading branch information
0 parents
commit 048b4ba
Showing
12 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class PopulateCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Populates configured entities with random data') | ||
->setHelp(<<<HELP | ||
The <info>faker:populate</info> command populates configured entities with random data. | ||
<info>php app/console faker:populate</info> | ||
HELP | ||
) | ||
->setName('faker:populate'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$populator = $this->getContainer()->get('faker.populator'); | ||
$insertedPks = $populator->execute(); | ||
|
||
$output->writeln(''); | ||
|
||
if (0 === count($insertedPks)) { | ||
$output->writeln('<error>No entities populated.</error>'); | ||
} else { | ||
foreach ($insertedPks as $class => $pks) { | ||
$reflClass = new \ReflectionClass($class); | ||
$shortClassName = $reflClass->getShortName(); | ||
$output->writeln(sprintf('Inserted <info>%s</info> new <info>%s</info> objects', count($pks), $shortClassName)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* Generates the configuration tree builder. | ||
* | ||
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('faker'); | ||
|
||
$rootNode->children() | ||
->scalarNode('populator')->end() | ||
->arrayNode('entities') | ||
->useAttributeAsKey('key') | ||
->prototype('scalar')->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Config\Definition\Processor; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class FakerExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$processor = new Processor(); | ||
$configuration = new Configuration(); | ||
$config = $processor->processConfiguration($configuration, $configs); | ||
|
||
if (!$container->hasDefinition('faker')) { | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.xml'); | ||
} | ||
|
||
if (isset($config['populator'])) { | ||
$container->setParameter('faker.populator.class', $config['populator']); | ||
} | ||
|
||
foreach ($config['entities'] as $class => $number) { | ||
$container | ||
->getDefinition('faker.populator') | ||
->addMethodCall('addEntity', array($class, $number)) | ||
; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class FakerBundle extends Bundle | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# FakerBundle # | ||
|
||
This bundle integrates [Faker](https://github.com/fzaninotto/Faker), a PHP library that generates fake data for you. | ||
It provides a command to load random data for your model objects as simple as possible. | ||
|
||
**Note:** It only works out of the box with [Propel ORM](https://github.com/propelorm/Propel) at the moment | ||
but you can configure your own `populator` so it can works with all database abstraction systems. | ||
|
||
|
||
## Installation ## | ||
|
||
Install this bundle as usual: | ||
|
||
> git submodule add git://github.com/willdurand/BazingaFakerBundle.git vendor/bundles/Bazinga/Bundle/FakerBundle | ||
Add the [Faker](https://github.com/fzaninotto/Faker) library: | ||
|
||
> git submodule add git://github.com/fzaninotto/Faker.git vendor/faker | ||
Register the namespace in `app/autoload.php`: | ||
|
||
// app/autoload.php | ||
$loader->registerNamespaces(array( | ||
// ... | ||
'Bazinga' => __DIR__.'/../vendor/bundles', | ||
'Faker' => __DIR__.'/../vendor/faker/src', | ||
)); | ||
|
||
Register the bundle in `app/AppKernel.php`: | ||
|
||
// app/AppKernel.php | ||
public function registerBundles() | ||
{ | ||
return array( | ||
// ... | ||
new Bazinga\Bundle\FakerBundle\FakerBundle(), | ||
); | ||
} | ||
|
||
## Configuration ## | ||
|
||
In order to use the `BazingaFakerBundle`, you have to configure it. | ||
Actually, you just need to configure entities you want to populate and in which quantity. | ||
|
||
``` yaml | ||
# app/config/config*.yml | ||
|
||
faker: | ||
# populator: \Your\Own\Populator | ||
entities: | ||
# Acme\LibraryBundle\Model\Author: 10 | ||
# Acme\LibraryBundle\Model\Book: 5 | ||
``` | ||
|
||
|
||
## Command ## | ||
|
||
The bundle provides a new Symfony2 command: `faker:populate` which will populate all configured entities. | ||
|
||
php app/console faker:populate | ||
|
||
|
||
## Usage ## | ||
|
||
In real life, you'll have to populate your database with data. It's often a pain because it requires imagination | ||
and time you probably don't have or you don't want to waste. Faker to the rescue! | ||
|
||
You just have to configure your entities to populate, then run the `faker:populate` command to add new data without effort. | ||
It's better than fixtures because you don't need to write anything. | ||
If you drop your database, then re create it and load fresh data. | ||
|
||
|
||
## Credits ## | ||
|
||
* François Zaninotto (Creator of Faker) | ||
* William Durand | ||
|
||
|
||
## License ## | ||
|
||
See `Resources/meta/LICENSE`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="faker.generator.class">Faker\Factory</parameter> | ||
<parameter key="faker.populator.class">Faker\ORM\Propel\Populator</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="faker.generator" factory-class="%faker.generator.class%" class="%faker.generator.class%" factory-method="create" /> | ||
<service id="faker.populator" class="%faker.populator.class%"> | ||
<argument type="service" id="faker.generator" /> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2011 William Durand <william.durand1@gmail.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle\Tests\DependencyInjection; | ||
|
||
use Bazinga\Bundle\FakerBundle\Tests\TestCase; | ||
use Bazinga\Bundle\FakerBundle\DependencyInjection\FakerExtension; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class FakerExtensionTest extends TestCase | ||
{ | ||
public function getContainer() | ||
{ | ||
return new ContainerBuilder(new ParameterBag(array( | ||
'kernel.root_dir' => __DIR__.'/../../../../', | ||
))); | ||
} | ||
|
||
public function testLoadWithCustomPopulator() | ||
{ | ||
$container = $this->getContainer(); | ||
$loader = new FakerExtension(); | ||
|
||
$loader->load(array(array('populator' => '\Foo\Bar')), $container); | ||
|
||
$this->assertEquals('\Foo\Bar', $container->getParameter('faker.populator.class')); | ||
try { | ||
$container->get('faker.populator'); | ||
$this->fail('\Foo\Bar doesn\'t exist so it should throw an exception'); | ||
} catch (\ReflectionException $e) { | ||
$this->assertEquals('Class \Foo\Bar does not exist', $e->getMessage(), 'Check that the loaded populator is well configured'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Bazinga\Bundle\FakerBundle\Tests\Fixtures; | ||
|
||
class Foo | ||
{ | ||
public static $PEER = ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the FakerBundle package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Bazinga\Bundle\FakerBundle\Tests; | ||
|
||
/** | ||
* @author William Durand <william.durand1@gmail.com> | ||
*/ | ||
class TestCase extends \PHPUnit_Framework_TestCase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
// this file searches for the autoload file of your project, and includes it | ||
$dir = __DIR__; | ||
$lastDir = null; | ||
while (($dir = dirname($dir)) && $dir !== $lastDir) { | ||
$lastDir = $dir; | ||
|
||
if (file_exists($file = $dir.'/app/bootstrap.php.cache')) { | ||
require_once $file; | ||
return; | ||
} | ||
} | ||
|
||
throw new \RuntimeException('Could not locate the project\'s bootstrap.php.cache. If your bundle is not inside a project, you need to replace this bootstrap file.'); |
Oops, something went wrong.