Skip to content

Commit

Permalink
Support indexes on fields (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Jul 2, 2017
1 parent 8c3359f commit ce3da3d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/Builders/Field.php
Expand Up @@ -50,7 +50,12 @@ class Field implements Buildable
/**
* @var FieldBuilder
*/
protected $builder;
protected $fieldBuilder;

/**
* @var ClassMetadataBuilder
*/
protected $metaDatabuilder;

/**
* @var ClassMetadataInfo
Expand All @@ -70,17 +75,18 @@ class Field implements Buildable
/**
* Protected constructor to force usage of factory method.
*
* @param FieldBuilder $builder
* @param ClassMetadataInfo $classMetadata
* @param Type $type
* @param string $name
* @param FieldBuilder $fieldBuilder
* @param ClassMetadataBuilder $builder
* @param Type $type
* @param string $name
*/
protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata, Type $type, $name)
protected function __construct(FieldBuilder $fieldBuilder, ClassMetadataBuilder $builder, Type $type, $name)
{
$this->builder = $builder;
$this->classMetadata = $classMetadata;
$this->type = $type;
$this->name = $name;
$this->fieldBuilder = $fieldBuilder;
$this->metaDatabuilder = $builder;
$this->classMetadata = $builder->getClassMetadata();
$this->type = $type;
$this->name = $name;
}

/**
Expand All @@ -89,7 +95,6 @@ protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMe
* @param string $name
*
* @throws \Doctrine\DBAL\DBALException
*
* @return Field
*/
public static function make(ClassMetadataBuilder $builder, $type, $name)
Expand All @@ -98,7 +103,7 @@ public static function make(ClassMetadataBuilder $builder, $type, $name)

$field = $builder->createField($name, $type->getName());

return new static($field, $builder->getClassMetadata(), $type, $name);
return new static($field, $builder, $type, $name);
}

/**
Expand Down Expand Up @@ -157,7 +162,7 @@ public function autoIncrement()
*/
public function generatedValue(callable $callback = null)
{
$generatedValue = new GeneratedValue($this->builder, $this->classMetadata);
$generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata);

if ($callback) {
$callback($generatedValue);
Expand All @@ -176,7 +181,7 @@ public function generatedValue(callable $callback = null)
*/
public function unsigned()
{
$this->builder->option('unsigned', true);
$this->fieldBuilder->option('unsigned', true);

return $this;
}
Expand All @@ -191,7 +196,7 @@ public function unsigned()
*/
public function fixed($fixed)
{
$this->builder->option('fixed', $fixed);
$this->fieldBuilder->option('fixed', $fixed);

return $this;
}
Expand All @@ -205,7 +210,7 @@ public function fixed($fixed)
*/
public function comment($comment)
{
$this->builder->option('comment', $comment);
$this->fieldBuilder->option('comment', $comment);

return $this;
}
Expand All @@ -219,7 +224,7 @@ public function comment($comment)
*/
public function collation($collation)
{
$this->builder->option('collation', $collation);
$this->fieldBuilder->option('collation', $collation);

return $this;
}
Expand All @@ -229,7 +234,28 @@ public function collation($collation)
*/
public function primary()
{
$this->builder->makePrimaryKey();
$this->fieldBuilder->makePrimaryKey();

return $this;
}

/**
* @param null $name
*
* @return Field
*/
public function index($name = null)
{
$index = new Index(
$this->metaDatabuilder,
[$this->getName()]
);

if ($name !== null) {
$index->name($name);
}

$this->callbackAndQueue($index);

return $this;
}
Expand All @@ -239,7 +265,7 @@ public function primary()
*/
public function useForVersioning()
{
$this->builder->isVersionField();
$this->fieldBuilder->isVersionField();

return $this;
}
Expand All @@ -249,7 +275,7 @@ public function useForVersioning()
*/
public function build()
{
$this->builder->build();
$this->fieldBuilder->build();

$this->buildQueued();

Expand All @@ -261,7 +287,7 @@ public function build()
*/
public function getBuilder()
{
return $this->builder;
return $this->fieldBuilder;
}

/**
Expand All @@ -271,7 +297,6 @@ public function getBuilder()
* @param array $args
*
* @throws BadMethodCallException
*
* @return $this
*/
public function __call($method, $args)
Expand Down Expand Up @@ -303,7 +328,7 @@ public function __call($method, $args)
*/
protected function setDefault($default)
{
$this->builder->option('default', $default);
$this->fieldBuilder->option('default', $default);

return $this;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Builders/FieldTest.php
Expand Up @@ -30,6 +30,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->builder = new ClassMetadataBuilder(new ClassMetadataInfo(StubEntity::class));
$this->builder->setTable('stub_entities');

$this->field = Field::make($this->builder, 'string', 'name');
}
Expand All @@ -48,6 +49,32 @@ public function test_can_make_field_nullable()
$this->assertTrue($this->builder->getClassMetadata()->getFieldMapping('name')['nullable']);
}

public function test_can_make_field_index()
{
$this->field->index();

$this->field->build();

$indexes = $this->builder->getClassMetadata()->table['indexes'];

$this->assertArrayHasKey('stub_entities_name_index', $indexes);
$this->assertCount(1, $indexes['stub_entities_name_index']['columns']);
$this->assertContains('name', $indexes['stub_entities_name_index']['columns']);
}

public function test_can_make_field_index_with_custom_name()
{
$this->field->index('index_name');

$this->field->build();

$indexes = $this->builder->getClassMetadata()->table['indexes'];

$this->assertArrayHasKey('index_name', $indexes);
$this->assertCount(1, $indexes['index_name']['columns']);
$this->assertContains('name', $indexes['index_name']['columns']);
}

public function test_can_set_column_name()
{
$this->field->columnName('name_column');
Expand Down

0 comments on commit ce3da3d

Please sign in to comment.