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,635 @@
<?php
/**
* RedUNIT Shared Test Classes / Mock Objects
* This file contains a collection of test classes that can be used by
* the test suite. None of these classes should be used by users of
* the RedBeanPHP library, they are meant for internal use only!
* These classes are most of the time, single purpose classes, that are
* only used once or twice. They are written down in a compact format
* because the overview of their limited functionality is handy wereas
* documentation per method is not very useful in this case.
*/
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Observable Mock
*/
class ObservableMock extends \RedBeanPHP\Observable
{
public function test( $eventname, $info ){ $this->signal( $eventname, $info ); }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Observer Mock
*/
class ObserverMock implements \RedBeanPHP\Observer
{
public $event = FALSE;
public $info = FALSE;
public function onEvent( $event, $info ){ $this->event = $event; $this->info = $info; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A test model to test FUSE functions.
*/
class Model_Band extends RedBeanPHP\SimpleModel
{
public function after_update() { }
private $notes = array();
public function update()
{
if ( count( $this->ownBandmember ) > 4 ) throw new Exception( 'too many!' );
}
public function __toString(){ return 'bigband'; }
public function setProperty( $prop, $value ) { $this->$prop = $value; }
public function checkProperty( $prop ) { return isset( $this->$prop ); }
public function setNote( $note, $value ){ $this->notes[ $note ] = $value; }
public function getNote( $note ) { return $this->notes[ $note ]; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
*/
class Model_Box extends RedBeanPHP\SimpleModel
{
public function delete() { $a = $this->bean->ownBottle; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
*/
class Model_Cocoa extends RedBeanPHP\SimpleModel
{
public function update(){}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
*/
class Model_Taste extends RedBeanPHP\SimpleModel
{
public function after_update()
{
asrt( count( $this->bean->ownCocoa ), 0 );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
*/
class Model_Coffee extends RedBeanPHP\SimpleModel
{
public static $defaults = array();
public function dispense()
{
if ( count( self::$defaults ) && !$this->bean->id ) {
foreach (self::$defaults as $key => $value) {
$this->{$key} = $value;
}
}
}
public function __jsonSerialize()
{
return array_merge(
$this->bean->export(),
array( 'description' => "{$this->bean->variant}.{$this->bean->strength}" )
);
}
public function update()
{
while ( count( $this->bean->ownSugar ) > 3 ) {
array_pop( $this->bean->ownSugar );
}
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
*/
class Model_Test extends RedBeanPHP\SimpleModel
{
public function update()
{
if ( $this->bean->item->val ) {
$this->bean->item->val = 'Test2';
$can = R::dispense( 'can' );
$can->name = 'can for bean';
$s = reset( $this->bean->sharedSpoon );
$s->name = "S2";
$this->bean->item->deep->name = '123';
$this->bean->ownCan[] = $can;
$this->bean->sharedPeas = R::dispense( 'peas', 10 );
$this->bean->ownChip = R::dispense( 'chip', 9 );
}
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Shared helper class for tests.
* A Model class for testing Models/FUSE and related features.
* Used in Blackhole/Export.
*/
global $lifeCycle;
class Model_Bandmember extends RedBeanPHP\SimpleModel
{
public function open(){ global $lifeCycle; $lifeCycle .= "\n called open: " . $this->id; }
public function dispense(){ global $lifeCycle; $lifeCycle .= "\n called dispense() " . $this->bean; }
public function update() { global $lifeCycle; $lifeCycle .= "\n called update() " . $this->bean; }
public function after_update(){ global $lifeCycle; $lifeCycle .= "\n called after_update() " . $this->bean; }
public function delete(){ global $lifeCycle; $lifeCycle .= "\n called delete() " . $this->bean; }
public function after_delete(){ global $lifeCycle; $lifeCycle .= "\n called after_delete() " . $this->bean; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* A custom BeanHelper to test custom FUSE operations in
* Blackhole/Fusebox
*/
class Model_Soup extends \RedBeanPHP\SimpleModel
{
private $flavour = '';
public function taste() { return 'A bit too salty'; }
public function setFlavour( $flavour ) { $this->flavour = $flavour; }
public function getFlavour(){ return $this->flavour; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* A custom BeanHelper to test custom FUSE operations in
* Base/Fuse.
*/
class SoupBeanHelper extends \RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper
{
public function getModelForBean( \RedBeanPHP\OODBBean $bean )
{
if ( $bean->getMeta( 'type' ) === 'meal' ) {
$model = new Model_Soup;
$model->loadBean( $bean );
return $model;
} else {
return parent::getModelForBean( $bean );
}
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Base/Boxing and Base/Misc to test boxing of beans.
* Just a plain model for use with a bean with nothing in it.
*/
class Model_Boxedbean extends \RedBeanPHP\SimpleModel{}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Mysql/Uuid, Postgres/Uuid and Base/Association. Meant
* to be a versatile, generic test model.
*/
class Model_Ghost_House extends \RedBeanPHP\SimpleModel
{
public static $deleted = FALSE;
public function delete() { self::$deleted = TRUE; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Mysql/Uuid, Postgres/Uuid and Base/Association. Meant
* to be a versatile, generic test model for N-M relations.
*/
class Model_Ghost_Ghost extends \RedBeanPHP\SimpleModel
{
public static $deleted = FALSE;
public function delete() { self::$deleted = TRUE; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Mock class for testing purposes. Used in Base/Association and
* Base/Foreignkeys to emit errors to test handling of errors
* originating from the Query Writer.
*/
class FaultyWriter extends \RedBeanPHP\QueryWriter\MySQL
{
protected $sqlState;
public function setSQLState( $sqlState ){ $this->sqlState = $sqlState; }
public function addUniqueConstraint( $sourceType, $destType ){
$exception = new \RedBeanPHP\RedException\SQL;
$exception->setSQLState( $this->sqlState );
throw $exception;
}
protected function getKeyMapForType( $type ){throw new \RedBeanPHP\RedException\SQL;}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Mock class to test default implementations in AQueryWriter.
*/
class NullWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Base/Foreignkeys (testFKInspect) to test foreign keys.
*/
class ProxyWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {
public static function callMethod( $object, $method, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL ) {
return $object->$method( $arg1, $arg2, $arg3 );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Mock class to test proper model name
* beautificattion for link table beans in FUSE.
*/
class Model_PageWidget extends RedBean_SimpleModel {
private static $test = '';
public static function getTestReport(){ return self::$test; }
public function update(){ self::$test = 'didSave'; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Mock class to test proper model name
* beautificattion for link table beans in FUSE.
*/
class Model_Gadget_Page extends RedBean_SimpleModel {
private static $test = '';
public static function getTestReport(){ return self::$test;}
public function update(){ self::$test = 'didSave'; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Mock class to test proper model name
* beautificattion for link table beans in FUSE.
*/
class Model_A_B_C extends RedBean_SimpleModel {
private static $test = '';
public static function getTestReport(){ return self::$test; }
public function update() { self::$test = 'didSave'; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Base/Update to test SQL filters with links
*/
class Model_BookBook extends \RedBean_SimpleModel {
public function delete() {
asrt($this->bean->shelf, 'x13');
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Base/Fuse (error handling in Fuse) and
* Base/Issue408 (export issue).
*/
class Model_Feed extends \RedbeanPHP\SimpleModel {
public function update() { $this->bean->post = json_encode( $this->bean->post );}
public function open() { $this->bean->post = json_decode( $this->bean->post, TRUE );}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* UUID QueryWriter for MySQL for testing purposes.
* Used in Mysql/Uuid to test if RedBeanPHP can be used with a
* UUID-strategy. While UUID keys are not part of the RedBeanPHP core,
* examples are given on the website and this test makes sure those examples
* are working as expected.
*/
class UUIDWriterMySQL extends \RedBeanPHP\QueryWriter\MySQL {
protected $defaultValue = '@uuid';
const C_DATATYPE_SPECIAL_UUID = 97;
public function __construct( \RedBeanPHP\Adapter $adapter )
{
parent::__construct( $adapter );
$this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'char(36)' );
}
public function createTable( $table )
{
$table = $this->esc( $table );
$sql = "
CREATE TABLE {$table} (
id char(36) NOT NULL,
PRIMARY KEY ( id ))
ENGINE = InnoDB DEFAULT
CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci ";
$this->adapter->exec( $sql );
}
public function updateRecord($table, $updateValues, $id = NULL)
{
$flagNeedsReturnID = (!$id);
if ($flagNeedsReturnID) R::exec('SET @uuid = uuid() ');
$id = parent::updateRecord( $table, $updateValues, $id );
if ( $flagNeedsReturnID ) $id = R::getCell('SELECT @uuid');
return $id;
}
public function getTypeForID(){return self::C_DATATYPE_SPECIAL_UUID;}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* UUID QueryWriter for PostgreSQL for testing purposes.
* Used in Postgres/Uuid to test if RedBeanPHP can be used with a
* UUID-strategy. While UUID keys are not part of the RedBeanPHP core,
* examples are given on the website and this test makes sure those examples
* are working as expected.
*/
class UUIDWriterPostgres extends \RedBeanPHP\QueryWriter\PostgreSQL {
protected $defaultValue = 'uuid_generate_v4()';
const C_DATATYPE_SPECIAL_UUID = 97;
public function __construct( \RedBeanPHP\Adapter $adapter )
{
parent::__construct( $adapter );
$this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'uuid' );
}
public function createTable( $table )
{
$table = $this->esc( $table );
$this->adapter->exec( " CREATE TABLE $table (id uuid PRIMARY KEY); " );
}
public function getTypeForID()
{
return self::C_DATATYPE_SPECIAL_UUID;
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* This diagnostic bean class adds a method to read the current
* status of the modifier flags. Used to test interactions with
* beans and monitor the effect on the internal flags.
*/
class DiagnosticBean extends \RedBeanPHP\OODBBean {
/**
* Returns current status of modification flags.
*
* @return string
*/
public function getModFlags()
{
$modFlags = '';
if ($this->aliasName !== NULL) $modFlags .= 'a';
if ($this->fetchType !== NULL) $modFlags .= 'f';
if ($this->noLoad === TRUE) $modFlags .= 'n';
if ($this->all === TRUE) $modFlags .= 'r';
if ($this->withSql !== '') $modFlags .= 'w';
return $modFlags;
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* This is diagnostic class that allows access to otherwise
* protected methods.Used to test FUSE hooks in Base/Fuse.php
* Subclassed by Model_Probe.
*/
class DiagnosticModel extends \RedBeanPHP\SimpleModel
{
private $logs = array();
public function open() { $this->logs[] = array('action' => 'open','data'=> array('id' => $this->id));}
public function dispense(){$this->logs[] = array('action' => 'dispense','data' => array('bean' => $this->bean));}
public function update(){$this->logs[] = array('action' => 'update','data' => array('bean' => $this->bean));}
public function after_update(){$this->logs[] = array('action' => 'after_update','data'=> array('bean' => $this->bean));}
public function delete(){$this->logs[] = array('action' => 'delete','data'=> array('bean' => $this->bean));}
public function after_delete(){$this->logs[] = array('action' => 'after_delete','data' => array('bean' => $this->bean));}
public function getLogs(){return $this->logs;}
public function getLogActionCount( $action = NULL )
{
if ( is_null( $action ) ) return count( $this->logs );
$counter = 0;
foreach( $this->logs as $log ) if ( $log['action'] == $action ) $counter ++;
return $counter;
}
public function clearLog(){return $this->logs = array();}
public function getDataFromLog( $logIndex = 0, $property ){return $this->logs[$logIndex]['data'][$property];}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Base/Database (testDatabaseCapabilityChecker) to check
* database capabilities.
*/
class DatabaseCapabilityChecker extends \RedBeanPHP\Driver\RPDO {
public function __construct( \PDO $pdo )
{
$this->pdo = $pdo;
}
public function checkCapability( $capID )
{
return $this->hasCap( $capID );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Used in Test Suite Base/Bean (testToStringOverride)
* to test string overrides.
*/
class Model_String extends \RedBeanPHP\SimpleModel {
public function __toString() {
return base64_encode( $this->bean->text );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* This is diagnostic class that allows access to otherwise
* protected methods.Used to test FUSE hooks in Base/Fuse.php
*/
class Model_Probe extends DiagnosticModel {};
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Class to mock adapter.
* Inspects behavior of classes interacting with the adapter class
* by capturing the method invocations.
*/
class Mockdapter implements \RedBeanPHP\Adapter {
public function answer( $id )
{
$error = "error{$id}";
$property = "answer{$id}";
if (isset($this->$error)) throw $this->$error;
if (isset($this->$property)) return $this->$property;
}
public function getSQL(){}
public function exec( $sql, $bindings = array(), $noevent = FALSE ){ return $this->answer('Exec'); }
public function get( $sql, $bindings = array() ){ return $this->answer('GetSQL'); }
public function getRow( $sql, $bindings = array() ){ return array(); }
public function getCol( $sql, $bindings = array() ){ return $this->answer('GetCol'); }
public function getCell( $sql, $bindings = array() ){ return ''; }
public function getAssoc( $sql, $bindings = array() ){ return array(); }
public function getAssocRow( $sql, $bindings = array() ){ return array(); }
public function getInsertID(){}
public function getAffectedRows(){}
public function getCursor( $sql, $bindings = array() ){}
public function getDatabase(){}
public function startTransaction(){}
public function commit(){}
public function rollback(){}
public function close(){}
public function setOption( $optionKey, $optionValue ){}
public function getDatabaseServerVersion(){ return 'Mock'; }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Custom Logger class.
*/
class CustomLogger extends \RedBeanPHP\Logger\RDefault
{
private $log;
public function getLogMessage(){ return $this->log; }
public function log() { $this->log = func_get_args(); }
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* This is diagnostic class that allows access to otherwise
* protected methods.
* Class to test protected method hasCap in RPDO.
*/
class TestRPO extends \RedBeanPHP\Driver\RPDO {
public function testCap( $cap ) {
return $this->hasCap( $cap );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* Class to mock PDO behavior.
*/
class MockPDO extends \PDO {
public $attributes = array();
public function __construct() { }
public function setAttribute( $att, $val = NULL ){ $this->attributes[ $att ] = $val; }
public function getDiagAttribute( $att ){ return $this->attributes[ $att ]; }
public function getAttribute( $att ) {
if ($att == \PDO::ATTR_SERVER_VERSION) return '5.5.3';
return 'x';
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* DiagnosticCUBRIDWriter
* Class for stub test for CUBRID database support.
*/
class DiagnosticCUBRIDWriter extends \RedBeanPHP\QueryWriter\CUBRID {
public function callMethod( $method, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL, $arg5 = NULL ) {
return $this->$method( $arg1, $arg2, $arg3, $arg4, $arg5 );
}
}
/**
* Test utility class.
* This class is meant for testing purposes only and should
* never be used for anything else than RedBeanPHP Unit Testing.
* This is an error class that allows RedBeanPHP Unit Tests to
* test error handling.
* Test Model that throws an exception upon update().
*/
class Model_Brokentoy extends \RedbeanPHP\SimpleModel {
public function update(){
throw new \Exception('Totally on purpose.');
}
}
define('REDBEAN_OODBBEAN_CLASS', '\DiagnosticBean');

View File

@@ -0,0 +1,520 @@
<?php
/**
* Convenience function for test hook.
* If you added the proper Writer class, the facade should be able
* to automatically load it, i.e. \RedBeanPHP\QueryWriter\MyWriter
*
* @global array $ini
*
* @param string $name name of the connection (key)
* @param string $dsn DSN to connect
* @param string $user username
* @param string $pass passwords
*/
function add_writer_to_tests( $name, $dsn, $user, $pass )
{
global $ini;
\RedUNIT\Base::addToDriverList( $name );
R::addDatabase( $name, $dsn, $user, $pass );
$ini[ $name ] = TRUE;
}
/**
* A simple print function that works
* both for CLI and HTML.
*
* @param string $text
*/
function printtext( $text )
{
if ( $_SERVER["DOCUMENT_ROOT"] ) {
echo "<BR>" . $text;
} else {
echo "\n" . $text;
}
}
/**
* Tests whether a === b. The minimalistic core of this little
* unit test framework.
*
* @global integer $tests
*
* @param mixed $a value for A
* @param mixed $b value for B
*/
function asrt( $a, $b )
{
if ( $a === $b ) {
global $tests;
$tests++;
print( "[" . $tests . "]" );
} else {
printtext( "FAILED TEST: EXPECTED $b BUT GOT: $a " );
fail();
}
}
/**
* called when a test is passed. prints the test number to the screen.
*/
function pass()
{
global $tests;
$tests++;
print( "[" . $tests . "]" );
}
/**
* called when a test fails. shows debug info and exits.
*/
function fail()
{
printtext( "FAILED TEST" );
debug_print_backtrace();
exit( 1 );
}
/**
* prints out the name of the current test pack.
*
* @param string $name name of the test pack
*/
function testpack( $name )
{
printtext( "\n\tSub testpack: " . $name . " \n\t" );
}
/**
* prints out the name of the current test pack.
*
* @param string $name name of the test pack
*/
function maintestpack( $name )
{
printtext( "\n\nTestpack: " . $name . " \n\t" );
}
/**
* Quickly resolves the formatted table name
*/
function tbl( $table )
{
return R::$writer->getFormattedTableName( $table );
}
/**
* Quickly resolves the formatted ID
*/
function ID( $table )
{
return R::$writer->getIDField( $table );
}
/**
* Emulates legacy function for use with older tests.
*/
function set1toNAssoc( $a, \RedBeanPHP\OODBBean $bean1, \RedBeanPHP\OODBBean $bean2 )
{
$type = $bean1->getMeta( "type" );
$a->clearRelations( $bean2, $type );
$a->associate( $bean1, $bean2 );
if ( count( $a->related( $bean2, $type ) ) === 1 ) {
;
} else {
throw new \RedBeanPHP\RedException\SQL( "Failed to enforce 1-N Relation for $type " );
}
}
/**
* Returns all property values of beans as a
* comma separated string sorted.
*
* @param array $beans beans
* @param string $property name of the property
*
* @return string $values values
*/
function getList( $beans, $property )
{
$items = array();
foreach ( $beans as $bean ) {
$items[] = $bean->$property;
}
sort( $items );
return implode( ",", $items );
}
/**
* Helper function to test IDs
*
* @param array $array array
*/
function testids( $array )
{
foreach ( $array as $key => $bean ) {
asrt( intval( $key ), intval( $bean->getID() ) );
}
}
/**
* Group modifier function. Tests random modifications
* of groups of beans. For use with tests that test N:1 relation mapping
* features.
*
* @param mixed $book3 book
* @param mixed $quotes quotes
* @param mixed $pictures pictures
* @param mixed $topics topics
*/
function modgr( $book3, $quotes, $pictures, $topics )
{
$key = array_rand( $quotes );
$quote = $quotes[$key];
$keyPic = array_rand( $pictures );
$picture = $pictures[$keyPic];
$keyTop = array_rand( $topics );
$topic = $topics[$keyTop];
if ( rand( 0, 1 ) ) {
$f = 0;
foreach ( $book3->ownQuote as $z ) {
if ( $z->note == $quote->note ) {
$f = 1;
break;
}
}
if ( !$f ) {
//echo "\n add a quote ";
$book3->ownQuote[] = $quote;
}
}
if ( rand( 0, 1 ) ) {
$f = 0;
foreach ( $book3->ownPicture as $z ) {
if ( $z->note == $picture->note ) {
$f = 1;
break;
}
}
if ( !$f ) {
// echo "\n add a picture ";
$book3->ownPicture[] = $picture;
}
}
if ( rand( 0, 1 ) ) {
$f = 0;
foreach ( $book3->sharedTopic as $z ) {
if ( $z->note == $topic->note ) {
$f = 1;
break;
}
}
if ( !$f ) {
$book3->sharedTopic[] = $topic;
}
}
if ( rand( 0, 1 ) && count( $book3->ownQuote ) > 0 ) {
$key = array_rand( $book3->ownQuote );
unset( $book3->ownQuote[$key] );
}
if ( rand( 0, 1 ) && count( $book3->ownPicture ) > 0 ) {
$key = array_rand( $book3->ownPicture );
unset( $book3->ownPicture[$key] );
}
if ( rand( 0, 1 ) && count( $book3->sharedTopic ) > 0 ) {
$key = array_rand( $book3->sharedTopic );
unset( $book3->sharedTopic[$key] );
}
if ( rand( 0, 1 ) && count( $book3->ownPicture ) > 0 ) {
$key = array_rand( $book3->ownPicture );
$book3->ownPicture[$key]->change = rand( 0, 100 );
}
if ( rand( 0, 1 ) && count( $book3->ownQuote ) > 0 ) {
$key = array_rand( $book3->ownQuote );
$book3->ownQuote[$key]->change = 'note ch ' . rand( 0, 100 );
}
if ( rand( 0, 1 ) && count( $book3->sharedTopic ) > 0 ) {
$key = array_rand( $book3->sharedTopic );
$book3->sharedTopic[$key]->change = rand( 0, 100 );
}
}
/**
* SetGet function, sets a value in a bean and retrieves it again
* after storage, useful for tests that want to make sure the same value
* that gets in, comes out again.
*
* @param mixed $val the value that needs to be preserved
*
* @return mixed $val the value as returned after storage-retrieval cycle.
*/
function setget( $val )
{
R::nuke();
$bean = R::dispense( "page" );
$bean->prop = $val;
$id = R::store( $bean );
$bean = R::load( "page", $id );
return $bean->prop;
}
/**
* Wrapper function to test BeanCan Server, does the boring
* plumming work.
*
* @param mixed $data Data for JSON-RPC request object
* @param mixed $params Parameters for JSON-RPC request object
* @param string $id Identification of JSON-RPC request to connect to response
*
* @return string $out Output JSON from BeanCan server.
*/
function fakeBeanCanServerRequest( $data, $params = NULL, $id = "1234", $whiteList = 'all' )
{
$j = array(
"jsonrpc" => "2.0",
"method" => $data,
"params" => $params,
"id" => $id
);
$can = new \RedBeanPHP\Plugin\BeanCan;
$request = json_encode( $j );
$can->setWhitelist( $whiteList );
$out = $can->handleJSONRequest( $request );
return $out;
}
/**
* Candy Cane Factory. Produces lots of candy canes.
*
* @return array $canes canes
*/
function candy_canes()
{
$canes = R::dispense( 'cane', 10 );
$i = 0;
foreach ( $canes as $k => $cane ) {
$canes[$k]->label = 'Cane No. ' . ( $i++ );
}
$canes[0]->cane = $canes[1];
$canes[1]->cane = $canes[4];
$canes[9]->cane = $canes[4];
$canes[6]->cane = $canes[4];
$canes[4]->cane = $canes[7];
$canes[8]->cane = $canes[7];
return $canes;
}
/**
* Returns an array containing the index names of all
* indexes on the specified table name.
*
* @param $tableNoQ table name without quotes or backticks
*
* @return array
*/
function getIndexes( $tableNoQ )
{
$writer = R::getWriter();
if ( ( $writer instanceof \RedBeanPHP\QueryWriter\MySQL ) || ( $writer instanceof \RedBeanPHP\QueryWriter\CUBRID ) ) {
$indexes = array();
$list = R::getAll( "SHOW INDEX FROM `{$tableNoQ}`" );
foreach( $list as $listItem ) {
$indexes[] = $listItem['Key_name'];
}
return $indexes;
}
if ( ( $writer instanceof \RedBeanPHP\QueryWriter\SQLiteT ) ) {
$indexes = array();
$list = R::getAll( " pragma index_list(`{$tableNoQ}`) " );
foreach( $list as $listItem ) {
$indexes[] = $listItem['name'];
}
return $indexes;
}
if ( ( $writer instanceof \RedBeanPHP\QueryWriter\PostgreSQL ) ) {
return R::getCol( " SELECT indexname FROM pg_indexes WHERE tablename = '{$tableNoQ}' AND schemaname = 'public' " );
}
return array();
}
function are_cols_in_unique( $type, $properties )
{
sort( $properties );
$propertyFootprint = implode( ',', $properties );
$uniques = get_uniques_for_type( $type );
foreach( $uniques as $unique ) {
sort( $unique );
$uniqueFootprint = implode( ',', $unique );
if ( $uniqueFootprint === $propertyFootprint ) return TRUE;
}
return FALSE;
}
function get_uniques_for_type( $type )
{
$list = array();
$writer = R::getWriter();
$adapter = R::getDatabaseAdapter();
global $currentDriver;
switch( $currentDriver ) {
case 'mysql':
$table = $writer->esc( $type, TRUE );
$columns = $adapter->get('
SELECT
information_schema.key_column_usage.constraint_name,
information_schema.key_column_usage.column_name
FROM
information_schema.table_constraints
INNER JOIN information_schema.key_column_usage
ON (
information_schema.table_constraints.constraint_name = information_schema.key_column_usage.constraint_name
AND information_schema.table_constraints.constraint_schema = information_schema.key_column_usage.constraint_schema
AND information_schema.table_constraints.constraint_catalog = information_schema.key_column_usage.constraint_catalog
)
WHERE
information_schema.table_constraints.table_schema IN (SELECT DATABASE())
AND information_schema.table_constraints.table_name = ?
AND information_schema.table_constraints.constraint_type = \'UNIQUE\'
', array( $table ) );
$uniques = array();
foreach( $columns as $column ) {
if ( !isset( $uniques[ $column['constraint_name'] ] ) ) $uniques[ $column['constraint_name'] ] = array();
$uniques[ $column['constraint_name'] ][] = $column['column_name'];
}
$list = $uniques;
break;
case 'pgsql':
$table = $writer->esc( $type, TRUE );
$columns = $adapter->get('
SELECT
information_schema.key_column_usage.constraint_name,
information_schema.key_column_usage.column_name
FROM
information_schema.table_constraints
INNER JOIN information_schema.key_column_usage
ON (
information_schema.table_constraints.constraint_name = information_schema.key_column_usage.constraint_name
AND information_schema.table_constraints.constraint_schema = information_schema.key_column_usage.constraint_schema
AND information_schema.table_constraints.constraint_catalog = information_schema.key_column_usage.constraint_catalog
)
WHERE
information_schema.table_constraints.table_catalog = current_database()
AND information_schema.key_column_usage.table_schema = ANY( current_schemas( FALSE ) )
AND information_schema.table_constraints.table_name = ?
AND information_schema.table_constraints.constraint_type = \'UNIQUE\'
', array( $table ) );
$uniques = array();
foreach( $columns as $column ) {
if ( !isset( $uniques[ $column['constraint_name'] ] ) ) $uniques[ $column['constraint_name'] ] = array();
$uniques[ $column['constraint_name'] ][] = $column['column_name'];
}
$list= $uniques;
break;
case 'sqlite':
$uniques = array();
$table = $writer->esc( $type, TRUE );
$indexes = $adapter->get( "PRAGMA index_list({$table})" );
foreach( $indexes as $index ) {
if ( $index['unique'] == 1 ) {
$info = $adapter->get( "PRAGMA index_info({$index['name']})" );
if ( !isset( $uniques[$index['name']] ) ) $uniques[$index['name']] = array();
foreach( $info as $piece ) {
$uniques[$index['name']][] = $piece['name'];
}
}
}
$list = $uniques;
break;
case 'CUBRID':
try {
$sqlCode = $adapter->get("SHOW CREATE TABLE `{$type}`");
} catch ( \Exception $e ) {
$sqlCode = '';
}
if (!isset($sqlCode[0])) return array();
$matches = array();
preg_match_all('/CONSTRAINT\s+\[([\w_]+)\]\s+UNIQUE\s+KEY\s+\(([^\)]+)\)/', $sqlCode[0]['CREATE TABLE'], $matches);
$list = array();
if (!isset($matches[0])) return $list;
$max = count($matches[0]);
for($i = 0; $i < $max; $i++) {
$columns = explode(',', $matches[2][$i]);
foreach( $columns as $key => $column ) {
$columns[$key] = trim( $column, '[] ');
}
$list[ $matches[1][$i] ] = $columns;
}
break;
}
return $list;
}
//array_column PHP 7 implementation
function colfield( $objects, $field ) {
$ids = array();
foreach($objects as $object) $ids[] = $object->{$field};
return $ids;
}