Conversation
leeqvip
left a comment
There was a problem hiding this comment.
Please modify, do not change all lines in Test file.
| ]); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Please modify, do not change all lines
There was a problem hiding this comment.
This is due to different line endings (CRLF vs LF). I'm using WSL (Linux) so if CRLF is needed, I'd appreciate if someone else normalized the line endings.
I highly recommend adding a .gitattributes and normalizing all line endings to LF.
There was a problem hiding this comment.
For the sake of reviewing the actual changes, I've attached diffs without whitespace changes for both files below:
diff --git a/tests/AdapterTest.php b/tests/AdapterTest.php
index b27f34a..1b5eb2a 100644
--- a/tests/AdapterTest.php
+++ b/tests/AdapterTest.php
@@ -6,10 +6,10 @@ use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
+use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;
class AdapterTest extends TestCase
{
-
/**
* @throws Exception
*/
@@ -17,8 +17,7 @@ class AdapterTest extends TestCase
{
$this->initConfig();
$connConfig = new Configuration();
- $logger = new DebugStackLogger();
- $connConfig->setSQLLogger($logger);
+ $this->configureLogger($connConfig);
$conn = DriverManager::getConnection(
$this->config,
$connConfig
@@ -40,11 +39,31 @@ class AdapterTest extends TestCase
["p", "bob", "data", "write", "allow"]
], $oldRules);
- $stmt = $conn->createQueryBuilder()->from($adapter->policyTableName)->where('p_type = "p" and v1 = "data" and v3 = "allow"')->select("v0", "v1", "v2", "v3")->execute();
+ $query = $conn->createQueryBuilder()->from($adapter->policyTableName)->where('p_type = "p" and v1 = "data" and v3 = "allow"')->select("v0", "v1", "v2", "v3");
+ $stmt = method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
$result = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAllAssociative() : $stmt->fetchAll();
$result = array_map([$adapter, "filterRule"], $result);
$this->assertEquals($newPolicies, $result);
}
+
+ /**
+ *
+ * @param \Doctrine\DBAL\Configuration $connConfig
+ * @return void
+ */
+ private function configureLogger($connConfig)
+ {
+ // Doctrine < 4.0
+ if(method_exists($connConfig, "setSQLLogger")) {
+ $connConfig->setSQLLogger(new DebugStackLogger());
+ }
+ // Doctrine >= 4.0
+ else {
+ $connConfig->setMiddlewares([
+ new LoggingMiddleware(new PsrLogger())
+ ]);
+ }
+ }
}diff --git a/tests/TestCase.php b/tests/TestCase.php
index f5371fa..eeb21b7 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -32,7 +32,8 @@ class TestCase extends \PHPUnit\Framework\TestCase
$tableName = $adapter->policyTableName;
$conn = $adapter->getConnection();
$queryBuilder = $conn->createQueryBuilder();
- $queryBuilder->delete($tableName)->where('1 = 1')->execute();
+ $query = $queryBuilder->delete($tableName)->where('1 = 1');
+ method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
$data = [
['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'],
@@ -42,7 +43,8 @@ class TestCase extends \PHPUnit\Framework\TestCase
['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'],
];
foreach ($data as $row) {
- $queryBuilder->insert($tableName)->values(array_combine(array_keys($row), array_fill(0, count($row), '?')))->setParameters(array_values($row))->execute();
+ $query = $queryBuilder->insert($tableName)->values(array_combine(array_keys($row), array_fill(0, count($row), '?')))->setParameters(array_values($row));
+ method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
}
}|
🎉 This issue has been resolved in version 2.4.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
|
Thank you! Just as a reminder, the files in the repository have mixed line endings (this was already a case for some files before this PR), so if anyone else submits PRs from non-Windows platforms, the diffs might not display correctly. As I mentioned before, I suggest normalizing everything to |
Fixes adapter not working in Doctrine 4. I only tested this in PHP 8.3 and Doctrine 4 (using in-memory SQLite DB instead of MySQL) so feel free to test on other PHP and Doctrine versions as well.
Fixes #12.