TokenizerTestCase.php 5.09 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id: TokenizerTestCase.php 1181 2007-03-20 23:22:51Z gnat $
 *
 * 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
19
 * <http://www.phpdoctrine.org>.
zYne's avatar
zYne committed
20 21 22 23 24 25 26 27 28
 */

/**
 * Doctrine_Tokenizer_TestCase
 * This class tests the functinality of Doctrine_Tokenizer component
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
29
 * @link        www.phpdoctrine.org
zYne's avatar
zYne committed
30 31 32 33
 * @since       1.0
 * @version     $Revision: 1181 $
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
34
class Doctrine_Tokenizer_TestCase extends Doctrine_UnitTestCase
zYne's avatar
zYne committed
35
{
36
    public function prepareData()
zYne's avatar
zYne committed
37
    { }
38
    public function prepareTables()
zYne's avatar
zYne committed
39
    { }
40

zYne's avatar
zYne committed
41 42
    public function testSqlExplode()
    {
43
        $tokenizer = new Doctrine_Query_Tokenizer();
44

zYne's avatar
zYne committed
45
        $str = "word1 word2 word3";
46
        $a   = $tokenizer->sqlExplode($str);
47

zYne's avatar
zYne committed
48
        $this->assertEqual($a, array("word1", "word2", "word3"));
49

zYne's avatar
zYne committed
50
        $str = "word1 (word2 word3)";
51
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
52
        $this->assertEqual($a, array("word1", "(word2 word3)"));
53

zYne's avatar
zYne committed
54
        $str = "word1 'word2 word3'";
55
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
56 57 58
        $this->assertEqual($a, array("word1", "'word2 word3'"));

        $str = "word1 'word2 word3'";
59
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
60 61 62
        $this->assertEqual($a, array("word1", "'word2 word3'"));

        $str = "word1 \"word2 word3\"";
63
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
64 65 66
        $this->assertEqual($a, array("word1", "\"word2 word3\""));

        $str = "word1 ((word2) word3)";
67
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
68 69 70
        $this->assertEqual($a, array("word1", "((word2) word3)"));

        $str = "word1 ( (word2) 'word3')";
71
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
72 73 74
        $this->assertEqual($a, array("word1", "( (word2) 'word3')"));

        $str = "word1 ( \"(word2) 'word3')";
75
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
76 77 78
        $this->assertEqual($a, array("word1", "( \"(word2) 'word3')"));

        $str = "word1 ( ''(word2) 'word3')";
79
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
80 81 82
        $this->assertEqual($a, array("word1", "( ''(word2) 'word3')"));

        $str = "word1 ( '()()'(word2) 'word3')";
83
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
84 85 86
        $this->assertEqual($a, array("word1", "( '()()'(word2) 'word3')"));

        $str = "word1 'word2)() word3'";
87
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
88 89 90
        $this->assertEqual($a, array("word1", "'word2)() word3'"));

        $str = "word1 (word2() word3)";
91
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
92 93 94
        $this->assertEqual($a, array("word1", "(word2() word3)"));

        $str = "word1 \"word2)() word3\"";
95
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
96 97 98
        $this->assertEqual($a, array("word1", "\"word2)() word3\""));

        $str = "something (subquery '')";
99
        $a   = $tokenizer->sqlExplode($str);
zYne's avatar
zYne committed
100 101 102
        $this->assertEqual($a, array("something", "(subquery '')"));

        $str = "something ((  ))";
103
        $a   = $tokenizer->sqlExplode($str);
104 105 106 107 108
        $this->assertEqual($a, array("something", "((  ))"));
    }

    public function testSqlExplode2()
    {
109
        $tokenizer = new Doctrine_Query_Tokenizer();
110
        $str = 'rdbms (dbal OR database)';
111
        $a   = $tokenizer->sqlExplode($str, ' OR ');
112 113

        $this->assertEqual($a, array('rdbms (dbal OR database)'));
zYne's avatar
zYne committed
114
    }
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129
    public function testBracketExplode()
    {
        $tokenizer = new Doctrine_Query_Tokenizer();

        $str = 'foo.field AND bar.field';
        $a   = $tokenizer->bracketExplode($str, array(' \&\& ', ' AND '), '(', ')');
        $this->assertEqual($a, array('foo.field', 'bar.field'));

        // delimiters should be case insensitive
        $str = 'foo.field and bar.field';
        $a   = $tokenizer->bracketExplode($str, array(' \&\& ', ' AND '), '(', ')');
        $this->assertEqual($a, array('foo.field', 'bar.field'));
    }

130 131 132

    public function testQuoteExplodedShouldQuoteArray()
    {
133 134
        $tokenizer = new Doctrine_Query_Tokenizer();
        $term = $tokenizer->quoteExplode("test", array("'test'", "test2"));
135 136
        $this->assertEqual($term[0], "test");
    }
zYne's avatar
zYne committed
137
}