First basic working version

This commit is contained in:
Daniel Siepmann 2017-02-03 16:42:19 +01:00
parent 9d5e6ca018
commit ee310bd580
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
8 changed files with 1412 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
vendor

12
app Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env php
<?php
namespace DSiepmann;
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new Command\FakeMysqlCommand());
$application->run();

24
composer.json Normal file
View file

@ -0,0 +1,24 @@
{
"name": "DSiepmann\\faker",
"description": "Insert fake data into MySQL Table using PHP Edit",
"type": "project",
"authors": [
{
"name": "Daniel Siepmann",
"email": "coding@daniel-siepmann.de",
"homepage": "https://daniel-siepmann.de",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"DSiepmann\\": "src"
}
},
"require": {
"fzaninotto/faker": "^1.6",
"symfony/console": "^3.2",
"doctrine/dbal": "^2.5",
"symfony/yaml": "^3.2"
}
}

1125
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,19 @@
database:
dbname: typo3_git_62
user: root
password:
host: localhost
unix_socket: /tmp/mysql.sock
driver: pdo_mysql
data:
pid:
type: static
value: 1
count:
type: static
value: 1
name:
type: userfunc
class: DSiepmann\Userfunction\RepeatingFilenames
method: filename

76
readme.rst Normal file
View file

@ -0,0 +1,76 @@
.. _highlight: bash
About
=====
Small wrapper around `faker`_ and `Doctrine DBAL`_ to seed MySQL Tables with dummy data.
Because no other found solution did work out of the box.
Installation
============
Run::
composer update
Usage
=====
Provide a configuration somewhere, containing necessary database connection information and data
seed information.
Call ``fake:mysql`` with configuration file and table name::
./app fake:mysql configs/typo3_downloads.yml tx_downloadcounter_domain_model_download
An example file is provided.
Userfunctions
=============
You can define custom PHP Code to provide information for each column.
Configuration
=============
The configuration has two sections, ``database`` and ``data``.
``database`` is just a plain key-value array passed to doctrines dbal. So take a look at
http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html for
further information.
``data`` is a key-value pair of column names and information which data to use. The structure is
like the following:
.. code:: yml
pid:
type: static
Where ``pid`` is the column name and ``type`` defines how to get the data to use. The following
types are supported:
``static``
Uses the value of ``value`` as static value.
E.g.:
.. code:: yml
pid:
type: static
value: 1
``userfunc``
Uses a user defined function defined by ``class`` and ``method``.
.. code:: yml
name:
type: userfunc
class: DSiepmann\Userfunction\RepeatingFilenames
method: filename
.. _faker: https://github.com/fzaninotto/Faker
.. _Doctrine DBAL: http://www.doctrine-project.org/projects/dbal.html

View file

@ -0,0 +1,126 @@
<?php
namespace DSiepmann\Command;
use Doctrine\DBAL;
use Faker\Factory as Faker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
class FakeMysqlCommand extends Command
{
/**
* @var array
*/
protected $config;
/**
* @var Faker\Generator
*/
protected $faker;
/**
* @var Dbal\Connection
*/
protected $connection;
/**
* @var array
*/
protected $userfunctions = [];
protected function configure()
{
$this
->setName('fake:mysql')
->setDescription('Fake MySQL Data.')
->addOption('number', null, InputOption::VALUE_REQUIRED, 'How many fake data should be generated.', 200)
->addArgument('config', InputArgument::REQUIRED, 'The configuration file to use.')
->addArgument('tableName', InputArgument::REQUIRED, 'The name of the Mysql Table.')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->config = Yaml::parse(file_get_contents($input->getArgument('config')));
$this->faker = Faker::create();
$this->connection = DBAL\DriverManager::getConnection($this->config['database'], new DBAL\Configuration());
$this->truncateTable($input->getArgument('tableName'));
$output->writeLn('<info>Table ' . $input->getArgument('tableName') . ' was truncated.</info>');
foreach (range(1, $input->getOption('number')) as $number) {
$this->connection->insert(
$input->getArgument('tableName'),
$this->getData($input->getArgument('tableName'))
);
$output->writeLn('<info>Inserting record number: ' . $number . '.</info>');
// if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
// $output->writeLn('<info>With data: ' . var_export($data, true) . '</info>');
// }
}
}
protected function truncateTable($tableName)
{
$platform = $this->connection->getDatabasePlatform();
$this->connection->executeUpdate($platform->getTruncateTableSQL($tableName, false));
}
protected function getData($tableName)
{
$data = [];
foreach ($this->connection->getSchemaManager()->listTableColumns($tableName) as $name => $column) {
$columnData = $this->getColumnData($name, $column);
if ($columnData !== null) {
$data[$name] = $columnData;
}
}
return $data;
}
protected function getColumnData($name, DBAL\Schema\Column $column)
{
if ($column->getAutoincrement()) {
return;
}
if (isset($this->config['data'][$name])) {
return $this->getConfiguredColumnData($this->config['data'][$name]);
}
if (is_callable([$this->faker, $column->getType()])) {
return call_user_func([$this->faker, $column->getType()]);
}
return '';
}
protected function getConfiguredColumnData($config)
{
if ($config['type'] === 'static') {
return $config['value'];
}
if ($config['type'] === 'userfunc') {
if (! isset($this->userfunc[$config['class']])) {
$this->userfunc[$config['class']] = new $config['class'];
}
$userfunction = $this->userfunc[$config['class']];
$userMethod = $config['method'];
return $userfunction->$userMethod();
}
throw new \Exception('Unkown configured value.', 1486135949);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace DSiepmann\Userfunction;
use Faker\Factory as Faker;
class RepeatingFilenames
{
protected $fileExtensions = ['jpg', 'png', 'pdf'];
protected $fileNames = [];
/**
* @var Faker\Generator
*/
protected $faker;
public function __construct()
{
$this->faker = Faker::create();
foreach (range(0, 12) as $number) {
$this->fileNames[] = $this->faker->word . '.' . $this->faker->randomElement($this->fileExtensions);
}
}
public function filename()
{
return $this->faker->randomElement($this->fileNames);
}
}