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