Skip to content

Commit

Permalink
Merge pull request #16 from Fakerino/fix-doctrine-datetime
Browse files Browse the repository at this point in the history
Fix doctrine datetime # and improve table filler
  • Loading branch information
niklongstone committed Feb 12, 2017
2 parents a71acd6 + 991aaef commit 920df67
Show file tree
Hide file tree
Showing 45 changed files with 349 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -18,7 +18,7 @@ before_script:
- travis_retry composer install --no-interaction --prefer-source

script:
- phpunit --coverage-text --coverage-clover=coverage.clover
- phpunit --debug --coverage-text --coverage-clover=coverage.clover

after_script:
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
Expand Up @@ -25,7 +25,7 @@ final class IniConfigurationFile extends FakerinoConfigurationLoader implements
*/
public function toArray()
{
$array = parse_ini_file($this->getConfFilePath(), true);
$array = parse_ini_file($this->getConfFilePath(), true);
if (empty($array)) {

return array();
Expand Down
Expand Up @@ -28,7 +28,7 @@ public function toArray()
{
$conf = null;
$phpConfFile = $this->getConfFilePath();
require ($phpConfFile);
require($phpConfFile);
if ($conf === null) {
throw new ConfNotSupportedException($phpConfFile);
}
Expand Down
Expand Up @@ -35,28 +35,29 @@ public function toArray()
/**
* Converts an XML string in an array
*
* @param \SimpleXMLElement $xmlObject
* @param array $out
* @param \SimpleXMLElement $xmlObject
* @param array $out
*
* @return array
*/
private function xml2array($xmlObject, $out = array())
{
foreach ($xmlObject->attributes() as $attr => $val) {
$out['@attributes'][$attr] = (string) $val;
$out['@attributes'][$attr] = (string)$val;
}
$hasChilds = false;
foreach ($xmlObject as $index => $node) {
$hasChilds = true;
$out[$index][] = $this->xml2array($node);
}
if (!$hasChilds && $val = (string) $xmlObject) {
$out['@value'] = $val;
if (!$hasChilds && $val = (string)$xmlObject) {
$out['@value'] = $val;
}
foreach ($out as $key => $vals) {
if (is_array($vals)
&& count($vals) === 1
&& array_key_exists(0, $vals)) {
&& array_key_exists(0, $vals)
) {
$out[$key] = $vals[0];
}
}
Expand Down
Expand Up @@ -20,6 +20,7 @@ class ConfNotSupportedException extends \RuntimeException
{
/**
* Constructor
*
* @param string $file
*/
public function __construct($file)
Expand Down
22 changes: 11 additions & 11 deletions src/Fakerino/Configuration/FakerinoConf.php
Expand Up @@ -69,6 +69,16 @@ public function get($value)
throw new ConfValueNotFoundException($value);
}

/**
* Returns the configuration in an array.
*
* @return array
*/
public function toArray()
{
return $this->conf;
}

/**
* Initializes the default values.
*
Expand All @@ -84,17 +94,7 @@ private function loadDefault()
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . '..'
. '/data'
. '/data',
);
}

/**
* Returns the configuration in an array.
*
* @return array
*/
public function toArray()
{
return $this->conf;
}
}
36 changes: 18 additions & 18 deletions src/Fakerino/Core/Console/FakeConsole.php
Expand Up @@ -48,22 +48,6 @@ public function __construct($input)
$this->getParameters();
}

private function getParam($flag, $hasValue = false)
{
$flagValue = false;
$flagIndex = array_search($flag, $this->input);
if ($flagIndex !== false) {
$flagValue = true;
unset($this->input[$flagIndex]);
if ($hasValue) {
$flagValue = $this->input[$flagIndex+1];
unset($this->input[$flagIndex+1]);
}
}

return $flagValue;
}

/**
* Runs the command.
*
Expand Down Expand Up @@ -101,7 +85,7 @@ public function run()
if ($this->json) {
$result = $fakerino->toJson();
} else {
$result = (string) $fakerino;
$result = (string)$fakerino;
}

return $result . PHP_EOL;
Expand All @@ -118,10 +102,26 @@ private function getParameters()
$this->templateSource = $this->getParam('-s', true);
}

private function getParam($flag, $hasValue = false)
{
$flagValue = false;
$flagIndex = array_search($flag, $this->input);
if ($flagIndex !== false) {
$flagValue = true;
unset($this->input[$flagIndex]);
if ($hasValue) {
$flagValue = $this->input[$flagIndex + 1];
unset($this->input[$flagIndex + 1]);
}
}

return $flagValue;
}

private function showHelp()
{
$helper = 'Usage:' . PHP_EOL;
$helper .= ' app/fake <fake data name> [-j] [-n <integer>] [-c <config file path>]' .PHP_EOL . PHP_EOL;
$helper .= ' app/fake <fake data name> [-j] [-n <integer>] [-c <config file path>]' . PHP_EOL . PHP_EOL;
$helper .= 'Options:' . PHP_EOL;
$helper .= str_pad(' -j', 20) . 'Returns JSON format (default string)' . PHP_EOL;
$helper .= str_pad(' -n <num>', 20) . 'Returns <num> times the result' . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/Core/Database/DbInterface.php
Expand Up @@ -28,7 +28,7 @@ public function connect();
/**
* Inserts row in $tableName.
*
* @param DbRowEntity $row
* @param DbRowEntity $row
*
* @return bool
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Fakerino/Core/Database/DbRowEntity.php
Expand Up @@ -22,19 +22,19 @@ class DbRowEntity
private $fields;

/**
* @param DbFieldEntity $field
* @return array
*/
public function setFields(DbFieldEntity $field)
public function getFields()
{
$this->fields[] = $field;
return $this->fields;
}

/**
* @return array
* @param DbFieldEntity $field
*/
public function getFields()
public function setFields(DbFieldEntity $field)
{
return $this->fields;
$this->fields[] = $field;
}

/**
Expand Down
29 changes: 23 additions & 6 deletions src/Fakerino/Core/Database/DoctrineLayer.php
Expand Up @@ -30,7 +30,7 @@ final class DoctrineLayer implements DbInterface
private $databaseConfig;

/**
* @param array|null $databaseConfig
* @param array $databaseConfig
*/
public function __construct($databaseConfig)
{
Expand All @@ -55,6 +55,8 @@ public function setTable($tableName)
{
$this->tableName = $tableName;
$schemaManager = self::$conn->getSchemaManager();
self::$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

$tableSchema = $schemaManager->listTableColumns($tableName);
foreach ($tableSchema as $column) {
$this->columns[] = $column;
Expand All @@ -78,7 +80,7 @@ public function getTotalColumns()
*/
public function getColumnType($num)
{
$doctrineType = $this->columns[$num]->getType();
$doctrineType = $this->columns[$num]->getType()->getName();

return $this->getFakeType($doctrineType);
}
Expand Down Expand Up @@ -119,14 +121,27 @@ public function insert(DbRowEntity $rows)
$rowsElement = $rows->getFields();
foreach ($rowsElement as $field) {
$sql->setValue($field->getName(), '?');
$values[] = $field->getValue();
if ($this->isDate($field->getType())) {
$values[] = new \DateTime($field->getValue());
} else {
$values[] = $field->getValue();
}
$types[] = $field->getType();
}
self::$conn->executeQuery($sql, $values, $types);

return true;
}

/**
* @param $columnType
* @return bool
*/
private function isDate($columnType)
{
return in_array($columnType, array(Type::DATETIME, Type::DATETIMETZ, Type::DATE, Type::TIME));
}

/**
* {@inheritdoc}
*/
Expand All @@ -143,10 +158,12 @@ public function getFakeType($columnType)
Type::INTEGER => 'integer',
Type::SMALLINT => 'integer',
Type::STRING => 'string',
Type::TEXT => 'string',
Type::BLOB => 'string',
Type::TEXT => 'text',
Type::BLOB => 'text',
Type::FLOAT => 'integer',
Type::GUID => 'integer'
Type::GUID => 'integer',
Type::SIMPLE_ARRAY => 'string',
Type::BINARY => 'string',
);

return $fakeType[strtolower($columnType)];
Expand Down
36 changes: 18 additions & 18 deletions src/Fakerino/Core/Entity/EntityInfo.php
Expand Up @@ -18,13 +18,13 @@
*/
class EntityInfo
{
const EXCLUDE_STATIC = true;

/**
* @var \ReflectionClass
*/
private $reflectionEntity;

const EXCLUDE_STATIC = true;

/**
* Constructor
*
Expand Down Expand Up @@ -59,40 +59,40 @@ public function getProperties($filter = \ReflectionProperty::IS_PUBLIC)
}

/**
* Gets the methods of the object provided.
* Returns the public setters methods.
*
* @param int $filter
*
* @return array
*/
public function getMethods($filter = \ReflectionProperty::IS_PUBLIC)
public function getSetters($filter = \ReflectionProperty::IS_PUBLIC)
{
$methods = array();
$reflectionMethods = $this->reflectionEntity->getMethods($filter);
foreach ($reflectionMethods as $method) {
$methods[] = new Method($method->name, $method->isStatic());
$setters = array();
$methods = $this->getMethods($filter);
foreach ($methods as $method) {
if (substr($method->getName(), 0, 3) === 'set') {
$setters[] = $method;
}
}

return $methods;
return $setters;
}

/**
* Returns the public setters methods.
* Gets the methods of the object provided.
*
* @param int $filter
*
* @return array
*/
public function getSetters($filter = \ReflectionProperty::IS_PUBLIC)
public function getMethods($filter = \ReflectionProperty::IS_PUBLIC)
{
$setters = array();
$methods = $this->getMethods($filter);
foreach ($methods as $method) {
if (substr($method->getName(), 0, 3) === 'set') {
$setters[] = $method;
}
$methods = array();
$reflectionMethods = $this->reflectionEntity->getMethods($filter);
foreach ($reflectionMethods as $method) {
$methods[] = new Method($method->name, $method->isStatic());
}

return $setters;
return $methods;
}
}
6 changes: 3 additions & 3 deletions src/Fakerino/Core/FakeDataFactory.php
Expand Up @@ -34,13 +34,13 @@ final class FakeDataFactory implements FakeDataFactoryInterface
/** @var string|array */
private $startElement;

/** @var \Fakerino\Core\FakeHandler\HandlerInterface */
/** @var \Fakerino\Core\FakeHandler\HandlerInterface */
private $fakeHandler;

/** @var \Fakerino\Core\Database\DbInterface */
/** @var \Fakerino\Core\Database\DbInterface */
private $db;

/** @var \Fakerino\Core\Template\TemplateInterface */
/** @var \Fakerino\Core\Template\TemplateInterface */
private $template;

/** @var int */
Expand Down
3 changes: 3 additions & 0 deletions src/Fakerino/Core/FakeHandler/FileFakerClass.php
Expand Up @@ -18,6 +18,8 @@
*/
final class FileFakerClass extends Handler
{
/** @var string */
private $filePath;

/**
* @param string $filePath
Expand All @@ -26,6 +28,7 @@ public function __construct($filePath)
{
$this->filePath = $filePath;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 920df67

Please sign in to comment.