Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Indexes.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Indexes
10  *
11  * Tests whether indexes are created properly and whether
12  * index creation errors are supressed (they are not important
13  * enough to cause exceptions during development phase).
14  *
15  * @file    RedUNIT/Base/Indexes.php
16  * @desc    Tests whether indexes are applied properly.
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 Indexes extends Base {
25
26         /**
27          * Tests whether a regular index is created properly.
28          *
29          * @return void
30          */
31         public function testIndexCreation()
32         {
33                 R::nuke();
34                 $book = R::dispense( 'book' );
35                 $page = R::dispense( 'page' );
36                 $book->ownPageList[] = $page;
37                 R::store( $book );
38                 $indexes = getIndexes( 'page' );
39                 asrt( in_array( 'index_foreignkey_page_book', $indexes ), TRUE );
40         }
41
42         /**
43          * Tests indexes on parent beans.
44          *
45          * @return void
46          */
47         public function testIndexCreationParentBean()
48         {
49                 R::nuke();
50                 $book = R::dispense( 'book' );
51                 $page = R::dispense( 'page' );
52                 $page->book = $book;
53                 R::store( $page );
54                 $indexes = getIndexes( 'page' );
55                 asrt( in_array( 'index_foreignkey_page_book', $indexes ), TRUE );
56         }
57
58         /**
59          * Tests indexes on link tables.
60          *
61          * @return void
62          */
63         public function testIndexCreationMany2Many()
64         {
65                 R::nuke();
66                 $book = R::dispense( 'book' );
67                 $category = R::dispense( 'category' );
68                 $book->sharedCategoryList[] = $category;
69                 R::store( $book );
70                 $indexes = getIndexes( 'book_category' );
71                 asrt( in_array( 'index_foreignkey_book_category_book', $indexes ), TRUE );
72                 asrt( in_array( 'index_foreignkey_book_category_category', $indexes ), TRUE );
73                 R::nuke();
74                 R::nuke();
75                 $book = R::dispense( 'book' );
76                 $category = R::dispense( 'category' );
77                 $category->sharedBookList[] = $book;
78                 R::store( $category );
79                 $indexes = getIndexes( 'book_category' );
80                 asrt( in_array( 'index_foreignkey_book_category_book', $indexes ), TRUE );
81                 asrt( in_array( 'index_foreignkey_book_category_category', $indexes ), TRUE );
82         }
83
84         /**
85          * Tests indexes on aliases.
86          *
87          * @return void
88          */
89         public function testIndexCreationAlias()
90         {
91                 R::nuke();
92                 $book = R::dispense( 'book' );
93                 $author = R::dispense( 'author' );
94                 $book->coAuthor = $author;
95                 R::store( $book );
96                 $indexes = getIndexes( 'book' );
97                 asrt( in_array( 'index_foreignkey_book_co_author', $indexes ), TRUE );
98                 R::nuke();
99                 $project = R::dispense( 'project' );
100                 $person = R::dispense( 'person' );
101                 $person->alias( 'teacher' )->ownProject[] = $project;
102                 $person2 = R::dispense( 'person' );
103                 $person2->alias( 'student' )->ownProject[] = $project;
104                 R::store( $person );
105                 $indexes = getIndexes( 'project' );
106                 asrt( in_array( 'index_foreignkey_project_teacher', $indexes ), TRUE );
107                 R::store( $person2 );
108                 $indexes = getIndexes( 'project' );
109                 asrt( in_array( 'index_foreignkey_project_student', $indexes ), TRUE );
110         }
111
112         /**
113          * Tests index fails.
114          *
115          * @return void
116          */
117         public function testIndexCreationFail()
118         {
119                 R::nuke();
120                 $book = R::dispense( 'book' );
121                 $book->author_id = 'a';
122                 R::store( $book );
123                 $indexes = getIndexes( 'book' );
124                 //should just work fine
125                 asrt( in_array( 'index_foreignkey_book_author', $indexes ), TRUE );
126                 //these should just pass, no indexes but no errors as well
127                 R::getWriter()->addIndex( 'book', 'bla', 'nonexist' );
128                 pass();
129                 R::getWriter()->addIndex( 'book', '@#$', 'nonexist' );
130                 pass();
131                 R::getWriter()->addIndex( 'nonexist', 'bla', 'nonexist' );
132                 pass();
133                 $indexesAfter = getIndexes( 'book' );
134                 asrt( count( $indexesAfter ), count( $indexes ) );
135         }
136 }