Skip to content

Commit c69f0e3

Browse files
committed
Fix and cleanup existing test suite
1 parent 81acd04 commit c69f0e3

29 files changed

Lines changed: 357 additions & 1090 deletions

src/Schema/Builder.php

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace StellarWP\Schema;
44

55
use StellarWP\Schema\Config;
6-
use StellarWP\Schema\Fields;
7-
use StellarWP\Schema\Fields\Contracts\Schema_Interface as Field_Schema_Interface;
86
use StellarWP\Schema\Tables;
9-
use StellarWP\Schema\Tables\Contracts\Schema_Interface as Table_Schema_Interface;
7+
use StellarWP\Schema\Tables\Contracts\Table_Interface as Table_Schema_Interface;
108
use StellarWP\Schema\Tables\Filters\Group_FilterIterator;
119
use WP_CLI;
1210

@@ -37,7 +35,7 @@ public function __construct( $db = null, $container = null ) {
3735
}
3836

3937
/**
40-
* Whether all the custom tables exist or not. Does not check custom fields.
38+
* Whether all the custom tables exist or not.
4139
*
4240
* Note: the method will return `false` if even one table is missing.
4341
*
@@ -47,7 +45,7 @@ public function __construct( $db = null, $container = null ) {
4745
*
4846
* @param string|null $group An optional group name to restrict the check to.
4947
*
50-
* @return bool Whether all custom tables exist or not. Does not check custom fields.
48+
* @return bool Whether all custom tables exist or not.
5149
*/
5250
public function all_tables_exist( $group = null ) {
5351
$table_schemas = $this->get_registered_table_schemas();
@@ -64,7 +62,6 @@ public function all_tables_exist( $group = null ) {
6462
$result = $this->db::get_col( 'SHOW TABLES' );
6563
foreach ( $table_schemas as $table_schema ) {
6664
if ( ! in_array( $table_schema::table_name(), $result, true ) ) {
67-
6865
return false;
6966
}
7067
}
@@ -108,35 +105,6 @@ public function down() {
108105
* @since 1.0.0
109106
*/
110107
do_action( 'stellarwp_post_drop_tables' );
111-
112-
/**
113-
* Runs before the custom fields are dropped.
114-
*
115-
* @since 1.0.0
116-
*/
117-
do_action( 'stellarwp_pre_drop_fields' );
118-
119-
$field_schemas = $this->get_registered_field_schemas();
120-
121-
/**
122-
* Filters the fields to be dropped.
123-
*
124-
* @since 1.0.0
125-
*
126-
* @param \Iterator $field_classes A list of Field_Schema_Interface objects that will have their fields dropped.
127-
*/
128-
$field_schemas = apply_filters( 'stellarwp_fields_to_drop', $field_schemas );
129-
130-
foreach ( $field_schemas as $field_schema ) {
131-
$field_schema->drop();
132-
}
133-
134-
/**
135-
* Runs after the custom tables have been dropped by The Events Calendar.
136-
*
137-
* @since 1.0.0
138-
*/
139-
do_action( 'stellarwp_post_drop_fields' );
140108
}
141109

142110
/**
@@ -153,17 +121,6 @@ public function empty_custom_tables() {
153121
}
154122
}
155123

156-
/**
157-
* Get the registered field handlers.
158-
*
159-
* @since 1.0.0
160-
*
161-
* @return Fields\Collection
162-
*/
163-
public function get_registered_field_schemas(): Fields\Collection {
164-
return $this->container->get( Fields\Collection::class );
165-
}
166-
167124
/**
168125
* Get the md5 hash of all the registered schemas classes with their versions.
169126
*

src/Schema/Columns/Contracts/Auto_Incrementable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @package StellarWP\Schema\Columns\Contracts
2020
*/
21-
interface Auto_Incrementable {
21+
interface Auto_Incrementable extends Primarable {
2222
/**
2323
* Get the auto increment of the column.
2424
*

src/Schema/Columns/Contracts/Column.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function get_nullable(): bool {
136136
*
137137
* @return mixed The default value of the column.
138138
*/
139-
public function get_default(): mixed {
139+
public function get_default() {
140140
return $this->default;
141141
}
142142

@@ -255,9 +255,9 @@ public function set_on_update( ?string $on_update ): self {
255255
/**
256256
* Get the definition of the column.
257257
*
258-
* @return string The definition of the column.
258+
* @return array The definition of the column.
259259
*/
260-
public function get_definition(): string {
260+
public function get_definition(): array {
261261
$sql = "`{$this->get_name()}` {$this->get_type()}";
262262

263263
if ( $this instanceof Lengthable && $this instanceof Precisionable ) {
@@ -289,7 +289,20 @@ public function get_definition(): string {
289289
$sql .= ' ON UPDATE ' . $this->get_on_update();
290290
}
291291

292-
return $sql;
292+
$index_sql = '';
293+
294+
if ( $this->is_index() ) {
295+
if ( $this->is_primary_key() ) {
296+
$index_sql = 'PRIMARY KEY';
297+
} elseif ( $this->is_unique() ) {
298+
$index_sql = 'UNIQUE KEY `' . $this->get_name() . '`';
299+
} elseif ( $this->is_index() ) {
300+
$index_sql = 'INDEX `' . $this->get_name() . '`';
301+
}
302+
$index_sql = "{$index_sql} ({$this->get_name()})";
303+
}
304+
305+
return [ $sql, $index_sql ];
293306
}
294307

295308
/**

src/Schema/Columns/Contracts/Column_Interface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function get_nullable(): bool;
5454
*
5555
* @return mixed The default value of the column.
5656
*/
57-
public function get_default(): mixed;
57+
public function get_default();
5858

5959
/**
6060
* Get the on update value of the column.
@@ -66,9 +66,9 @@ public function get_on_update(): ?string;
6666
/**
6767
* Get the definition of the column.
6868
*
69-
* @return string The definition of the column.
69+
* @return array The definition of the column.
7070
*/
71-
public function get_definition(): string;
71+
public function get_definition(): array;
7272

7373
/**
7474
* Get whether the column is searchable.

src/Schema/Columns/Contracts/Datetime_Column.php renamed to src/Schema/Columns/Datetime_Column.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
*
55
* @since TBD
66
*
7-
* @package StellarWP\Schema\Columns\Contracts
7+
* @package StellarWP\Schema\Columns
88
*/
99

1010
declare( strict_types=1 );
1111

12-
namespace StellarWP\Schema\Columns\Contracts;
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\Contracts\Column;
1315

1416
/**
1517
* Class Datetime_Column
1618
*
1719
* @since TBD
1820
*
19-
* @package StellarWP\Schema\Columns\Contracts
21+
* @package StellarWP\Schema\Columns
2022
*/
21-
abstract class Datetime_Column extends Column {
23+
class Datetime_Column extends Column {
2224
/**
2325
* The type of the column.
2426
*

src/Schema/Columns/Contracts/Float_Column.php renamed to src/Schema/Columns/Float_Column.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@
44
*
55
* @since TBD
66
*
7-
* @package StellarWP\Schema\Columns\Contracts
7+
* @package StellarWP\Schema\Columns
88
*/
99

1010
declare( strict_types=1 );
1111

12-
namespace StellarWP\Schema\Columns\Contracts;
12+
namespace StellarWP\Schema\Columns;
1313

14-
abstract class Float_Column extends Column implements Lengthable, Signable, Precisionable, Uniquable {
14+
use StellarWP\Schema\Columns\Contracts\Column;
15+
use StellarWP\Schema\Columns\Contracts\Lengthable;
16+
use StellarWP\Schema\Columns\Contracts\Signable;
17+
use StellarWP\Schema\Columns\Contracts\Precisionable;
18+
use StellarWP\Schema\Columns\Contracts\Uniquable;
19+
20+
/**
21+
* Class Float_Column
22+
*
23+
* @since TBD
24+
*
25+
* @package StellarWP\Schema\Columns
26+
*/
27+
class Float_Column extends Column implements Lengthable, Signable, Precisionable, Uniquable {
1528
/**
1629
* The length of the column.
1730
*
@@ -36,9 +49,9 @@ abstract class Float_Column extends Column implements Lengthable, Signable, Prec
3649
/**
3750
* The default value of the column.
3851
*
39-
* @var int
52+
* @var float
4053
*/
41-
protected ?int $default = null;
54+
protected $default = null;
4255

4356
/**
4457
* The type of the column.

src/Schema/Columns/ID.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace StellarWP\Schema\Columns;
1313

14-
use StellarWP\Schema\Columns\Contracts\Integer_Column;
15-
1614
/**
1715
* Class ID
1816
*
@@ -41,4 +39,11 @@ class ID extends Integer_Column {
4139
* @var string
4240
*/
4341
protected string $name = 'id';
42+
43+
/**
44+
* Whether the column is a primary key.
45+
*
46+
* @var bool
47+
*/
48+
protected bool $is_primary_key = true;
4449
}

src/Schema/Columns/Contracts/Integer_Column.php renamed to src/Schema/Columns/Integer_Column.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@
44
*
55
* @since TBD
66
*
7-
* @package StellarWP\Schema\Columns\Contracts
7+
* @package StellarWP\Schema\Columns
88
*/
99

1010
declare( strict_types=1 );
1111

12-
namespace StellarWP\Schema\Columns\Contracts;
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\Contracts\Column;
15+
use StellarWP\Schema\Columns\Contracts\Lengthable;
16+
use StellarWP\Schema\Columns\Contracts\Signable;
17+
use StellarWP\Schema\Columns\Contracts\Auto_Incrementable;
18+
use StellarWP\Schema\Columns\Contracts\Uniquable;
1319

1420
/**
1521
* Class Integer_Column
1622
*
1723
* @since TBD
1824
*
19-
* @package StellarWP\Schema\Columns\Contracts
25+
* @package StellarWP\Schema\Columns
2026
*/
21-
abstract class Integer_Column extends Column implements Lengthable, Signable, Auto_Incrementable, Primarable, Uniquable {
27+
class Integer_Column extends Column implements Lengthable, Signable, Auto_Incrementable, Uniquable {
2228
/**
2329
* The length of the column.
2430
*
@@ -45,7 +51,7 @@ abstract class Integer_Column extends Column implements Lengthable, Signable, Au
4551
*
4652
* @var int
4753
*/
48-
protected ?int $default = null;
54+
protected $default = null;
4955

5056
/**
5157
* The type of the column.
@@ -97,6 +103,7 @@ public function get_length(): int {
97103
*/
98104
public function set_auto_increment( bool $auto_increment ): self {
99105
$this->auto_increment = $auto_increment;
106+
$this->set_is_primary_key( true );
100107
return $this;
101108
}
102109

src/Schema/Columns/Contracts/String_Column.php renamed to src/Schema/Columns/String_Column.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
*
55
* @since TBD
66
*
7-
* @package StellarWP\Schema\Columns\Contracts
7+
* @package StellarWP\Schema\Columns
88
*/
99

1010
declare( strict_types=1 );
1111

12-
namespace StellarWP\Schema\Columns\Contracts;
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\Contracts\Column;
15+
use StellarWP\Schema\Columns\Contracts\Lengthable;
16+
use StellarWP\Schema\Columns\Contracts\Uniquable;
1317

1418
/**
1519
* Class String_Column
1620
*
1721
* @since TBD
1822
*
19-
* @package StellarWP\Schema\Columns\Contracts
23+
* @package StellarWP\Schema\Columns
2024
*/
21-
abstract class String_Column extends Column implements Lengthable, Uniquable {
25+
class String_Column extends Column implements Lengthable, Uniquable {
2226
/**
2327
* The length of the column.
2428
*
@@ -31,7 +35,7 @@ abstract class String_Column extends Column implements Lengthable, Uniquable {
3135
*
3236
* @var string
3337
*/
34-
protected ?string $default = null;
38+
protected $default = null;
3539

3640
/**
3741
* The type of the column.

src/Schema/Columns/Contracts/Text_Column.php renamed to src/Schema/Columns/Text_Column.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
*
55
* @since TBD
66
*
7-
* @package StellarWP\Schema\Columns\Contracts
7+
* @package StellarWP\Schema\Columns
88
*/
99

1010
declare( strict_types=1 );
1111

12-
namespace StellarWP\Schema\Columns\Contracts;
12+
namespace StellarWP\Schema\Columns;
1313

14+
use StellarWP\Schema\Columns\Contracts\Column;
1415
use InvalidArgumentException;
1516

1617
/**
1718
* Class Text_Column
1819
*
1920
* @since TBD
2021
*
21-
* @package StellarWP\Schema\Columns\Contracts
22+
* @package StellarWP\Schema\Columns
2223
*/
23-
abstract class Text_Column extends Column {
24+
class Text_Column extends Column {
2425
/**
2526
* The type of the column.
2627
*

0 commit comments

Comments
 (0)