|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | +use WP_MySQL_Proxy\Adapter\SQLite_Adapter; |
| 5 | +use WP_MySQL_Proxy\MySQL_Authentication; |
| 6 | +use WP_MySQL_Proxy\MySQL_Protocol; |
| 7 | +use WP_MySQL_Proxy\MySQL_Session; |
| 8 | + |
| 9 | +class WP_MySQL_SQLite_Adapter_Test extends TestCase { |
| 10 | + public function test_query_error_is_reported(): void { |
| 11 | + $result = ( new SQLite_Adapter( ':memory:' ) )->handle_query( 'SELECT * FROM missing_table' ); |
| 12 | + $this->assertSame( array( '42S02', 1146, "Table 'missing_table' doesn't exist" ), $result->error_info ); |
| 13 | + } |
| 14 | + |
| 15 | + public function test_driver_message_is_reported(): void { |
| 16 | + $cause = new RuntimeException( 'Unsupported ALTER TABLE ADD action: X' ); |
| 17 | + $result = $this->adapter_failing_with( $cause )->handle_query( 'SELECT 1' ); |
| 18 | + $this->assertSame( array( 'HY000', 1105, 'Unsupported ALTER TABLE ADD action: X' ), $result->error_info ); |
| 19 | + } |
| 20 | + |
| 21 | + /** @dataProvider internal_errors */ |
| 22 | + public function test_internal_error_is_not_reported( Throwable $cause ): void { |
| 23 | + $this->expectException( WP_MySQL_On_SQLite_Exception::class ); |
| 24 | + $this->adapter_failing_with( $cause )->handle_query( 'SELECT 1' ); |
| 25 | + } |
| 26 | + |
| 27 | + public function internal_errors(): array { |
| 28 | + return array( |
| 29 | + 'PHP error' => array( new TypeError( 'Private detail from /srv/secret/path.php' ) ), |
| 30 | + 'wrapped PHP error' => array( new RuntimeException( 'Wrapper', 0, new TypeError( 'Private detail' ) ) ), |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_internal_error_does_not_reach_the_client(): void { |
| 35 | + $cause = new TypeError( 'f(): Argument #1 must be of type string, called in /srv/secret/path.php on line 1' ); |
| 36 | + $session = new MySQL_Session( $this->adapter_failing_with( $cause ), 1, new MySQL_Authentication( 'user', 'password' ) ); |
| 37 | + $challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() ); |
| 38 | + $session->receive_bytes( MySQL_Test_Client::handshake_response( $challenge ) ); |
| 39 | + $this->assertTrue( $session->is_authenticated() ); |
| 40 | + |
| 41 | + // The detail goes to the log, which the test runner shows on stderr. |
| 42 | + $response = $session->receive_bytes( MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_QUERY ) . 'SELECT 1' ) ); |
| 43 | + $this->assertSame( 1105, unpack( 'v', substr( $response, 5, 2 ) )[1] ); |
| 44 | + $this->assertSame( 'Unknown error', substr( $response, 13 ) ); |
| 45 | + $this->assertStringNotContainsString( '/srv/secret/path.php', $response ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * An adapter whose driver fails a query the way the driver reports any error. |
| 50 | + */ |
| 51 | + private function adapter_failing_with( Throwable $cause ): SQLite_Adapter { |
| 52 | + $adapter = new SQLite_Adapter( ':memory:' ); |
| 53 | + $property = new ReflectionProperty( SQLite_Adapter::class, 'sqlite_driver' ); |
| 54 | + if ( PHP_VERSION_ID < 80100 ) { |
| 55 | + $property->setAccessible( true ); |
| 56 | + } |
| 57 | + $exception = new WP_MySQL_On_SQLite_Exception( $property->getValue( $adapter ), $cause->getMessage(), $cause->getCode(), $cause ); |
| 58 | + $property->setValue( |
| 59 | + $adapter, |
| 60 | + new class( $exception ) { |
| 61 | + private $exception; |
| 62 | + |
| 63 | + public function __construct( Throwable $exception ) { |
| 64 | + $this->exception = $exception; |
| 65 | + } |
| 66 | + |
| 67 | + public function query(): void { |
| 68 | + throw $this->exception; |
| 69 | + } |
| 70 | + } |
| 71 | + ); |
| 72 | + return $adapter; |
| 73 | + } |
| 74 | +} |
0 commit comments