Skip to content

Commit 442be02

Browse files
committed
Fix tests
1 parent cf4aea7 commit 442be02

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,28 @@ public static function get_all_by( string $column, $value, string $operator = '=
708708
*
709709
* @since 3.0.0
710710
* @since 3.1.4 Enabled unfolding the value if is an array.
711+
* @since 3.1.4 Added the $operator parameter.
711712
*
712713
* @param string $column The column to get the model by.
713714
* @param mixed $value The value to get the model by.
715+
* @param string $operator The operator to use.
714716
*
715717
* @return ?mixed The model, or `null` if no model is found.
716718
*
717719
* @throws InvalidArgumentException If the column does not exist.
720+
* @throws InvalidArgumentException If the operator is invalid.
718721
*/
719-
public static function get_first_by( string $column, $value ) {
722+
public static function get_first_by( string $column, $value, string $operator = '=' ) {
720723
[ $value, $placeholder ] = self::prepare_value_for_query( $column, $value );
721724

725+
$operator = strtoupper( $operator );
726+
727+
if ( ! in_array( $operator, self::operators(), true ) ) {
728+
throw new InvalidArgumentException( "Invalid operator: {$operator}." );
729+
}
730+
722731
$database = Config::get_db();
723-
$data_array = static::fetch_first_where( $database::prepare( "WHERE %i = {$placeholder}", ...array_filter( [ $column, ...self::ensure_array( $value ) ], static fn( $v ) => null !== $v ) ), ARRAY_A );
732+
$data_array = static::fetch_first_where( $database::prepare( "WHERE %i {$operator} {$placeholder}", ...array_filter( [ $column, ...self::ensure_array( $value ) ], static fn( $v ) => null !== $v ) ), ARRAY_A );
724733

725734
if ( empty( $data_array[ static::uid_column() ] ) ) {
726735
return null;

tests/wpunit/Traits/Custom_Table_Query_MethodsTest.php

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@
1212
use StellarWP\Schema\Collections\Column_Collection;
1313
use StellarWP\Schema\Tables\Contracts\Table;
1414
use StellarWP\Schema\Tables\Table_Schema;
15+
use StellarWP\DB\DB;
1516

1617
class Custom_Table_Query_MethodsTest extends SchemaTestCase {
1718
use Table_Fixtures;
1819

1920
/**
2021
* @before
22+
* @after
2123
*/
2224
public function drop_tables() {
2325
$this->get_query_test_table()->drop();
2426
}
2527

2628
/**
27-
* Test that update_multiple handles array values correctly.
28-
*
29-
* This test covers the fix in commit d4aea4a that ensures array values
30-
* returned from prepare_value_for_query are properly unfolded when used
31-
* in database::prepare() calls.
32-
*
3329
* @test
3430
*/
3531
public function should_update_multiple_with_array_values() {
@@ -43,23 +39,27 @@ public function should_update_multiple_with_array_values() {
4339
'status' => 1,
4440
] );
4541

42+
$id1 = DB::last_insert_id();
43+
4644
$table::insert( [
4745
'name' => 'Test 2',
4846
'slug' => 'test-2',
4947
'status' => 1,
5048
] );
5149

50+
$id2 = DB::last_insert_id();
51+
5252
// Update multiple rows using array values
53-
$updated = $table::update_multiple( [
53+
$updated = $table::update_many( [
5454
[
55-
'slug' => 'test-1',
55+
'id' => $id1,
5656
'name' => 'Updated Test 1',
5757
],
5858
[
59-
'slug' => 'test-2',
59+
'id' => $id2,
6060
'name' => 'Updated Test 2',
6161
],
62-
], 'slug' );
62+
] );
6363

6464
$this->assertEquals( 2, $updated );
6565

@@ -72,42 +72,52 @@ public function should_update_multiple_with_array_values() {
7272
}
7373

7474
/**
75-
* Test that get_all_by handles array values correctly with IN operator.
76-
*
7775
* @test
7876
*/
7977
public function should_get_all_by_with_array_values() {
8078
$table = $this->get_query_test_table();
8179
Register::table( $table );
8280

8381
// Insert test data
84-
$id1 = $table::insert( [
82+
$table::insert( [
8583
'name' => 'Test 1',
8684
'slug' => 'test-1',
8785
'status' => 1,
8886
] );
8987

90-
$id2 = $table::insert( [
88+
$id1 = DB::last_insert_id();
89+
90+
$table::insert( [
9191
'name' => 'Test 2',
9292
'slug' => 'test-2',
9393
'status' => 1,
9494
] );
9595

96-
$id3 = $table::insert( [
96+
$id2 = DB::last_insert_id();
97+
98+
$table::insert( [
9799
'name' => 'Test 3',
98100
'slug' => 'test-3',
99101
'status' => 0,
100102
] );
101103

104+
$id3 = DB::last_insert_id();
105+
102106
// Get all by status using array (simulating IN operator scenario)
103-
$results = $table::get_all_by( 'status', 1 );
107+
$results = $table::get_all_by( 'status', [ 1, 0 ], 'IN' );
108+
109+
$this->assertCount( 3, $results );
104110

105-
$this->assertCount( 2, $results );
111+
$this->assertEquals( 'Test 1', $results[0]['name'] );
112+
$this->assertEquals( 'Test 2', $results[1]['name'] );
113+
$this->assertEquals( 'Test 3', $results[2]['name'] );
114+
115+
$this->assertEquals( 1, $results[0]['status'] );
116+
$this->assertEquals( 1, $results[1]['status'] );
117+
$this->assertEquals( 0, $results[2]['status'] );
106118
}
107119

108120
/**
109-
* Test that get_first_by handles array values correctly.
110-
*
111121
* @test
112122
*/
113123
public function should_get_first_by_with_array_values() {
@@ -128,15 +138,13 @@ public function should_get_first_by_with_array_values() {
128138
] );
129139

130140
// Get first by slug
131-
$result = $table::get_first_by( 'slug', 'first-match' );
141+
$result = $table::get_first_by( 'slug', [ 'second-match' ], 'NOT IN' );
132142

133143
$this->assertNotNull( $result );
134144
$this->assertEquals( 'First Match', $result['name'] );
135145
}
136146

137147
/**
138-
* Test that update_multiple handles integer array values.
139-
*
140148
* @test
141149
*/
142150
public function should_update_multiple_with_integer_array_values() {
@@ -150,13 +158,15 @@ public function should_update_multiple_with_integer_array_values() {
150158
'status' => 1,
151159
] );
152160

161+
$id1 = DB::last_insert_id();
162+
153163
// Update using integer value
154-
$updated = $table::update_multiple( [
164+
$updated = $table::update_many( [
155165
[
156-
'slug' => 'active-item',
166+
'id' => $id1,
157167
'status' => 0,
158168
],
159-
], 'slug' );
169+
] );
160170

161171
$this->assertEquals( 1, $updated );
162172

@@ -166,23 +176,21 @@ public function should_update_multiple_with_integer_array_values() {
166176
}
167177

168178
/**
169-
* Test that ensure_array helper works correctly with scalar values.
170-
*
171-
* This indirectly tests the ensure_array method through the public API.
172-
*
173179
* @test
174180
*/
175181
public function should_handle_scalar_values_in_queries() {
176182
$table = $this->get_query_test_table();
177183
Register::table( $table );
178184

179185
// Insert test data with scalar values
180-
$id = $table::insert( [
186+
$table::insert( [
181187
'name' => 'Scalar Test',
182188
'slug' => 'scalar-test',
183189
'status' => 1,
184190
] );
185191

192+
$id = DB::last_insert_id();
193+
186194
$this->assertIsInt( $id );
187195
$this->assertGreaterThan( 0, $id );
188196

@@ -210,7 +218,7 @@ public static function get_schema_history(): array {
210218
$columns[] = ( new ID( 'id' ) )->set_length( 11 )->set_type( Column_Types::INT );
211219
$columns[] = ( new String_Column( 'name' ) )->set_length( 255 );
212220
$columns[] = ( new String_Column( 'slug' ) )->set_length( 255 )->set_is_index( true );
213-
$columns[] = ( new Integer_Column( 'status' ) )->set_length( 1 )->set_default_value( 0 );
221+
$columns[] = ( new Integer_Column( 'status' ) )->set_length( 1 )->set_default( 0 );
214222

215223
return new Table_Schema( $table_name, $columns );
216224
};

0 commit comments

Comments
 (0)