Skip to content

Commit

Permalink
Move module testing to controller
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>

Refactor acp test

Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Sep 23, 2023
1 parent 5211be5 commit 1bffabf
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions tests/acp/module_test.php → tests/acp/acp_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

require_once __DIR__ . '/../../../../../includes/functions_acp.php';

class module_test extends \phpbb_database_test_case
class acp_test extends \phpbb_database_test_case
{
/** @var bool A return value for check_form_key() */
public static $valid_form = false;

/** @var \vse\abbc3\controller\acp_controller */
protected $acp_controller;

/** @var ContainerInterface|MockObject */
protected $container;

Expand Down Expand Up @@ -88,28 +91,44 @@ protected function setUp(): void
$user->lang = new \phpbb_mock_lang();
}

public function main_module_data()
{
return [
[0],
[E_USER_NOTICE],
[E_USER_WARNING],
];
}

/**
* @return \vse\abbc3\acp\abbc3_module
* @dataProvider main_module_data
* @param $error
* @return void
* @throws \Exception
*/
public function get_main_module()
public function test_main_module($error)
{
$this->container->expects(self::once())
$controller = $this->container->expects(self::once())
->method('get')
->willReturnMap([
['vse.abbc3.acp_controller', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->acp_controller],
]);

// Test basic module instantiation
if ($error)
{
$controller->willThrowException(new \RuntimeException('ERROR_TEST', $error));
$this->setExpectedTriggerError($error, 'ERROR_TEST');
}

$module = new \vse\abbc3\acp\abbc3_module();
self::assertInstanceOf('\vse\abbc3\acp\abbc3_module', $module);

return $module;
$module->main();

self::assertEquals('acp_abbc3_settings', $module->tpl_name);
}

public function test_main_display()
{
$module = $this->get_main_module();

$this->template->expects(self::once())
->method('assign_vars')
->with([
Expand All @@ -120,56 +139,52 @@ public function test_main_display()
'S_ABBC3_ICONS_TYPE' => build_select(['png' => 'PNG', 'svg' => 'SVG'], $this->config['abbc3_icons_type']),
'S_ABBC3_GOOGLE_FONTS' => "Droid Sans\nRoboto",
'S_ABBC3_MEDIA_EMBED' => 0,
'U_ACTION' => $module->u_action,
'U_ACTION' => 'foo',
]);

$module->main();

self::assertEquals('acp_abbc3_settings', $module->tpl_name);
$this->acp_controller
->set_u_action('foo')
->handle();
}

public function test_main_save()
{
self::$valid_form = true;

$module = $this->get_main_module();

$this->request->expects(self::once())
->method('is_set_post')
->willReturn('submit');

// Throws Notice in PHP 8.0+ and Error in earlier versions
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Notice::class;
$this->expectException($exceptionName);
$this->expectException('\RuntimeException');
$this->expectExceptionCode(E_USER_NOTICE);
$this->expectExceptionMessage('CONFIG_UPDATED');

$module->main();
$this->acp_controller->handle();
}

public function test_main_save_error()
{
self::$valid_form = false;

$module = $this->get_main_module();

$this->request->expects(self::once())
->method('is_set_post')
->willReturn('submit');

// Throws E_WARNING in PHP 8.0+ and E_USER_WARNING in earlier versions
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
$this->expectException($exceptionName);
$this->expectException('\RuntimeException');
$this->expectExceptionCode(E_USER_WARNING);
$this->expectExceptionMessage($this->lang->lang('FORM_INVALID'));

$module->main();
$this->acp_controller->handle();
}

public function save_google_fonts_data()
{
return [
['', '[]', \PHPUnit\Framework\Error\Notice::class],
['Droid Sans', '["Droid Sans"]', \PHPUnit\Framework\Error\Notice::class],
["Droid Sans\nRoboto", '["Droid Sans","Roboto"]', \PHPUnit\Framework\Error\Notice::class],
["Droid Sans\nRoboto\nMac Donald", '["Droid Sans","Roboto"]', \PHPUnit\Framework\Error\Warning::class],
['Mac Donald', '[]', \PHPUnit\Framework\Error\Warning::class],
['', '[]', E_USER_NOTICE, 'CONFIG_UPDATED'],
['Droid Sans', '["Droid Sans"]', E_USER_NOTICE, 'CONFIG_UPDATED'],
["Droid Sans\nRoboto", '["Droid Sans","Roboto"]', E_USER_NOTICE, 'CONFIG_UPDATED'],
["Droid Sans\nRoboto\nMac Donald", '["Droid Sans","Roboto"]', E_USER_WARNING, 'ABBC3_INVALID_FONT'],
['Mac Donald', '[]', E_USER_WARNING, 'ABBC3_INVALID_FONT'],
];
}

Expand All @@ -178,14 +193,12 @@ public function save_google_fonts_data()
* @param $input
* @param $expected
* @param $error
* @throws \Exception
* @param $error_message
*/
public function test_save_google_fonts($input, $expected, $error)
public function test_save_google_fonts($input, $expected, $error, $error_message)
{
self::$valid_form = true;

$module = $this->get_main_module();

$this->request->expects(self::once())
->method('is_set_post')
->willReturn('submit');
Expand All @@ -201,19 +214,18 @@ public function test_save_google_fonts($input, $expected, $error)
['abbc3_google_fonts', '', false, \phpbb\request\request_interface::REQUEST, $input],
]);

// Throws Notice in PHP 8.0+ and Error in earlier versions
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : $error;
$this->expectException($exceptionName);
$this->expectException('\RuntimeException');
$this->expectExceptionCode($error);
$this->expectExceptionMessage($error_message);

$module->main();
$this->acp_controller->handle();

$this->assertSame($expected, $this->config_text->get('abbc3_google_fonts'));
}

public function test_info()
{
$info_class = new \vse\abbc3\acp\abbc3_info();
self::assertInstanceOf('\vse\abbc3\acp\abbc3_info', $info_class);
$info_array = $info_class->module();
self::assertArrayHasKey('filename', $info_array);
self::assertEquals('\vse\abbc3\acp\abbc3_module', $info_array['filename']);
Expand All @@ -229,7 +241,7 @@ public function test_info()
*/
function check_form_key()
{
return \vse\abbc3\controller\module_test::$valid_form;
return \vse\abbc3\controller\acp_test::$valid_form;
}

/**
Expand Down

0 comments on commit 1bffabf

Please sign in to comment.