Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Mysql / Bigint.php
1 <?php
2
3 namespace RedUNIT\Mysql;
4
5 use RedUNIT\Mysql as Mysql;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Bigint
10  *
11  * Tests handling of bigint type columns for primary key IDs,
12  * can we use bigint primary keys without issues ?
13  * These tests should be able to detect incorrect intval
14  * casts for instance.
15  *
16  * @file    RedUNIT/Mysql/Bigint.php
17  * @desc    Tests support for BIGINT primary keys.
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 Bigint extends Mysql
26 {
27         /**
28          * Test BIG INT primary key support.
29          *
30          * @return void
31          */
32         public function testBigIntSupport()
33         {
34                 R::nuke();
35
36                 $createPageTableSQL = '
37                         CREATE TABLE
38                         `page`
39                         (
40                                 id BIGINT(20) UNSIGNED NOT NULL,
41                                 book_id BIGINT(20) UNSIGNED NOT NULL,
42                                 magazine_id BIGINT(20) UNSIGNED NOT NULL,
43                                 title VARCHAR(255),
44                                 PRIMARY KEY ( id )
45                         )
46                         ENGINE = InnoDB DEFAULT
47                         CHARSET=utf8mb4
48                         COLLATE=utf8mb4_unicode_ci
49                         AUTO_INCREMENT = 1223372036854775808';
50
51                 $createBookTableSQL = '
52                         CREATE TABLE
53                         `book`
54                         (
55                                 id BIGINT(20) UNSIGNED NOT NULL,
56                                 title VARCHAR(255),
57                                 PRIMARY KEY ( id )
58                         )
59                         ENGINE = InnoDB DEFAULT
60                         CHARSET=utf8mb4
61                         COLLATE=utf8mb4_unicode_ci
62                         AUTO_INCREMENT = 2223372036854775808';
63
64                 $createPagePageTableSQL = '
65                         CREATE TABLE
66                         `page_page`
67                         (
68                                 id BIGINT(20) UNSIGNED NOT NULL,
69                                 page_id BIGINT(20) UNSIGNED NOT NULL,
70                                 page2_id BIGINT(20) UNSIGNED NOT NULL,
71                                 PRIMARY KEY ( id )
72                         )
73                         ENGINE = InnoDB DEFAULT
74                         CHARSET=utf8mb4
75                         COLLATE=utf8mb4_unicode_ci
76                         AUTO_INCREMENT = 3223372036854775808';
77
78                 R::exec( $createBookTableSQL );
79                 R::exec( $createPageTableSQL );
80                 R::exec( $createPagePageTableSQL );
81
82                 //insert some records
83
84                 $book1ID     = '2223372036854775808';
85                 $book2ID     = '2223372036854775809';
86                 $page1ID     = '1223372036854775808';
87                 $page2ID     = '1223372036854775809';
88                 $page3ID     = '1223372036854775890';
89                 $pagePage1ID = '3223372036854775808';
90
91                 $insertBook1SQL = "
92                         INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
93                 ";
94
95                 $insertBook2SQL = "
96                         INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
97                 ";
98
99                 $insertPage1SQL = "
100                         INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
101                 ";
102
103                 $insertPage2SQL = "
104                         INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
105                 ";
106
107                 $insertPage3SQL = "
108                         INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
109                 ";
110
111                 $insertPagePage1SQL = "
112                         INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
113                 ";
114
115                 R::exec( $insertBook1SQL );
116                 R::exec( $insertBook2SQL );
117                 R::exec( $insertPage1SQL );
118                 R::exec( $insertPage2SQL );
119                 R::exec( $insertPage3SQL );
120                 R::exec( $insertPagePage1SQL );
121
122                 //basic tour of basic functions....
123
124                 $book1 = R::load( 'book', $book1ID );
125
126                 asrt( $book1->id, $book1ID );
127                 asrt( $book1->title, 'book 1' );
128
129                 $book2 = R::load( 'book', $book2ID );
130
131                 asrt( $book2->id, $book2ID );
132                 asrt( $book2->title, 'book 2' );
133
134                 asrt( count( $book1->ownPage ), 2 );
135                 asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
136                 asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
137
138                 asrt( count($book2->ownPage), 1 );
139                 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
140
141                 $page1 = R::load( 'page', $page1ID );
142                 asrt( count( $page1->sharedPage ), 0 );
143                 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
144
145                 $page2 = R::load( 'page', $page2ID );
146                 asrt( count($page2->sharedPage), 1 );
147                 asrt( $page2->fresh()->countShared( 'page' ), 1 );
148
149                 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
150                 asrt( $page3->id, $page3ID );
151                 asrt( $page3->book->id, $book2ID );
152
153         }
154 }