diff --git a/phpmyfaq/admin/api/index.php b/phpmyfaq/admin/api/index.php index faae54665a..5a4a1aa36a 100644 --- a/phpmyfaq/admin/api/index.php +++ b/phpmyfaq/admin/api/index.php @@ -17,15 +17,26 @@ */ use phpMyFAQ\Application; -use phpMyFAQ\Configuration; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; require '../../src/Bootstrap.php'; -$faqConfig = Configuration::getConfigurationInstance(); +// +// Service Containers +// +$container = new ContainerBuilder(); +$loader = new PhpFileLoader($container, new FileLocator(__DIR__)); +try { + $loader->load('../../src/services.php'); +} catch (\Exception $e) { + echo $e->getMessage(); +} $routes = include PMF_SRC_DIR . '/admin-routes.php'; -$app = new Application($faqConfig); +$app = new Application($container); try { $app->run($routes); } catch (Exception $exception) { diff --git a/phpmyfaq/api/index.php b/phpmyfaq/api/index.php index c44b97a6bd..76a66ed93b 100644 --- a/phpmyfaq/api/index.php +++ b/phpmyfaq/api/index.php @@ -16,15 +16,26 @@ */ use phpMyFAQ\Application; -use phpMyFAQ\Configuration; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; require '../src/Bootstrap.php'; -$faqConfig = Configuration::getConfigurationInstance(); +// +// Service Containers +// +$container = new ContainerBuilder(); +$loader = new PhpFileLoader($container, new FileLocator(__DIR__)); +try { + $loader->load('../src/services.php'); +} catch (\Exception $e) { + echo $e->getMessage(); +} $routes = include PMF_SRC_DIR . '/api-routes.php'; -$app = new Application($faqConfig); +$app = new Application($container); try { $app->run($routes); } catch (Exception $exception) { diff --git a/phpmyfaq/content/plugins/HelloWorld/HelloWorldPlugin.php b/phpmyfaq/content/plugins/HelloWorld/HelloWorldPlugin.php index 88db2abf89..e7b4498d1c 100644 --- a/phpmyfaq/content/plugins/HelloWorld/HelloWorldPlugin.php +++ b/phpmyfaq/content/plugins/HelloWorld/HelloWorldPlugin.php @@ -32,7 +32,7 @@ public function getName(): string public function getVersion(): string { - return '0.1.0'; + return '0.2.0'; } public function getDependencies(): array diff --git a/phpmyfaq/services/webauthn/index.php b/phpmyfaq/services/webauthn/index.php index 28db3f90a6..d18adc3e1e 100644 --- a/phpmyfaq/services/webauthn/index.php +++ b/phpmyfaq/services/webauthn/index.php @@ -3,12 +3,24 @@ use phpMyFAQ\Application; use phpMyFAQ\Configuration; use phpMyFAQ\Controller\WebAuthnController; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; require '../../src/Bootstrap.php'; -$faqConfig = Configuration::getConfigurationInstance(); +// +// Service Containers +// +$container = new ContainerBuilder(); +$loader = new PhpFileLoader($container, new FileLocator(__DIR__)); +try { + $loader->load('../../src/services.php'); +} catch (\Exception $e) { + echo $e->getMessage(); +} $routes = new RouteCollection(); $routes->add( @@ -16,7 +28,7 @@ new Route('/', ['_controller' => [WebAuthnController::class, 'index']]) ); -$app = new Application($faqConfig); +$app = new Application($container); try { $app->run($routes); } catch (Exception $exception) { diff --git a/phpmyfaq/sitemap.xml.php b/phpmyfaq/sitemap.xml.php index 2c6e36d157..dacd38ca53 100644 --- a/phpmyfaq/sitemap.xml.php +++ b/phpmyfaq/sitemap.xml.php @@ -39,12 +39,10 @@ echo $e->getMessage(); } -$faqConfig = $container->get('phpmyfaq.configuration'); - $routes = new RouteCollection(); $routes->add('public.sitemap.xml', new Route('/sitemap.xml', ['_controller' => [SitemapController::class, 'index']])); -$app = new Application($faqConfig); +$app = new Application($container); try { $app->run($routes); } catch (Exception $exception) { diff --git a/phpmyfaq/src/phpMyFAQ/Application.php b/phpmyfaq/src/phpMyFAQ/Application.php index 550166680f..93903cc769 100644 --- a/phpmyfaq/src/phpMyFAQ/Application.php +++ b/phpmyfaq/src/phpMyFAQ/Application.php @@ -19,6 +19,7 @@ use ErrorException; use phpMyFAQ\Core\Exception; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -32,7 +33,7 @@ readonly class Application { - public function __construct(private ?Configuration $configuration = null) + public function __construct(private ?ContainerInterface $container = null) { } @@ -49,8 +50,9 @@ public function run(RouteCollection $routeCollection): void private function setLanguage(): string { - if ($this->configuration) { - $language = new Language($this->configuration); + if (!is_null($this->container)) { + $configuration = $this->container->get('phpmyfaq.configuration'); + $language = $this->container->get('phpmyfaq.language'); $currentLanguage = $language->setLanguageByAcceptLanguage(); require sprintf('%s/language_en.php', PMF_TRANSLATION_DIR); @@ -58,7 +60,7 @@ private function setLanguage(): string require sprintf('%s/language_%s.php', PMF_TRANSLATION_DIR, $currentLanguage); } - $this->configuration->setLanguage($language); + $configuration->setLanguage($language); return $currentLanguage; } diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Frontend/SetupController.php b/phpmyfaq/src/phpMyFAQ/Controller/Frontend/SetupController.php index 7aa3c7b27c..66df7afc25 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Frontend/SetupController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Frontend/SetupController.php @@ -139,7 +139,7 @@ public function update(Request $request): Response public function render(string $pathToTwigFile, array $templateVars = [], ?Response $response = null): Response { $response ??= new Response(); - $twigWrapper = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates'); + $twigWrapper = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates', true); $twigWrapper->setSetup(true); $template = $twigWrapper->loadTemplate($pathToTwigFile); diff --git a/phpmyfaq/src/phpMyFAQ/Language.php b/phpmyfaq/src/phpMyFAQ/Language.php index eb2293b59f..45947054d6 100644 --- a/phpmyfaq/src/phpMyFAQ/Language.php +++ b/phpmyfaq/src/phpMyFAQ/Language.php @@ -21,6 +21,7 @@ use phpMyFAQ\Language\LanguageCodes; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Class Language @@ -42,8 +43,10 @@ class Language /** * Constructor. */ - public function __construct(private readonly Configuration $configuration) - { + public function __construct( + private readonly Configuration $configuration, + private readonly SessionInterface $session + ) { } /** @@ -95,7 +98,7 @@ public function setLanguage(bool $configDetection, string $configLanguage): stri { $detectedLang = $this->detectLanguage($configDetection, $configLanguage); self::$language = $this->selectLanguage($detectedLang); - $_SESSION['lang'] = self::$language; + $this->session->set('lang', self::$language); return self::$language; } @@ -166,7 +169,7 @@ private function getArtGetLanguage(): ?string private function getSessionLanguage(): ?string { - $lang = $_SESSION['lang'] ?? null; + $lang = $this->session->get('lang'); return $this->isASupportedLanguage($lang) ? trim((string) $lang) : null; } diff --git a/phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php b/phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php index 595327a38d..2af499e957 100644 --- a/phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php +++ b/phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php @@ -36,13 +36,14 @@ class TwigWrapper { private Environment $twigEnvironment; - private bool $isSetup = false; + private bool $isSetup; /** @var string Name of an active template set. */ private static string $templateSetName = 'default'; - public function __construct(string $templatePath) + public function __construct(string $templatePath, bool $isSetup = false) { + $this->isSetup = $isSetup; $filesystemLoader = new FilesystemLoader($templatePath); $this->twigEnvironment = new Environment( $filesystemLoader, diff --git a/phpmyfaq/src/services.php b/phpmyfaq/src/services.php index 2e58cb4dc3..033f0ebb47 100644 --- a/phpmyfaq/src/services.php +++ b/phpmyfaq/src/services.php @@ -28,6 +28,7 @@ use phpMyFAQ\Instance; use phpMyFAQ\Language; use phpMyFAQ\Services\Gravatar; +use phpMyFAQ\Session\Token; use phpMyFAQ\Sitemap; use phpMyFAQ\Tags; use phpMyFAQ\User\CurrentUser; @@ -35,6 +36,7 @@ use phpMyFAQ\Visits; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpFoundation\Session\Session; return static function (ContainerConfigurator $container): void { // Parameters @@ -43,6 +45,8 @@ // Services $services = $container->services(); + $services->set('session', Session::class); + $services->set('phpmyfaq.admin.category', Category::class) ->args([ new Reference('phpmyfaq.configuration'), @@ -101,7 +105,8 @@ $services->set('phpmyfaq.language', Language::class) ->args([ - new Reference('phpmyfaq.configuration') + new Reference('phpmyfaq.configuration'), + new Reference('session') ]); $services->set('phpmyfaq.session', UserSession::class) @@ -109,6 +114,12 @@ new Reference('phpmyfaq.configuration') ]); + $services->set('phpmyfaq.session.token', Token::class) + ->factory([Token::class, 'getInstance']) + ->args([ + new Reference('session') + ]); + $services->set('phpmyfaq.sitemap', Sitemap::class) ->args([ new Reference('phpmyfaq.configuration') diff --git a/phpmyfaq/update/index.php b/phpmyfaq/update/index.php index 662546a583..aebf61acb4 100644 --- a/phpmyfaq/update/index.php +++ b/phpmyfaq/update/index.php @@ -18,19 +18,30 @@ */ use phpMyFAQ\Application; -use phpMyFAQ\Configuration; use phpMyFAQ\Controller\Frontend\SetupController; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; require '../src/Bootstrap.php'; -$faqConfig = Configuration::getConfigurationInstance(); +// +// Service Containers +// +$container = new ContainerBuilder(); +$loader = new PhpFileLoader($container, new FileLocator(__DIR__)); +try { + $loader->load('../src/services.php'); +} catch (\Exception $e) { + echo $e->getMessage(); +} $routes = new RouteCollection(); $routes->add('public.update.index', new Route('/', ['_controller' => [SetupController::class, 'update']])); -$app = new Application($faqConfig); +$app = new Application($container); try { $app->run($routes); } catch (Exception $exception) { diff --git a/tests/phpMyFAQ/ApplicationTest.php b/tests/phpMyFAQ/ApplicationTest.php index 5833282d08..91b6e4214f 100644 --- a/tests/phpMyFAQ/ApplicationTest.php +++ b/tests/phpMyFAQ/ApplicationTest.php @@ -2,32 +2,34 @@ namespace phpMyFAQ; -use phpMyFAQ\Application; -use phpMyFAQ\Configuration; -use phpMyFAQ\Core\Exception; -use phpMyFAQ\Language; -use phpMyFAQ\Translation; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\RouteCollection; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; -use Symfony\Component\HttpKernel\Controller\ArgumentResolver; use Symfony\Component\HttpKernel\Controller\ControllerResolver; class ApplicationTest extends TestCase { + /** + * @throws Exception + */ public function testConstructor(): void { - $config = $this->createMock(Configuration::class); - $application = new Application($config); + $container = $this->createMock(ContainerInterface::class); + $application = new Application($container); $this->assertInstanceOf(Application::class, $application); } + /** + * @throws Exception + * @throws \ReflectionException + */ public function testHandleRequest(): void { $routeCollection = $this->createMock(RouteCollection::class); diff --git a/tests/phpMyFAQ/BookmarkTest.php b/tests/phpMyFAQ/BookmarkTest.php index 744c6a5045..e7364f7ff1 100644 --- a/tests/phpMyFAQ/BookmarkTest.php +++ b/tests/phpMyFAQ/BookmarkTest.php @@ -4,13 +4,19 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\User\CurrentUser; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; class BookmarkTest extends TestCase { private Bookmark $bookmark; + /** + * @throws Exception + * @throws Core\Exception + */ protected function setUp(): void { parent::setUp(); @@ -31,7 +37,7 @@ protected function setUp(): void $configuration->set('main.referenceURL', 'http://example.com'); $user = CurrentUser::getCurrentUser($configuration); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/CategoryTest.php b/tests/phpMyFAQ/CategoryTest.php index 725f54ac5e..3921b9a6dd 100644 --- a/tests/phpMyFAQ/CategoryTest.php +++ b/tests/phpMyFAQ/CategoryTest.php @@ -4,12 +4,17 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\CategoryEntity; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class CategoryTest extends TestCase { private Category $category; + /** + * @throws Exception + */ protected function setUp(): void { parent::setUp(); @@ -18,7 +23,7 @@ protected function setUp(): void $dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($dbHandle); $configuration->set('main.currentVersion', System::getVersion()); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/CommentsTest.php b/tests/phpMyFAQ/CommentsTest.php index 3878c41e7b..a8c7d26305 100644 --- a/tests/phpMyFAQ/CommentsTest.php +++ b/tests/phpMyFAQ/CommentsTest.php @@ -5,7 +5,9 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\Comment; use phpMyFAQ\Entity\CommentType; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class CommentsTest extends TestCase { @@ -13,6 +15,9 @@ class CommentsTest extends TestCase private Configuration $configuration; + /** + * @throws Exception + */ protected function setUp(): void { parent::setUp(); @@ -20,7 +25,7 @@ protected function setUp(): void $dbHandle = new Sqlite3(); $dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $this->configuration = new Configuration($dbHandle); - $language = new Language($this->configuration); + $language = new Language($this->configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $this->configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/Controller/Api/LanguageControllerTest.php b/tests/phpMyFAQ/Controller/Api/LanguageControllerTest.php index 5e901882c1..9f75395ab0 100644 --- a/tests/phpMyFAQ/Controller/Api/LanguageControllerTest.php +++ b/tests/phpMyFAQ/Controller/Api/LanguageControllerTest.php @@ -4,15 +4,20 @@ use phpMyFAQ\Configuration; use phpMyFAQ\Language; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Session\Session; class LanguageControllerTest extends TestCase { + /** + * @throws Exception + */ public function testIndex(): void { $configuration = Configuration::getConfigurationInstance(); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(true, 'language_en.php'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/ExportTest.php b/tests/phpMyFAQ/ExportTest.php index 0cd3c5b4d4..e73da68ac3 100644 --- a/tests/phpMyFAQ/ExportTest.php +++ b/tests/phpMyFAQ/ExportTest.php @@ -7,6 +7,7 @@ use phpMyFAQ\Export\Json; use phpMyFAQ\Export\Pdf; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class ExportTest extends TestCase { @@ -16,6 +17,7 @@ class ExportTest extends TestCase /** * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { @@ -33,7 +35,7 @@ protected function setUp(): void $this->configuration = new Configuration($dbHandle); $this->configuration->set('main.currentVersion', System::getVersion()); - $language = new Language($this->configuration); + $language = new Language($this->configuration, $this->createMock(Session::class)); $this->configuration->setLanguage($language); $this->faq = new Faq($this->configuration); diff --git a/tests/phpMyFAQ/Faq/QueryHelperTest.php b/tests/phpMyFAQ/Faq/QueryHelperTest.php index 2ad26bed1f..f4a53744fd 100644 --- a/tests/phpMyFAQ/Faq/QueryHelperTest.php +++ b/tests/phpMyFAQ/Faq/QueryHelperTest.php @@ -3,14 +3,20 @@ namespace phpMyFAQ\Faq; use phpMyFAQ\Configuration; +use phpMyFAQ\Core\Exception; use phpMyFAQ\Database; use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Language; use phpMyFAQ\Translation; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class QueryHelperTest extends TestCase { + /** + * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception + */ protected function setUp(): void { parent::setUp(); @@ -22,7 +28,7 @@ protected function setUp(): void ->setMultiByteLanguage(); $configuration = Configuration::getConfigurationInstance(); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(true, 'language_en.php'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/FaqTest.php b/tests/phpMyFAQ/FaqTest.php index cd8587f036..c290fbb5c0 100644 --- a/tests/phpMyFAQ/FaqTest.php +++ b/tests/phpMyFAQ/FaqTest.php @@ -9,6 +9,7 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\FaqEntity; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class FaqTest extends TestCase { @@ -20,6 +21,7 @@ class FaqTest extends TestCase /** * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { @@ -36,7 +38,7 @@ protected function setUp(): void $this->configuration = new Configuration($dbHandle); $this->configuration->set('main.currentVersion', System::getVersion()); - $language = new Language($this->configuration); + $language = new Language($this->configuration, $this->createMock(Session::class)); $this->configuration->setLanguage($language); $this->faq = new Faq($this->configuration); diff --git a/tests/phpMyFAQ/FormsTest.php b/tests/phpMyFAQ/FormsTest.php index 57ce239463..3709c990dc 100644 --- a/tests/phpMyFAQ/FormsTest.php +++ b/tests/phpMyFAQ/FormsTest.php @@ -2,13 +2,19 @@ namespace phpMyFAQ; +use phpMyFAQ\Core\Exception; use phpMyFAQ\Database\Sqlite3; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class FormsTest extends TestCase { private Forms $forms; + /** + * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception + */ protected function setUp(): void { parent::setUp(); @@ -22,7 +28,7 @@ protected function setUp(): void $dbHandle = new Sqlite3(); $dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($dbHandle); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/GlossaryTest.php b/tests/phpMyFAQ/GlossaryTest.php index 143d36d2b6..1c19ae4114 100644 --- a/tests/phpMyFAQ/GlossaryTest.php +++ b/tests/phpMyFAQ/GlossaryTest.php @@ -3,14 +3,17 @@ namespace phpMyFAQ; use phpMyFAQ\Database\Sqlite3; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class GlossaryTest extends TestCase { - private Configuration $config; - private Glossary $glossary; + /** + * @throws Exception + */ protected function setUp(): void { parent::setUp(); @@ -19,12 +22,12 @@ protected function setUp(): void $dbHandle = new Sqlite3(); $dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); - $this->config = new Configuration($dbHandle); - $language = new Language($this->config); + $config = new Configuration($dbHandle); + $language = new Language($config, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); - $this->config->setLanguage($language); + $config->setLanguage($language); - $this->glossary = new Glossary($this->config); + $this->glossary = new Glossary($config); $this->glossary->setLanguage('en'); } diff --git a/tests/phpMyFAQ/LanguageTest.php b/tests/phpMyFAQ/LanguageTest.php index 7a2d9b2f0f..1e86e6dbab 100644 --- a/tests/phpMyFAQ/LanguageTest.php +++ b/tests/phpMyFAQ/LanguageTest.php @@ -4,7 +4,10 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Plugin\PluginException; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class LanguageTest extends TestCase { @@ -12,8 +15,10 @@ class LanguageTest extends TestCase private Sqlite3 $dbHandle; + private Session $session; + /** - * @throws PluginException + * @throws Exception */ protected function setUp(): void { @@ -21,11 +26,12 @@ protected function setUp(): void Strings::init(); + $this->session = $this->createMock(Session::class); $this->dbHandle = new Sqlite3(); $this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($this->dbHandle); - $this->language = new Language($configuration); + $this->language = new Language($configuration, $this->session); } public function testIsLanguageAvailableWithId(): void @@ -86,10 +92,9 @@ public function testSetLanguageReturnsCorrectLanguageForValidConfigLanguage(): v public function testSetLanguageReturnsCorrectLanguageForValidSessionLanguage(): void { - $_SESSION['lang'] = 'fr'; + $this->session->method('get')->willReturn('en'); $language = $this->language->setLanguage(true, 'language_en.php'); - $this->assertEquals('fr', $language); - unset($_SESSION['lang']); + $this->assertEquals('en', $language); } public function testSetLanguageReturnsFallbackLanguageForInvalidConfigLanguageAndNoOtherDetectedLanguages(): void diff --git a/tests/phpMyFAQ/NewsTest.php b/tests/phpMyFAQ/NewsTest.php index 5ad7ab3b7e..40646aec0e 100644 --- a/tests/phpMyFAQ/NewsTest.php +++ b/tests/phpMyFAQ/NewsTest.php @@ -5,11 +5,17 @@ use DateTime; use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\NewsMessage; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class NewsTest extends TestCase { private News $news; + + /** + * @throws Exception + */ protected function setUp(): void { parent::setUp(); @@ -18,7 +24,7 @@ protected function setUp(): void $dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($dbHandle); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $configuration->setLanguage($language); $this->news = new News($configuration); diff --git a/tests/phpMyFAQ/QuestionTest.php b/tests/phpMyFAQ/QuestionTest.php index 715187d23c..e10c81e120 100644 --- a/tests/phpMyFAQ/QuestionTest.php +++ b/tests/phpMyFAQ/QuestionTest.php @@ -3,12 +3,18 @@ namespace phpMyFAQ; use phpMyFAQ\Database\Sqlite3; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class QuestionTest extends TestCase { private Sqlite3 $dbHandle; private Question $question; + + /** + * @throws Exception + */ protected function setUp(): void { parent::setUp(); @@ -16,7 +22,7 @@ protected function setUp(): void $this->dbHandle = new Sqlite3(); $this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($this->dbHandle); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/RatingTest.php b/tests/phpMyFAQ/RatingTest.php index f7da050998..3231a16692 100644 --- a/tests/phpMyFAQ/RatingTest.php +++ b/tests/phpMyFAQ/RatingTest.php @@ -6,6 +6,7 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\Vote; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class RatingTest extends TestCase { @@ -15,6 +16,7 @@ class RatingTest extends TestCase /** * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { @@ -29,7 +31,7 @@ protected function setUp(): void $this->dbHandle = new Sqlite3(); $this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($this->dbHandle); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/RelationTest.php b/tests/phpMyFAQ/RelationTest.php index 689a46a7d2..9bd0502645 100644 --- a/tests/phpMyFAQ/RelationTest.php +++ b/tests/phpMyFAQ/RelationTest.php @@ -4,8 +4,10 @@ use phpMyFAQ\Configuration\DatabaseConfiguration; use phpMyFAQ\Database\Sqlite3; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use stdClass; +use Symfony\Component\HttpFoundation\Session\Session; class RelationTest extends TestCase { @@ -21,6 +23,7 @@ protected function tearDown(): void /** * @throws \Exception + * @throws Exception */ public function testGetAllRelatedByQuestion(): void { @@ -37,7 +40,7 @@ public function testGetAllRelatedByQuestion(): void $configuration = new Configuration($this->db); $configuration->set('search.enableRelevance', false); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/SitemapTest.php b/tests/phpMyFAQ/SitemapTest.php index f17617d826..0a51d31e30 100644 --- a/tests/phpMyFAQ/SitemapTest.php +++ b/tests/phpMyFAQ/SitemapTest.php @@ -4,8 +4,10 @@ use phpMyFAQ\Configuration\DatabaseConfiguration; use phpMyFAQ\Database\Sqlite3; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; use stdClass; +use Symfony\Component\HttpFoundation\Session\Session; class SitemapTest extends TestCase { @@ -13,6 +15,10 @@ class SitemapTest extends TestCase private Sqlite3 $db; + /** + * @throws Exception + * @throws Core\Exception + */ protected function setUp(): void { parent::setUp(); @@ -32,7 +38,7 @@ protected function setUp(): void $configuration = new Configuration($this->db); $configuration->set('main.referenceURL', 'https://example.com/'); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/StopWordsTest.php b/tests/phpMyFAQ/StopWordsTest.php index c39e15ab8f..c0bb1421a6 100644 --- a/tests/phpMyFAQ/StopWordsTest.php +++ b/tests/phpMyFAQ/StopWordsTest.php @@ -5,6 +5,7 @@ use phpMyFAQ\Core\Exception; use phpMyFAQ\Database\Sqlite3; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class StopWordsTest extends TestCase { @@ -13,6 +14,7 @@ class StopWordsTest extends TestCase /** * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { @@ -27,7 +29,7 @@ protected function setUp(): void $this->dbHandle = new Sqlite3(); $this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); $configuration = new Configuration($this->dbHandle); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/TagsTest.php b/tests/phpMyFAQ/TagsTest.php index 19193b4d54..b31409f510 100644 --- a/tests/phpMyFAQ/TagsTest.php +++ b/tests/phpMyFAQ/TagsTest.php @@ -5,14 +5,16 @@ use phpMyFAQ\Database\Sqlite3; use phpMyFAQ\Entity\Tag; use phpMyFAQ\Plugin\PluginException; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class TagsTest extends TestCase { private Tags $tags; /** - * @throws PluginException + * @throws Exception */ protected function setUp(): void { @@ -25,7 +27,7 @@ protected function setUp(): void $configuration = new Configuration($dbHandle); $configuration->set('main.referenceURL', 'http://example.com'); - $language = new Language($configuration); + $language = new Language($configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $configuration->setLanguage($language); diff --git a/tests/phpMyFAQ/VisitsTest.php b/tests/phpMyFAQ/VisitsTest.php index 5352d86ea5..448c7a89d5 100644 --- a/tests/phpMyFAQ/VisitsTest.php +++ b/tests/phpMyFAQ/VisitsTest.php @@ -2,8 +2,10 @@ namespace phpMyFAQ; +use phpMyFAQ\Core\Exception; use phpMyFAQ\Database\Sqlite3; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Session\Session; class VisitsTest extends TestCase { @@ -12,6 +14,10 @@ class VisitsTest extends TestCase private Visits $visits; + /** + * @throws Exception + * @throws \PHPUnit\Framework\MockObject\Exception + */ protected function setUp(): void { parent::setUp(); @@ -27,7 +33,7 @@ protected function setUp(): void $this->configuration = new Configuration($dbHandle); $this->configuration->set('main.currentVersion', System::getVersion()); - $language = new Language($this->configuration); + $language = new Language($this->configuration, $this->createMock(Session::class)); $language->setLanguage(false, 'en'); $this->configuration->setLanguage($language);