2006-10-09 18:00:14 +00:00
< ? php
2006-12-27 21:20:26 +00:00
/*
* $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_Enum_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 23:33:04 +00:00
class Doctrine_Enum_TestCase extends Doctrine_UnitTestCase
{
public function prepareData ()
{ }
public function prepareTables ()
{
2007-06-29 21:27:41 +00:00
$this -> tables = array ( " EnumTest " , " EnumTest2 " , " EnumTest3 " );
2006-10-09 18:00:14 +00:00
parent :: prepareTables ();
}
2007-06-19 23:33:04 +00:00
public function testParameterConversion ()
{
2006-10-09 18:00:14 +00:00
$test = new EnumTest ();
$test -> status = 'open' ;
$this -> assertEqual ( $test -> status , 'open' );
$test -> save ();
2006-10-15 22:50:46 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
2007-06-25 20:08:16 +00:00
$ret = $query -> parseQuery ( 'FROM EnumTest WHERE EnumTest.status = ?' )
-> execute ( array ( 'open' ));
2006-10-15 22:50:46 +00:00
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
}
2006-10-09 18:00:14 +00:00
2006-10-15 22:50:46 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
2006-10-26 20:53:59 +00:00
$ret = $query -> query ( " FROM EnumTest WHERE EnumTest.status = 'open' " );
2006-10-15 22:50:46 +00:00
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
2007-06-29 21:52:18 +00:00
}
}
public function testEnumFetchArray () {
$q = new Doctrine_Query ();
$q -> select ( 'e.*' )
-> from ( 'EnumTest e' )
-> limit ( 1 );
$ret = $q -> execute ( array (), Doctrine :: FETCH_ARRAY );
if ( is_numeric ( $ret [ 0 ][ 'status' ]))
{
$this -> fail ();
2006-10-15 22:50:46 +00:00
}
}
2007-06-19 23:33:04 +00:00
public function testInAndNotIn ()
{
2006-10-15 22:50:46 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
2006-10-26 20:53:59 +00:00
$ret = $query -> query ( " FROM EnumTest WHERE EnumTest.status IN ('open') " );
2006-10-15 22:50:46 +00:00
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
}
2006-10-09 18:00:14 +00:00
2006-10-15 22:50:46 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
2006-10-26 20:53:59 +00:00
$ret = $query -> query ( " FROM EnumTest WHERE EnumTest.status NOT IN ('verified', 'closed') " );
2006-10-15 22:50:46 +00:00
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
}
2006-10-09 18:00:14 +00:00
}
2006-10-15 22:50:46 +00:00
2007-06-19 23:33:04 +00:00
public function testExpressionComposition ()
{
2006-10-15 22:50:46 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
2006-10-26 20:53:59 +00:00
$ret = $query -> query ( " FROM EnumTest e WHERE e.id > 0 AND (e.status != 'closed' OR e.status = 'verified') " );
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
}
}
2007-06-19 23:33:04 +00:00
public function testNotEqual ()
{
2006-10-26 20:53:59 +00:00
try {
$query = new Doctrine_Query ( $this -> connection );
$ret = $query -> query ( " FROM EnumTest WHERE EnumTest.status != 'closed' " );
2006-10-15 22:50:46 +00:00
$this -> assertEqual ( count ( $ret ), 1 );
} catch ( Exception $e ) {
$this -> fail ();
}
}
2007-06-19 23:33:04 +00:00
public function testEnumType ()
{
2006-10-09 18:00:14 +00:00
$enum = new EnumTest ();
2007-06-25 20:08:16 +00:00
$enum -> status = 'open' ;
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
$enum -> save ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
$enum -> refresh ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
2007-06-25 20:08:16 +00:00
$enum -> status = 'closed' ;
2006-10-09 18:00:14 +00:00
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2006-10-09 18:00:14 +00:00
$enum -> save ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2006-10-09 18:00:14 +00:00
$this -> assertTrue ( is_numeric ( $enum -> id ));
$enum -> refresh ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2006-10-09 18:00:14 +00:00
}
2007-06-19 23:33:04 +00:00
public function testEnumTypeWithCaseConversion ()
{
$this -> conn -> setAttribute ( PDO :: ATTR_CASE , PDO :: CASE_UPPER );
2006-10-09 18:00:14 +00:00
$enum = new EnumTest ();
2007-06-25 20:08:16 +00:00
$enum -> status = 'open' ;
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
$enum -> save ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
$enum -> refresh ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'open' );
2006-10-09 18:00:14 +00:00
2007-06-25 20:08:16 +00:00
$enum -> status = 'closed' ;
2006-10-09 18:00:14 +00:00
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2006-10-09 18:00:14 +00:00
$enum -> save ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2006-10-09 18:00:14 +00:00
$enum -> refresh ();
2007-06-25 20:08:16 +00:00
$this -> assertEqual ( $enum -> status , 'closed' );
2007-06-19 23:33:04 +00:00
$this -> conn -> setAttribute ( PDO :: ATTR_CASE , PDO :: CASE_NATURAL );
2006-10-09 18:00:14 +00:00
}
2007-06-19 23:33:04 +00:00
public function testFailingRefresh ()
{
2006-10-09 18:00:14 +00:00
$enum = $this -> connection -> getTable ( 'EnumTest' ) -> find ( 1 );
2007-06-19 23:33:04 +00:00
$this -> conn -> exec ( 'DELETE FROM enum_test WHERE id = 1' );
2006-10-09 18:00:14 +00:00
$f = false ;
try {
$enum -> refresh ();
} catch ( Doctrine_Record_Exception $e ) {
$f = true ;
}
$this -> assertTrue ( $f );
}
2006-10-26 20:53:59 +00:00
2007-06-29 21:27:41 +00:00
public function testEnumWithLeftJoin ()
{
// sorry, odd I know, best I can do... pookey!
try {
$q = new Doctrine_Query ( $this -> connection );
$q -> select ( 'e.*, f.*, g.status' )
-> from ( 'EnumTest e' )
-> innerjoin ( 'e.Enum2 g' )
-> leftjoin ( 'e.Enum3 f' )
-> where ( " e.status = 'verified' " )
-> limit ( 1 )
-> execute ();
$this -> assertNotEqual ( $q -> getQuery (), " SELECT e.id AS e__id, e.status AS e__status, e.text AS e__text, e2.id AS e2__id, e2.status AS e2__status, e3.text AS e3__text FROM enum_test e INNER JOIN enum_test2 e2 ON e.id = e2.enum_test_id LEFT JOIN enum_test3 e3 ON e.text = e3.text WHERE e.id IN (SELECT DISTINCT e4.id FROM enum_test e4 INNER JOIN enum_test2 e5 ON e4.id = e5.enum_test_id LEFT JOIN enum_test3 e6 ON e4.text = e6.text WHERE e4.status = 'verified' LIMIT 1) AND e.status = 'verified' " );
} catch ( Exception $e ) {
$this -> fail ();
}
}
2006-10-09 18:00:14 +00:00
}
?>