-
Notifications
You must be signed in to change notification settings - Fork 2
Fix/ensuring array values can be used #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d4aea4a
Ensures array values can be used
dpanta94 b64dbe0
Ading test coverage
dpanta94 3b3cc7e
Fixing some variable names
dpanta94 cf4aea7
added changelog
dpanta94 442be02
Fix tests
dpanta94 20a6443
Update CHANGELOG.md
dpanta94 9db4cd0
amend changelog entries
dpanta94 f6020f3
add test case for empty array
dpanta94 e530895
trying to handle empty arrays
dpanta94 205e138
amend docblock to fix static analysis
dpanta94 a3a970b
Ensuring expected variable type
dpanta94 1103b8d
amend value types
dpanta94 d01b131
include nulls
dpanta94 88069c9
cr amends
dpanta94 b3c191e
CR amends
dpanta94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| <?php | ||
|
|
||
| namespace StellarWP\Schema\Tests\Traits; | ||
|
|
||
| use StellarWP\Schema\Register; | ||
| use StellarWP\Schema\Tests\SchemaTestCase; | ||
| use StellarWP\Schema\Tests\Traits\Table_Fixtures; | ||
| use StellarWP\Schema\Columns\Integer_Column; | ||
| use StellarWP\Schema\Columns\String_Column; | ||
| use StellarWP\Schema\Columns\ID; | ||
| use StellarWP\Schema\Columns\Column_Types; | ||
| use StellarWP\Schema\Collections\Column_Collection; | ||
| use StellarWP\Schema\Tables\Contracts\Table; | ||
| use StellarWP\Schema\Tables\Table_Schema; | ||
| use StellarWP\DB\DB; | ||
|
|
||
| class Custom_Table_Query_MethodsTest extends SchemaTestCase { | ||
| use Table_Fixtures; | ||
|
|
||
| /** | ||
| * @before | ||
| * @after | ||
| */ | ||
| public function drop_tables() { | ||
| $this->get_query_test_table()->drop(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function should_update_multiple_with_array_values() { | ||
| $table = $this->get_query_test_table(); | ||
| Register::table( $table ); | ||
|
|
||
| // Insert test data | ||
| $table::insert( [ | ||
| 'name' => 'Test 1', | ||
| 'slug' => 'test-1', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id1 = DB::last_insert_id(); | ||
|
|
||
| $table::insert( [ | ||
| 'name' => 'Test 2', | ||
| 'slug' => 'test-2', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id2 = DB::last_insert_id(); | ||
|
|
||
| // Update multiple rows using array values | ||
| $updated = $table::update_many( [ | ||
| [ | ||
| 'id' => $id1, | ||
| 'name' => 'Updated Test 1', | ||
| ], | ||
| [ | ||
| 'id' => $id2, | ||
| 'name' => 'Updated Test 2', | ||
| ], | ||
| ] ); | ||
|
|
||
| $this->assertEquals( 2, $updated ); | ||
|
|
||
| // Verify the updates | ||
| $result1 = $table::get_first_by( 'slug', 'test-1' ); | ||
| $this->assertEquals( 'Updated Test 1', $result1['name'] ); | ||
|
|
||
| $result2 = $table::get_first_by( 'slug', 'test-2' ); | ||
| $this->assertEquals( 'Updated Test 2', $result2['name'] ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function should_get_all_by_with_array_values() { | ||
| $table = $this->get_query_test_table(); | ||
| Register::table( $table ); | ||
|
|
||
| // Insert test data | ||
| $table::insert( [ | ||
| 'name' => 'Test 1', | ||
| 'slug' => 'test-1', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id1 = DB::last_insert_id(); | ||
|
|
||
| $table::insert( [ | ||
| 'name' => 'Test 2', | ||
| 'slug' => 'test-2', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id2 = DB::last_insert_id(); | ||
|
|
||
| $table::insert( [ | ||
| 'name' => 'Test 3', | ||
| 'slug' => 'test-3', | ||
| 'status' => 0, | ||
| ] ); | ||
|
|
||
| $id3 = DB::last_insert_id(); | ||
|
|
||
| // Get all by status using array (simulating IN operator scenario) | ||
| $results = $table::get_all_by( 'status', [ 1, 0 ], 'IN' ); | ||
|
|
||
| $this->assertCount( 3, $results ); | ||
|
|
||
| $this->assertEquals( 'Test 1', $results[0]['name'] ); | ||
| $this->assertEquals( 'Test 2', $results[1]['name'] ); | ||
| $this->assertEquals( 'Test 3', $results[2]['name'] ); | ||
|
|
||
| $this->assertEquals( 1, $results[0]['status'] ); | ||
| $this->assertEquals( 1, $results[1]['status'] ); | ||
| $this->assertEquals( 0, $results[2]['status'] ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function should_get_first_by_with_array_values() { | ||
| $table = $this->get_query_test_table(); | ||
| Register::table( $table ); | ||
|
|
||
| // Insert test data | ||
| $table::insert( [ | ||
| 'name' => 'First Match', | ||
| 'slug' => 'first-match', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $table::insert( [ | ||
| 'name' => 'Second Match', | ||
| 'slug' => 'second-match', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| // Get first by slug | ||
| $result = $table::get_first_by( 'slug', [ 'second-match' ], 'NOT IN' ); | ||
|
|
||
| $this->assertNotNull( $result ); | ||
| $this->assertEquals( 'First Match', $result['name'] ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function should_update_multiple_with_integer_array_values() { | ||
| $table = $this->get_query_test_table(); | ||
| Register::table( $table ); | ||
|
|
||
| // Insert test data | ||
| $table::insert( [ | ||
| 'name' => 'Active Item', | ||
| 'slug' => 'active-item', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id1 = DB::last_insert_id(); | ||
|
|
||
| // Update using integer value | ||
| $updated = $table::update_many( [ | ||
| [ | ||
| 'id' => $id1, | ||
| 'status' => 0, | ||
| ], | ||
| ] ); | ||
|
|
||
| $this->assertEquals( 1, $updated ); | ||
|
|
||
| // Verify the update | ||
| $result = $table::get_first_by( 'slug', 'active-item' ); | ||
| $this->assertEquals( 0, $result['status'] ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function should_handle_scalar_values_in_queries() { | ||
| $table = $this->get_query_test_table(); | ||
| Register::table( $table ); | ||
|
|
||
| // Insert test data with scalar values | ||
| $table::insert( [ | ||
| 'name' => 'Scalar Test', | ||
| 'slug' => 'scalar-test', | ||
| 'status' => 1, | ||
| ] ); | ||
|
|
||
| $id = DB::last_insert_id(); | ||
|
|
||
| $this->assertIsInt( $id ); | ||
| $this->assertGreaterThan( 0, $id ); | ||
|
|
||
| // Verify scalar value retrieval | ||
| $result = $table::get_first_by( 'id', $id ); | ||
| $this->assertEquals( 'Scalar Test', $result['name'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Get a test table for query method testing. | ||
| */ | ||
| private function get_query_test_table() { | ||
| return new class extends Table { | ||
| const SCHEMA_VERSION = '1.0.0'; | ||
|
|
||
| protected static $base_table_name = 'query_test'; | ||
| protected static $group = 'test'; | ||
| protected static $schema_slug = 'test-query'; | ||
|
|
||
| public static function get_schema_history(): array { | ||
| $table_name = static::table_name( true ); | ||
| $callable = function() use ( $table_name ) { | ||
| $columns = new Column_Collection(); | ||
|
|
||
| $columns[] = ( new ID( 'id' ) )->set_length( 11 )->set_type( Column_Types::INT ); | ||
| $columns[] = ( new String_Column( 'name' ) )->set_length( 255 ); | ||
| $columns[] = ( new String_Column( 'slug' ) )->set_length( 255 )->set_is_index( true ); | ||
| $columns[] = ( new Integer_Column( 'status' ) )->set_length( 1 )->set_default( 0 ); | ||
|
|
||
| return new Table_Schema( $table_name, $columns ); | ||
| }; | ||
|
|
||
| return [ | ||
| static::SCHEMA_VERSION => $callable, | ||
| ]; | ||
| } | ||
|
|
||
| public static function transform_from_array( array $result_array ) { | ||
| return $result_array; | ||
| } | ||
| }; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.