Begin Refactor

This commit is contained in:
color.diff=auto
2021-03-21 13:47:29 -06:00
parent 7622dc0d02
commit 018931d7ae
197 changed files with 47799 additions and 6 deletions

View File

@@ -0,0 +1,120 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
/**
* Bigint
*
* Tests handling of bigint type columns for primary key IDs,
* can we use bigint primary keys without issues ?
* These tests should be able to detect incorrect intval
* casts for instance.
*
* @file RedUNIT/Postgres/Bigint.php
* @desc Tests support for BIGINT primary keys.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Bigint extends Postgres
{
/**
* Test BIG INT primary key support.
*
* @return void
*/
public function testBigIntSupport()
{
R::nuke();
$createPageTableSQL = '
CREATE TABLE
page
(
id BIGSERIAL PRIMARY KEY,
book_id BIGSERIAL,
magazine_id BIGSERIAL,
title VARCHAR(255)
)';
$createBookTableSQL = '
CREATE TABLE
book
(
id BIGSERIAL PRIMARY KEY,
title VARCHAR(255)
)';
$createPagePageTableSQL = '
CREATE TABLE
page_page
(
id BIGSERIAL PRIMARY KEY,
page_id BIGSERIAL,
page2_id BIGSERIAL
) ';
R::exec( $createBookTableSQL );
R::exec( $createPageTableSQL );
R::exec( $createPagePageTableSQL );
//insert some records
$book1ID = '2223372036854775808';
$book2ID = '2223372036854775809';
$page1ID = '1223372036854775808';
$page2ID = '1223372036854775809';
$page3ID = '1223372036854775890';
$pagePage1ID = '3223372036854775808';
R::exec("ALTER SEQUENCE book_id_seq RESTART WITH $book1ID");
R::exec("ALTER SEQUENCE page_id_seq RESTART WITH $page1ID");
R::exec("ALTER SEQUENCE page_page_id_seq RESTART WITH $pagePage1ID");
$insertBook1SQL = "
INSERT INTO book (title) VALUES( 'book 1' );
";
$insertBook2SQL = "
INSERT INTO book (title) VALUES( 'book 2' );
";
$insertPage1SQL = "
INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
";
$insertPage2SQL = "
INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
";
$insertPage3SQL = "
INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
";
$insertPagePage1SQL = "
INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
";
R::exec( $insertBook1SQL );
R::exec( $insertBook2SQL );
R::exec( $insertPage1SQL );
R::exec( $insertPage2SQL );
R::exec( $insertPage3SQL );
R::exec( $insertPagePage1SQL );
//basic tour of basic functions....
$book1 = R::load( 'book', $book1ID );
asrt( $book1->id, $book1ID );
asrt( $book1->title, 'book 1' );
$book2 = R::load( 'book', $book2ID );
asrt( $book2->id, $book2ID );
asrt( $book2->title, 'book 2' );
asrt( count( $book1->ownPage ), 2 );
asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
asrt( count($book2->ownPage), 1 );
asrt( $book2->fresh()->countOwn( 'page' ), 1 );
$page1 = R::load( 'page', $page1ID );
asrt( count( $page1->sharedPage ), 0 );
asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
$page2 = R::load( 'page', $page2ID );
asrt( count($page2->sharedPage), 1 );
asrt( $page2->fresh()->countShared( 'page' ), 1 );
$page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
asrt( $page3->id, $page3ID );
asrt( $page3->book->id, $book2ID );
}
}

View File

