|
2 | 2 |
|
3 | 3 | use PHPUnit\Framework\TestCase; |
4 | 4 |
|
| 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 | + |
5 | 19 | class WP_PDO_MySQL_On_SQLite_PDO_API_Tests extends TestCase { |
6 | 20 | /** @var WP_PDO_MySQL_On_SQLite */ |
7 | 21 | private $driver; |
@@ -388,6 +402,42 @@ public function test_fetch_column_negative_index(): void { |
388 | 402 | $stmt->fetchColumn( -1 ); |
389 | 403 | } |
390 | 404 |
|
| 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 | + |
391 | 441 | public function test_attr_default_fetch_mode(): void { |
392 | 442 | $this->driver->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM ); |
393 | 443 | $result = $this->driver->query( "SELECT 'a', 'b', 'c'" ); |
|
0 commit comments