1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/tests/RawSqlTestCase.php

252 lines
10 KiB
PHP
Raw Normal View History

2006-07-22 03:22:15 +04:00
<?php
2007-05-16 13:07:22 +04:00
/*
* $Id: RawSqlTestCase.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
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_RawSql_TestCase
* This class tests the functinality of Doctrine_RawSql component
*
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1181 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{
2007-06-26 14:05:26 +04:00
2007-05-19 21:49:50 +04:00
public function testQueryParser()
2007-05-16 13:07:22 +04:00
{
2007-05-19 21:49:50 +04:00
$sql = 'SELECT {p.*} FROM photos p';
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-08-20 23:21:52 +04:00
$query->parseQuery($sql);
2007-05-19 21:49:50 +04:00
$this->assertEqual($query->getQueryPart('from'), array('photos p'));
2006-08-20 23:21:52 +04:00
2007-05-19 21:49:50 +04:00
$sql = 'SELECT {p.*} FROM (SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE p.can_see = -1 AND t.tag_id = 62 LIMIT 200';
2006-08-20 23:21:52 +04:00
$query->parseQuery($sql);
2007-05-19 21:49:50 +04:00
$this->assertEqual($query->getQueryPart('from'), array('(SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id'));
$this->assertEqual($query->getQueryPart('where'), array('p.can_see = -1 AND t.tag_id = 62'));
$this->assertEqual($query->getQueryPart('limit'), array(200));
2006-08-20 23:21:52 +04:00
}
2007-05-16 13:07:22 +04:00
public function testAsteriskOperator()
{
2006-07-22 03:22:15 +04:00
// Selecting with *
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.*} FROM entity');
2006-07-22 03:22:15 +04:00
$fields = $query->getFields();
2007-05-19 21:49:50 +04:00
$this->assertEqual($fields, array('entity.*'));
2006-07-22 03:22:15 +04:00
2007-05-19 21:49:50 +04:00
$query->addComponent('entity', 'Entity');
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
}
2007-05-16 13:07:22 +04:00
public function testLazyPropertyLoading()
{
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
$this->connection->clear();
2006-07-22 03:22:15 +04:00
// selecting proxy objects (lazy property loading)
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
2006-07-22 03:22:15 +04:00
$fields = $query->getFields();
2007-05-19 21:49:50 +04:00
$this->assertEqual($fields, array('entity.name', 'entity.id'));
$query->addComponent('entity', 'Entity');
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY);
2006-07-22 03:22:15 +04:00
}
2007-05-16 13:07:22 +04:00
public function testSmartMapping()
{
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
// smart component mapping (no need for additional addComponent call
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
2006-07-22 03:22:15 +04:00
$fields = $query->getFields();
2007-05-19 21:49:50 +04:00
$this->assertEqual($fields, array('entity.name', 'entity.id'));
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY);
2006-07-22 03:22:15 +04:00
}
2007-05-16 13:07:22 +04:00
public function testMultipleComponents()
{
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
// multi component fetching
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id');
2006-07-22 03:22:15 +04:00
2007-05-19 21:49:50 +04:00
$query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber');
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$count = $this->conn->count();
2006-08-07 00:46:12 +04:00
2007-05-19 21:49:50 +04:00
$coll[4]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->conn->count());
2007-05-19 21:49:50 +04:00
$coll[5]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->conn->count());
2007-05-19 21:49:50 +04:00
}
public function testAliasesAreSupportedInAddComponent()
{
$query = new Doctrine_RawSql();
$query->parseQuery('SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id');
$query->addComponent('entity', 'Entity e');
$query->addComponent('phonenumber', 'e.Phonenumber');
$this->assertEqual(array_keys($query->getAliasMap()), array('e', 'e.Phonenumber'));
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2006-08-07 00:46:12 +04:00
$count = $this->conn->count();
2006-07-22 03:22:15 +04:00
2007-06-26 14:05:26 +04:00
$coll[4]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count());
2006-07-22 03:22:15 +04:00
2007-06-26 14:05:26 +04:00
$coll[5]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count());
2006-07-22 03:22:15 +04:00
}
2007-05-19 21:49:50 +04:00
public function testPrimaryKeySelectForcing()
2007-05-16 13:07:22 +04:00
{
2006-07-22 03:22:15 +04:00
// forcing the select of primary key fields
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name} FROM entity');
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testConvenienceMethods()
2007-05-16 13:07:22 +04:00
{
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
$query->select('{entity.name}')->from('entity');
2007-05-19 21:49:50 +04:00
$query->addComponent('entity', 'User');
2007-05-21 00:35:35 +04:00
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
2006-08-22 23:34:40 +04:00
2007-05-16 13:07:22 +04:00
public function testColumnAggregationInheritance()
{
2006-07-22 03:22:15 +04:00
// forcing the select of primary key fields
2007-05-19 21:49:50 +04:00
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name} FROM entity');
$query->addComponent('entity', 'User');
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
2006-08-22 23:34:40 +04:00
2007-05-16 13:07:22 +04:00
public function testColumnAggregationInheritanceWithOrderBy()
{
2006-08-22 23:34:40 +04:00
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->connection);
2007-05-19 21:49:50 +04:00
$query->parseQuery('SELECT {entity.name} FROM entity ORDER BY entity.name');
$query->addComponent('entity', 'User');
2006-08-22 23:34:40 +04:00
$this->assertEqual($query->getQuery(), "SELECT entity.name AS entity__name, entity.id AS entity__id FROM entity WHERE entity.type = 0 ORDER BY entity.name");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
2007-05-16 13:07:22 +04:00
public function testQueryParser2()
{
$query = new Doctrine_RawSql();
$query->parseQuery("SELECT {entity.name} FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
$this->assertEqual($query->getQuery(),
"SELECT entity.name AS entity__name, entity.id AS entity__id FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
}
2007-06-20 16:39:56 +04:00
2007-06-26 14:05:26 +04:00
public function testSelectingWithoutIdentifiersOnRootComponent()
2007-06-20 16:39:56 +04:00
{
$query = new Doctrine_RawSql();
2007-06-26 14:05:26 +04:00
$query->parseQuery('SELECT {entity.name}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
2007-06-20 16:39:56 +04:00
$query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber');
2007-06-26 14:05:26 +04:00
$this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$coll = $query->execute(array(), Doctrine::FETCH_ARRAY);
2007-06-20 16:39:56 +04:00
2007-06-26 14:05:26 +04:00
$this->assertEqual(count($coll), 3);
}
public function testSwitchingTheFieldOrder()
{
$query = new Doctrine_RawSql();
$query->parseQuery('SELECT {phonenumber.*}, {entity.name} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber');
$this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$coll = $query->execute(array(), Doctrine::FETCH_ARRAY);
$this->assertEqual(count($coll), 3);
2007-06-20 16:39:56 +04:00
}
2006-07-22 03:22:15 +04:00
}