forked from propelorm/Propel2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request propelorm#1728 from mringler/bugfix/failed_queries…
…_should_be_written_to_log Bugfix: Failed queries should be written to log & Fix PdoConnection::exec()
- Loading branch information
Showing
7 changed files
with
246 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
107 changes: 107 additions & 0 deletions
107
tests/Propel/Tests/Runtime/Connection/ConnectionWrapperTest.php
This file contains 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,107 @@ | ||
<?php | ||
|
||
/** | ||
* MIT License. This file is part of the Propel package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Propel\Tests\Runtime\Connection; | ||
|
||
use Exception; | ||
use Propel\Runtime\Connection\ConnectionWrapper; | ||
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase; | ||
|
||
/** | ||
* @group database | ||
*/ | ||
class ConnectionWrapperTest extends BookstoreTestBase | ||
{ | ||
/** | ||
* Make sure logging is done after execution, otherwise Profiler will give wrong data. | ||
* | ||
* @return void | ||
*/ | ||
public function testQueriesAreLoggedAfterExecution() | ||
{ | ||
$wrapper = new class ($this->con) extends ConnectionWrapper{ | ||
public $orderStack = []; | ||
|
||
public function pushToOrderStack(string $op) | ||
{ | ||
$this->orderStack[] = $op; | ||
} | ||
|
||
public function log($msg) | ||
{ | ||
$this->pushToOrderStack('log'); | ||
} | ||
}; | ||
$wrapper->callUserFunctionWithLogging([$wrapper, 'pushToOrderStack'], ['execute'], ''); | ||
$this->assertEquals(['execute', 'log'], $wrapper->orderStack, 'Execute should run before logging'); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testQueryIsUnaffectedByDebugMode() | ||
{ | ||
$con = new ConnectionWrapper($this->con->getWrappedConnection()); | ||
|
||
$query = 'SELECT * FROM book'; | ||
|
||
$con->useDebug(false); | ||
$resultWithoutDebug = null; | ||
try { | ||
$resultWithoutDebug = $con->query($query)->fetch(); | ||
} catch (Exception $e) { | ||
$this->fail('Could not execute query with debug mode DISABLED: ' . $e->getMessage()); | ||
} | ||
|
||
$con->useDebug(true); | ||
$resultWithDebug = null; | ||
try { | ||
$resultWithDebug = $con->query($query)->fetch(); | ||
} catch (Exception $e) { | ||
$this->fail('Could not execute query with debug mode ENABLED: ' . $e->getMessage()); | ||
} | ||
|
||
$this->assertEquals($resultWithoutDebug, $resultWithDebug); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testExecuteRunsInDebugMode() | ||
{ | ||
$this->assertExecSimpleInsertWithGivenDebugModeWorks('ENABLED', true); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testExecuteRunsWithoutDebugMode() | ||
{ | ||
$this->assertExecSimpleInsertWithGivenDebugModeWorks('DISABLED', false); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function assertExecSimpleInsertWithGivenDebugModeWorks(string $description, bool $debugMode): void | ||
{ | ||
$con = new ConnectionWrapper($this->con->getWrappedConnection()); | ||
|
||
$query = "INSERT INTO publisher(name) VALUES('Le Publisher')"; | ||
|
||
$con->useDebug($debugMode); | ||
$affectedRows = -1; | ||
try { | ||
$affectedRows = $con->exec($query); | ||
} catch (Exception $e) { | ||
$this->fail("Could not execute query with debug mode $description: " . $e->getMessage()); | ||
} | ||
|
||
$this->assertEquals(1, $affectedRows, "ConnectionWrapper::exec() should have inserted one rows with $description debug mode"); | ||
} | ||
} |
This file contains 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
55 changes: 55 additions & 0 deletions
55
tests/Propel/Tests/Runtime/Connection/StatementWrapperTest.php
This file contains 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,55 @@ | ||
<?php | ||
|
||
/** | ||
* MIT License. This file is part of the Propel package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Propel\Tests\Runtime\Connection; | ||
|
||
use Propel\Runtime\Connection\ConnectionWrapper; | ||
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase; | ||
use Exception; | ||
|
||
/** | ||
* @group database | ||
*/ | ||
class StatementWrapperTest extends BookstoreTestBase | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function testExecuteRunsInDebugMode() | ||
{ | ||
$exitCode = $this->executeSimpleQueryWithGivenDebugMode('ENABLED', true); | ||
$this->assertTrue($exitCode); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testExecuteRunsWithoutDebugMode() | ||
{ | ||
$exitCode = $this->executeSimpleQueryWithGivenDebugMode('DISABLED', false); | ||
$this->assertTrue($exitCode); | ||
} | ||
|
||
/** | ||
* | ||
* @return void | ||
*/ | ||
private function executeSimpleQueryWithGivenDebugMode(string $description, bool $debugMode): bool | ||
{ | ||
$query = 'SELECT * FROM book'; | ||
$wrapper = new ConnectionWrapper($this->con->getWrappedConnection()); | ||
$wrapper->useDebug($debugMode); | ||
|
||
$stmt = $wrapper->prepare($query); | ||
try{ | ||
return $stmt->execute(); | ||
} catch (Exception $e) { | ||
$this->fail("Could not run query with debug mode $description: " . $e->getMessage()); | ||
} | ||
} | ||
} |