1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/tests/Search/QueryTestCase.php

329 lines
13 KiB
PHP
Raw Normal View History

2007-07-13 01:50:08 +04:00
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Search_Query_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Search_Query_TestCase extends Doctrine_UnitTestCase
{
public function prepareTables()
{
$this->tables = array('SearchTest', 'SearchTestIndex');
2007-07-28 00:19:19 +04:00
2007-07-13 01:50:08 +04:00
parent::prepareTables();
}
public function prepareData()
{ }
public function testInitData()
{
$e = new SearchTest();
2007-07-13 01:50:08 +04:00
$e->title = 'Once there was an ORM framework';
$e->content = 'There are many ORM frameworks, but nevertheless we decided to create one.';
$e->save();
$e = new SearchTest();
2007-07-13 01:50:08 +04:00
$e->title = 'Doctrine development continues';
$e->content = 'The development has been going well so far.';
$e->save();
}
2007-07-30 23:34:05 +04:00
2007-07-27 23:03:48 +04:00
public function testParseClauseSupportsAndOperator()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('doctrine AND orm');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-07-28 00:19:19 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperatorsAndParenthesis()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('((doctrine OR orm) AND dbal) OR database');
$sql = '(id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)) OR keyword = ?';
2007-07-27 23:03:48 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperators3()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('doctrine OR orm AND dbal');
$sql = 'keyword = ? OR id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-07-28 00:19:19 +04:00
2007-07-27 23:03:48 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperators()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('(doctrine OR orm) AND dbal');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-07-28 00:19:19 +04:00
2007-07-27 23:03:48 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperators2()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('(doctrine OR orm) dbal');
2007-07-28 00:19:19 +04:00
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-07-28 00:19:19 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperatorsAndDeeplyNestedParenthesis()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('(((doctrine OR orm) AND dbal) OR database) AND rdbms');
$sql = '((id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)) OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-08-02 03:41:51 +04:00
$this->assertEqual($ret, $sql);
}
public function testParseClauseSupportsMixingOfOperatorsAndParenthesis2()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('rdbms (dbal OR database)');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?)';
2007-07-28 00:19:19 +04:00
2007-07-27 23:03:48 +04:00
$this->assertEqual($ret, $sql);
}
2007-08-02 03:41:51 +04:00
2007-07-30 19:54:50 +04:00
public function testParseClauseSupportsMixingOfOperatorsAndDeeplyNestedParenthesis2()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('rdbms (((doctrine OR orm) AND dbal) OR database)');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND ((id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) AND id IN (SELECT id FROM search_test_index WHERE keyword = ?)) OR keyword = ?)';
2007-07-27 23:03:48 +04:00
2007-07-30 19:54:50 +04:00
$this->assertEqual($ret, $sql);
}
2007-07-30 23:34:05 +04:00
2007-07-30 23:01:59 +04:00
public function testParseClauseSupportsNegationOperator()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('rdbms -doctrine');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND '
. 'id NOT IN (SELECT id FROM search_test_index WHERE keyword = ?)';
2007-07-30 23:01:59 +04:00
$this->assertEqual($ret, $sql);
}
2007-08-02 03:41:51 +04:00
2007-07-30 23:01:59 +04:00
public function testParseClauseOrOperator2()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('rdbms doctrine OR database');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND '
. 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
2007-07-30 23:01:59 +04:00
. 'OR keyword = ?';
$this->assertEqual($ret, $sql);
}
2007-08-02 03:41:51 +04:00
2007-07-30 23:01:59 +04:00
public function testParseClauseSupportsNegationOperatorWithOrOperator()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$ret = $q->parseClause('rdbms -doctrine OR database');
$sql = 'id IN (SELECT id FROM search_test_index WHERE keyword = ?) AND '
. 'id NOT IN (SELECT id FROM search_test_index WHERE keyword = ?) '
2007-07-30 23:01:59 +04:00
. 'OR keyword = ?';
$this->assertEqual($ret, $sql);
}
2007-08-02 03:41:51 +04:00
2007-07-27 23:03:48 +04:00
public function testSearchSupportsAndOperator()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doctrine AND orm');
2007-07-27 23:03:48 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-27 23:03:48 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-07-27 23:03:48 +04:00
$this->assertEqual($q->getSql(), $sql);
}
2007-08-02 03:41:51 +04:00
2007-07-27 23:03:48 +04:00
public function testSearchSupportsOrOperator()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doctrine OR orm');
2007-07-27 23:03:48 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-27 23:03:48 +04:00
. 'FROM search_test_index '
. 'WHERE keyword = ? OR keyword = ? '
. 'GROUP BY id ORDER BY relevance';
2007-07-27 23:03:48 +04:00
$this->assertEqual($q->getSql(), $sql);
}
2007-07-30 23:01:59 +04:00
public function testQuerySupportsSingleWordquery()
2007-07-27 23:03:48 +04:00
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doctrine');
2007-07-27 23:03:48 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
. 'FROM search_test_index WHERE keyword = ? GROUP BY id ORDER BY relevance';
2007-08-02 16:23:41 +04:00
$this->assertEqual($q->getParams(), array('doctrine'));
2007-07-27 23:03:48 +04:00
$this->assertEqual($q->getSql(), $sql);
}
2007-07-30 23:34:05 +04:00
2007-07-30 23:01:59 +04:00
public function testSearchSupportsMixingOfOperators()
2007-07-13 01:50:08 +04:00
{
2007-07-26 23:06:42 +04:00
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('(doctrine OR orm) AND dbal');
2007-07-27 03:44:18 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-30 23:01:59 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword = ? OR keyword = ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-07-27 03:44:18 +04:00
2007-08-02 16:23:41 +04:00
$this->assertEqual($q->getParams(), array('doctrine', 'orm', 'dbal'));
2007-07-27 03:44:18 +04:00
$this->assertEqual($q->getSql(), $sql);
}
public function testSearchSupportsSingleTermWithQuotes()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query("'doctrine orm'");
2007-07-30 23:01:59 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-27 03:44:18 +04:00
. 'FROM search_test_index WHERE keyword = ? '
. 'AND (position + 1) = (SELECT position FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-07-27 03:44:18 +04:00
2007-08-02 16:23:41 +04:00
$this->assertEqual($q->getParams(), array('doctrine', 'orm'));
2007-07-27 03:44:18 +04:00
$this->assertEqual($q->getSql(), $sql);
}
2007-07-30 23:01:59 +04:00
2007-07-27 03:44:18 +04:00
public function testSearchSupportsSingleLongTermWithQuotes()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query("'doctrine orm dbal'");
2007-07-30 23:01:59 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-27 03:44:18 +04:00
. 'FROM search_test_index WHERE keyword = ? '
. 'AND (position + 1) = (SELECT position FROM search_test_index WHERE keyword = ?) '
. 'AND (position + 2) = (SELECT position FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-07-27 03:44:18 +04:00
2007-08-02 16:07:56 +04:00
$this->assertEqual($q->getParams(), array('doctrine', 'orm', 'dbal'));
2007-07-26 23:06:42 +04:00
$this->assertEqual($q->getSql(), $sql);
2007-07-13 01:50:08 +04:00
}
2007-07-27 03:44:18 +04:00
public function testQuerySupportsMultiWordquery()
2007-07-13 01:50:08 +04:00
{
2007-07-26 23:06:42 +04:00
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doctrine orm');
2007-07-13 01:50:08 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-26 23:06:42 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-07-13 01:50:08 +04:00
2007-07-26 23:06:42 +04:00
$this->assertEqual($q->getSql(), $sql);
2007-07-13 01:50:08 +04:00
}
2007-07-27 03:44:18 +04:00
2007-08-02 16:01:22 +04:00
public function testQuerySupportsMultiWordSearchAndSingleLetterWildcards()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doct?ine orm');
2007-08-02 16:01:22 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-08-02 16:01:22 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword LIKE ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-08-02 16:01:22 +04:00
$this->assertEqual($q->getParams(), array('doct?ine', 'orm'));
$this->assertEqual($q->getSql(), $sql);
}
public function testQuerySupportsMultiWordSearchAndMultiLetterWildcards()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query('doc* orm');
2007-08-02 16:01:22 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-08-02 16:01:22 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword LIKE ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'GROUP BY id ORDER BY relevance';
2007-08-02 16:01:22 +04:00
$this->assertEqual($q->getParams(), array('doc%', 'orm'));
$this->assertEqual($q->getSql(), $sql);
}
2007-07-27 03:44:18 +04:00
public function testSearchSupportsMultipleTermsWithQuotes()
2007-07-13 01:50:08 +04:00
{
2007-07-26 23:06:42 +04:00
$q = new Doctrine_Search_Query('SearchTestIndex');
$q->query("doctrine 'orm database'");
2007-07-26 23:06:42 +04:00
$sql = 'SELECT COUNT(keyword) AS relevance, id '
2007-07-26 23:06:42 +04:00
. 'FROM search_test_index '
. 'WHERE id IN (SELECT id FROM search_test_index WHERE keyword = ?) '
. 'AND id IN (SELECT id FROM search_test_index WHERE keyword = ? '
2007-08-02 03:41:51 +04:00
. 'AND (position + 1) = (SELECT position FROM search_test_index WHERE keyword = ?)) '
. 'GROUP BY id ORDER BY relevance';
2007-07-13 01:50:08 +04:00
2007-08-02 16:07:56 +04:00
$this->assertEqual($q->getParams(), array('doctrine', 'orm', 'database'));
2007-07-26 23:06:42 +04:00
$this->assertEqual($q->getSql(), $sql);
}
2007-07-30 23:01:59 +04:00
2007-07-27 03:44:18 +04:00
public function testSearchReturnsFalseForEmptyStrings()
{
$q = new Doctrine_Search_Query('SearchTestIndex');
$result = $q->query(' ');
2007-07-27 03:44:18 +04:00
$this->assertFalse($result);
2007-07-13 01:50:08 +04:00
}
2007-08-02 04:05:24 +04:00
2007-07-13 01:50:08 +04:00
}