@@ -0,0 +1,292 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
/**
* Foreignkeys
*
* Tests creation and validity of foreign keys,
* foreign key constraints and indexes in PostgreSQL.
* Also tests whether the correct contraint action has been selected.
*
* @file RedUNIT/Postgres/Foreignkeys.php
* @desc Tests the creation of foreign keys.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Foreignkeys extends Postgres
{
/**
* Test foreign keys with postgres.
*/
public function testForeignKeysWithPostgres()
{
testpack( 'Test Postgres Foreign keys' );
$a = R::getWriter()->addFK( 'a', 'b', 'c', 'd' ); //must fail
pass(); //survive without exception
asrt( $a, FALSE ); //must return FALSE
$book = R::dispense( 'book' );
$page = R::dispense( 'page' );
$cover = R::dispense( 'cover' );
list( $g1, $g2 ) = R::dispense( 'genre', 2 );
$g1->name = '1';
$g2->name = '2';
$book->ownPage = array( $page );
$book->cover = $cover;
$book->sharedGenre = array( $g1, $g2 );
R::store( $book );
$sql = "SELECT
tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY' AND (tc.table_name='book' OR tc.table_name='book_genre' OR tc.table_name='page');";
$fks = R::getAll( $sql );
$json = '[
{
"constraint_name": "book_cover_id_fkey",
"table_name": "book",
"column_name": "cover_id",
"foreign_table_name": "cover",
"foreign_column_name": "id"
},
{
"constraint_name": "page_book_id_fkey",
"table_name": "page",
"column_name": "book_id",
"foreign_table_name": "book",
"foreign_column_name": "id"
},
{
"constraint_name": "book_genre_genre_id_fkey",
"table_name": "book_genre",
"column_name": "genre_id",
"foreign_table_name": "genre",
"foreign_column_name": "id"
},
{
"constraint_name": "book_genre_book_id_fkey",
"table_name": "book_genre",
"column_name": "book_id",
"foreign_table_name": "book",
"foreign_column_name": "id"
}
]';
$j = json_encode( $fks );
$j1 = json_decode( $j, TRUE );
$j2 = json_decode( $json, TRUE );
foreach ( $j1 as $jrow ) {
$s = json_encode( $jrow );
$found = 0;
foreach ( $j2 as $k => $j2row ) {
if ( json_encode( $j2row ) === $s ) {
pass();
unset( $j2[$k] );
$found = 1;
}
}
if ( !$found ) fail();
}
}
/**
* Test constraint function directly in Writer.
*
* @return void
*/
public function testConstraint()
{
R::nuke();
$database = R::getCell('SELECT current_database()');
$sql = 'CREATE TABLE book (id SERIAL PRIMARY KEY)';
R::exec( $sql );
$sql = 'CREATE TABLE page (id SERIAL PRIMARY KEY)';
R::exec( $sql );
$sql = 'CREATE TABLE book_page (
id SERIAL PRIMARY KEY,
book_id INTEGER,
page_id INTEGER
)';
R::exec( $sql );
$writer = R::getWriter();
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'book_page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 0 );
$writer->addFK( 'book_page', 'book', 'book_id', 'id', TRUE );
$writer->addFK( 'book_page', 'page', 'page_id', 'id', TRUE );
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 2 );
$writer->addFK( 'book_page', 'book', 'book_id', 'id', TRUE );
$writer->addFK( 'book_page', 'page', 'page_id', 'id', TRUE );
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 2 );
}
/**
* Test adding foreign keys.
*
* @return void
*/
public function testAddingForeignKey()
{
R::nuke();
$database = R::getCell('SELECT current_database()');
$sql = 'CREATE TABLE book (
id SERIAL PRIMARY KEY
)';
R::exec( $sql );
$sql = 'CREATE TABLE page (
id SERIAL PRIMARY KEY,
book_id INTEGER
)';
R::exec( $sql );
$writer = R::getWriter();
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 0 );
$writer->addFK('page', 'page', 'book_id', 'id', TRUE);
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 1 );
//dont add twice
$writer->addFK('page', 'page', 'book_id', 'id', TRUE);
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 1 );
//even if it is different
$writer->addFK('page', 'page', 'book_id', 'id', FALSE);
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 1 );
R::nuke();
$sql = 'CREATE TABLE book (
id SERIAL PRIMARY KEY
)';
R::exec( $sql );
$sql = 'CREATE TABLE page (
id SERIAL PRIMARY KEY,
book_id INTEGER
)';
R::exec( $sql );
$writer = R::getWriter();
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 0 );
$writer->addFK('page', 'page', 'book_id', 'id', FALSE);
$sql = "
SELECT
COUNT(*)
FROM information_schema.key_column_usage AS k
LEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name
WHERE k.table_catalog = '$database'
AND k.table_schema = 'public'
AND k.table_name = 'page'
AND c.constraint_type = 'FOREIGN KEY'";
$numFKS = R::getCell( $sql );
asrt( (int) $numFKS, 1 );
}
/**
* Test whether we can manually create indexes.
*
* @return void
*/
public function testAddingIndex()
{
R::nuke();
$sql = 'CREATE TABLE song (
id SERIAL PRIMARY KEY,
album_id INTEGER,
category VARCHAR(255)
)';
R::exec( $sql );
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 1 );
$writer = R::getWriter();
$writer->addIndex( 'song', 'index1', 'album_id' );
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 2 );
//Cant add the same index twice
$writer->addIndex( 'song', 'index1', 'album_id' );
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 2 );
$writer->addIndex( 'song', 'index2', 'category' );
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 3 );
//Dont fail, just dont
try {
$writer->addIndex( 'song', 'index3', 'nonexistant' );
pass();
} catch( \Exception $e ) {
fail();
}
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 3 );
try {
$writer->addIndex( 'nonexistant', 'index4', 'nonexistant' );
pass();
} catch( \Exception $e ) {
fail();
}
$indexes = R::getAll( " SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' ");
asrt( count( $indexes ), 3 );
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
/**
* Parambind
*
* Tests the parameter binding functionality in RedBeanPHP.
* These test scenarios include for instance: NULL handling,
* binding parameters in LIMIT clauses and so on.
*
* @file RedUNIT/Postgres/Parambind.php
* @desc Tests\PDO parameter binding for Postgres.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Parambind extends Postgres
{
/**
* Test parameter binding.
*
* @return void
*/
public function testParamBindingWithPostgres()
{
testpack( "param binding pgsql" );
$page = R::dispense( "page" );
$page->name = "abc";
$page->number = 2;
R::store( $page );
R::exec( "insert into page (name) values(:name) ", array( ":name" => "my name" ) );
R::exec( "insert into page (number) values(:one) ", array( ":one" => 1 ) );
R::exec( "insert into page (number) values(:one) ", array( ":one" => "1" ) );
R::exec( "insert into page (number) values(:one) ", array( ":one" => "1234" ) );
R::exec( "insert into page (number) values(:one) ", array( ":one" => "-21" ) );
pass();
testpack( 'Test whether we can properly bind and receive NULL values' );
$adapter = R::getDatabaseAdapter();
asrt( $adapter->getCell( 'SELECT TEXT( :nil ) ', array( ':nil' => 'NULL' ) ), 'NULL' );
asrt( $adapter->getCell( 'SELECT TEXT( :nil ) ', array( ':nil' => NULL ) ), NULL );
asrt( $adapter->getCell( 'SELECT TEXT( ? ) ', array( 'NULL' ) ), 'NULL' );
asrt( $adapter->getCell( 'SELECT TEXT( ? ) ', array( NULL ) ), NULL );
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
use RedBeanPHP\RedException\SQL as SQLException;
/**
* Partial Beans
*
* This class has been designed to test 'partial bean mode'.
* In 'partial bean mode' only changed properties are being saved,
* not entire beans. This can be useful when you have unsupported
* column types in your table, this is what we test here. In this
* example we have a table that contains a boolean column, this column
* does not accept the value '' as FALSE as shown in the test, it will
* trigger an Invalid Text Representation Exception. Thanks to 'partial beans'
* we can work around this, by selectively updating the non-boolean properties
* of the bean. If we choose to update the boolean property this is no longer
* a problem because we set the value ourselves it will be compatible. However
* automatically loaded and stored properties by RedBeanPHP are subject to
* type inference, and the boolean FALSE value will become '', which is the
* crux of the issue here.
*
* @file RedUNIT/Postgres/Partial.php
* @desc Tests whether 'partial beans' can be used to support non-RB columns
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Partial extends Postgres
{
/**
* Excerpt from issue #547:
* "When I load a bean (via $bean = R::findOne(...);), only change a few
* values and then call R::store($bean);, it can happen that I get an error like:
* Error in SQL query:
* SQLSTATE[22P02]: Invalid text representation:
* 7 ERROR: invalid input syntax for type boolean: ""
* This happens, when there is a boolean field set to FALSE and
* I don't update that field. When R::store() is called,
* the value isn't translated to '0' but instead stays FALSE. PostgreSQL doesn't like this.
* When setting a boolean value, it gets converted correctly,
* but any unchanged values stay of type boolean.
*
* @return void
*/
public function testIssue547BoolCol()
{
R::nuke();
R::usePartialBeans( FALSE );
$bean = R::dispense( 'bean' );
$bean->property1 = 'value1';
$id = R::store( $bean );
R::freeze( TRUE );
R::exec('ALTER TABLE bean ADD COLUMN "property2" BOOLEAN DEFAULT FALSE;');
$bean = R::load( 'bean', $id );
$bean->property1 = 'value1b';
/* we cant save the bean, because there is an unsupported field type in the table */
/* this was the bug... (or missing feature?) */
try {
R::store( $bean );
fail();
} catch( SQLException $e ) {
asrt( strpos( $e->getMessage(), 'Invalid text representation' ) > 1, TRUE );
asrt( $e->getSQLState(), '22P02' );
}
/* solved by adding feature partial beans, now only the changed properties get saved */
R::usePartialBeans( TRUE );
$id = R::store( $bean );
/* no exception... */
pass();
/* also test behavior of boolean column in general */
$bean = R::load( 'bean', $id );
asrt( $bean->property1, 'value1b' );
asrt( $bean->property2, FALSE );
$bean->property2 = TRUE;
$id = R::store( $bean );
$bean = R::load( 'bean', $id );
asrt( $bean->property1, 'value1b' );
asrt( $bean->property2, TRUE );
$bean->property2 = FALSE;
$id = R::store( $bean );
$bean = R::load( 'bean', $id );
asrt( $bean->property1, 'value1b' );
asrt( $bean->property2, FALSE );
$bean->property2 = 't';
$id = R::store( $bean );
$bean = R::load( 'bean', $id );
asrt( $bean->property1, 'value1b' );
asrt( $bean->property2, TRUE );
$bean->property2 = 'f';
$id = R::store( $bean );
$bean = R::load( 'bean', $id );
asrt( $bean->property1, 'value1b' );
asrt( $bean->property2, FALSE );
/* but invalid text should not work */
$bean->property2 = 'tx'; //instead of just 't'
try {
$id = R::store( $bean );
fail();
} catch( SQLException $e ) {
asrt( strpos( $e->getMessage(), 'Invalid text representation' ) > 1, TRUE );
asrt( $e->getSQLState(), '22P02' );
}
R::usePartialBeans( FALSE );
}
}

View File

