Repository navigation
Report zero-row statements as successful in Connection::safeQuery() #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/unit/xoopsLib/Xoops/Core/Database/ConnectionSafeQueryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
| require_once(__DIR__ . '/../../../../init_new.php'); | ||
|
Check failure on line 2 in tests/unit/xoopsLib/Xoops/Core/Database/ConnectionSafeQueryTest.php
|
||
|
|
||
| use Doctrine\DBAL\Configuration; | ||
| use Doctrine\DBAL\Driver\PDO\SQLite\Driver; | ||
| use Doctrine\DBAL\Result; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
| use Xoops\Core\Database\Connection; | ||
|
|
||
| /** | ||
| * safeQuery() return values, against an in-memory SQLite database | ||
| */ | ||
| #[CoversClass(Connection::class)] | ||
| #[RequiresPhpExtension('pdo_sqlite')] | ||
| class ConnectionSafeQueryTest extends \PHPUnit\Framework\TestCase | ||
| { | ||
| /** @var Connection */ | ||
| private $db; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->db = new Connection(['driver' => 'pdo_sqlite', 'memory' => true], new Driver(), new Configuration()); | ||
| $this->db->setSafe(true); | ||
| } | ||
|
|
||
| public function testDdlThatChangesNoRowsReportsSuccess(): void | ||
| { | ||
| $this->assertTrue($this->db->query('CREATE TABLE items (id INTEGER, title TEXT)')); | ||
| } | ||
|
|
||
| public function testWriteReturnsAffectedRowCount(): void | ||
| { | ||
| $this->db->query('CREATE TABLE items (id INTEGER, title TEXT)'); | ||
|
|
||
| $this->assertSame(2, $this->db->query("INSERT INTO items VALUES (1, 'a'), (2, 'b')")); | ||
| $this->assertSame(1, $this->db->query("UPDATE items SET title = 'c' WHERE id = 2")); | ||
| } | ||
|
|
||
| public function testWriteMatchingNoRowsReportsSuccess(): void | ||
| { | ||
| $this->db->query('CREATE TABLE items (id INTEGER, title TEXT)'); | ||
|
|
||
| $this->assertTrue($this->db->query("UPDATE items SET title = 'x' WHERE id = 99")); | ||
| $this->assertTrue($this->db->safeQuery('DELETE FROM items')); | ||
| } | ||
|
|
||
| public function testSelectReturnsResult(): void | ||
| { | ||
| $this->db->query('CREATE TABLE items (id INTEGER)'); | ||
|
|
||
| $result = $this->db->query('SELECT * FROM items'); | ||
|
|
||
| $this->assertInstanceOf(Result::class, $result); | ||
| $this->assertFalse($result->fetchAssociative()); | ||
| } | ||
|
|
||
| public function testFailedStatementReturnsNull(): void | ||
| { | ||
| $this->assertNull($this->db->query('CREATE TABLE broken (')); | ||
| $this->assertNull($this->db->query('SELECT * FROM missing_table')); | ||
| } | ||
|
|
||
| public function testTablesExecuteQueueCreatesTableThroughConnection(): void | ||
| { | ||
| // SQLite has no INFORMATION_SCHEMA, so seed the definition addTable() would have built | ||
| $tables = (new \ReflectionClass(\Xmf\Database\Tables::class))->newInstanceWithoutConstructor(); | ||
| $state = [ | ||
| 'db' => $this->db, | ||
| 'tables' => ['fresh' => [ | ||
| 'name' => 'x_fresh', | ||
| 'options' => '', | ||
| 'columns' => [['name' => 'id', 'attributes' => 'INTEGER NOT NULL']], | ||
| 'keys' => ['PRIMARY' => ['columns' => '`id`', 'unique' => true]], | ||
| 'create' => true, | ||
| ]], | ||
| 'queue' => [['createtable' => 'fresh']], | ||
| ]; | ||
| foreach ($state as $name => $value) { | ||
| (new \ReflectionProperty(\Xmf\Database\Tables::class, $name))->setValue($tables, $value); | ||
| } | ||
|
|
||
| $this->assertTrue($tables->executeQueue(true)); | ||
| $this->assertSame( | ||
| 'x_fresh', | ||
| $this->db->query("SELECT name FROM sqlite_master WHERE name = 'x_fresh'")->fetchOne() | ||
| ); | ||
| } | ||
|
|
||
| public function testWriteIsRefusedWhenNotSafe(): void | ||
| { | ||
| $this->db->query('CREATE TABLE items (id INTEGER)'); | ||
| $this->db->setSafe(false); | ||
|
|
||
| $this->assertNull($this->db->query('INSERT INTO items VALUES (1)')); | ||
| $this->assertInstanceOf(Result::class, $this->db->query('SELECT * FROM items')); | ||
|
|
||
| $this->db->setForce(true); | ||
| $this->assertSame(1, $this->db->query('INSERT INTO items VALUES (1)')); | ||
| $this->assertFalse($this->db->getForce(), 'force applies to one statement'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.