Skip to content
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

Doctrine 4 fix #13

Merged
merged 2 commits into from
Jul 22, 2024
Merged

Doctrine 4 fix #13

merged 2 commits into from
Jul 22, 2024

Conversation

Cryszon
Copy link
Contributor

@Cryszon Cryszon commented Jul 19, 2024

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.

Copy link
Member

@leeqvip leeqvip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify, do not change all lines in Test file.

]);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify, do not change all lines

Copy link
Contributor Author

@Cryszon Cryszon Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
         }
     }

@leeqvip leeqvip merged commit 0408d3d into php-casbin:master Jul 22, 2024
11 checks passed
@leeqvip
Copy link
Member

leeqvip commented Jul 22, 2024

🎉 This issue has been resolved in version 2.4.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@Cryszon
Copy link
Contributor Author

Cryszon commented Jul 22, 2024

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 LF, but that's a housekeeping task that doesn't affect functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doesn't work in Doctrine 4.0 and broken automated checks
2 participants