Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Namedparams.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Namedparams
10  *
11  * Tests whether we can use named parameters in queries and
12  * SQL snippets.
13  *
14  * @file    RedUNIT/Base/Namedparams.php
15  * @desc    Test whether you can use named parameters in SQL snippets.
16  * @author  Gabor de Mooij and the RedBeanPHP Community
17  * @license New BSD/GPLv2
18  *
19  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
20  * This source file is subject to the New BSD/GPLv2 License that is bundled
21  * with this source code in the file license.txt.
22  */
23 class Namedparams extends Base
24 {
25         /**
26          * Test usage of named parameters in SQL snippets.
27          * Issue #299 on Github.
28          *
29          * @return void
30          */
31         public function testNamedParamsInSnippets()
32         {
33                 testpack( 'Test whether we can use named parameters in SQL snippets.' );
34                 R::nuke();
35                 $book = R::dispense( 'book' );
36                 $page = R::dispense( 'page' );
37                 $book->title = 'book';
38                 $book->sharedPage[] = $page;
39                 R::store($book);
40                 //should not give error like: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
41                 $books = $page->withCondition(' title = :title ', array( ':title' => 'book' ) )->sharedBook;
42                 asrt( count( $books ), 1 );
43                 //should not give error...
44                 $books = $page->withCondition( ' title = :title ', array( ':title' => 'book' ) )->sharedBook;
45                 asrt( count( $books ), 1 );
46                 R::nuke();
47                 $book = R::dispense( 'book' );
48                 $page = R::dispense( 'page' );
49                 $book->title = 'book';
50                 $book->comment = 'comment';
51                 $page->title = 'page';
52                 $book->ownPage[] = $page;
53                 R::store( $book );
54                 //should also not give error..
55                 $count = $book->countOwn( 'page' );
56                 asrt( $count, 1 );
57                 $book = $book->fresh();
58                 //should also not give error..
59                 $count = $book->withCondition( ' title = ? ', array( 'page' ) )->countOwn( 'page' );
60                 asrt( $count, 1 );
61                 $book = $book->fresh();
62                 //should also not give error..
63                 $count = $book->withCondition( ' title = :title ', array( ':title' => 'page' ) )->countOwn( 'page' );
64                 asrt( $count, 1 );
65                 $book = $book->fresh();
66                 $pages = $book->withCondition( ' title = :title ', array( ':title' => 'page' ) )->ownPage;
67                 asrt( count( $pages ), 1 );
68                 //test with duplicate slots...
69                 $page = reset( $pages );
70                 $page2 = R::dispense( 'page' );
71                 $page2->ownPage[] = $page;
72                 R::store( $page2 );
73                 $page2 = $page2->fresh();
74                 $pages = $page2->withCondition( ' title = :title ', array( ':title' => 'page' ) )->ownPage;
75                 asrt( count( $pages ), 1 );
76                 //test with find()
77                 $books = R::getRedBean()->find( 'book',
78                                   array(
79                                                 'title' => array('book')),
80                                   ' AND title = :title ', array(':title'=>'book'));
81                 asrt( count( $books ), 1 );
82                 $books = R::getRedBean()->find( 'book',
83                                   array(
84                                                 'title'   => array('book', 'book2'),
85                                                 'comment' => array('comment', 'comment2')),
86                                   ' AND title = :title ', array(':title'=>'book'));
87                 asrt( count( $books ), 1 );
88                 //just check numeric works as well...
89                 $books = R::getRedBean()->find( 'book',
90                                   array(
91                                                 'title'   => array('book', 'book2'),
92                                                 'comment' => array('comment', 'comment2')),
93                                   ' AND title = ? ', array('book'));
94                 asrt( count( $books ), 1 );
95                 //just extra check to verify glue works
96                 $books = R::getRedBean()->find( 'book',
97                                   array(
98                                                 'title'   => array('book', 'book2'),
99                                                 'comment' => array('comment', 'comment2')),
100                                   ' ORDER BY id ');
101                 asrt( count( $books ), 1 );
102         }
103 }