@@ -0,0 +1,138 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
/**
* Setget
*
* This class has been designed to test set/get operations
* for a specific Query Writer / Adapter. Since RedBeanPHP
* creates columns based on values it's essential that you
* get back the 'same' value as you put in - or - if that's
* not the case, that there are at least very clear rules
* about what to expect. Examples of possible issues tested in
* this class include:
*
* - Test whether booleans are returned correctly (they will become integers)
* - Test whether large numbers are preserved
* - Test whether floating point numbers are preserved
* - Test whether date/time values are preserved
* and so on...
*
* @file RedUNIT/Postgres/Setget.php
* @desc Tests whether values are correctly stored.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Setget extends Postgres
{
/**
* Test whether we can store DateTime objects and get them back
* as 'date-time' strings representing the same date and time.
*
* @return void
*/
public function testDateObject()
{
$dt = new \DateTime();
$dt->setTimeZone( new \DateTimeZone( 'Europe/Amsterdam' ) );
$dt->setDate( 1981, 5, 1 );
$dt->setTime( 3, 13, 13 );
asrt( setget( $dt ), '1981-05-01 03:13:13' );
$bean = R::dispense( 'bean' );
$bean->dt = $dt;
}
/**
* Test numbers.
*
* @return void
*/
public function testNumbers()
{
asrt( setget( "-1" ), "-1" );
asrt( setget( -1 ), "-1" );
asrt( setget( "1.0" ), "1" );
asrt( setget( 1.0 ), "1" );
asrt( setget( "-0.25" ), "-0.25" );
asrt( setget( -0.25 ), "-0.25" );
asrt( setget( "3.20" ), "3.20" );
asrt( setget( "13.20" ), "13.20" );
asrt( setget( "134.20" ), "134.20" );
asrt( setget( 3.21 ), '3.21' );
asrt( setget( "0.12345678" ), "0.12345678" );
asrt( setget( 0.12345678 ), "0.12345678" );
asrt( setget( "-0.12345678" ), "-0.12345678" );
asrt( setget( -0.12345678 ), "-0.12345678" );
asrt( setget( "2147483647" ), "2147483647" );
asrt( setget( 2147483647 ), "2147483647" );
asrt( setget( -2147483647 ), "-2147483647" );
asrt( setget( "-2147483647" ), "-2147483647" );
asrt( setget( "2147483648" ), "2147483648" );
asrt( setget( "-2147483648" ), "-2147483648" );
asrt( setget( "199936710040730" ), "199936710040730" );
asrt( setget( "-199936710040730" ), "-199936710040730" );
}
/**
* Test dates.
*
* @return void
*/
public function testDates()
{
asrt( setget( "2010-10-11" ), "2010-10-11" );
asrt( setget( "2010-10-11 12:10" ), "2010-10-11 12:10" );
asrt( setget( "2010-10-11 12:10:11" ), "2010-10-11 12:10:11" );
asrt( setget( "x2010-10-11 12:10:11" ), "x2010-10-11 12:10:11" );
}
/**
* Test strings.
*
* @return void
*/
public function testStrings()
{
asrt( setget( "a" ), "a" );
asrt( setget( "." ), "." );
asrt( setget( "\"" ), "\"" );
asrt( setget( "just some text" ), "just some text" );
}
/**
* Test booleans.
*
* @return void
*/
public function testBool()
{
asrt( setget( TRUE ), "1" );
asrt( setget( FALSE ), "0" );
asrt( setget( "TRUE" ), "TRUE" );
asrt( setget( "FALSE" ), "FALSE" );
}
/**
* Test NULL.
*
* @return void
*/
public function testNull()
{
asrt( setget( "NULL" ), "NULL" );
asrt( setget( "NULL" ), "NULL" );
asrt( setget( NULL ), NULL );
asrt( ( setget( 0 ) == 0 ), TRUE );
asrt( ( setget( 1 ) == 1 ), TRUE );
asrt( ( setget( TRUE ) == TRUE ), TRUE );
asrt( ( setget( FALSE ) == FALSE ), TRUE );
}
}

View File

