mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-07 20:08:53 +00:00
[ticket/12683] Add basic tests
PHPBB3-12683
This commit is contained in:
parent
d55f1e04eb
commit
ac227a9ea6
9 changed files with 361 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
|||
services:
|
||||
post.helper:
|
||||
class: phpbb\post\post_helper
|
||||
class: phpbb\posting\post_helper
|
||||
arguments:
|
||||
- '@dbal.conn'
|
||||
|
|
|
@ -17,7 +17,7 @@ use phpbb\config\config;
|
|||
use phpbb\console\command\command;
|
||||
use phpbb\language\language;
|
||||
use phpbb\log\log;
|
||||
use phpbb\post\post_helper;
|
||||
use phpbb\posting\post_helper;
|
||||
use phpbb\search\exception\no_search_backend_found_exception;
|
||||
use phpbb\search\search_backend_factory;
|
||||
use phpbb\search\state_helper;
|
||||
|
|
|
@ -17,7 +17,7 @@ use phpbb\config\config;
|
|||
use phpbb\console\command\command;
|
||||
use phpbb\language\language;
|
||||
use phpbb\log\log;
|
||||
use phpbb\post\post_helper;
|
||||
use phpbb\posting\post_helper;
|
||||
use phpbb\search\exception\no_search_backend_found_exception;
|
||||
use phpbb\search\search_backend_factory;
|
||||
use phpbb\search\state_helper;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace phpbb\post;
|
||||
namespace phpbb\posting;
|
||||
|
||||
use phpbb\db\driver\driver_interface;
|
||||
|
74
tests/console/searchindex/base.php
Normal file
74
tests/console/searchindex/base.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
use phpbb\config\config;
|
||||
use phpbb\di\service_collection;
|
||||
use phpbb\language\language;
|
||||
use phpbb\log\log;
|
||||
use phpbb\posting\post_helper;
|
||||
use phpbb\search\search_backend_factory;
|
||||
use phpbb\search\state_helper;
|
||||
use phpbb\user;
|
||||
|
||||
require_once __DIR__ . '/mock/search_backend_mock.php';
|
||||
|
||||
class base extends phpbb_test_case
|
||||
{
|
||||
/** @var config */
|
||||
protected $config;
|
||||
|
||||
/** @var language */
|
||||
protected $language;
|
||||
|
||||
/** @var log */
|
||||
protected $log;
|
||||
|
||||
/** @var post_helper */
|
||||
protected $post_helper;
|
||||
|
||||
/** @var user */
|
||||
protected $user;
|
||||
|
||||
/** @var search_backend_factory */
|
||||
protected $search_backend_factory;
|
||||
|
||||
/** @var state_helper */
|
||||
protected $state_helper;
|
||||
|
||||
/** @var service_collection */
|
||||
protected $search_backend_collection;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
global $phpbb_root_path, $phpEx;
|
||||
|
||||
$this->config = new \phpbb\config\config([
|
||||
'search_indexing_state' => [],
|
||||
'search_type' => 'search_backend_mock'
|
||||
]);
|
||||
|
||||
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
|
||||
$this->language = new \phpbb\language\language($lang_loader);
|
||||
|
||||
$this->log = $this->createMock('\phpbb\log\log');
|
||||
|
||||
$this->post_helper = $this->createMock('\phpbb\posting\post_helper');
|
||||
$this->post_helper
|
||||
->method('get_max_post_id')
|
||||
->willReturn(1000);
|
||||
|
||||
$this->user = $this->createMock('\phpbb\user');
|
||||
|
||||
$phpbb_container = new phpbb_mock_container_builder();
|
||||
$search_backend_mock = new search_backend_mock();
|
||||
$this->search_backend_collection = new \phpbb\di\service_collection($phpbb_container);
|
||||
$this->search_backend_collection->add('search_backend_mock');
|
||||
$this->search_backend_collection->add_service_class('search_backend_mock', 'search_backend_mock');
|
||||
$phpbb_container->set('search_backend_mock', $search_backend_mock);
|
||||
|
||||
$this->search_backend_factory = new search_backend_factory($this->config, $this->search_backend_collection);
|
||||
|
||||
$this->state_helper = new state_helper($this->config, $this->search_backend_factory);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
}
|
70
tests/console/searchindex/create_test.php
Normal file
70
tests/console/searchindex/create_test.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
use phpbb\console\command\searchindex\create;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
require_once __DIR__ . '/base.php';
|
||||
require_once __DIR__ . '/mock/search_backend_mock.php';
|
||||
|
||||
class create_test extends base
|
||||
{
|
||||
public function get_command_tester()
|
||||
{
|
||||
$application = new Application();
|
||||
$application->add(new create(
|
||||
$this->config,
|
||||
$this->language,
|
||||
$this->log,
|
||||
$this->post_helper,
|
||||
$this->search_backend_factory,
|
||||
$this->state_helper,
|
||||
$this->user
|
||||
));
|
||||
|
||||
$command = $application->find('searchindex:create');
|
||||
|
||||
return new CommandTester($command);
|
||||
}
|
||||
|
||||
public function test_create()
|
||||
{
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'search_backend_mock',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_CREATE_SUCCESS', $command_tester->getDisplay());
|
||||
}
|
||||
|
||||
public function test_create_when_search_backend_dont_exist()
|
||||
{
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'missing',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::FAILURE, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $command_tester->getDisplay());
|
||||
}
|
||||
|
||||
public function test_create_when_action_in_progress()
|
||||
{
|
||||
$this->config['search_indexing_state'] = ['not', 'empty'];
|
||||
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'search_backend_mock',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::FAILURE, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $command_tester->getDisplay());
|
||||
|
||||
$this->config['search_indexing_state'] = [];
|
||||
}
|
||||
}
|
70
tests/console/searchindex/delete_test.php
Normal file
70
tests/console/searchindex/delete_test.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
use phpbb\console\command\searchindex\delete;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
require_once __DIR__ . '/base.php';
|
||||
require_once __DIR__ . '/mock/search_backend_mock.php';
|
||||
|
||||
class delete_test extends base
|
||||
{
|
||||
public function get_command_tester()
|
||||
{
|
||||
$application = new Application();
|
||||
$application->add(new delete(
|
||||
$this->config,
|
||||
$this->language,
|
||||
$this->log,
|
||||
$this->post_helper,
|
||||
$this->search_backend_factory,
|
||||
$this->state_helper,
|
||||
$this->user
|
||||
));
|
||||
|
||||
$command = $application->find('searchindex:delete');
|
||||
|
||||
return new CommandTester($command);
|
||||
}
|
||||
|
||||
public function test_delete()
|
||||
{
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'search_backend_mock',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_DELETE_SUCCESS', $command_tester->getDisplay());
|
||||
}
|
||||
|
||||
public function test_delete_when_search_backend_dont_exist()
|
||||
{
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'missing',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::FAILURE, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $command_tester->getDisplay());
|
||||
}
|
||||
|
||||
public function test_delete_when_action_in_progress()
|
||||
{
|
||||
$this->config['search_indexing_state'] = ['not', 'empty'];
|
||||
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([
|
||||
'search-backend' => 'search_backend_mock',
|
||||
]);
|
||||
|
||||
$this->assertEquals(Command::FAILURE, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $command_tester->getDisplay());
|
||||
|
||||
$this->config['search_indexing_state'] = [];
|
||||
}
|
||||
}
|
38
tests/console/searchindex/list_all_test.php
Normal file
38
tests/console/searchindex/list_all_test.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use phpbb\console\command\searchindex\list_all;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
require_once __DIR__ . '/base.php';
|
||||
require_once __DIR__ . '/mock/search_backend_mock.php';
|
||||
|
||||
class list_all_test extends base
|
||||
{
|
||||
public function get_command_tester()
|
||||
{
|
||||
$application = new Application();
|
||||
$application->add(new list_all(
|
||||
$this->config,
|
||||
$this->language,
|
||||
$this->search_backend_collection,
|
||||
$this->user
|
||||
));
|
||||
|
||||
$command = $application->find('searchindex:list');
|
||||
|
||||
return new CommandTester($command);
|
||||
}
|
||||
|
||||
public function test_list()
|
||||
{
|
||||
$command_tester = $this->get_command_tester();
|
||||
|
||||
$command_tester->execute([]);
|
||||
|
||||
$this->assertEquals(Command::SUCCESS, $command_tester->getStatusCode());
|
||||
$this->assertStringContainsString('Mock search backend', $command_tester->getDisplay());
|
||||
$this->assertStringContainsString('ACTIVE', $command_tester->getDisplay());
|
||||
}
|
||||
}
|
105
tests/console/searchindex/mock/search_backend_mock.php
Normal file
105
tests/console/searchindex/mock/search_backend_mock.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
use phpbb\search\backend\search_backend_interface;
|
||||
|
||||
class search_backend_mock implements search_backend_interface
|
||||
{
|
||||
public $index_created = false;
|
||||
|
||||
public function get_name(): string
|
||||
{
|
||||
return 'Mock search backend';
|
||||
}
|
||||
|
||||
public function is_available(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function get_search_query(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function get_common_words(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_word_length()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function split_keywords(string &$keywords, string $terms): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function keyword_search(string $type, string $fields, string $terms, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function author_search(string $type, bool $firstpost_only, array $sort_by_sql, string $sort_key, string $sort_dir, string $sort_days, array $ex_fid_ary, string $post_visibility, int $topic_id, array $author_ary, string $author_name, array &$id_ary, int &$start, int $per_page)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function supports_phrase_search(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function index(string $mode, int $post_id, string &$message, string &$subject, int $poster_id, int $forum_id)
|
||||
{
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public function index_remove(array $post_ids, array $author_ids, array $forum_ids): void
|
||||
{
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public function tidy(): void
|
||||
{
|
||||
// Nothing
|
||||
}
|
||||
|
||||
public function create_index(int &$post_counter = 0): ?array
|
||||
{
|
||||
$this->index_created = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function delete_index(int &$post_counter = 0): ?array
|
||||
{
|
||||
$this->index_created = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function index_created(): bool
|
||||
{
|
||||
return $this->index_created;
|
||||
}
|
||||
|
||||
public function index_stats()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_acp_options(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_type(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue