Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / helpers / classes.php
1 <?php
2
3 /**
4  * RedUNIT Shared Test Classes / Mock Objects
5  * This file contains a collection of test classes that can be used by
6  * and shared by tests.
7  */
8
9 /**
10  * Observable Mock
11  * This is just for testing
12  */
13 class ObservableMock extends \RedBeanPHP\Observable
14 {
15         /**
16          * @param $eventname
17          * @param $info
18          */
19         public function test( $eventname, $info )
20         {
21                 $this->signal( $eventname, $info );
22         }
23 }
24
25 /**
26  * Observer Mock
27  * This is just for testing
28  */
29 class ObserverMock implements \RedBeanPHP\Observer
30 {
31         /**
32          * @var bool
33          */
34         public $event = FALSE;
35
36         /**
37          * @var bool
38          */
39         public $info = FALSE;
40
41         /**
42          * @param string $event
43          * @param        $info
44          */
45         public function onEvent( $event, $info )
46         {
47                 $this->event = $event;
48                 $this->info  = $info;
49         }
50 }
51
52 /**
53  * Shared helper class for tests.
54  * A test model to test FUSE functions.
55  */
56 class Model_Band extends RedBeanPHP\SimpleModel
57 {
58         public function after_update() { }
59
60         private $notes = array();
61
62         /**
63          * @throws Exception
64          */
65         public function update()
66         {
67                 if ( count( $this->ownBandmember ) > 4 ) {
68                         throw new Exception( 'too many!' );
69                 }
70         }
71
72         /**
73          * @return string
74          */
75         public function __toString()
76         {
77                 return 'bigband';
78         }
79
80         /**
81          * @param $prop
82          * @param $value
83          */
84         public function setProperty( $prop, $value )
85         {
86                 $this->$prop = $value;
87         }
88
89         /**
90          * @param $prop
91          *
92          * @return bool
93          */
94         public function checkProperty( $prop )
95         {
96                 return isset( $this->$prop );
97         }
98
99         /**
100          * Sets a note.
101          *
102          * @param string $note
103          *
104          * @param mixed $value
105          *
106          * @return void
107          */
108         public function setNote( $note, $value )
109         {
110                 $this->notes[ $note ] = $value;
111         }
112
113         /**
114          * Returns the value of a note.
115          *
116          * @param string $note
117          *
118          * @return string
119          */
120         public function getNote( $note )
121         {
122                 return $this->notes[ $note ];
123         }
124 }
125
126 /**
127  * Shared helper class for tests.
128  * A Model class for testing Models/FUSE and related features.
129  */
130 class Model_Box extends RedBeanPHP\SimpleModel
131 {
132         public function delete() { $a = $this->bean->ownBottle; }
133 }
134
135 /**
136  * Shared helper class for tests.
137  * A Model class for testing Models/FUSE and related features.
138  */
139 class Model_Cocoa extends RedBeanPHP\SimpleModel
140 {
141         public function update(){}
142 }
143
144 /**
145  * Shared helper class for tests.
146  * A Model class for testing Models/FUSE and related features.
147  */
148 class Model_Taste extends RedBeanPHP\SimpleModel
149 {
150         public function after_update()
151         {
152                 asrt( count( $this->bean->ownCocoa ), 0 );
153         }
154 }
155
156 /**
157  * Shared helper class for tests.
158  * A Model class for testing Models/FUSE and related features.
159  */
160 class Model_Coffee extends RedBeanPHP\SimpleModel
161 {
162         public function update()
163         {
164                 while ( count( $this->bean->ownSugar ) > 3 ) {
165                         array_pop( $this->bean->ownSugar );
166                 }
167         }
168 }
169
170 /**
171  * Shared helper class for tests.
172  * A Model class for testing Models/FUSE and related features.
173  */
174 class Model_Test extends RedBeanPHP\SimpleModel
175 {
176         public function update()
177         {
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 );
183                         $s->name                      = "S2";
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 );
188                 }
189         }
190 }
191
192 global $lifeCycle;
193
194 /**
195  * Shared helper class for tests.
196  * A Model class for testing Models/FUSE and related features.
197  */
198 class Model_Bandmember extends RedBeanPHP\SimpleModel
199 {
200         public function open()
201         {
202                 global $lifeCycle;
203
204                 $lifeCycle .= "\n called open: " . $this->id;
205         }
206
207         public function dispense()
208         {
209                 global $lifeCycle;
210
211                 $lifeCycle .= "\n called dispense() " . $this->bean;
212         }
213
214         public function update()
215         {
216                 global $lifeCycle;
217
218                 $lifeCycle .= "\n called update() " . $this->bean;
219         }
220
221         public function after_update()
222         {
223                 global $lifeCycle;
224
225                 $lifeCycle .= "\n called after_update() " . $this->bean;
226         }
227
228         public function delete()
229         {
230                 global $lifeCycle;
231
232                 $lifeCycle .= "\n called delete() " . $this->bean;
233         }
234
235         public function after_delete()
236         {
237                 global $lifeCycle;
238
239                 $lifeCycle .= "\n called after_delete() " . $this->bean;
240         }
241 }
242
243 /**
244  * A model to box soup models :)
245  */
246 class Model_Soup extends \RedBeanPHP\SimpleModel
247 {
248         private $flavour = '';
249
250         public function taste()
251         {
252                 return 'A bit too salty';
253         }
254
255         public function setFlavour( $flavour )
256         {
257                 $this->flavour = $flavour;
258         }
259
260         public function getFlavour()
261         {
262                 return $this->flavour;
263         }
264 }
265
266 /**
267  * A custom BeanHelper to test custom FUSE operations.
268  */
269 class SoupBeanHelper extends \RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper
270 {
271         /**
272          * Returns a model for a bean based on its type.
273          *
274          * @param OODBBean $bean
275          *
276          * @return object
277          */
278         public function getModelForBean( \RedBeanPHP\OODBBean $bean )
279         {
280                         if ( $bean->getMeta( 'type' ) === 'meal' ) {
281                                 $model = new Model_Soup;
282                                 $model->loadBean( $bean );
283                                 return $model;
284                         } else {
285                                 return parent::getModelForBean( $bean );
286                         }
287         }
288 }
289
290 /**
291  * Test Model.
292  */
293 class Model_Boxedbean extends \RedBeanPHP\SimpleModel
294 {
295 }
296
297 /**
298  * Mock class for testing purposes.
299  */
300 class Model_Ghost_House extends \RedBeanPHP\SimpleModel
301 {
302         public static $deleted = FALSE;
303
304         public function delete()
305         {
306                 self::$deleted = TRUE;
307         }
308 }
309
310 /**
311  * Mock class for testing purposes.
312  */
313 class Model_Ghost_Ghost extends \RedBeanPHP\SimpleModel
314 {
315         public static $deleted = FALSE;
316
317         public function delete()
318         {
319                 self::$deleted = TRUE;
320         }
321 }
322
323 /**
324  * Mock class for testing purposes.
325  */
326 class FaultyWriter extends \RedBeanPHP\QueryWriter\MySQL
327 {
328
329         protected $sqlState;
330
331         /**
332          * Mock method.
333          *
334          * @param string $sqlState sql state
335          */
336         public function setSQLState( $sqlState )
337         {
338                 $this->sqlState = $sqlState;
339         }
340
341         /**
342          * Mock method
343          *
344          * @param string $sourceType destination type
345          * @param string $destType   source type
346          *
347          * @throws SQL
348          */
349         public function addUniqueConstraint( $sourceType, $destType )
350         {
351                 $exception = new \RedBeanPHP\RedException\SQL;
352                 $exception->setSQLState( $this->sqlState );
353                 throw $exception;
354         }
355
356         /**
357          * Mock method.
358          * Generates an exception when trying to invoke
359          * getKeyMapForType via getForeignKeyForTypeProperty.
360          *
361          * @return void
362          */
363         protected function getKeyMapForType( $type )
364         {
365                 throw new \RedBeanPHP\RedException\SQL;
366         }
367 }
368
369 /**
370  * Mock class to test default implementations in AQueryWriter.
371  */
372 class NullWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {
373 }
374
375 class ProxyWriter extends \RedBeanPHP\QueryWriter\AQueryWriter {
376
377         public static function callMethod( $object, $method, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL ) {
378                 return $object->$method( $arg1, $arg2, $arg3 );
379         }
380
381 }
382
383 /**
384  * Mock class to test proper model name
385  * beautificattion for link table beans in FUSE.
386  */
387 class Model_PageWidget extends RedBean_SimpleModel {
388         /**
389          * @var string
390          */
391         private static $test = '';
392
393         /**
394          * Returns the test flag.
395          *
396          * @return string
397          */
398         public static function getTestReport()
399         {
400                 return self::$test;
401         }
402
403         /**
404          * Update method to set the flag.
405          */
406         public function update()
407         {
408                 self::$test = 'didSave';
409         }
410 }
411
412 /**
413  * Mock class to test proper model name
414  * beautificattion for link table beans in FUSE.
415  */
416 class Model_Gadget_Page extends RedBean_SimpleModel {
417         /**
418          * @var string
419          */
420         private static $test = '';
421
422         /**
423          * Returns the test flag.
424          *
425          * @return string
426          */
427         public static function getTestReport()
428         {
429                 return self::$test;
430         }
431
432         /**
433          * Update method to set the flag.
434          */
435         public function update()
436         {
437                 self::$test = 'didSave';
438         }
439 }
440
441 /**
442  * Mock class to test proper model name
443  * beautificattion for link table beans in FUSE.
444  */
445 class Model_A_B_C extends RedBean_SimpleModel {
446         /**
447          * @var string
448          */
449         private static $test = '';
450
451         /**
452          * Returns the test flag.
453          *
454          * @return string
455          */
456         public static function getTestReport()
457         {
458                 return self::$test;
459         }
460
461         /**
462          * Update method to set the flag.
463          */
464         public function update()
465         {
466                 self::$test = 'didSave';
467         }
468 }
469
470 class Model_BookBook extends \RedBean_SimpleModel {
471         public function delete() {
472                 asrt($this->bean->shelf, 'x13');
473         }
474 }
475
476 class Model_Feed extends \RedbeanPHP\SimpleModel {
477     public function update() {
478         $this->bean->post = json_encode($this->bean->post);
479     }
480
481     public function open() {
482         $this->bean->post = json_decode($this->bean->post, true);
483     }
484 }
485
486 /**
487  * UUID QueryWriter for MySQL for testing purposes.
488  */
489 class UUIDWriterMySQL extends \RedBeanPHP\QueryWriter\MySQL {
490
491         protected $defaultValue = '@uuid';
492         const C_DATATYPE_SPECIAL_UUID  = 97;
493
494         public function __construct( \RedBeanPHP\Adapter $adapter )
495         {
496                 parent::__construct( $adapter );
497                 $this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'char(36)'  );
498         }
499
500         public function createTable( $table )
501         {
502                 $table = $this->esc( $table );
503                 $sql   = "
504                         CREATE TABLE {$table} (
505                         id char(36) NOT NULL,
506                         PRIMARY KEY ( id ))
507                         ENGINE = InnoDB DEFAULT
508                         CHARSET=utf8mb4
509                         COLLATE=utf8mb4_unicode_ci ";
510                 $this->adapter->exec( $sql );
511         }
512
513         public function updateRecord($table, $updateValues, $id = NULL)
514         {
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');
519                 return $id;
520         }
521
522         public function getTypeForID()
523         {
524                 return self::C_DATATYPE_SPECIAL_UUID;
525         }
526 }
527
528 /**
529  * UUID QueryWriter for PostgreSQL for testing purposes.
530  */
531 class UUIDWriterPostgres extends \RedBeanPHP\QueryWriter\PostgreSQL {
532
533         protected $defaultValue = 'uuid_generate_v4()';
534         const C_DATATYPE_SPECIAL_UUID  = 97;
535
536         public function __construct( \RedBeanPHP\Adapter $adapter )
537         {
538                 parent::__construct( $adapter );
539                 $this->addDataType( self::C_DATATYPE_SPECIAL_UUID, 'uuid'  );
540         }
541
542         public function createTable( $table )
543         {
544                 $table = $this->esc( $table );
545                 $this->adapter->exec( " CREATE TABLE $table (id uuid PRIMARY KEY); " );
546         }
547
548         public function getTypeForID()
549         {
550                 return self::C_DATATYPE_SPECIAL_UUID;
551         }
552 }
553
554 class DiagnosticBean extends \RedBeanPHP\OODBBean {
555
556         /**
557          * Returns current status of modification flags.
558          *
559          * @return string
560          */
561         public function getModFlags()
562         {
563                 $modFlags = '';
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';
569
570                 return $modFlags;
571         }
572
573 }
574
575 class DiagnosticModel extends \RedBeanPHP\SimpleModel
576 {
577
578         private $logs = array();
579
580         public function open()
581         {
582                 $this->logs[] = array(
583                         'action' => 'open',
584                         'data'   => array(
585                                 'id' => $this->id
586                         )
587                 );
588         }
589
590         public function dispense()
591         {
592                 $this->logs[] = array(
593                         'action' => 'dispense',
594                         'data'   => array(
595                                 'bean' => $this->bean
596                         )
597                 );
598         }
599
600         public function update()
601         {
602                 $this->logs[] = array(
603                         'action' => 'update',
604                         'data'   => array(
605                                 'bean' => $this->bean
606                         )
607                 );
608         }
609
610         public function after_update()
611         {
612                 $this->logs[] = array(
613                         'action' => 'after_update',
614                         'data'   => array(
615                                 'bean' => $this->bean
616                         )
617                 );
618         }
619
620         public function delete()
621         {
622                 $this->logs[] = array(
623                         'action' => 'delete',
624                         'data'   => array(
625                                 'bean' => $this->bean
626                         )
627                 );
628         }
629
630         public function after_delete()
631         {
632                 $this->logs[] = array(
633                         'action' => 'after_delete',
634                         'data'   => array(
635                                 'bean' => $this->bean
636                         )
637                 );
638         }
639
640         public function getLogs()
641         {
642                 return $this->logs;
643         }
644
645         public function getLogActionCount( $action = NULL )
646         {
647                 if ( is_null( $action ) ) return count( $this->logs );
648                 $counter = 0;
649                 foreach( $this->logs as $log ) {
650                         if ( $log['action'] == $action ) $counter ++;
651                 }
652                 return $counter;
653         }
654
655         public function clearLog()
656         {
657                 return $this->logs = array();
658         }
659
660         public function getDataFromLog( $logIndex = 0, $property )
661         {
662                 return $this->logs[$logIndex]['data'][$property];
663         }
664
665 }
666
667 class Model_Probe extends DiagnosticModel {};
668
669 define('REDBEAN_OODBBEAN_CLASS', '\DiagnosticBean');