|
| 1 | +<?php |
| 2 | + |
| 3 | +class WP_MySQL_Proxy_PDO_Test extends WP_MySQL_Proxy_Test { |
| 4 | + /** @var PDO */ |
| 5 | + private $pdo; |
| 6 | + |
| 7 | + public function setUp(): void { |
| 8 | + parent::setUp(); |
| 9 | + |
| 10 | + $this->pdo = new PDO( |
| 11 | + sprintf( 'mysql:host=127.0.0.1;port=%d', $this->port ), |
| 12 | + 'WordPress', |
| 13 | + 'WordPress' |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + public function test_exec(): void { |
| 18 | + $result = $this->pdo->exec( 'CREATE TABLE t (id INT PRIMARY KEY, name TEXT)' ); |
| 19 | + $this->assertEquals( 0, $result ); |
| 20 | + |
| 21 | + $result = $this->pdo->exec( 'INSERT INTO t (id, name) VALUES (123, "abc"), (456, "def")' ); |
| 22 | + $this->assertEquals( 2, $result ); |
| 23 | + } |
| 24 | + |
| 25 | + public function test_query(): void { |
| 26 | + $this->pdo->exec( 'CREATE TABLE t (id INT PRIMARY KEY, name TEXT)' ); |
| 27 | + $this->pdo->exec( 'INSERT INTO t (id, name) VALUES (123, "abc"), (456, "def")' ); |
| 28 | + |
| 29 | + $result = $this->pdo->query( "SELECT 'test'" ); |
| 30 | + $this->assertEquals( 'test', $result->fetchColumn() ); |
| 31 | + |
| 32 | + $result = $this->pdo->query( 'SELECT * FROM t' ); |
| 33 | + $this->assertEquals( 2, $result->rowCount() ); |
| 34 | + $this->assertEquals( |
| 35 | + array( |
| 36 | + array( |
| 37 | + 'id' => 123, |
| 38 | + 'name' => 'abc', |
| 39 | + ), |
| 40 | + array( |
| 41 | + 'id' => 456, |
| 42 | + 'name' => 'def', |
| 43 | + ), |
| 44 | + ), |
| 45 | + $result->fetchAll( PDO::FETCH_ASSOC ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_prepared_statement(): void { |
| 50 | + $this->pdo->exec( 'CREATE TABLE t (id INT PRIMARY KEY, name TEXT)' ); |
| 51 | + $this->pdo->exec( 'INSERT INTO t (id, name) VALUES (123, "abc"), (456, "def")' ); |
| 52 | + |
| 53 | + $stmt = $this->pdo->prepare( 'SELECT * FROM t WHERE id = ?' ); |
| 54 | + $stmt->execute( array( 123 ) ); |
| 55 | + $this->assertEquals( |
| 56 | + array( |
| 57 | + array( |
| 58 | + 'id' => 123, |
| 59 | + 'name' => 'abc', |
| 60 | + ), |
| 61 | + ), |
| 62 | + $stmt->fetchAll( PDO::FETCH_ASSOC ) |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
0 commit comments