Skip to content

Commit 040fe41

Browse files
committed
test: added more controller tests for the public API (#3830)
1 parent dea4698 commit 040fe41

16 files changed

+765
-127
lines changed

tests/phpMyFAQ/Controller/BackupControllerTest.php renamed to tests/phpMyFAQ/Controller/Administration/BackupControllerTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
declare(strict_types=1);
44

5-
namespace phpMyFAQ\Controller;
5+
namespace phpMyFAQ\Controller\Administration;
66

77
use phpMyFAQ\Administration\AdminLog;
88
use phpMyFAQ\Administration\Backup;
99
use phpMyFAQ\Administration\Backup\BackupExportResult;
1010
use phpMyFAQ\Configuration;
11-
use phpMyFAQ\Controller\Administration\BackupController;
1211
use phpMyFAQ\Enums\BackupType;
1312
use phpMyFAQ\Permission\BasicPermission;
1413
use phpMyFAQ\User\CurrentUser;
1514
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
16-
use PHPUnit\Framework\MockObject\MockObject;
1715
use PHPUnit\Framework\TestCase;
1816
use Symfony\Component\DependencyInjection\ContainerBuilder;
1917
use Symfony\Component\HttpFoundation\Request;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpMyFAQ\Controller\Api;
6+
7+
use phpMyFAQ\Configuration;
8+
use phpMyFAQ\Core\Exception;
9+
use phpMyFAQ\Language;
10+
use phpMyFAQ\Strings;
11+
use phpMyFAQ\Translation;
12+
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
13+
use PHPUnit\Framework\MockObject\Exception as MockException;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\JsonResponse;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Session\Session;
18+
19+
#[AllowMockObjectsWithoutExpectations]
20+
class CategoryControllerTest extends TestCase
21+
{
22+
private Configuration $configuration;
23+
24+
/**
25+
* @throws Exception
26+
*/
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
Strings::init();
32+
33+
Translation::create()
34+
->setTranslationsDir(PMF_TRANSLATION_DIR)
35+
->setDefaultLanguage('en')
36+
->setCurrentLanguage('en')
37+
->setMultiByteLanguage();
38+
39+
$this->configuration = Configuration::getConfigurationInstance();
40+
}
41+
42+
/**
43+
* @throws MockException
44+
*/
45+
public function testListReturnsJsonResponse(): void
46+
{
47+
$language = new Language($this->configuration, $this->createStub(Session::class));
48+
$language->setLanguageWithDetection('language_en.php');
49+
$this->configuration->setLanguage($language);
50+
51+
$controller = new CategoryController();
52+
$response = $controller->list();
53+
54+
$this->assertInstanceOf(JsonResponse::class, $response);
55+
}
56+
57+
/**
58+
* @throws MockException
59+
*/
60+
public function testListReturnsValidStatusCode(): void
61+
{
62+
$language = new Language($this->configuration, $this->createStub(Session::class));
63+
$language->setLanguageWithDetection('language_en.php');
64+
$this->configuration->setLanguage($language);
65+
66+
$controller = new CategoryController();
67+
$response = $controller->list();
68+
69+
$this->assertContains($response->getStatusCode(), [200, 404]);
70+
}
71+
72+
public function testCreateRequiresValidToken(): void
73+
{
74+
$requestData = json_encode([
75+
'language' => 'en',
76+
'parent-id' => 0,
77+
'category-name' => 'Test Category',
78+
'description' => 'Test Description',
79+
'user-id' => 1,
80+
'group-id' => -1,
81+
'is-active' => true,
82+
'show-on-homepage' => true,
83+
]);
84+
85+
$request = new Request([], [], [], [], [], [], $requestData);
86+
$controller = new CategoryController();
87+
88+
$this->expectException(\Exception::class);
89+
$controller->create($request);
90+
}
91+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpMyFAQ\Controller\Api;
6+
7+
use Exception;use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\HttpFoundation\JsonResponse;
10+
use Symfony\Component\HttpFoundation\Request;
11+
12+
#[AllowMockObjectsWithoutExpectations]
13+
class CommentControllerTest extends TestCase
14+
{
15+
/**
16+
* @throws Exception
17+
*/public function testListReturnsJsonResponse(): void
18+
{
19+
$request = new Request();
20+
$request->attributes->set('recordId', '1');
21+
22+
$controller = new CommentController();
23+
$response = $controller->list($request);
24+
25+
$this->assertInstanceOf(JsonResponse::class, $response);
26+
}
27+
28+
/**
29+
* @throws Exception
30+
*/public function testListReturnsValidStatusCode(): void
31+
{
32+
$request = new Request();
33+
$request->attributes->set('recordId', '1');
34+
35+
$controller = new CommentController();
36+
$response = $controller->list($request);
37+
38+
$this->assertContains($response->getStatusCode(), [200, 404]);
39+
}
40+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpMyFAQ\Controller\Api;
6+
7+
use phpMyFAQ\Configuration;
8+
use phpMyFAQ\Core\Exception;
9+
use phpMyFAQ\Strings;
10+
use phpMyFAQ\Translation;
11+
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\HttpFoundation\JsonResponse;
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
#[AllowMockObjectsWithoutExpectations]
17+
class FaqControllerTest extends TestCase
18+
{
19+
private Configuration $configuration;
20+
21+
/**
22+
* @throws Exception
23+
*/
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
Strings::init();
29+
30+
Translation::create()
31+
->setTranslationsDir(PMF_TRANSLATION_DIR)
32+
->setDefaultLanguage('en')
33+
->setCurrentLanguage('en')
34+
->setMultiByteLanguage();
35+
36+
$this->configuration = Configuration::getConfigurationInstance();
37+
}
38+
39+
/**
40+
* @throws Exception
41+
*/public function testGetByCategoryIdReturnsJsonResponse(): void
42+
{
43+
$request = new Request();
44+
$request->attributes->set('categoryId', '1');
45+
46+
$controller = new FaqController();
47+
$response = $controller->getByCategoryId($request);
48+
49+
$this->assertInstanceOf(JsonResponse::class, $response);
50+
}
51+
52+
/**
53+
* @throws Exception
54+
*/public function testGetByIdReturnsJsonResponse(): void
55+
{
56+
$request = new Request();
57+
$request->attributes->set('faqId', '1');
58+
$request->attributes->set('categoryId', '1');
59+
60+
$controller = new FaqController();
61+
$response = $controller->getById($request);
62+
63+
$this->assertInstanceOf(JsonResponse::class, $response);
64+
}
65+
66+
/**
67+
* @throws \Exception
68+
*/public function testGetByTagIdReturnsJsonResponse(): void
69+
{
70+
$request = new Request();
71+
$request->attributes->set('tagId', '1');
72+
73+
$controller = new FaqController();
74+
$response = $controller->getByTagId($request);
75+
76+
$this->assertInstanceOf(JsonResponse::class, $response);
77+
}
78+
79+
/**
80+
* @throws Exception
81+
*/public function testGetPopularReturnsJsonResponse(): void
82+
{
83+
$controller = new FaqController();
84+
$response = $controller->getPopular();
85+
86+
$this->assertInstanceOf(JsonResponse::class, $response);
87+
}
88+
89+
/**
90+
* @throws Exception
91+
*/public function testGetLatestReturnsJsonResponse(): void
92+
{
93+
$controller = new FaqController();
94+
$response = $controller->getLatest();
95+
96+
$this->assertInstanceOf(JsonResponse::class, $response);
97+
}
98+
99+
/**
100+
* @throws Exception
101+
*/public function testGetTrendingReturnsJsonResponse(): void
102+
{
103+
$controller = new FaqController();
104+
$response = $controller->getTrending();
105+
106+
$this->assertInstanceOf(JsonResponse::class, $response);
107+
}
108+
109+
/**
110+
* @throws Exception
111+
*/public function testGetStickyReturnsJsonResponse(): void
112+
{
113+
$controller = new FaqController();
114+
$response = $controller->getSticky();
115+
116+
$this->assertInstanceOf(JsonResponse::class, $response);
117+
}
118+
119+
/**
120+
* @throws Exception
121+
*/public function testListReturnsJsonResponse(): void
122+
{
123+
$controller = new FaqController();
124+
$response = $controller->list();
125+
126+
$this->assertInstanceOf(JsonResponse::class, $response);
127+
}
128+
129+
/**
130+
* @throws Exception
131+
* @throws \JsonException
132+
*/public function testCreateRequiresValidToken(): void
133+
{
134+
$requestData = json_encode([
135+
'language' => 'en',
136+
'category-id' => 1,
137+
'question' => 'Test Question?',
138+
'answer' => 'Test Answer',
139+
'keywords' => 'test',
140+
'author' => 'Test Author',
141+
'email' => '[email protected]',
142+
'is-active' => true,
143+
'is-sticky' => false,
144+
]);
145+
146+
$request = new Request([], [], [], [], [], [], $requestData);
147+
$controller = new FaqController();
148+
149+
$this->expectException(\Exception::class);
150+
$controller->create($request);
151+
}
152+
153+
/**
154+
* @throws Exception
155+
* @throws \JsonException
156+
*/public function testUpdateRequiresValidToken(): void
157+
{
158+
$requestData = json_encode([
159+
'faq-id' => 1,
160+
'language' => 'en',
161+
'category-id' => 1,
162+
'question' => 'Updated Question?',
163+
'answer' => 'Updated Answer',
164+
'keywords' => 'test',
165+
'author' => 'Test Author',
166+
'email' => '[email protected]',
167+
'is-active' => true,
168+
'is-sticky' => false,
169+
]);
170+
171+
$request = new Request([], [], [], [], [], [], $requestData);
172+
$controller = new FaqController();
173+
174+
$this->expectException(\Exception::class);
175+
$controller->update($request);
176+
}
177+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpMyFAQ\Controller\Api;
6+
7+
use Exception;use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
8+
use PHPUnit\Framework\TestCase;
9+
10+
#[AllowMockObjectsWithoutExpectations]
11+
class GroupControllerTest extends TestCase
12+
{
13+
public function testListRequiresAuthentication(): void
14+
{
15+
$controller = new GroupController();
16+
17+
$this->expectException(Exception::class);
18+
$controller->list();
19+
}
20+
}

0 commit comments

Comments
 (0)