@@ -0,0 +1,317 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
/**
* Trees
*
* This class has been designed to test tree traversal using
* R::children() and R::parents() relying on
* recursive common table expressions.
*
* @file RedUNIT/Postgres/Trees.php
* @desc Tests trees
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Trees extends Postgres
{
protected function summarize( $beans )
{
$names = array();
foreach( $beans as $bean ) {
$names[] = $bean->title;
}
return implode( ',', $names );
}
/**
* Test trees
*
* @return void
*/
public function testCTETrees()
{
R::nuke();
$pages = R::dispense(array(
'_type' => 'page',
'title' => 'home',
'ownPageList' => array(array(
'_type' => 'page',
'title' => 'shop',
'ownPageList' => array(array(
'_type' => 'page',
'title' => 'wines',
'ownPageList' => array(array(
'_type' => 'page',
'title' => 'whiskies',
))
))
))
));
R::store( $pages );
$whiskyPage = R::findOne( 'page', 'title = ?', array('whiskies') );
asrt( $this->summarize( R::parents( $whiskyPage, ' ORDER BY title ASC ' ) ), 'home,shop,whiskies,wines' );
asrt( $this->summarize( R::children( $whiskyPage, ' ORDER BY title ASC ' ) ), 'whiskies' );
$homePage = R::findOne( 'page', 'title = ?', array('home') );
asrt( $this->summarize( R::parents( $homePage, ' ORDER BY title ASC ' ) ), 'home' );
asrt( $this->summarize( R::children( $homePage, ' ORDER BY title ASC ' ) ), 'home,shop,whiskies,wines' );
$shopPage = R::findOne( 'page', 'title = ?', array('shop') );
asrt( $this->summarize( R::parents( $shopPage, ' ORDER BY title ASC ' ) ), 'home,shop' );
asrt( $this->summarize( R::children( $shopPage, ' ORDER BY title ASC ' ) ), 'shop,whiskies,wines' );
$winePage = R::findOne( 'page', 'title = ?', array('wines') );
asrt( $this->summarize( R::parents( $winePage, ' ORDER BY title ASC ' ) ), 'home,shop,wines' );
asrt( $this->summarize( R::children( $winePage, ' ORDER BY title ASC ' ) ), 'whiskies,wines' );
asrt( $this->summarize( R::children( $winePage, ' title NOT IN (\'wines\') ORDER BY title ASC ' ) ), 'whiskies' );
asrt( $this->summarize( R::parents( $winePage, ' title NOT IN (\'home\') ORDER BY title ASC ' ) ), 'shop,wines' );
asrt( $this->summarize( R::parents( $winePage, ' ORDER BY title ASC ', array() ) ), 'home,shop,wines' );
asrt( $this->summarize( R::children( $winePage, ' ORDER BY title ASC ', array() ) ), 'whiskies,wines' );
asrt( $this->summarize( R::children( $winePage, ' title NOT IN (\'wines\') ORDER BY title ASC ', array() ) ), 'whiskies' );
asrt( $this->summarize( R::parents( $winePage, ' title NOT IN (\'home\') ORDER BY title ASC ', array() ) ), 'shop,wines' );
asrt( $this->summarize( R::children( $winePage, ' title != ? ORDER BY title ASC ', array( 'wines' ) ) ), 'whiskies' );
asrt( $this->summarize( R::parents( $winePage, ' title != ? ORDER BY title ASC ', array( 'home' ) ) ), 'shop,wines' );
asrt( $this->summarize( R::children( $winePage, ' title != :title ORDER BY title ASC ', array( ':title' => 'wines' ) ) ), 'whiskies' );
asrt( $this->summarize( R::parents( $winePage, ' title != :title ORDER BY title ASC ', array( ':title' => 'home' ) ) ), 'shop,wines' );
asrt( R::countChildren( $homePage ), 3 );
asrt( R::countParents( $whiskyPage ), 3 );
asrt( R::countChildren( $winePage, ' title != :title ', array( ':title' => 'wines' ) ) , 1 );
asrt( R::countChildren( $winePage, ' title = :title ', array( ':title' => 'wines' ) ) , 1 );
asrt( R::countParents( $winePage, ' title != :title ', array( ':title' => 'home' ) ) , 2 );
}
/**
* Test CTE and Parsed Joins.
*
* @return void
*/
public function testCTETreesAndParsedJoins()
{
R::nuke();
list($cards, $details, $colors) = R::dispenseAll('card*9,detail*4,color*2');
$colors[0]->name = 'red';
$colors[1]->name = 'black';
$details[0]->points = 500;
$details[1]->points = 200;
$details[2]->points = 300;
$details[3]->points = 100;
$cards[0]->ownCardList = array( $cards[1] );
$cards[1]->ownCardList = array( $cards[2], $cards[3] );
$cards[2]->ownCardList = array( $cards[4], $cards[5] );
$cards[3]->ownCardList = array( $cards[6], $cards[7], $cards[8] );
$cards[0]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User0'));
$cards[1]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User1'));
$cards[2]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User2'));
$cards[3]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User3'));
$cards[4]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User4'));
$cards[5]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User5'));
$cards[6]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User6'));
$cards[7]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User7'));
$cards[8]->ownOwner[] = R::dispense(array('_type'=>'owner', 'name'=>'User8'));
$cards[0]->detail = $details[0];
$cards[1]->detail = $details[0];
$cards[2]->detail = $details[1];
$cards[3]->detail = $details[2];
$cards[4]->detail = $details[3];
$cards[5]->detail = $details[3];
$cards[6]->detail = $details[3];
$cards[7]->detail = $details[3];
$cards[8]->detail = $details[3];
$colors[0]->sharedCardList = array( $cards[0], $cards[2], $cards[4], $cards[6], $cards[8] );
$colors[1]->sharedCardList = array( $cards[1], $cards[3], $cards[5], $cards[7] );
R::storeAll(array_merge($cards,$details,$colors));
$cardsWith100Points = R::children($cards[0], ' @joined.detail.points = ? ', array(100));
asrt(count($cardsWith100Points),5);
$cardsWith200Points = R::children($cards[0], ' @joined.detail.points = ? ', array(200));
asrt(count($cardsWith200Points),1);
$cardsWith300Points = R::children($cards[0], ' @joined.detail.points = ? ', array(300));
asrt(count($cardsWith200Points),1);
$cardsWith500Points = R::children($cards[0], ' @joined.detail.points = ? ', array(500));
asrt(count($cardsWith200Points),1);
for($i=8; $i>=4; $i--) {
$cardsWithMoreThan100Points = R::parents($cards[$i], ' @joined.detail.points > ? ', array(100));
asrt(count($cardsWithMoreThan100Points),3);
}
$cardsWithMoreThan200Points = R::parents($cards[8], ' @joined.detail.points > ? ', array(200));
asrt(count($cardsWithMoreThan200Points),3);
$cardsWithMoreThan200Points = R::parents($cards[7], ' @joined.detail.points > ? ', array(200));
asrt(count($cardsWithMoreThan200Points),3);
$cardsWithMoreThan200Points = R::parents($cards[6], ' @joined.detail.points > ? ', array(200));
asrt(count($cardsWithMoreThan200Points),3);
$cardsWithMoreThan200Points = R::parents($cards[5], ' @joined.detail.points > ? ', array(200));
asrt(count($cardsWithMoreThan200Points),2);
$cardsWithMoreThan200Points = R::parents($cards[4], ' @joined.detail.points > ? ', array(200));
asrt(count($cardsWithMoreThan200Points),2);
for($i=8; $i>=2; $i--) {
$cardsWithMoreThan300Points = R::parents($cards[4], ' @joined.detail.points > ? ', array(300));
asrt(count($cardsWithMoreThan200Points),2);
}
$black = R::children($cards[0], ' @shared.color.name = ? ', array('black'));
asrt(count($black),4);
$red = R::children($cards[0], ' @shared.color.name = ? ', array('red'));
asrt(count($red),5);
$black = R::parents($cards[8], ' @shared.color.name = ? ', array('black'));
asrt(count($black),2);
$red = R::parents($cards[6], ' @shared.color.name = ? ', array('red'));
asrt(count($red),2);
$found = R::children($cards[0], ' @own.owner.name = ? ', array('User0'));
asrt(count($found),1);
$found = R::children($cards[0], ' @own.owner.name IN (?,?) ', array('User1','User2'));
asrt(count($found),2);
$found = R::parents($cards[8], ' @own.owner.name IN (?,?) ', array('User3','User0'));
asrt(count($found),2);
$found = R::children($cards[8], ' @own.owner.name IN (?,?) ', array('User3','User0'));
asrt(count($found),0);
$found = R::children($cards[3], ' @own.owner.name IN (?,?) ', array('User3','User7'));
asrt(count($found),2);
}
/**
* Test CTE and Parsed Joins with aliases as well
* as chained parsed joins.
*
* @return void
*/
public function testCTETreesAndParsedJoinsAndAliases() {
R::nuke();
R::aliases(array( 'author' => 'person', 'chart' => 'image' ));
$person = R::dispense( 'person' );
$ceo = R::dispense('person');
$ceo->name = 'John';
$role = R::dispense('role');
$role->label = 'CEO';
$ceo->sharedRoleList = array( $role );
$person->name = 'James';
$editor = R::dispense('role');
$editor->label = 'editor';
$person->sharedRoleList = array( $editor );
$website = R::dispense('page');
$about = R::dispense('page');
$investor = R::dispense('page');
$links = R::dispense('page');
$category = R::dispense('category');
$category->title = 'business';
$about->sharedCategoryList = array( $category );
$blog = R::dispense('page');
$article = R::dispense('page');
$investor->title = 'investors';
$links->title = 'links';
$about->title = 'about';
$chart = R::dispense('image');
$chart->file = 'report.jpg';
$picture = R::dispense('image');
$picture->file = 'logo.jpg';
$picture->source = $website;
$website->ownImageList = array( $picture );
$article->ownImageList = array( $picture );
$investor->ownImageList = array( $picture );
$about->ownImageList = array( $chart );
$about->author = $ceo;
$about->coauthor = $person;
$article->title = 'a walk in the park';
$website->ownPageList = array( $about, $blog );
$blog->ownPageList = array( $article );
$article->ownPageList = array( $links );
$about->ownPageList = array( $investor );
$article->author = $person;
R::store( $website );
//joined-alias
$pages = R::children( $website, ' @joined.author.name = ? ', array('James') );
asrt(count($pages), 1);
$page = reset($pages);
asrt($page->title, 'a walk in the park');
//Chained joined-alias+shared
$pages = R::children( $website, ' @joined.author.shared.role.label = ? ', array('CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//joined-alias+shared parents
$pages = R::parents( $investor, ' @joined.author.shared.role.label = ? ', array('CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//joined-alias parents
$pages = R::parents( $links, ' @joined.author.name = ? ', array('James') );
asrt(count($pages), 1);
$page = reset($pages);
asrt($page->title, 'a walk in the park');
//own-alias
$pages = R::children( $website, '@own.chart.file = ?', array('report.jpg') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//own-alias parent
$pages = R::parents( $investor, '@own.chart.file = ?', array('report.jpg') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//own-alias parent + joined-alias+shared parents
$pages = R::parents( $investor, '@own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('report.jpg', 'CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//own-alias + Chained joined-alias+shared
$pages = R::children( $website, ' @own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('report.jpg', 'CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//own-alias + Chained joined-alias+shared + joined-alias
$pages = R::children( $website, ' @joined.author.name = ? AND @own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('John', 'report.jpg', 'CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//own-alias + Chained joined-alias+shared + joined-alias parents
$pages = R::parents( $investor, ' @joined.author.name = ? AND @own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('John', 'report.jpg', 'CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
$pages = R::parents( $links, ' @joined.author.name = ? AND @own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('James', 'logo.jpg', 'editor') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'a walk in the park');
$pages = R::parents( $links, ' @joined.author.name = ? AND @own.chart.file = ? AND @joined.author.shared.role.label = ? ', array('James', 'report.jpg', 'editor') );
asrt(count($pages),0);
//other variations
$pages = R::parents( $investor, ' @shared.category.title = ? ', array('business') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
R::aliases(array());
//joined-alias - explicit/non-global
$pages = R::children( $website, ' @joined.person[as:author].name = ? ', array('James') );
asrt(count($pages), 1);
$page = reset($pages);
asrt($page->title, 'a walk in the park');
//Chained joined-alias+shared - explicit/non-global
$pages = R::children( $website, ' @joined.person[as:author].shared.role.label = ? ', array('CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//joined-alias+shared parents - explicit/non-global
$pages = R::parents( $investor, ' @joined.person[as:author].shared.role.label = ? ', array('CEO') );
asrt(count($pages),1);
$page = reset($pages);
asrt($page->title,'about');
//joined-alias parents - explicit/non-global
$pages = R::parents( $links, ' @joined.person[as:author].name = ? ', array('James') );
asrt(count($pages), 1);
$page = reset($pages);
asrt($page->title, 'a walk in the park');
//double joined-alias parents - explicit/non-global
$pages = R::children( $website, ' @joined.person[as:author/coauthor].name = ? ', array('James') );
asrt(count($pages), 2);
//own-alias - explicit/non-global
$pages = R::children( $website, ' @own.image[alias:source].file = ?', array('logo.jpg') );
asrt(count($pages),1);
$pages = R::children( $website, ' @own.image[alias:source].file = ?', array('report.jpg') );
asrt(count($pages),0);
$pages = R::children( $about, ' @own.image[alias:source].file = ?', array('logo.jpg') );
asrt(count($pages),0);
}
}

View File

@@ -0,0 +1,278 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
use RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper as SimpleFacadeBeanHelper;
use RedBeanPHP\OODB as OODB;
use RedBeanPHP\OODBBean as OODBBean;
use RedBeanPHP\ToolBox as ToolBox;
/**
* UUID
*
* Tests whether we can use UUIDs with PostgreSQL, to this
* end we use a reference implementation of a UUID MySQL Writer:
* UUIDWriterPostgres, however this class is not part of the code base,
* it should be considered a reference or example implementation.
* These tests focus on whether UUIDs in general do not cause any
* unexpected issues.
*
* @file RedUNIT/Postgres/Uuid.php
* @desc Tests read support for UUID tables.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Uuid extends Postgres
{
/**
* Test Read-support.
*
* @return void
*/
public function testUUIDReadSupport()
{
R::nuke();
$createPageTableSQL = '
CREATE TABLE
page
(
id UUID PRIMARY KEY,
book_id UUID,
magazine_id UUID,
title VARCHAR(255)
)';
$createBookTableSQL = '
CREATE TABLE
book
(
id UUID PRIMARY KEY,
title VARCHAR(255)
)';
$createPagePageTableSQL = '
CREATE TABLE
page_page
(
id UUID PRIMARY KEY,
page_id UUID,
page2_id UUID
)';
R::exec( $createBookTableSQL );
R::exec( $createPageTableSQL );
R::exec( $createPagePageTableSQL );
//insert some records
$book1ID = '6ccd780c-baba-1026-9564-0040f4311e21';
$book2ID = '6ccd780c-baba-1026-9564-0040f4311e22';
$page1ID = '6ccd780c-baba-1026-9564-0040f4311e23';
$page2ID = '6ccd780c-baba-1026-9564-0040f4311e24';
$page3ID = '6ccd780c-baba-1026-9564-0040f4311e25';
$pagePage1ID = '6ccd780c-baba-1026-9564-0040f4311e26';
$insertBook1SQL = "
INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
";
$insertBook2SQL = "
INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
";
$insertPage1SQL = "
INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
";
$insertPage2SQL = "
INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
";
$insertPage3SQL = "
INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
";
$insertPagePage1SQL = "
INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
";
R::exec( $insertBook1SQL );
R::exec( $insertBook2SQL );
R::exec( $insertPage1SQL );
R::exec( $insertPage2SQL );
R::exec( $insertPage3SQL );
R::exec( $insertPagePage1SQL );
//basic tour of basic functions....
$book1 = R::load( 'book', $book1ID );
asrt( $book1->id, $book1ID );
asrt( $book1->title, 'book 1' );
$book2 = R::load( 'book', $book2ID );
asrt( $book2->id, $book2ID );
asrt( $book2->title, 'book 2' );
asrt( count( $book1->ownPage ), 2 );
asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
asrt( count($book2->ownPage), 1 );
asrt( $book2->fresh()->countOwn( 'page' ), 1 );
$page1 = R::load( 'page', $page1ID );
asrt( count( $page1->sharedPage ), 0 );
asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
$page2 = R::load( 'page', $page2ID );
asrt( count($page2->sharedPage), 1 );
asrt( $page2->fresh()->countShared( 'page' ), 1 );
$page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
asrt( $page3->id, $page3ID );
asrt( $page3->book->id, $book2ID );
}
/**
* Test Full fluid UUID support.
*
*/
public function testFullSupport()
{
//Rewire objects to support UUIDs.
$oldToolBox = R::getToolBox();
$oldAdapter = $oldToolBox->getDatabaseAdapter();
$uuidWriter = new \UUIDWriterPostgres( $oldAdapter );
$newRedBean = new OODB( $uuidWriter );
$newToolBox = new ToolBox( $newRedBean, $oldAdapter, $uuidWriter );
R::configureFacadeWithToolbox( $newToolBox );
list( $mansion, $rooms, $ghosts, $key ) = R::dispenseAll( 'mansion,room*3,ghost*4,key' );
$mansion->name = 'Haunted Mansion';
$mansion->xownRoomList = $rooms;
$rooms[0]->name = 'Green Room';
$rooms[1]->name = 'Red Room';
$rooms[2]->name = 'Blue Room';
$ghosts[0]->name = 'zero';
$ghosts[1]->name = 'one';
$ghosts[2]->name = 'two';
$ghosts[3]->name = 'three';
$rooms[0]->noLoad()->sharedGhostList = array( $ghosts[0], $ghosts[1] );
$rooms[1]->noLoad()->sharedGhostList = array( $ghosts[0], $ghosts[2] );
$rooms[2]->noLoad()->sharedGhostList = array( $ghosts[1], $ghosts[3], $ghosts[2] );
$rooms[2]->xownKey = array( $key );
//Can we store a bean hierachy with UUIDs?
$id = R::store( $mansion );
asrt( is_string( $id ), TRUE );
asrt( strlen( $id ), 36 );
$haunted = R::load( 'mansion', $id );
asrt( $haunted->name, 'Haunted Mansion' );
asrt( is_string( $haunted->id ), TRUE );
asrt( strlen( $haunted->id ), 36 );
asrt( is_array( $haunted->xownRoomList ), TRUE );
asrt( count( $haunted->ownRoom ), 3 );
$rooms = $haunted->xownRoomList;
//Do some counting...
$greenRoom = NULL;
foreach( $rooms as $room ) {
if ( $room->name === 'Green Room' ) {
$greenRoom = $room;
break;
}
}
asrt( !is_null( $greenRoom ), TRUE );
asrt( is_array( $greenRoom->with(' ORDER BY id ')->sharedGhostList ), TRUE );
asrt( count( $greenRoom->sharedGhostList ), 2 );
$names = array();
foreach( $greenRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
sort($names);
$names = implode(',', $names);
asrt($names, 'one,zero');
$rooms = $haunted->xownRoomList;
$blueRoom = NULL;
foreach( $rooms as $room ) {
if ( $room->name === 'Blue Room' ) {
$blueRoom = $room;
break;
}
}
asrt( !is_null( $blueRoom ), TRUE );
asrt( is_array( $blueRoom->sharedGhostList ), TRUE );
asrt( count( $blueRoom->sharedGhostList ), 3 );
$names = array();
foreach( $blueRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
sort($names);
$names = implode(',', $names);
asrt($names, 'one,three,two');
$rooms = $haunted->xownRoomList;
$redRoom = NULL;
foreach( $rooms as $room ) {
if ( $room->name === 'Red Room' ) {
$redRoom = $room; break;
}
}
$names = array();
foreach( $redRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
sort($names);
$names = implode(',', $names);
asrt($names, 'two,zero');
asrt( !is_null( $redRoom ), TRUE );
asrt( is_array( $redRoom->sharedGhostList ), TRUE );
asrt( count( $redRoom->sharedGhostList ), 2 );
//Can we repaint a room?
$redRoom->name = 'Yellow Room';
$id = R::store($redRoom);
$yellowRoom = R::load( 'room', $id );
asrt( $yellowRoom->name, 'Yellow Room');
asrt( !is_null( $yellowRoom ), TRUE );
asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
asrt( count( $yellowRoom->sharedGhostList ), 2 );
//Can we throw one ghost out?
array_pop( $yellowRoom->sharedGhost );
R::store( $yellowRoom );
$yellowRoom = $yellowRoom->fresh();
asrt( $yellowRoom->name, 'Yellow Room');
asrt( !is_null( $yellowRoom ), TRUE );
asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
asrt( count( $yellowRoom->sharedGhostList ), 1 );
//can we remove one of the rooms?
asrt( R::count('key'), 1);
$list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
$room = reset($list);
unset($mansion->xownRoomList[$room->id]);
R::store($mansion);
asrt(R::count('room'), 2);
//and what about its dependent beans?
asrt(R::count('key'), 0);
asrt(R::count('ghost_room'), 3);
//and can we find ghosts?
$ghosts = R::find('ghost');
asrt(count($ghosts), 4);
$ghosts = R::findAll('ghost', 'ORDER BY id');
asrt(count($ghosts), 4);
$ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
asrt(count($ghosts), 2);
$ghostZero = R::findOne('ghost', ' "name" = ? ', array( 'zero' ) );
asrt( ($ghostZero instanceof OODBBean), TRUE );
//can we create link properties on existing tables?
$blackRoom = R::dispense( 'room' );
$blackRoom->name = 'Black Room';
$ghostZero->link('ghost_room', array('mood'=>'grumpy'))->room = $blackRoom;
R::store($ghostZero);
$ghostZero = $ghostZero->fresh();
$list = $ghostZero->sharedRoomList;
asrt(count($list), 3);
$ghostZero = $ghostZero->fresh();
$list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
asrt(count($list), 1);
//can we load a batch?
$ids = R::getCol('SELECT id FROM ghost');
$ghosts = R::batch('ghost', $ids);
asrt(count($ghosts), 4);
//can we do an aggregation?
$ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
asrt(count($ghosts), 2);
//can we duplicate the mansion?
asrt(R::count('mansion'), 1);
asrt(R::count('room'), 3);
asrt(R::count('ghost'), 4);
$copy = R::dup($mansion);
R::store($copy);
asrt(R::count('mansion'), 2);
asrt(R::count('room'), 5); //black room does not belong to mansion 1
asrt(R::count('ghost'), 4);
//can we do some counting using the list?
asrt( $copy->countOwn('room'), 2);
$rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
$room = reset($rooms);
asrt($room->countShared('ghost'), 2);
//Finally restore old toolbox
R::configureFacadeWithToolbox( $oldToolBox );
}
}

View File

@@ -0,0 +1,710 @@
<?php
namespace RedUNIT\Postgres;
use RedUNIT\Postgres as Postgres;
use RedBeanPHP\Facade as R;
use RedBeanPHP\AssociationManager as AssociationManager;
use RedBeanPHP\QueryWriter\PostgreSQL as PostgreSQL;
use RedBeanPHP\RedException\SQL as SQL;
use RedBeanPHP\RedException as RedException;
/**
* Writer
*
* Tests for PostgreSQL Query Writer.
* This test class contains Query Writer specific tests.
* Use this class to add tests to test Query Writer specific
* behaviours, quirks and issues.
*
* @file RedUNIT/Postgres/Writer.php
* @desc A collection of writer specific tests.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Writer extends Postgres
{
/**
* Test whether optimizations do not have effect on Writer query outcomes.
*
* @return void
*/
public function testWriterSpeedUp()
{
R::nuke();
$id = R::store( R::dispense( 'book' ) );
$writer = R::getWriter();
$count1 = $writer->queryRecordCount( 'book', array( 'id' => $id ), ' id = :id ', array( ':id' => $id ) );
$count2 = $writer->queryRecordCount( 'book', array( ), ' id = :id ', array( ':id' => $id ) );
$count3 = $writer->queryRecordCount( 'book', NULL, ' id = :id ', array( ':id' => $id ) );
$count4 = $writer->queryRecordCount( 'book', array( 'id' => $id ) );
asrt( $count1, $count2 );
asrt( $count2, $count3 );
asrt( $count3, $count4 );
R::nuke();
$books = R::dispenseAll( 'book*4' );
$ids = R::storeAll( $books[0] );
$writer->deleteRecord( 'book', array( 'id' => $ids[0] ) );
$writer->deleteRecord( 'book', array( 'id' => $ids[1] ), ' id = :id ', array( ':id' => $ids[1] ) );
$writer->deleteRecord( 'book', NULL, ' id = :id ', array( ':id' => $ids[2] ) );
$writer->deleteRecord( 'book', array(), ' id = :id ', array( ':id' => $ids[3] ) );
asrt( R::count( 'book' ), 0 );
R::nuke();
$id = R::store( R::dispense( 'book' ) );
$record = $writer->queryRecord( 'book', array( 'id' => $id ) );
asrt( is_array( $record ), TRUE );
asrt( is_array( $record[0] ), TRUE );
asrt( isset( $record[0]['id'] ), TRUE );
asrt( (int) $record[0]['id'], $id );
$record = $writer->queryRecord( 'book', array( 'id' => $id ), ' id = :id ', array( ':id' => $id ) );
asrt( is_array( $record ), TRUE );
asrt( is_array( $record[0] ), TRUE );
asrt( isset( $record[0]['id'] ), TRUE );
asrt( (int) $record[0]['id'], $id );
$record = $writer->queryRecord( 'book', NULL, ' id = :id ', array( ':id' => $id ) );
asrt( is_array( $record ), TRUE );
asrt( is_array( $record[0] ), TRUE );
asrt( isset( $record[0]['id'] ), TRUE );
asrt( (int) $record[0]['id'], $id );
$record = $writer->queryRecord( 'book', array(), ' id = :id ', array( ':id' => $id ) );
asrt( is_array( $record ), TRUE );
asrt( is_array( $record[0] ), TRUE );
asrt( isset( $record[0]['id'] ), TRUE );
asrt( (int) $record[0]['id'], $id );
}
/**
* Tests wheter we can write a deletion query
* for PostgreSQL using NO conditions but only an
* additional SQL snippet.
*
* @return void
*/
public function testWriteDeleteQuery()
{
$queryWriter = R::getWriter();
asrt( ( $queryWriter instanceof PostgreSQL ), TRUE );
R::nuke();
$bean = R::dispense( 'bean' );
$bean->name = 'a';
$id = R::store( $bean );
asrt( R::count( 'bean' ), 1 );
$queryWriter->deleteRecord( 'bean', array(), $addSql = ' id = :id ', $bindings = array( ':id' => $id ) );
asrt( R::count( 'bean' ), 0 );
}
/**
* Tests wheter we can write a counting query
* for PostgreSQL using conditions and an additional SQL snippet.
*
* @return void
*/
public function testWriteCountQuery()
{
$queryWriter = R::getWriter();
asrt( ( $queryWriter instanceof PostgreSQL ), TRUE );
R::nuke();
$bean = R::dispense( 'bean' );
$bean->name = 'a';
R::store( $bean );
$bean = R::dispense( 'bean' );
$bean->name = 'b';
R::store( $bean );
$bean = R::dispense( 'bean' );
$bean->name = 'b';
R::store( $bean );
$count = $queryWriter->queryRecordCount( 'bean', array( 'name' => 'b' ), $addSql = ' id > :id ', $bindings = array( ':id' => 0 ) );
asrt( $count, 2 );
}
/**
* Tests whether we can write a PostgreSQL join and
* whether the correct exception is thrown in case
* of an invalid join.
*
* @return void
*/
public function testWriteJoinSnippets()
{
$queryWriter = R::getWriter();
asrt( ( $queryWriter instanceof PostgreSQL ), TRUE );
$snippet = $queryWriter->writeJoin( 'book', 'page' ); //default must be LEFT
asrt( is_string( $snippet ), TRUE );
asrt( ( strlen( $snippet ) > 0 ), TRUE );
asrt( ' LEFT JOIN "page" ON "page".id = "book".page_id ', $snippet );
$snippet = $queryWriter->writeJoin( 'book', 'page', 'LEFT' );
asrt( is_string( $snippet ), TRUE );
asrt( ( strlen( $snippet ) > 0 ), TRUE );
asrt( ' LEFT JOIN "page" ON "page".id = "book".page_id ', $snippet );
$snippet = $queryWriter->writeJoin( 'book', 'page', 'RIGHT' );
asrt( is_string( $snippet ), TRUE );
asrt( ( strlen( $snippet ) > 0 ), TRUE );
asrt( ' RIGHT JOIN "page" ON "page".id = "book".page_id ', $snippet );
$snippet = $queryWriter->writeJoin( 'book', 'page', 'INNER' );
asrt( ' INNER JOIN "page" ON "page".id = "book".page_id ', $snippet );
$exception = NULL;
try {
$snippet = $queryWriter->writeJoin( 'book', 'page', 'MIDDLE' );
}
catch(\Exception $e) {
$exception = $e;
}
asrt( ( $exception instanceof RedException ), TRUE );
$errorMessage = $exception->getMessage();
asrt( is_string( $errorMessage ), TRUE );
asrt( ( strlen( $errorMessage ) > 0 ), TRUE );
asrt( $errorMessage, 'Invalid JOIN.' );
}
/**
* Test whether we can store JSON as a JSON column
* and whether this plays well with the other data types.
*/
public function testSetGetJSON()
{
global $travis;
if ($travis) return;
R::nuke();
$bean = R::dispense('bean');
$message = json_encode( array( 'message' => 'hello', 'type' => 'greeting' ) ) ;
$bean->data = $message;
R::store( $bean );
$columns = R::inspect('bean');
asrt( array_key_exists( 'data', $columns ), TRUE );
asrt( ( $columns['data'] !== 'json' ), TRUE );
R::useJSONFeatures( TRUE );
R::nuke();
$bean = R::dispense('bean');
$message = array( 'message' => 'hello', 'type' => 'greeting' );
$bean->data = $message;
R::store( $bean );
$columns = R::inspect('bean');
asrt( array_key_exists( 'data', $columns ), TRUE );
asrt( $columns['data'], 'json' );
$bean = $bean->fresh();
$message = json_decode( $bean->data, TRUE );
asrt( $message['message'], 'hello' );
asrt( $message['type'], 'greeting' );
$message['message'] = 'hi';
$bean->data = $message;
R::store( $bean );
pass();
$bean = R::findOne( 'bean' );
$message = json_decode( $bean->data );
asrt( $message->message, 'hi' );
$book = R::dispense( 'book' );
$book->page = 'lorem ipsum';
R::store( $book );
$book = $book->fresh();
asrt( $book->page, 'lorem ipsum' );
$book2 = R::dispense( 'book' );
$book2->page = array( 'chapter' => '1' );
R::store( $book2 );
pass(); //should not try to modify column and trigger exception
$book = $book->fresh();
asrt( $book->page, 'lorem ipsum' );
$columns = R::inspect('book');
asrt( ( $columns['page'] !== 'json' ), TRUE );
$building = R::dispense( 'building' );
$building->year = 'MLXXVIII';
R::store( $building );
$shop = R::dispense( 'building' );
$shop->year = '2010-01-01';
R::store( $shop );
$building = R::load( 'building', $building->id );
asrt( $building->year, 'MLXXVIII' );
$columns = R::inspect( 'building' );
asrt( strpos( strtolower( $columns['year'] ), 'date' ), FALSE );
$shop->anno = '2010-01-01';
R::store( $shop );
$columns = R::inspect( 'building' );
asrt( $columns['anno'], 'date' );
R::useJSONFeatures( FALSE );
}
/**
* Test scanning and coding.
*
* @return void
*/
public function testScanningAndCoding()
{
$toolbox = R::getToolBox();
$adapter = $toolbox->getDatabaseAdapter();
$writer = $toolbox->getWriter();
$redbean = $toolbox->getRedBean();
$pdo = $adapter->getDatabase();
$a = new AssociationManager( $toolbox );
$adapter->exec( "DROP TABLE IF EXISTS testtable" );
asrt( in_array( "testtable", $writer->getTables() ), FALSE );
$writer->createTable( "testtable" );
asrt( in_array( "testtable", $writer->getTables() ), TRUE );
asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 1 );
asrt( in_array( "id", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), FALSE );
$writer->addColumn( "testtable", "c1", 1 );
asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 2 );
asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
foreach ( $writer->sqltype_typeno as $key => $type ) {
if ( $type < 100 ) {
asrt( $writer->code( $key, TRUE ), $type );
} else {
asrt( $writer->code( $key ), PostgreSQL::C_DATATYPE_SPECIFIED );
}
}
asrt( $writer->code( PostgreSQL::C_DATATYPE_SPECIAL_DATETIME ), PostgreSQL::C_DATATYPE_SPECIFIED );
asrt( $writer->code( "unknown" ), PostgreSQL::C_DATATYPE_SPECIFIED );
asrt( $writer->scanType( FALSE ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( TRUE ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( NULL ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( 2 ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( 255 ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( 256 ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( -1 ), PostgreSQL::C_DATATYPE_INTEGER );
asrt( $writer->scanType( 1.5 ), PostgreSQL::C_DATATYPE_DOUBLE );
asrt( $writer->scanType( INF ), PostgreSQL::C_DATATYPE_TEXT );
asrt( $writer->scanType( "abc" ), PostgreSQL::C_DATATYPE_TEXT );
asrt( $writer->scanType( "2001-10-10", TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_DATE );
asrt( $writer->scanType( "2001-10-10 10:00:00", TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_DATETIME );
asrt( $writer->scanType( "2001-10-10 10:00:00" ), PostgreSQL::C_DATATYPE_TEXT );
asrt( $writer->scanType( "2001-10-10" ), PostgreSQL::C_DATATYPE_TEXT );
asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), PostgreSQL::C_DATATYPE_TEXT );
$writer->widenColumn( "testtable", "c1", PostgreSQL::C_DATATYPE_TEXT );
$cols = $writer->getColumns( "testtable" );
asrt( $writer->code( $cols["c1"] ), PostgreSQL::C_DATATYPE_TEXT );
$writer->addColumn( "testtable", "special", PostgreSQL::C_DATATYPE_SPECIAL_DATE );
$cols = $writer->getColumns( "testtable" );
asrt( $writer->code( $cols['special'], TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_DATE );
asrt( $writer->code( $cols['special'], FALSE ), PostgreSQL::C_DATATYPE_SPECIFIED );
$writer->addColumn( "testtable", "special2", PostgreSQL::C_DATATYPE_SPECIAL_DATETIME );
$cols = $writer->getColumns( "testtable" );
asrt( $writer->code( $cols['special2'], TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_DATETIME );
asrt( $writer->code( $cols['special'], FALSE ), PostgreSQL::C_DATATYPE_SPECIFIED );
$id = $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "lorem ipsum" ) ) );
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
asrt( $row[0]["c1"], "lorem ipsum" );
$writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "ipsum lorem" ) ), $id );
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
asrt( $row[0]["c1"], "ipsum lorem" );
$writer->deleteRecord( "testtable", array( "id" => array( $id ) ) );
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
asrt( empty( $row ), TRUE );
}
/**
* (FALSE should be stored as 0 not as '')
*
* @return void
*/
public function testZeroIssue()
{
testpack( "Zero issue" );
$toolbox = R::getToolBox();
$redbean = $toolbox->getRedBean();
$bean = $redbean->dispense( "zero" );
$bean->zero = FALSE;
$bean->title = "bla";
$redbean->store( $bean );
asrt( count( $redbean->find( "zero", array(), " zero = 0 " ) ), 1 );
testpack( "Test ANSI92 issue in clearrelations" );
$a = new AssociationManager( $toolbox );
$book = $redbean->dispense( "book" );
$author1 = $redbean->dispense( "author" );
$author2 = $redbean->dispense( "author" );
$book->title = "My First Post";
$author1->name = "Derek";
$author2->name = "Whoever";
set1toNAssoc( $a, $book, $author1 );
set1toNAssoc( $a, $book, $author2 );
pass();
}
/**
* Various.
* Tests whether writer correctly handles keyword 'group' and SQL state 23000 issue.
* These tests remain here to make sure issues 9 and 10 never happen again.
* However this bug will probably never re-appear due to changed architecture.
*
* @return void
*/
public function testIssue9and10()
{
$toolbox = R::getToolBox();
$redbean = $toolbox->getRedBean();
$adapter = $toolbox->getDatabaseAdapter();
$a = new AssociationManager( $toolbox );
$book = $redbean->dispense( "book" );
$author1 = $redbean->dispense( "author" );
$author2 = $redbean->dispense( "author" );
$book->title = "My First Post";
$author1->name = "Derek";
$author2->name = "Whoever";
$a->associate( $book, $author1 );
$a->associate( $book, $author2 );
pass();
testpack( "Test Association Issue Group keyword (Issues 9 and 10)" );
R::nuke();
$group = $redbean->dispense( "group" );
$group->name = "mygroup";
$redbean->store( $group );
try {
$a->associate( $group, $book );
pass();
} catch ( SQL $e ) {
fail();
}
// Test issue SQL error 23000
try {
$a->associate( $group, $book );
pass();
} catch ( SQL $e ) {
fail();
}
asrt( (int) $adapter->getCell( "select count(*) from book_group" ), 1 ); //just 1 rec!
}
/**
* Test various.
* Test various somewhat uncommon trash/unassociate scenarios.
* (i.e. unassociate unrelated beans, trash non-persistant beans etc).
* Should be handled gracefully - no output checking.
*
* @return void
*/
public function testVaria()
{
$toolbox = R::getToolBox();
$redbean = $toolbox->getRedBean();
$a = new AssociationManager( $toolbox );
$book = $redbean->dispense( "book" );
$author1 = $redbean->dispense( "author" );
$author2 = $redbean->dispense( "author" );
$book->title = "My First Post";
$author1->name = "Derek";
$author2->name = "Whoever";
$a->unassociate( $book, $author1 );
$a->unassociate( $book, $author2 );
pass();
$redbean->trash( $redbean->dispense( "bla" ) );
pass();
$bean = $redbean->dispense( "bla" );
$bean->name = 1;
$bean->id = 2;
$redbean->trash( $bean );
pass();
}
/**
* Test special types.
*
* @return void
*/
public function testTypes()
{
testpack( 'Special data types' );
$bean = R::dispense( 'bean' );
$bean->date = 'someday';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['date'], 'text' );
$bean = R::dispense( 'bean' );
$bean->date = '2011-10-10';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['date'], 'text' );
}
/**
* Test dates.
*
* @return void
*/
public function testTypesDates()
{
$bean = R::dispense( 'bean' );
$bean->date = '2011-10-10';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['date'], 'date' );
}
/**
* Datetime.
*
* @return void
*/
public function testTypesDateTimes()
{
$bean = R::dispense( 'bean' );
$bean->date = '2011-10-10 10:00:00';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['date'], 'timestamp without time zone' );
}
/**
* Test spatial data types.
*
* @return void
*/
public function testTypesPoints()
{
$bean = R::dispense( 'bean' );
$bean->point = '(92,12)';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['point'], 'point' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->point, '(92,12)' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->point, '(92,12)' );
}
/**
* Test points.
*
* @return void
*/
public function testTypesDecPoints()
{
$bean = R::dispense( 'bean' );
$bean->point = '(9.2,1.2)';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['point'], 'point' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->point, '(9.2,1.2)' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->point, '(9.2,1.2)' );
}
/**
* Test polygons.
*
* @return void
*/
public function testPolygons()
{
$bean = R::dispense( 'bean' );
$bean->polygon = '((0,0),(1,1),(2,0))';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['polygon'], 'polygon' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->polygon, '((0,0),(1,1),(2,0))' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->polygon, '((0,0),(1,1),(2,0))' );
$bean = R::dispense( 'bean' );
$bean->polygon = '((0,0),(1.2,1),(2,0.3))';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['polygon'], 'polygon' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->polygon, '((0,0),(1.2,1),(2,0.3))' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->polygon, '((0,0),(1.2,1),(2,0.3))' );
}
/**
* Test multi points.
*
* @return void
*/
public function testTypesMultiDecPoints()
{
$bean = R::dispense( 'bean' );
$bean->line = '[(1.2,1.4),(2.2,34)]';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['line'], 'lseg' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->line, '[(1.2,1.4),(2.2,34)]' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->line, '[(1.2,1.4),(2.2,34)]' );
}
/**
* More points...
*
* @return void
*/
public function testTypesWeirdPoints()
{
$bean = R::dispense( 'bean' );
$bean->circle = '<(9.2,1.2),7.9>';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['circle'], 'circle' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->circle, '<(9.2,1.2),7.9>' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->circle, '<(9.2,1.2),7.9>' );
}
/**
* Test money types.
*
* @return void
*/
public function testTypesMon()
{
$bean = R::dispense( 'bean' );
$bean->amount = '22.99';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['amount'], 'numeric' );
R::nuke();
$bean = R::dispense( 'bean' );
$bean->amount = '-22.99';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['amount'], 'numeric' );
}
/**
* Test money data type.
*
* @return void
*/
public function testTypesMoney()
{
$bean = R::dispense( 'bean' );
$bean->money = '$123.45';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['money'], 'money' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->money, '$123.45' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->money, '$123.45' );
$bean->money = '$123,455.01';
R::store($bean);
$bean = $bean->fresh();
asrt( $bean->money, '$123,455.01' );
}
/**
* Test negative money data type.
*
* @return void
*/
public function testTypesNegativeMoney()
{
$bean = R::dispense( 'bean' );
$bean->money = '-$123.45';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['money'], 'money' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->money, '-$123.45' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->money, '-$123.45' );
}
/**
* Issue #340
* Redbean is currently picking up bcrypt hashed passwords
* (which look like this: $2y$12$85lAS....SnpDNVGPAC7w0G)
* as PostgreSQL money types.
* Then, once R::store is called on the bean, it chokes and throws the following error:
* PHP Fatal error: Uncaught [22P02] - SQLSTATE[22P02]: Invalid text representation: 7 ERROR:
* invalid input syntax for type money: ....
*
* @return void
*/
public function testTypesInvalidMoney()
{
$bean = R::dispense( 'bean' );
$bean->nomoney = '$2y$12$85lAS';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['nomoney'], 'text' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->nomoney, '$2y$12$85lAS' );
$bean->note = 'taint';
R::store( $bean );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->nomoney, '$2y$12$85lAS' );
}
/**
* Test types of strings.
*
* @return void
*/
public function testTypesStrings()
{
$bean = R::dispense( 'bean' );
$bean->data = 'abcdefghijk';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
$bean = R::load( 'bean', $bean->id );
asrt( $bean->data, 'abcdefghijk' );
$bean->data = '(1,2)';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
$bean->data = '[(1.2,1.4),(2.2,34)]';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
$bean->data = '<(9.2,1.2),7.9>';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
$bean->data = '$25';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
$bean->data = '2012-10-10 10:00:00';
R::store( $bean );
$cols = R::getColumns( 'bean' );
asrt( $cols['data'], 'text' );
}
/**
* Can we manually add a Postgres time column without a time zone
* and with a time zone?
*
* @return void
*/
public function testTime()
{
R::nuke();
$clock = R::dispense('clock');
$clock->time = '10:00:00';
$clock->setMeta('cast.time', 'time');
R::store( $clock );
$columns = R::inspect('clock');
asrt( $columns['time'], 'time without time zone' );
R::nuke();
$clock = R::dispense('clock');
$clock->time = '10:00:00 PST';
$clock->setMeta('cast.time', 'time with time zone');
R::store( $clock );
$columns = R::inspect('clock');
asrt( $columns['time'], 'time with time zone' );
}
}