4 * RedUNIT Shared Test Classes / Mock Objects
5 * This file contains a collection of test classes that can be used by
11 * This is just for testing
13 class ObservableMock extends \RedBeanPHP\Observable
19 public function test( $eventname, $info )
21 $this->signal( $eventname, $info );
27 * This is just for testing
29 class ObserverMock implements \RedBeanPHP\Observer
34 public $event = FALSE;
42 * @param string $event
45 public function onEvent( $event, $info )
47 $this->event = $event;
53 * Shared helper class for tests.
54 * A test model to test FUSE functions.
56 class Model_Band extends RedBeanPHP\SimpleModel
58 public function after_update() { }
60 private $notes = array();
65 public function update()
67 if ( count( $this->ownBandmember ) > 4 ) {
68 throw new Exception( 'too many!' );
75 public function __toString()
84 public function setProperty( $prop, $value )
86 $this->$prop = $value;
94 public function checkProperty( $prop )
96 return isset( $this->$prop );
102 * @param string $note
104 * @param mixed $value
108 public function setNote( $note, $value )
110 $this->notes[ $note ] = $value;
114 * Returns the value of a note.
116 * @param string $note
120 public function getNote( $note )
122 return $this->notes[ $note ];
127 * Shared helper class for tests.
128 * A Model class for testing Models/FUSE and related features.
130 class Model_Box extends RedBeanPHP\SimpleModel
132 public function delete() { $a = $this->bean->ownBottle; }
136 * Shared helper class for tests.
137 * A Model class for testing Models/FUSE and related features.
139 class Model_Cocoa extends RedBeanPHP\SimpleModel
141 public function update(){}
145 * Shared helper class for tests.
146 * A Model class for testing Models/FUSE and related features.
148 class Model_Taste extends RedBeanPHP\SimpleModel
150 public function after_update()
152 asrt( count( $this->bean->ownCocoa ), 0 );
157 * Shared helper class for tests.
158 * A Model class for testing Models/FUSE and related features.
160 class Model_Coffee extends RedBeanPHP\SimpleModel
162 public function update()
164 while ( count( $this->bean->ownSugar ) > 3 ) {
165 array_pop( $this->bean->ownSugar );
171 * Shared helper class for tests.
172 * A Model class for testing Models/FUSE and related features.
174 class Model_Test extends RedBeanPHP\SimpleModel
176 public function update()
178 if ( $this->bean->item->val ) {
179 $this->bean->item->val = 'Test2';
180 $can = R::dispense( 'can' );
181 $can->name = 'can for bean';
182 $s = reset( $this->bean->sharedSpoon );
184 $this->bean->item->deep->name = '123';
185 $this->bean->ownCan[] = $can;
186 $this->bean->sharedPeas = R::dispense( 'peas', 10 );
187 $this->bean->ownChip = R::dispense( 'chip', 9 );
195 * Shared helper class for tests.
196 * A Model class for testing Models/FUSE and related features.
198 class Model_Bandmember extends RedBeanPHP\SimpleModel
200 public function open()
204 $lifeCycle .= "\n called open: " . $this->id;
207 public function dispense()
211 $lifeCycle .= "\n called dispense() " . $this->bean;
214 public function update()
218 $lifeCycle .= "\n called update() " . $this->bean;
221 public function after_update()
225 $lifeCycle .= "\n called after_update() " . $this->bean;
228 public function delete()
232 $lifeCycle .= "\n called delete() " . $this->bean;
235 public function after_delete()
239 $lifeCycle .= "\n called after_delete() " . $this->bean;
244 * A model to box soup models :)
246 class Model_Soup extends \RedBeanPHP\SimpleModel
248 private $flavour = '';
250 public function taste()
252 return 'A bit too salty';
255 public function setFlavour( $flavour )
257 $this->flavour = $flavour;
260 public function getFlavour()
262 return $this->flavour;
267 * A custom BeanHelper to test custom FUSE operations.
269 class SoupBeanHelper extends \RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper
272 * Returns a model for a bean based on its type.
274 * @param OODBBean $bean
278 public function getModelForBean( \RedBeanPHP\OODBBean $bean )
280 if ( $bean->getMeta( 'type' ) === 'meal' ) {
281 $model = new Model_Soup;
282 $model->loadBean( $bean );
285 return parent::getModelForBean( $bean );
293 class Model_Boxedbean extends \RedBeanPHP\SimpleModel
298 * Mock class for testing purposes.
300 class Model_Ghost_House extends \RedBeanPHP\SimpleModel
302 public static $deleted = FALSE;
304 public function delete()
306 self::$deleted = TRUE;
311 * Mock class for testing purposes.
313 class Model_Ghost_Ghost extends \RedBeanPHP\SimpleModel
315 public static $deleted = FALSE;
317 public function delete()
319 self::$deleted = TRUE;
324 * Mock class for testing purposes.
326 class FaultyWriter extends \RedBeanPHP\QueryWriter\MySQL
334 * @param string $sqlState sql state
336 public function setSQLState( $sqlState )
338 $this->sqlState = $sqlState;
344 * @param string $sourceType destination type
345 * @param string $destType source type
349 public function addUniqueConstraint( $sourceType, $destType )
351 $exception = new \RedBeanPHP\RedException\SQL;
352 $exception->setSQLState( $this->sqlState );
358 * Generates an exception when trying to invoke
359 * getKeyMapForType via getForeignKeyForTypeProperty.
363 protected function getKeyMapForType( $type )
365 throw new \RedBeanPHP\RedException\SQL;
370 * Mock class to test default implementations in AQueryWriter.
372 class NullWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {
375 class ProxyWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {
377 public static function callMethod( $object, $method, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL ) {
378 return $object->$method( $arg1, $arg2, $arg3 );
384 * Mock class to test proper model name
385 * beautificattion for link table beans in FUSE.
387 class Model_PageWidget extends RedBean_SimpleModel {
391 private static $test = '';
394 * Returns the test flag.
398 public static function getTestReport()
404 * Update method to set the flag.
406 public function update()
408 self::$test = 'didSave';
413 * Mock class to test proper model name
414 * beautificattion for link table beans in FUSE.
416 class Model_Gadget_Page extends RedBean_SimpleModel {
420 private static $test = '';
423 * Returns the test flag.
427 public static function getTestReport()
433 * Update method to set the flag.
435 public function update()
437 self::$test = 'didSave';
442 * Mock class to test proper model name
443 * beautificattion for link table beans in FUSE.
445 class Model_A_B_C extends RedBean_SimpleModel {
449 private static $test = '';
452 * Returns the test flag.
456 public static function getTestReport()
462 * Update method to set the flag.
464 public function update()
466 self::$test = 'didSave';
470 class Model_BookBook extends \RedBean_SimpleModel {
471 public function delete() {
472 asrt($this->bean->shelf, 'x13');
476 class Model_Feed extends \RedbeanPHP\SimpleModel {
477 public function update() {
478 $this->bean->post = json_encode($this->bean->post);
481 public function open() {
482 $this->bean->post = json_decode($this->bean->post, true);
487 * UUID QueryWriter for MySQL for testing purposes.
489 class UUIDWriterMySQL extends \RedBeanPHP\QueryWriter\MySQL {
491 protected $defaultValue = '@uuid';
492 const C_DATATYPE_SPECIAL_UUID = 97;
494 public function __construct( \RedBeanPHP\Adapter $adapter )
496 parent::__construct( $adapter );
497 $this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'char(36)' );
500 public function createTable( $table )
502 $table = $this->esc( $table );
504 CREATE TABLE {$table} (
505 id char(36) NOT NULL,
507 ENGINE = InnoDB DEFAULT
509 COLLATE=utf8mb4_unicode_ci ";
510 $this->adapter->exec( $sql );
513 public function updateRecord($table, $updateValues, $id = NULL)
515 $flagNeedsReturnID = (!$id);
516 if ($flagNeedsReturnID) R::exec('SET @uuid = uuid() ');
517 $id = parent::updateRecord( $table, $updateValues, $id );
518 if ( $flagNeedsReturnID ) $id = R::getCell('SELECT @uuid');
522 public function getTypeForID()
524 return self::C_DATATYPE_SPECIAL_UUID;
529 * UUID QueryWriter for PostgreSQL for testing purposes.
531 class UUIDWriterPostgres extends \RedBeanPHP\QueryWriter\PostgreSQL {
533 protected $defaultValue = 'uuid_generate_v4()';
534 const C_DATATYPE_SPECIAL_UUID = 97;
536 public function __construct( \RedBeanPHP\Adapter $adapter )
538 parent::__construct( $adapter );
539 $this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'uuid' );
542 public function createTable( $table )
544 $table = $this->esc( $table );
545 $this->adapter->exec( " CREATE TABLE $table (id uuid PRIMARY KEY); " );
548 public function getTypeForID()
550 return self::C_DATATYPE_SPECIAL_UUID;
554 class DiagnosticBean extends \RedBeanPHP\OODBBean {
557 * Returns current status of modification flags.
561 public function getModFlags()
564 if ($this->aliasName !== NULL) $modFlags .= 'a';
565 if ($this->fetchType !== NULL) $modFlags .= 'f';
566 if ($this->noLoad === TRUE) $modFlags .= 'n';
567 if ($this->all === TRUE) $modFlags .= 'r';
568 if ($this->withSql !== '') $modFlags .= 'w';
575 class DiagnosticModel extends \RedBeanPHP\SimpleModel
578 private $logs = array();
580 public function open()
582 $this->logs[] = array(
590 public function dispense()
592 $this->logs[] = array(
593 'action' => 'dispense',
595 'bean' => $this->bean
600 public function update()
602 $this->logs[] = array(
603 'action' => 'update',
605 'bean' => $this->bean
610 public function after_update()
612 $this->logs[] = array(
613 'action' => 'after_update',
615 'bean' => $this->bean
620 public function delete()
622 $this->logs[] = array(
623 'action' => 'delete',
625 'bean' => $this->bean
630 public function after_delete()
632 $this->logs[] = array(
633 'action' => 'after_delete',
635 'bean' => $this->bean
640 public function getLogs()
645 public function getLogActionCount( $action = NULL )
647 if ( is_null( $action ) ) return count( $this->logs );
649 foreach( $this->logs as $log ) {
650 if ( $log['action'] == $action ) $counter ++;
655 public function clearLog()
657 return $this->logs = array();
660 public function getDataFromLog( $logIndex = 0, $property )
662 return $this->logs[$logIndex]['data'][$property];
667 class Model_Probe extends DiagnosticModel {};
669 define('REDBEAN_OODBBEAN_CLASS', '\DiagnosticBean');