Skip to content

Commit 2a5f8a8

Browse files
committed
Implement WP_PDO_Proxy_Statement::fetchObject()
1 parent 8d694f2 commit 2a5f8a8

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
use PHPUnit\Framework\TestCase;
44

5+
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
6+
class FetchObjectTestClass {
7+
public $col1;
8+
public $col2;
9+
public $col3;
10+
public $arg1;
11+
public $arg2;
12+
13+
public function __construct( $arg1 = null, $arg2 = null ) {
14+
$this->arg1 = $arg1;
15+
$this->arg2 = $arg2;
16+
}
17+
}
18+
519
class WP_PDO_MySQL_On_SQLite_PDO_API_Tests extends TestCase {
620
/** @var WP_PDO_MySQL_On_SQLite */
721
private $driver;
@@ -388,6 +402,42 @@ public function test_fetch_column_negative_index(): void {
388402
$stmt->fetchColumn( -1 );
389403
}
390404

405+
public function test_fetch_obj(): void {
406+
// No arguments (stdClass).
407+
$stmt = $this->driver->query( "SELECT 1, 'abc', true" );
408+
$this->assertEquals(
409+
(object) array(
410+
1 => '1',
411+
'abc' => 'abc',
412+
'true' => true,
413+
),
414+
$stmt->fetchObject()
415+
);
416+
$this->assertFalse( $stmt->fetchObject() );
417+
418+
// Custom class.
419+
$stmt = $this->driver->query( "SELECT 1 AS col1, 'abc' AS col2, true AS col3" );
420+
$result = $stmt->fetchObject( FetchObjectTestClass::class );
421+
$this->assertInstanceOf( FetchObjectTestClass::class, $result );
422+
$this->assertSame( '1', $result->col1 );
423+
$this->assertSame( 'abc', $result->col2 );
424+
$this->assertSame( '1', $result->col3 );
425+
$this->assertNull( $result->arg1 );
426+
$this->assertNull( $result->arg2 );
427+
$this->assertFalse( $stmt->fetchObject( FetchObjectTestClass::class ) );
428+
429+
// Custom class with constructor arguments.
430+
$stmt = $this->driver->query( "SELECT 1 AS col1, 'abc' AS col2, true AS col3" );
431+
$result = $stmt->fetchObject( FetchObjectTestClass::class, array( 'val1', 'val2' ) );
432+
$this->assertInstanceOf( FetchObjectTestClass::class, $result );
433+
$this->assertSame( '1', $result->col1 );
434+
$this->assertSame( 'abc', $result->col2 );
435+
$this->assertSame( '1', $result->col3 );
436+
$this->assertSame( 'val1', $result->arg1 );
437+
$this->assertSame( 'val2', $result->arg2 );
438+
$this->assertFalse( $stmt->fetchObject( FetchObjectTestClass::class, array( 'val1', 'val2' ) ) );
439+
}
440+
391441
public function test_attr_default_fetch_mode(): void {
392442
$this->driver->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM );
393443
$result = $this->driver->query( "SELECT 'a', 'b', 'c'" );

‎wp-includes/sqlite-ast/class-wp-pdo-proxy-statement.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function fetchColumn( $column = 0 ) {
208208
*/
209209
#[ReturnTypeWillChange]
210210
public function fetchObject( $class = 'stdClass', $constructorArgs = array() ) {
211-
throw new RuntimeException( 'Not implemented' );
211+
return $this->statement->fetchObject( $class, $constructorArgs );
212212
}
213213

214214
/**

0 commit comments

Comments
 (0)