diff options
author | Dhruv <dhruv.goel92@gmail.com> | 2012-11-12 13:46:15 +0100 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2012-11-27 08:48:15 -0500 |
commit | 6a76b85cbc741ef15c219c2c02c318bc43f29a59 (patch) | |
tree | f6a8debeddad4eb0601c5802e2a9e03fb19f7d0c | |
parent | 6e8f142d3994f4568e40447d7cfd60cb5b082824 (diff) | |
download | forums-6a76b85cbc741ef15c219c2c02c318bc43f29a59.tar forums-6a76b85cbc741ef15c219c2c02c318bc43f29a59.tar.gz forums-6a76b85cbc741ef15c219c2c02c318bc43f29a59.tar.bz2 forums-6a76b85cbc741ef15c219c2c02c318bc43f29a59.tar.xz forums-6a76b85cbc741ef15c219c2c02c318bc43f29a59.zip |
[ticket/11174] add mysql unit tests
PHPBB3-11174
-rw-r--r-- | tests/search/mysql_test.php | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/tests/search/mysql_test.php b/tests/search/mysql_test.php new file mode 100644 index 0000000000..d9da2dfb2d --- /dev/null +++ b/tests/search/mysql_test.php @@ -0,0 +1,155 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +function phpbb_search_wrapper($class) +{ + $wrapped = $class . '_wrapper'; + if (!class_exists($wrapped)) + { + $code = " +class $wrapped extends $class +{ + public function get_split_words() { return \$this->split_words; } +} + "; + eval($code); + } + return $wrapped; +} + +class phpbb_search_mysql_test extends phpbb_database_test_case +{ + protected $db; + protected $search; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/posts.xml'); + } + + protected function setUp() + { + global $phpbb_root_path, $phpEx, $config, $user, $cache; + + parent::setUp(); + + // dbal uses cache + $cache = new phpbb_cache_driver_null; + + $this->db = $this->new_dbal(); + $error = null; + $class = phpbb_search_wrapper('phpbb_search_fulltext_mysql'); + $this->search = new $class($error, $phpbb_root_path, $phpEx, null, $config, $this->db, $user); + } + + protected function tearDown() + { + parent::tearDown(); + } + + public function keywords() + { + return array( + // keywords + // terms + // ok + // split words + // common words + array( + 'fooo', + 'all', + true, + array('fooo'), + array(), + ), + array( + 'fooo baar', + 'all', + true, + array('fooo', 'baar'), + array(), + ), + // leading, trailing and multiple spaces + array( + ' foo bar ', + 'all', + true, + array('foo', 'bar'), + array(), + ), + // words too short + array( + 'f', + 'all', + false, + null, + // short words count as "common" words + array('f'), + ), + array( + 'f o o', + 'all', + false, + null, + array('f', 'o', 'o'), + ), + array( + 'foo -bar', + 'all', + true, + array('-bar', 'foo'), + array(), + ), + // all negative + array( + '-foo', + 'all', + false, + null, + array(), + ), + array( + '-foo -bar', + 'all', + false, + array('-foo', '-bar'), + array(), + ), + ); + } + + /** + * @dataProvider keywords + */ + public function test_split_keywords($keywords, $terms, $ok, $split_words, $common) + { + $rv = $this->search->split_keywords($keywords, $terms); + $this->assertEquals($ok, $rv); + if ($ok) + { + // only check criteria if the search is going to be performed + $this->assert_array_content_equals($split_words, $this->search->get_split_words()); + } + $this->assert_array_content_equals($common, $this->search->get_common_words()); + } + + public function assert_array_content_equals($one, $two) + { + if (sizeof(array_diff($one, $two)) || sizeof(array_diff($two, $one))) + { + // get a nice error message + $this->assertEquals($one, $two); + } + else + { + // increase assertion count + $this->assertTrue(true); + } + } +} |