Skip to content

Commit afe7ba7

Browse files
committed
Update tests
1 parent 6258432 commit afe7ba7

7 files changed

Lines changed: 56 additions & 19 deletions

File tree

controller/admin_controller.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace phpbb\topicprefixes\controller;
1212

13+
use phpbb\json_response;
1314
use phpbb\language\language;
1415
use phpbb\log\log;
1516
use phpbb\request\request;
@@ -188,8 +189,7 @@ public function edit_prefix($prefix_id)
188189

189190
if ($this->request->is_ajax())
190191
{
191-
$json_response = new \phpbb\json_response;
192-
$json_response->send(['success' => true]);
192+
$this->send_json_response(true);
193193
}
194194
}
195195

@@ -251,8 +251,7 @@ public function move_prefix($prefix_id, $direction, $amount = 1)
251251

252252
if ($this->request->is_ajax())
253253
{
254-
$json_response = new \phpbb\json_response;
255-
$json_response->send(['success' => true]);
254+
$this->send_json_response(true);
256255
}
257256
}
258257

@@ -334,4 +333,21 @@ protected function get_forum_info($forum_id)
334333

335334
return $acp_forums->get_forum_info($forum_id);
336335
}
336+
337+
/**
338+
* Send a JSON response
339+
*
340+
* @param bool $content The content of the JSON response (true|false)
341+
* @access protected
342+
*/
343+
protected function send_json_response(bool $content): void
344+
{
345+
if ($this->request->is_ajax())
346+
{
347+
$json_response = new json_response;
348+
$json_response->send([
349+
'success' => (bool) $content,
350+
]);
351+
}
352+
}
337353
}

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
backupGlobals="true"
55
backupStaticAttributes="false"
66
colors="true"

tests/controller/add_prefix_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function test_add_prefix($prefix, $submit, $valid_form)
5353

5454
if (!$valid_form)
5555
{
56-
$this->expectException(\PHPUnit\Framework\Exception::class);
56+
$this->setExpectedTriggerError(E_USER_WARNING);
5757
}
5858
else
5959
{

tests/controller/admin_controller_base.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ class admin_controller_base extends \phpbb_test_case
5353
*/
5454
protected function setUp(): void
5555
{
56-
global $db, $language, $user, $phpbb_root_path, $phpEx;
56+
global $db, $language, $user, $phpbb_root_path, $phpEx, $phpbb_dispatcher;
5757

5858
parent::setUp();
5959

60+
$phpbb_dispatcher = new \phpbb_mock_event_dispatcher();
61+
6062
$this->manager = $this->getMockBuilder('\phpbb\topicprefixes\prefixes\manager')
6163
->disableOriginalConstructor()
6264
->getMock();
@@ -89,6 +91,9 @@ protected function setUp(): void
8991
->disableOriginalConstructor()
9092
->getMock();
9193

94+
$this->phpbb_root_path = $phpbb_root_path;
95+
$this->phpEx = $phpEx;
96+
9297
$this->controller = new \phpbb\topicprefixes\controller\admin_controller(
9398
$this->manager,
9499
$this->language,
@@ -100,6 +105,19 @@ protected function setUp(): void
100105
$phpEx
101106
);
102107
}
108+
109+
protected function get_testable_controller(): \phpbb\topicprefixes\controller\admin_controller
110+
{
111+
return new class($this->manager, $this->language, $this->log, $this->request, $this->template, $this->user, $this->phpbb_root_path, $this->phpEx) extends \phpbb\topicprefixes\controller\admin_controller {
112+
protected function send_json_response(bool $content): void
113+
{
114+
if ($this->request->is_ajax())
115+
{
116+
echo json_encode(['success' => (bool) $content]);
117+
}
118+
}
119+
};
120+
}
103121
}
104122

105123
/**

tests/controller/delete_prefix_test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function test_delete_prefix($prefix_id, $confirm)
6363
->will(self::throwException(new \OutOfBoundsException()));
6464
$this->log->expects(self::never())
6565
->method('add');
66-
$this->expectException(\PHPUnit\Framework\Exception::class);
66+
$this->setExpectedTriggerError(E_USER_WARNING);
6767
}
6868
else
6969
{
@@ -79,7 +79,7 @@ public function test_delete_prefix($prefix_id, $confirm)
7979
$this->db->expects(static::once())
8080
->method('sql_fetchrow')
8181
->willReturn(['forum_name' => 'Test Forum']);
82-
$this->expectException(\PHPUnit\Framework\Exception::class);
82+
$this->setExpectedTriggerError(E_USER_NOTICE);
8383
}
8484

8585
$this->controller->delete_prefix($prefix_id);

tests/controller/edit_prefix_test.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function test_edit_prefix($prefix_id, $valid_form, $is_ajax)
5555
->method('get_prefix');
5656
$this->manager->expects(self::never())
5757
->method('update_prefix');
58-
$this->expectException(\PHPUnit\Framework\Exception::class);
58+
$this->setExpectedTriggerError(E_USER_WARNING);
5959
}
6060
else if ($prefix_id === 0)
6161
{
@@ -66,7 +66,7 @@ public function test_edit_prefix($prefix_id, $valid_form, $is_ajax)
6666
$this->manager->expects(self::once())
6767
->method('update_prefix')
6868
->will(self::throwException(new \OutOfBoundsException));
69-
$this->expectException(\PHPUnit\Framework\Exception::class);
69+
$this->setExpectedTriggerError(E_USER_WARNING);
7070
}
7171
else
7272
{
@@ -77,17 +77,19 @@ public function test_edit_prefix($prefix_id, $valid_form, $is_ajax)
7777
$this->manager->expects(self::once())
7878
->method('update_prefix');
7979

80-
$this->request->expects(self::atMost(1))
80+
$this->request->expects(self::atMost(2))
8181
->method('is_ajax')
8282
->willReturn($is_ajax);
8383

8484
if ($is_ajax)
8585
{
8686
// Handle trigger_error() output called from json_response
87-
$this->setExpectedTriggerError(E_WARNING);
87+
$this->expectOutputString('{"success":true}');
8888
}
8989
}
9090

91-
$this->controller->edit_prefix($prefix_id);
91+
$controller = $this->get_testable_controller();
92+
93+
$controller->edit_prefix($prefix_id);
9294
}
9395
}

tests/controller/move_prefix_test.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,36 @@ public function test_move_prefix($prefix_id, $direction, $valid_form, $is_ajax)
5858
if (!$valid_form)
5959
{
6060
$prefix_id = 0;
61-
$this->expectException(\PHPUnit\Framework\Exception::class);
61+
$this->setExpectedTriggerError(E_USER_WARNING);
6262
$this->manager->expects(static::never())
6363
->method('move_prefix');
6464
}
6565
else if ($prefix_id === 0)
6666
{
67-
$this->expectException(\PHPUnit\Framework\Exception::class);
67+
$this->setExpectedTriggerError(E_USER_WARNING);
6868
$this->manager->expects(static::once())
6969
->method('move_prefix')
7070
->with(static::equalTo(0), static::stringContains($direction))
7171
->will(static::throwException(new \OutOfBoundsException));
7272
}
7373
else
7474
{
75-
$this->request->expects(static::once())
75+
$this->request->expects(static::atMost(2))
7676
->method('is_ajax')
7777
->willReturn($is_ajax);
7878

7979
if ($is_ajax)
8080
{
8181
// Handle trigger_error() output called from json_response
82-
$this->setExpectedTriggerError(E_WARNING);
82+
$this->expectOutputString('{"success":true}');
8383
}
8484

8585
$this->manager->expects(static::once())
8686
->method('move_prefix')
8787
->with(static::equalTo($prefix_id), static::stringContains($direction));
8888
}
8989

90-
$this->controller->move_prefix($prefix_id, $direction);
90+
$controller = $this->get_testable_controller();
91+
$controller->move_prefix($prefix_id, $direction);
9192
}
9293
}

0 commit comments

Comments
 (0)