Skip to content

Commit de6a6cd

Browse files
hiddecopatrickbrouwers
authored andcommitted
Table options support (#35)
* Table options support * Test setting options doesn't interfere with schema name Signed-off-by: Hidde Beydals <[email protected]> * Make primaryTable options buildable * Add tests for charset and collate * Test ->collate()->charset()->schema() chain * Call build() on Table instance * Refactor to use callbackAndQueue instead of build
1 parent c1f72e7 commit de6a6cd

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

src/Builders/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function table($name, callable $callback = null)
3232

3333
$table = new Table($this->builder, $name);
3434

35-
$this->callIfCallable($callback, $table);
35+
$this->callbackAndQueue($table, $callback);
3636

3737
return $table;
3838
}

src/Builders/Table.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
namespace LaravelDoctrine\Fluent\Builders;
44

55
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
6+
use LaravelDoctrine\Fluent\Buildable;
7+
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
68

7-
class Table extends AbstractBuilder
9+
class Table extends AbstractBuilder implements Buildable
810
{
11+
use Queueable {
12+
build as buildQueued;
13+
}
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $primaryTable = [];
19+
920
/**
1021
* @param ClassMetadataBuilder $builder
1122
* @param string|callable|null $name
@@ -40,8 +51,65 @@ public function setName($name)
4051
*/
4152
public function schema($schema)
4253
{
43-
$this->builder->getClassMetadata()->setPrimaryTable(['schema' => $schema]);
54+
$this->primaryTable['schema'] = $schema;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* @param string $charset
61+
*
62+
* @return $this
63+
*/
64+
public function charset($charset)
65+
{
66+
$this->option('charset', $charset);
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @param string $collate
73+
*
74+
* @return $this
75+
*/
76+
public function collate($collate)
77+
{
78+
$this->option('collate', $collate);
4479

4580
return $this;
4681
}
82+
83+
/**
84+
* @param array $options
85+
*
86+
* @return $this
87+
*/
88+
public function options(array $options = [])
89+
{
90+
$this->primaryTable['options'] = $options;
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param string $name
97+
* @param string $value
98+
*
99+
* @return $this
100+
*/
101+
public function option($name, $value)
102+
{
103+
$this->primaryTable['options'][$name] = $value;
104+
105+
return $this;
106+
}
107+
108+
/**
109+
* Execute the build process
110+
*/
111+
public function build()
112+
{
113+
$this->builder->getClassMetadata()->setPrimaryTable($this->primaryTable);
114+
}
47115
}

tests/Builders/TableTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,72 @@ public function test_can_change_only_the_schema()
6464
{
6565
$this->table->schema('a_schema');
6666

67+
$this->table->build();
68+
6769
$this->assertEquals('a_schema', $this->builder->getClassMetadata()->getSchemaName());
6870
}
71+
72+
public function test_can_set_options()
73+
{
74+
$this->table->options([
75+
'collate' => 'utf8mb4_unicode_ci',
76+
'charset' => 'utf8mb4'
77+
]);
78+
79+
$this->table->build();
80+
81+
$this->assertEquals([
82+
'collate' => 'utf8mb4_unicode_ci',
83+
'charset' => 'utf8mb4'
84+
], $this->builder->getClassMetadata()->table['options']);
85+
}
86+
87+
public function test_set_options_does_not_touch_other_data() {
88+
$table = $this->table->getClassMetadata()->table;
89+
90+
$this->table->options(['collate' => 'utf8mb4_unicode_ci']);
91+
$table['options'] = ['collate' => 'utf8mb4_unicode_ci'];
92+
93+
$this->table->build();
94+
95+
$this->assertEquals($table, $this->builder->getClassMetadata()->table);
96+
}
97+
98+
public function test_can_set_options_and_change_schema () {
99+
$this->table->options(['collate' => 'utf8mb4_unicode_ci']);
100+
$this->table->schema('a_schema');
101+
102+
$this->table->build();
103+
104+
$this->assertEquals('a_schema', $this->builder->getClassMetadata()->getSchemaName());
105+
$this->assertEquals(['collate' => 'utf8mb4_unicode_ci'], $this->builder->getClassMetadata()->table['options']);
106+
}
107+
108+
public function test_can_set_charset() {
109+
$this->table->charset('utf8mb4');
110+
111+
$this->table->build();
112+
113+
$this->assertEquals('utf8mb4', $this->builder->getClassMetadata()->table['options']['charset']);
114+
}
115+
116+
public function test_can_set_collate() {
117+
$this->table->collate('utf8mb4_unicode_ci');
118+
119+
$this->table->build();
120+
121+
$this->assertEquals('utf8mb4_unicode_ci', $this->builder->getClassMetadata()->table['options']['collate']);
122+
}
123+
124+
public function test_can_chain_collate_charset_schema() {
125+
$this->table->collate('utf8mb4_unicode_ci')->charset('utf8mb4')->schema('a_schema');
126+
127+
$this->table->build();
128+
129+
$this->assertEquals('a_schema', $this->builder->getClassMetadata()->table['schema']);
130+
$this->assertEquals([
131+
'collate' => 'utf8mb4_unicode_ci',
132+
'charset' => 'utf8mb4'
133+
], $this->builder->getClassMetadata()->table['options']);
134+
}
69135
}

0 commit comments

Comments
 (0)