3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
11 * Tests whether we can use named parameters in queries and
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
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.
23 class Namedparams extends Base
26 * Test usage of named parameters in SQL snippets.
27 * Issue #299 on Github.
31 public function testNamedParamsInSnippets()
33 testpack( 'Test whether we can use named parameters in SQL snippets.' );
35 $book = R::dispense( 'book' );
36 $page = R::dispense( 'page' );
37 $book->title = 'book';
38 $book->sharedPage[] = $page;
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 );
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;
54 //should also not give error..
55 $count = $book->countOwn( 'page' );
57 $book = $book->fresh();
58 //should also not give error..
59 $count = $book->withCondition( ' title = ? ', array( 'page' ) )->countOwn( 'page' );
61 $book = $book->fresh();
62 //should also not give error..
63 $count = $book->withCondition( ' title = :title ', array( ':title' => 'page' ) )->countOwn( 'page' );
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;
73 $page2 = $page2->fresh();
74 $pages = $page2->withCondition( ' title = :title ', array( ':title' => 'page' ) )->ownPage;
75 asrt( count( $pages ), 1 );
77 $books = R::getRedBean()->find( 'book',
79 'title' => array('book')),
80 ' AND title = :title ', array(':title'=>'book'));
81 asrt( count( $books ), 1 );
82 $books = R::getRedBean()->find( 'book',
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',
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',
98 'title' => array('book', 'book2'),
99 'comment' => array('comment', 'comment2')),
101 asrt( count( $books ), 1 );