Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Cursors.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8
9 /**
10  * Cursors
11  *
12  * Tests whether RedBeanPHP can use cursors (using the
13  * findCollection method) to iterate over large data sets.
14  *
15  * @file    RedUNIT/Base/Cursors.php
16  * @desc    Tests whether we can use cursors
17  * @author  Gabor de Mooij and the RedBeanPHP Community
18  * @license New BSD/GPLv2
19  *
20  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
21  * This source file is subject to the New BSD/GPLv2 License that is bundled
22  * with this source code in the file license.txt.
23  */
24 class Cursors extends Base
25 {
26         /**
27          * Test basic cursor functionality.
28          *
29          * @return void
30          */
31         public function testBasicCursors()
32         {
33                 R::nuke();
34                 for( $i=0; $i<20; $i++ ) {
35                         $page = R::dispense( 'page' );
36                         $page->number = $i;
37                         $page->content = sha1( $i );
38                         R::store( $page );
39                 }
40                 $collection = R::findCollection( 'page' );
41                 asrt( get_class( $collection ), 'RedBeanPHP\BeanCollection');
42                 $i = 0;
43                 $list = array();
44                 while( $bean = $collection->next() ) {
45                         asrt( ( $bean instanceof OODBBean ), TRUE );
46                         asrt( (string) $bean->number, strval( $i )  );
47                         asrt( $bean->content, sha1( $i ) );
48                         $list[] = $bean->content;
49                         $i ++;
50                 }
51                 $collection = R::findCollection( 'page', ' ORDER BY content ASC ' );
52                 sort( $list );
53                 $i = 0;
54                 while( $bean = $collection->next() ) {
55                         asrt( $bean->content, $list[$i] );
56                         $i ++;
57                 }
58                 $collection = R::findCollection( 'page', ' ORDER BY content ASC LIMIT 5 ' );
59                 sort( $list );
60                 $i = 0;
61                 while( $bean = $collection->next() ) {
62                         asrt( $bean->content, $list[$i] );
63                         $i ++;
64                         if ( $i > 5 ) break;
65                 }
66                 $key = array_rand( $list );
67                 $content = $list[ $key ];
68                 $collection = R::findCollection( 'page', ' content = ? ', array( $content ) );
69                 $bean = $collection->next();
70                 asrt( $bean->content, $content );
71                 $collection->close();
72         }
73
74         /**
75          * Test empty collections (NULLCursor).
76          *
77          * @return void
78          */
79         public function testEmptyCollection()
80         {
81                 R::nuke();
82                 $page = R::dispense( 'page' );
83                 $page->content = 'aaa';
84                 R::store( $page );
85                 $collection = R::findCollection( 'page' );
86                 asrt( get_class( $collection ), 'RedBeanPHP\BeanCollection');
87                 $collection = R::findCollection( 'page', ' content  =  ?', array( 'bbb' ) );
88                 asrt( get_class( $collection ), 'RedBeanPHP\BeanCollection');
89                 asrt( is_null( $collection->next() ), TRUE );
90                 $collection = R::findCollection( 'something' );
91                 asrt( get_class( $collection ), 'RedBeanPHP\BeanCollection');
92                 asrt( is_null( $collection->next() ), TRUE );
93                 $collection->close();
94         }
95 }