|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpMyFAQ\Controller\Frontend; |
| 4 | + |
| 5 | +use phpMyFAQ\Strings; |
| 6 | +use PHPUnit\Framework\MockObject\Exception; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use phpMyFAQ\Translation; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 11 | +use Symfony\Component\HttpFoundation\Response; |
| 12 | + |
| 13 | +class TranslationControllerTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @throws \phpMyFAQ\Core\Exception |
| 17 | + */ |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + Strings::init(); |
| 23 | + |
| 24 | + Translation::create() |
| 25 | + ->setLanguagesDir(PMF_TRANSLATION_DIR) |
| 26 | + ->setDefaultLanguage('en') |
| 27 | + ->setCurrentLanguage('en') |
| 28 | + ->setMultiByteLanguage(); |
| 29 | + } |
| 30 | + /** |
| 31 | + * @throws Exception |
| 32 | + * @throws \phpMyFAQ\Core\Exception |
| 33 | + */ |
| 34 | + public function testTranslationsWithSupportedLanguage(): void |
| 35 | + { |
| 36 | + $language = 'en'; |
| 37 | + Translation::getInstance()->setCurrentLanguage($language); |
| 38 | + $translations = Translation::getAll(); |
| 39 | + |
| 40 | + $request = $this->createMock(Request::class); |
| 41 | + $request->method('get')->with('language')->willReturn($language); |
| 42 | + |
| 43 | + $controller = new TranslationController(); |
| 44 | + $response = $controller->translations($request); |
| 45 | + |
| 46 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 47 | + $this->assertEquals($translations, json_decode($response->getContent(), true)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @throws Exception |
| 52 | + */ |
| 53 | + public function testTranslationsWithUnsupportedLanguage(): void |
| 54 | + { |
| 55 | + $language = 'unsupported'; |
| 56 | + |
| 57 | + $request = $this->createMock(Request::class); |
| 58 | + $request->method('get')->with('language')->willReturn($language); |
| 59 | + |
| 60 | + $controller = new TranslationController(); |
| 61 | + $response = $controller->translations($request); |
| 62 | + |
| 63 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 64 | + $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
| 65 | + $this->assertEquals(['error' => 'Language not supported'], json_decode($response->getContent(), true)); |
| 66 | + } |
| 67 | +} |
0 commit comments