Skip to content

Commit

Permalink
Feature/indexable columns (#47)
Browse files Browse the repository at this point in the history
* Support indexes on fields (#43)

* Apply fixes from StyleCI

[ci skip] [skip ci]
  • Loading branch information
patrickbrouwers authored Jul 4, 2017
1 parent 8c3359f commit 99f1182
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
65 changes: 46 additions & 19 deletions src/Builders/Field.php
Original file line number Diff line number Diff line change
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,15 +75,16 @@ 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->fieldBuilder = $fieldBuilder;
$this->metaDatabuilder = $builder;
$this->classMetadata = $builder->getClassMetadata();
$this->type = $type;
$this->name = $name;
}
Expand All @@ -98,7 +104,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 +163,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 +182,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 +197,7 @@ public function unsigned()
*/
public function fixed($fixed)
{
$this->builder->option('fixed', $fixed);
$this->fieldBuilder->option('fixed', $fixed);

return $this;
}
Expand All @@ -205,7 +211,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 +225,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 +235,28 @@ public function collation($collation)
*/
public function primary()
{
$this->builder->makePrimaryKey();
$this->fieldBuilder->makePrimaryKey();

return $this;
}

/**
* @param string|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 +266,7 @@ public function primary()
*/
public function useForVersioning()
{
$this->builder->isVersionField();
$this->fieldBuilder->isVersionField();

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

$this->buildQueued();

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

/**
Expand Down Expand Up @@ -303,7 +330,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
Original file line number Diff line number Diff line change
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 99f1182

Please sign in to comment.