Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Mysql / Parambind.php
1 <?php
2
3 namespace RedUNIT\Mysql;
4
5 use RedUNIT\Mysql as Mysql;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\RedException\SQL as SQL;
8
9 /**
10  * Parambind
11  *
12  * Tests the parameter binding functionality in RedBeanPHP.
13  * These test scenarios include for instance: NULL handling,
14  * binding parameters in LIMIT clauses and so on.
15  *
16  * @file    RedUNIT/Mysql/Parambind.php
17  * @desc    Tests\PDO parameter binding.
18  * @author  Gabor de Mooij and the RedBeanPHP Community
19  * @license New BSD/GPLv2
20  *
21  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22  * This source file is subject to the New BSD/GPLv2 License that is bundled
23  * with this source code in the file license.txt.
24  */
25 class Parambind extends Mysql
26 {
27         /**
28          * Test parameter binding with\PDO.
29          *
30          * @return void
31          */
32         public function testPDOParameterBinding()
33         {
34                 $toolbox = R::getToolBox();
35                 $adapter = $toolbox->getDatabaseAdapter();
36                 $writer  = $toolbox->getWriter();
37                 $redbean = $toolbox->getRedBean();
38                 $pdo     = $adapter->getDatabase();
39                 R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding( TRUE );
40                 try {
41                         R::getAll( "select * from job limit ? ", array( 1 ) );
42                         fail();
43                 } catch (\Exception $e ) {
44                         pass();
45                 }
46                 try {
47                         R::getAll( "select * from job limit :l ", array( ":l" => 1 ) );
48                         fail();
49                 } catch (\Exception $e ) {
50                         pass();
51                 }
52                 try {
53                         R::exec( "select * from job limit ? ", array( 1 ) );
54                         fail();
55                 } catch (\Exception $e ) {
56                         pass();
57                 }
58                 try {
59                         R::exec( "select * from job limit :l ", array( ":l" => 1 ) );
60                         fail();
61                 } catch (\Exception $e ) {
62                         pass();
63                 }
64                 R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding( FALSE );
65                 try {
66                         R::getAll( "select * from job limit ? ", array( 1 ) );
67                         pass();
68                 } catch (\Exception $e ) {
69                         fail();
70                 }
71                 try {
72                         R::getAll( "select * from job limit :l ", array( ":l" => 1 ) );
73                         pass();
74                 } catch (\Exception $e ) {
75                         fail();
76                 }
77                 try {
78                         R::exec( "select * from job limit ? ", array( 1 ) );
79                         pass();
80                 } catch (\Exception $e ) {
81                         fail();
82                 }
83                 try {
84                         R::exec( "select * from job limit :l ", array( ":l" => 1 ) );
85                         pass();
86                 } catch (\Exception $e ) {
87                         fail();
88                 }
89                 testpack( "Test findOrDispense" );
90                 $person = R::findOrDispense( "person", " job = ? ", array( "developer" ) );
91                 asrt( ( count( $person ) > 0 ), TRUE );
92                 $person = R::findOrDispense( "person", " job = ? ", array( "musician" ) );
93                 asrt( ( count( $person ) > 0 ), TRUE );
94                 $musician = array_pop( $person );
95                 asrt( intval( $musician->id ), 0 );
96                 try {
97                         $adapter->exec( "an invalid query" );
98                         fail();
99                 } catch ( SQL $e ) {
100                         pass();
101                 }
102                 asrt( (int) $adapter->getCell( "SELECT 123" ), 123 );
103                 asrt( (int) $adapter->getCell( "SELECT ?", array( "987" ) ), 987 );
104                 asrt( (int) $adapter->getCell( "SELECT ?+?", array( "987", "2" ) ), 989 );
105                 asrt( (int) $adapter->getCell( "SELECT :numberOne+:numberTwo", array(
106                         ":numberOne" => 42, ":numberTwo" => 50 ) ), 92 );
107                 $pair = $adapter->getAssoc( "SELECT 'thekey','thevalue' " );
108                 asrt( is_array( $pair ), TRUE );
109                 asrt( count( $pair ), 1 );
110                 asrt( isset( $pair["thekey"] ), TRUE );
111                 asrt( $pair["thekey"], "thevalue" );
112                 testpack( 'Test whether we can properly bind and receive NULL values' );
113                 asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => 'NULL' ) ), 'NULL' );
114                 asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => NULL ) ), NULL );
115                 asrt( $adapter->getCell( 'SELECT ? ', array( 'NULL' ) ), 'NULL' );
116                 asrt( $adapter->getCell( 'SELECT ? ', array( NULL ) ), NULL );
117         }
118 }