3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
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).
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
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.
24 class Indexes extends Base {
27 * Tests whether a regular index is created properly.
31 public function testIndexCreation()
34 $book = R::dispense( 'book' );
35 $page = R::dispense( 'page' );
36 $book->ownPageList[] = $page;
38 $indexes = getIndexes( 'page' );
39 asrt( in_array( 'index_foreignkey_page_book', $indexes ), TRUE );
43 * Tests indexes on parent beans.
47 public function testIndexCreationParentBean()
50 $book = R::dispense( 'book' );
51 $page = R::dispense( 'page' );
54 $indexes = getIndexes( 'page' );
55 asrt( in_array( 'index_foreignkey_page_book', $indexes ), TRUE );
59 * Tests indexes on link tables.
63 public function testIndexCreationMany2Many()
66 $book = R::dispense( 'book' );
67 $category = R::dispense( 'category' );
68 $book->sharedCategoryList[] = $category;
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 );
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 );
85 * Tests indexes on aliases.
89 public function testIndexCreationAlias()
92 $book = R::dispense( 'book' );
93 $author = R::dispense( 'author' );
94 $book->coAuthor = $author;
96 $indexes = getIndexes( 'book' );
97 asrt( in_array( 'index_foreignkey_book_co_author', $indexes ), TRUE );
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;
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 );
117 public function testIndexCreationFail()
120 $book = R::dispense( 'book' );
121 $book->author_id = 'a';
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' );
129 R::getWriter()->addIndex( 'book', '@#$', 'nonexist' );
131 R::getWriter()->addIndex( 'nonexist', 'bla', 'nonexist' );
133 $indexesAfter = getIndexes( 'book' );
134 asrt( count( $indexesAfter ), count( $indexes ) );