Skip to content

Commit

Permalink
Merge pull request #45 from skeets23/master
Browse files Browse the repository at this point in the history
Allow "enum" type
jasonmccreary authored Dec 2, 2019
2 parents 9554a51 + bbf39a8 commit cae981c
Showing 2 changed files with 63 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
@@ -2,14 +2,18 @@

namespace Mpociot\LaravelTestFactoryHelper\Console;

use Doctrine\DBAL\Types\Type;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Mpociot\LaravelTestFactoryHelper\Types\EnumType;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;


class GenerateCommand extends Command
{
/**
@@ -71,6 +75,7 @@ public function __construct(Filesystem $files, $view)
*/
public function handle()
{
Type::addType('customEnum', EnumType::class);
$this->dir = $this->option('dir');
$this->force = $this->option('force');

@@ -210,7 +215,7 @@ protected function getPropertiesFromTable($model)
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager($table);
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
$databasePlatform->registerDoctrineTypeMapping('enum', 'customEnum');

$platformName = $databasePlatform->getName();
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array());
@@ -238,7 +243,7 @@ protected function getPropertiesFromTable($model)
$name !== $model::UPDATED_AT
) {
if (!method_exists($model, 'getDeletedAtColumn') || (method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn())) {
$this->setProperty($name, $type);
$this->setProperty($name, $type, $table);
}
}
}
@@ -294,7 +299,7 @@ protected function getPropertiesFromMethods($model)
* @param string $name
* @param string|null $type
*/
protected function setProperty($name, $type = null)
protected function setProperty($name, $type = null, $table = null)
{
if ($type !== null && Str::startsWith($type, 'function (')) {
$this->properties[$name] = $type;
@@ -303,6 +308,7 @@ protected function setProperty($name, $type = null)
}

$fakeableTypes = [
'enum' => '$faker->randomElement(' . $this->enumValues($table, $name) . ')',
'string' => '$faker->word',
'text' => '$faker->text',
'date' => '$faker->date()',
@@ -367,6 +373,21 @@ protected function setProperty($name, $type = null)
$this->properties[$name] = '$faker->word';
}

public static function enumValues($table, $name)
{
if ($table === null) {
return "[]";
}

$type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type;

preg_match_all("/'([^']+)'/", $type, $matches);

$values = isset($matches[1]) ? $matches[1] : array();

return "['" . implode("', '", $values) . "']";
}


/**
* @param string $class
39 changes: 39 additions & 0 deletions src/Types/EnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Mpociot\LaravelTestFactoryHelper\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class EnumType extends Type
{

const ENUM = 'enum';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
/*
* This is wrong, but it also isn't important, since we're not modifying
* tables anyways. This method must exist to match the abstract parent
* class, but we won't use it. Normally, this should return the SQL
* required to create an 'enum' column, but since this function doesn't
* know the values of the enum field, that's not possible.
*/
return 'ENUM';
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value;
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value;
}

public function getName()
{
return self::ENUM;
}
}

0 comments on commit cae981c

Please sign in to comment.