1212use phpMyFAQ \Language ;
1313use PHPUnit \Framework \Attributes \AllowMockObjectsWithoutExpectations ;
1414use PHPUnit \Framework \TestCase ;
15+ use ReflectionClass ;
1516use Symfony \Component \HttpFoundation \Session \Session ;
1617
1718#[AllowMockObjectsWithoutExpectations]
1819class CommentsRepositoryTest extends TestCase
1920{
2021 private Configuration $ configuration ;
2122 private CommentsRepository $ repository ;
23+ private string $ databasePath ;
24+ private Sqlite3 $ dbHandle ;
25+ private ?Configuration $ previousConfiguration = null ;
2226
2327 protected function setUp (): void
2428 {
2529 parent ::setUp ();
2630
27- $ dbHandle = new Sqlite3 ();
28- $ dbHandle ->connect (PMF_TEST_DIR . '/test.db ' , '' , '' );
29- $ this ->configuration = new Configuration ($ dbHandle );
31+ $ configurationReflection = new ReflectionClass (Configuration::class);
32+ $ configurationProperty = $ configurationReflection ->getProperty ('configuration ' );
33+ $ this ->previousConfiguration = $ configurationProperty ->getValue ();
34+
35+ $ databasePath = tempnam (sys_get_temp_dir (), 'pmf-comments-repository- ' );
36+ self ::assertNotFalse ($ databasePath );
37+ self ::assertTrue (copy (PMF_TEST_DIR . '/test.db ' , $ databasePath ));
38+ $ this ->databasePath = $ databasePath ;
39+
40+ $ this ->dbHandle = new Sqlite3 ();
41+ $ this ->dbHandle ->connect ($ this ->databasePath , '' , '' );
42+ $ this ->configuration = new Configuration ($ this ->dbHandle );
43+ $ this ->initializeDatabaseStatics ($ this ->dbHandle );
3044 $ language = new Language ($ this ->configuration , $ this ->createStub (Session::class));
3145 $ language ->setLanguageFromConfiguration ('en ' );
3246 $ this ->configuration ->setLanguage ($ language );
@@ -36,14 +50,19 @@ protected function setUp(): void
3650
3751 protected function tearDown (): void
3852 {
53+ $ configurationReflection = new ReflectionClass (Configuration::class);
54+ $ configurationProperty = $ configurationReflection ->getProperty ('configuration ' );
55+ $ configurationProperty ->setValue (null , $ this ->previousConfiguration );
56+
57+ if (isset ($ this ->dbHandle )) {
58+ $ this ->dbHandle ->close ();
59+ }
60+
61+ if (isset ($ this ->databasePath ) && is_file ($ this ->databasePath )) {
62+ unlink ($ this ->databasePath );
63+ }
64+
3965 parent ::tearDown ();
40- // Best-effort cleanup: remove comment with id_comment = 1 if exists (first insert in clean DB)
41- $ this ->repository ->deleteByTypeAndId (CommentType::FAQ , 1 );
42- // Remove potential category relation
43- $ category = new Category ($ this ->configuration );
44- $ category ->setLanguage ('en ' );
45- $ relation = new Relation ($ this ->configuration , $ category );
46- $ relation ->delete (1 , 'en ' );
4766 }
4867
4968 private function makeComment (): CommentEntity
@@ -147,4 +166,17 @@ public function testIsCommentAllowed(): void
147166
148167 $ this ->assertTrue ($ this ->repository ->isCommentAllowed (1 , 'en ' , CommentType::FAQ ));
149168 }
169+
170+ private function initializeDatabaseStatics (Sqlite3 $ dbHandle ): void
171+ {
172+ $ databaseReflection = new ReflectionClass (Database::class);
173+
174+ $ databaseDriverProperty = $ databaseReflection ->getProperty ('databaseDriver ' );
175+ $ databaseDriverProperty ->setValue (null , $ dbHandle );
176+
177+ $ dbTypeProperty = $ databaseReflection ->getProperty ('dbType ' );
178+ $ dbTypeProperty ->setValue (null , 'sqlite3 ' );
179+
180+ Database::setTablePrefix ('' );
181+ }
150182}
0 commit comments