1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/tests/HookTestCase.php

173 lines
6.8 KiB
PHP
Raw Normal View History

2006-12-24 01:45:36 +03: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_Hook_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$
*/
2007-06-19 14:09:48 +04:00
class Doctrine_Hook_TestCase extends Doctrine_UnitTestCase
{
public function testWordLikeParserSupportsHyphens()
{
2007-01-06 01:07:50 +03:00
$parser = new Doctrine_Hook_WordLike();
$parser->parse('u', 'name', "'some guy' OR zYne");
$this->assertEqual($parser->getCondition(), '(u.name LIKE ? OR u.name LIKE ?)');
2007-11-16 01:12:00 +03:00
$this->assertEqual($parser->getParams(), array('%some guy%', '%zYne%'));
2007-01-06 01:07:50 +03:00
}
2007-11-16 01:12:00 +03:00
public function testHookOrderbyAcceptsArray()
2007-06-19 14:09:48 +04:00
{
2006-12-26 23:33:40 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['orderby'] = array('u.name ASC');
$hook->hookOrderBy($a['orderby']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) ORDER BY e.name ASC');
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testHookOrderbyAcceptsDescendingOrder()
2007-06-19 14:09:48 +04:00
{
2006-12-26 23:33:40 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['orderby'] = array('u.name DESC');
$hook->hookOrderBy($a['orderby']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) ORDER BY e.name DESC');
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testHookOrderbyDoesntAcceptUnknownColumn()
2007-06-19 14:09:48 +04:00
{
2006-12-26 23:33:40 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['orderby'] = array('u.unknown DESC');
$hook->hookOrderBy($a['orderby']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testHookOrderbyAcceptsMultipleParameters()
2007-06-19 14:09:48 +04:00
{
2006-12-26 23:33:40 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['orderby'] = array('u.name ASC', 'u.id DESC');
2006-12-24 01:45:36 +03:00
2006-12-26 23:33:40 +03:00
$hook->hookOrderBy($a['orderby']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) ORDER BY e.name ASC, e.id DESC');
2007-11-16 01:12:00 +03:00
2006-12-26 23:33:40 +03:00
$users = $hook->getQuery()->execute();
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testHookWhereAcceptsArrays()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['where'] = array('u.name' => 'Jack Daniels',
'u.loginname' => 'TheMan');
$hook->hookWhere($a['where']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.name LIKE ? OR e.name LIKE ?) AND e.loginname LIKE ? AND (e.type = 0)');
2007-11-16 01:12:00 +03:00
$this->assertEqual($hook->getQuery()->getParams(), array('%Jack%', '%Daniels%', '%TheMan%'));
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testHookWhereSupportsIntegerTypes()
2007-06-19 14:09:48 +04:00
{
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
2006-12-26 23:33:40 +03:00
$a['where'] = array('u.id' => 10000);
$hook->hookWhere($a['where']);
$this->assertEqual($hook->getQuery()->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id = ? AND (e.type = 0)');
$this->assertEqual($hook->getQuery()->getParams(), array(10000));
2006-12-26 23:33:40 +03:00
}
2007-11-16 01:12:00 +03:00
public function testHookWhereDoesntAcceptUnknownColumn()
2007-06-19 14:09:48 +04:00
{
2006-12-26 23:33:40 +03:00
$hook = new Doctrine_Hook('SELECT u.name FROM User u LEFT JOIN u.Phonenumber p');
$a['where'] = array('u.unknown' => 'Jack Daniels');
$hook->hookWhere($a['where']);
2007-07-06 03:47:48 +04:00
$this->assertEqual($hook->getQuery()->getSql(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
2006-12-24 01:45:36 +03:00
}
2007-01-06 01:07:50 +03:00
2007-11-16 01:12:00 +03:00
public function testEqualParserUsesEqualOperator()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$parser = new Doctrine_Hook_Equal();
2007-01-06 01:07:50 +03:00
2006-12-24 01:45:36 +03:00
$parser->parse('u', 'name', 'zYne');
2007-11-16 01:12:00 +03:00
2006-12-24 01:45:36 +03:00
$this->assertEqual($parser->getCondition(), 'u.name = ?');
$this->assertEqual($parser->getParams(), array('zYne'));
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testWordLikeParserUsesLikeOperator()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$parser = new Doctrine_Hook_WordLike();
2007-11-16 01:12:00 +03:00
2006-12-24 01:45:36 +03:00
$parser->parse('u', 'name', 'zYne');
2007-11-16 01:12:00 +03:00
2006-12-24 01:45:36 +03:00
$this->assertEqual($parser->getCondition(), 'u.name LIKE ?');
2007-11-16 01:12:00 +03:00
$this->assertEqual($parser->getParams(), array('%zYne%'));
2006-12-24 01:45:36 +03:00
}
2007-06-19 14:09:48 +04:00
2007-11-16 01:12:00 +03:00
public function testIntegerParserSupportsIntervals()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$parser = new Doctrine_Hook_Integer();
$parser->parse('m', 'year', '1998-2000');
2007-11-16 01:12:00 +03:00
2006-12-24 01:45:36 +03:00
$this->assertEqual($parser->getCondition(), '(m.year > ? AND m.year < ?)');
$this->assertEqual($parser->getParams(), array('1998', '2000'));
}
2007-11-16 01:12:00 +03:00
public function testIntegerParserSupportsEqualOperator()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$parser = new Doctrine_Hook_Integer();
$parser->parse('m', 'year', '1998');
$this->assertEqual($parser->getCondition(), 'm.year = ?');
$this->assertEqual($parser->getParams(), array('1998'));
}
2007-11-16 01:12:00 +03:00
public function testIntegerParserSupportsNestingConditions()
2007-06-19 14:09:48 +04:00
{
2006-12-24 01:45:36 +03:00
$parser = new Doctrine_Hook_Integer();
$parser->parse('m', 'year', '1998-2000 OR 2001');
$this->assertEqual($parser->getCondition(), '((m.year > ? AND m.year < ?) OR m.year = ?)');
$this->assertEqual($parser->getParams(), array('1998', '2000', '2001'));
}
2007-01-06 01:07:50 +03:00
2006-12-24 01:45:36 +03:00
}